From 7441f76e79741b9ea050d76dede1266305615b90 Mon Sep 17 00:00:00 2001 From: onlyLYJ <382266293@qq.com> Date: Sun, 11 Jun 2017 22:12:13 +0800 Subject: [PATCH 001/214] Merge pull request #6 from onlyliuxin/master season 2 --- students/382266293/src/pom.xml | 32 ++++ .../com/coderising/ood/srp/Configuration.java | 27 +++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 27 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 172 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + students/382266293/src/test.java | 9 + 8 files changed, 298 insertions(+) create mode 100644 students/382266293/src/pom.xml create mode 100644 students/382266293/src/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/382266293/src/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/382266293/src/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/382266293/src/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/382266293/src/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/382266293/src/src/main/java/com/coderising/ood/srp/product_promotion.txt create mode 100644 students/382266293/src/test.java diff --git a/students/382266293/src/pom.xml b/students/382266293/src/pom.xml new file mode 100644 index 0000000000..2bf55e64c9 --- /dev/null +++ b/students/382266293/src/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/382266293/src/src/main/java/com/coderising/ood/srp/Configuration.java b/students/382266293/src/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..db20a8a5d5 --- /dev/null +++ b/students/382266293/src/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,27 @@ +package src.main.java.com.coderising.ood.srp; + +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + + static { + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/382266293/src/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/382266293/src/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..4946f09f38 --- /dev/null +++ b/students/382266293/src/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package src.main.java.com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/382266293/src/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/382266293/src/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..6edf953e19 --- /dev/null +++ b/students/382266293/src/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,27 @@ +package src.main.java.com.coderising.ood.srp; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * + * @param sql + * @return + */ + public static List query(String sql) { + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/382266293/src/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/382266293/src/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..3b6716de70 --- /dev/null +++ b/students/382266293/src/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package src.main.java.com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/382266293/src/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/382266293/src/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..e23638c4b2 --- /dev/null +++ b/students/382266293/src/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,172 @@ +package src.main.java.com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + private static Configuration config; + protected String sendMailQuery = null; + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + protected String productID = null; + protected String productDesc = null; + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + public static void main(String[] args) throws Exception { + + File f = new File("E:\\git\\coding2017\\students\\382266293\\src\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + protected void setProductID(String productID) { + this.productID = productID; + + } + + protected String getproductID() { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID + "' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } catch (Exception e) { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/382266293/src/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/382266293/src/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/382266293/src/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/382266293/src/test.java b/students/382266293/src/test.java new file mode 100644 index 0000000000..56d4d4396e --- /dev/null +++ b/students/382266293/src/test.java @@ -0,0 +1,9 @@ +/** + * Created by onlyLYJ on 2017/6/11. + */ + + +public class test { + + +} From 883818b1bad21bdb336ddac8dbc7af674cb40b7c Mon Sep 17 00:00:00 2001 From: Wen Wei Date: Sun, 11 Jun 2017 23:02:32 +0800 Subject: [PATCH 002/214] test --- students/463256809/src/Demo.java | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 students/463256809/src/Demo.java diff --git a/students/463256809/src/Demo.java b/students/463256809/src/Demo.java new file mode 100644 index 0000000000..a67375a605 --- /dev/null +++ b/students/463256809/src/Demo.java @@ -0,0 +1,6 @@ + +public class Demo { + public static void main(String[] args) { + System.out.println("Test"); + } +} From 704865b3cefc5da7e1487acb5a6af8062c43995f Mon Sep 17 00:00:00 2001 From: SYCHS <429301805@qq.com> Date: Sun, 11 Jun 2017 23:14:59 +0800 Subject: [PATCH 003/214] This is a test --- students/429301805/src/gz/sychs/cn/test.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 students/429301805/src/gz/sychs/cn/test.java diff --git a/students/429301805/src/gz/sychs/cn/test.java b/students/429301805/src/gz/sychs/cn/test.java new file mode 100644 index 0000000000..9fa342b44f --- /dev/null +++ b/students/429301805/src/gz/sychs/cn/test.java @@ -0,0 +1,10 @@ +package gz.sychs.cn; + +public class test { + + public static void main(String[] args) { + // TODO Auto-generated method stub + System.out.println("This is a test"); + } + +} From 488d0db333eecadf382e098f1237fc94f9744f61 Mon Sep 17 00:00:00 2001 From: zhanglei <383117348@qq.com> Date: Mon, 12 Jun 2017 09:12:19 +0800 Subject: [PATCH 004/214] =?UTF-8?q?2017-06-12=E4=B8=8B=E6=8B=89=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/14703250/readme.md | 2 +- students/383117348/ood-assignment/pom.xml | 32 +++ .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + .../coderising/ood_assignment/AppTest.java | 38 ++++ 9 files changed, 349 insertions(+), 1 deletion(-) create mode 100644 students/383117348/ood-assignment/pom.xml create mode 100644 students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt create mode 100644 students/383117348/ood-assignment/src/test/java/com/coderising/ood_assignment/AppTest.java diff --git a/students/14703250/readme.md b/students/14703250/readme.md index 2e9f085533..19422f702b 100644 --- a/students/14703250/readme.md +++ b/students/14703250/readme.md @@ -1 +1 @@ -愿意自荐代码的,可以每个人一个目录 以自己的QQ号命名 ,把自荐的代码放到里边去 +愿意自荐代码的,可以每个人一个目录 以自己的QQ号命名 ,把自荐的代码放到里边去 diff --git a/students/383117348/ood-assignment/pom.xml b/students/383117348/ood-assignment/pom.xml new file mode 100644 index 0000000000..1be81576cc --- /dev/null +++ b/students/383117348/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..819e85fb8a --- /dev/null +++ b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..868a03ff83 --- /dev/null +++ b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..65383e4dba --- /dev/null +++ b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..373f3ee306 --- /dev/null +++ b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..94bfcbaf54 --- /dev/null +++ b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/383117348/ood-assignment/src/test/java/com/coderising/ood_assignment/AppTest.java b/students/383117348/ood-assignment/src/test/java/com/coderising/ood_assignment/AppTest.java new file mode 100644 index 0000000000..931f1f5362 --- /dev/null +++ b/students/383117348/ood-assignment/src/test/java/com/coderising/ood_assignment/AppTest.java @@ -0,0 +1,38 @@ +package com.coderising.ood_assignment; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} From 28941d0b4451135c9e637a21abf1792ffe812567 Mon Sep 17 00:00:00 2001 From: robert <592146505@qq.com> Date: Mon, 12 Jun 2017 09:45:01 +0800 Subject: [PATCH 005/214] first implport --- .../592146505/data-structure/answer/pom.xml | 32 +++ .../coderising/download/DownloadThread.java | 20 ++ .../coderising/download/FileDownloader.java | 73 +++++++ .../download/FileDownloaderTest.java | 59 ++++++ .../coderising/download/api/Connection.java | 23 ++ .../download/api/ConnectionException.java | 5 + .../download/api/ConnectionManager.java | 10 + .../download/api/DownloadListener.java | 5 + .../download/impl/ConnectionImpl.java | 27 +++ .../download/impl/ConnectionManagerImpl.java | 15 ++ .../coderising/litestruts/LoginAction.java | 39 ++++ .../com/coderising/litestruts/Struts.java | 34 +++ .../com/coderising/litestruts/StrutsTest.java | 43 ++++ .../java/com/coderising/litestruts/View.java | 23 ++ .../java/com/coderising/litestruts/struts.xml | 11 + .../main/java/com/coding/basic/Iterator.java | 7 + .../src/main/java/com/coding/basic/List.java | 9 + .../com/coding/basic/array/ArrayList.java | 35 +++ .../com/coding/basic/array/ArrayUtil.java | 96 +++++++++ .../coding/basic/linklist/LRUPageFrame.java | 164 +++++++++++++++ .../basic/linklist/LRUPageFrameTest.java | 34 +++ .../com/coding/basic/linklist/LinkedList.java | 125 +++++++++++ .../com/coding/basic/queue/CircleQueue.java | 47 +++++ .../coding/basic/queue/CircleQueueTest.java | 44 ++++ .../java/com/coding/basic/queue/Josephus.java | 39 ++++ .../com/coding/basic/queue/JosephusTest.java | 27 +++ .../java/com/coding/basic/queue/Queue.java | 61 ++++++ .../basic/queue/QueueWithTwoStacks.java | 55 +++++ .../com/coding/basic/stack/QuickMinStack.java | 44 ++++ .../coding/basic/stack/QuickMinStackTest.java | 39 ++++ .../java/com/coding/basic/stack/Stack.java | 24 +++ .../com/coding/basic/stack/StackUtil.java | 168 +++++++++++++++ .../com/coding/basic/stack/StackUtilTest.java | 86 ++++++++ .../basic/stack/StackWithTwoQueues.java | 53 +++++ .../basic/stack/StackWithTwoQueuesTest.java | 36 ++++ .../java/com/coding/basic/stack/Tail.java | 5 + .../basic/stack/TwoStackInOneArray.java | 117 ++++++++++ .../basic/stack/TwoStackInOneArrayTest.java | 65 ++++++ .../coding/basic/stack/expr/InfixExpr.java | 72 +++++++ .../basic/stack/expr/InfixExprTest.java | 52 +++++ .../basic/stack/expr/InfixToPostfix.java | 43 ++++ .../basic/stack/expr/InfixToPostfixTest.java | 41 ++++ .../coding/basic/stack/expr/PostfixExpr.java | 46 ++++ .../basic/stack/expr/PostfixExprTest.java | 41 ++++ .../coding/basic/stack/expr/PrefixExpr.java | 52 +++++ .../basic/stack/expr/PrefixExprTest.java | 45 ++++ .../com/coding/basic/stack/expr/Token.java | 50 +++++ .../coding/basic/stack/expr/TokenParser.java | 57 +++++ .../basic/stack/expr/TokenParserTest.java | 41 ++++ .../coding/basic/tree/BinarySearchTree.java | 189 +++++++++++++++++ .../basic/tree/BinarySearchTreeTest.java | 108 ++++++++++ .../com/coding/basic/tree/BinaryTreeNode.java | 36 ++++ .../com/coding/basic/tree/BinaryTreeUtil.java | 116 ++++++++++ .../coding/basic/tree/BinaryTreeUtilTest.java | 75 +++++++ .../java/com/coding/basic/tree/FileList.java | 34 +++ .../data-structure/assignment/pom.xml | 32 +++ .../coderising/download/DownloadThread.java | 20 ++ .../coderising/download/FileDownloader.java | 73 +++++++ .../download/FileDownloaderTest.java | 59 ++++++ .../coderising/download/api/Connection.java | 23 ++ .../download/api/ConnectionException.java | 5 + .../download/api/ConnectionManager.java | 10 + .../download/api/DownloadListener.java | 5 + .../download/impl/ConnectionImpl.java | 27 +++ .../download/impl/ConnectionManagerImpl.java | 15 ++ .../coderising/litestruts/LoginAction.java | 39 ++++ .../com/coderising/litestruts/Struts.java | 34 +++ .../com/coderising/litestruts/StrutsTest.java | 43 ++++ .../java/com/coderising/litestruts/View.java | 23 ++ .../java/com/coderising/litestruts/struts.xml | 11 + .../com/coderising/ood/course/bad/Course.java | 24 +++ .../ood/course/bad/CourseOffering.java | 26 +++ .../ood/course/bad/CourseService.java | 16 ++ .../coderising/ood/course/bad/Student.java | 14 ++ .../coderising/ood/course/good/Course.java | 18 ++ .../ood/course/good/CourseOffering.java | 34 +++ .../ood/course/good/CourseService.java | 14 ++ .../coderising/ood/course/good/Student.java | 21 ++ .../java/com/coderising/ood/ocp/DateUtil.java | 10 + .../java/com/coderising/ood/ocp/Logger.java | 38 ++++ .../java/com/coderising/ood/ocp/MailUtil.java | 10 + .../java/com/coderising/ood/ocp/SMSUtil.java | 10 + .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + .../main/java/com/coding/basic/Iterator.java | 7 + .../src/main/java/com/coding/basic/List.java | 9 + .../com/coding/basic/array/ArrayList.java | 35 +++ .../com/coding/basic/array/ArrayUtil.java | 96 +++++++++ .../coding/basic/linklist/LRUPageFrame.java | 57 +++++ .../basic/linklist/LRUPageFrameTest.java | 34 +++ .../com/coding/basic/linklist/LinkedList.java | 125 +++++++++++ .../com/coding/basic/queue/CircleQueue.java | 39 ++++ .../java/com/coding/basic/queue/Josephus.java | 18 ++ .../com/coding/basic/queue/JosephusTest.java | 27 +++ .../java/com/coding/basic/queue/Queue.java | 61 ++++++ .../basic/queue/QueueWithTwoStacks.java | 47 +++++ .../com/coding/basic/stack/QuickMinStack.java | 19 ++ .../java/com/coding/basic/stack/Stack.java | 24 +++ .../com/coding/basic/stack/StackUtil.java | 48 +++++ .../com/coding/basic/stack/StackUtilTest.java | 65 ++++++ .../basic/stack/StackWithTwoQueues.java | 16 ++ .../basic/stack/TwoStackInOneArray.java | 57 +++++ .../coding/basic/stack/expr/InfixExpr.java | 15 ++ .../basic/stack/expr/InfixExprTest.java | 52 +++++ .../basic/stack/expr/InfixToPostfix.java | 14 ++ .../coding/basic/stack/expr/PostfixExpr.java | 18 ++ .../basic/stack/expr/PostfixExprTest.java | 41 ++++ .../coding/basic/stack/expr/PrefixExpr.java | 18 ++ .../basic/stack/expr/PrefixExprTest.java | 45 ++++ .../com/coding/basic/stack/expr/Token.java | 50 +++++ .../coding/basic/stack/expr/TokenParser.java | 57 +++++ .../basic/stack/expr/TokenParserTest.java | 41 ++++ .../coding/basic/tree/BinarySearchTree.java | 55 +++++ .../basic/tree/BinarySearchTreeTest.java | 109 ++++++++++ .../com/coding/basic/tree/BinaryTreeNode.java | 35 +++ .../com/coding/basic/tree/BinaryTreeUtil.java | 66 ++++++ .../coding/basic/tree/BinaryTreeUtilTest.java | 75 +++++++ .../java/com/coding/basic/tree/FileList.java | 10 + students/592146505/ood/ood-assignment/pom.xml | 32 +++ .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 129 files changed, 5624 insertions(+) create mode 100644 students/592146505/data-structure/answer/pom.xml create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/View.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/Iterator.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/List.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtilTest.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java create mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java create mode 100644 students/592146505/data-structure/assignment/pom.xml create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/download/DownloadThread.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/download/FileDownloader.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/download/FileDownloaderTest.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/download/api/Connection.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionException.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionManager.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/download/api/DownloadListener.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionImpl.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/LoginAction.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/Struts.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/StrutsTest.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/View.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/struts.xml create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Course.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseOffering.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseService.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Student.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Course.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseOffering.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseService.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Student.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/ocp/Logger.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/Iterator.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/List.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayList.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayUtil.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrame.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/linklist/LinkedList.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/CircleQueue.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/Josephus.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/JosephusTest.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/Queue.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/QuickMinStack.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/Stack.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtil.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtilTest.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExpr.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/Token.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParser.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTree.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeNode.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java create mode 100644 students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/FileList.java create mode 100644 students/592146505/ood/ood-assignment/pom.xml create mode 100644 students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/592146505/data-structure/answer/pom.xml b/students/592146505/data-structure/answer/pom.xml new file mode 100644 index 0000000000..ac6ba882df --- /dev/null +++ b/students/592146505/data-structure/answer/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ds-answer + 0.0.1-SNAPSHOT + jar + + ds-answer + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..900a3ad358 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java @@ -0,0 +1,20 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + + } +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..c3c8a3f27d --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java @@ -0,0 +1,73 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn,0,length-1).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..4ff7f46ae0 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..36a9d2ce15 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,27 @@ +package com.coderising.download.impl; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + @Override + public int getContentLength() { + + return 0; + } + + @Override + public void close() { + + + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..172371dd55 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return null; + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/View.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml b/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/Iterator.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/List.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java new file mode 100644 index 0000000000..4576c016af --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java @@ -0,0 +1,35 @@ +package com.coding.basic.array; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java new file mode 100644 index 0000000000..45740e6d57 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coding.basic.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..24b9d8b155 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,164 @@ +package com.coding.basic.linklist; + + +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + private int currentSize; + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + this.currentSize = 0; + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + + Node node = find(pageNum); + //在该队列中存在, 则提到队列头 + if (node != null) { + + moveExistingNodeToHead(node); + + } else{ + + node = new Node(); + node.pageNum = pageNum; + + // 缓存容器是否已经超过大小. + if (currentSize >= capacity) { + removeLast(); + + } + + addNewNodetoHead(node); + + + + + } + } + + private void addNewNodetoHead(Node node) { + + if(isEmpty()){ + + node.prev = null; + node.next = null; + first = node; + last = node; + + } else{ + node.prev = null; + node.next = first; + first.prev = node; + first = node; + } + this.currentSize ++; + } + + private Node find(int data){ + + Node node = first; + while(node != null){ + if(node.pageNum == data){ + return node; + } + node = node.next; + } + return null; + + } + + + + + + + /** + * 删除链表尾部节点 表示 删除最少使用的缓存对象 + */ + private void removeLast() { + Node prev = last.prev; + prev.next = null; + last.prev = null; + last = prev; + this.currentSize --; + } + + /** + * 移动到链表头,表示这个节点是最新使用过的 + * + * @param node + */ + private void moveExistingNodeToHead(Node node) { + + if (node == first) { + + return; + } + else if(node == last){ + //当前节点是链表尾, 需要放到链表头 + Node prevNode = node.prev; + prevNode.next = null; + last.prev = null; + last = prevNode; + + } else{ + //node 在链表的中间, 把node 的前后节点连接起来 + Node prevNode = node.prev; + prevNode.next = node.next; + + Node nextNode = node.next; + nextNode.prev = prevNode; + + + } + + node.prev = null; + node.next = first; + first.prev = node; + first = node; + + } + private boolean isEmpty(){ + return (first == null) && (last == null); + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + + + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..7fd72fc2b4 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,34 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + frame.access(5); + Assert.assertEquals("5,4,0", frame.toString()); + + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java new file mode 100644 index 0000000000..f4c7556a2e --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java @@ -0,0 +1,125 @@ +package com.coding.basic.linklist; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java new file mode 100644 index 0000000000..f169d5f8e4 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java @@ -0,0 +1,47 @@ +package com.coding.basic.queue; + +public class CircleQueue { + + //用数组来保存循环队列的元素 + private Object[] elementData ; + int size = 0; + //队头 + private int front = 0; + //队尾 + private int rear = 0; + + public CircleQueue(int capacity){ + elementData = new Object[capacity]; + } + public boolean isEmpty() { + return (front == rear) && !isFull(); + + } + + public boolean isFull(){ + return size == elementData.length; + } + public int size() { + return size; + } + + public void enQueue(E data) { + if(isFull()){ + throw new RuntimeException("The queue is full"); + } + rear = (rear+1) % elementData.length; + elementData[rear++] = data; + size++; + } + + public E deQueue() { + if(isEmpty()){ + throw new RuntimeException("The queue is empty"); + } + E data = (E)elementData[front]; + elementData[front] = null; + front = (front+1) % elementData.length; + size --; + return data; + } +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java new file mode 100644 index 0000000000..7307eb77d4 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java @@ -0,0 +1,44 @@ +package com.coding.basic.queue; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class CircleQueueTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + CircleQueue queue = new CircleQueue(5); + Assert.assertTrue(queue.isEmpty()); + Assert.assertFalse(queue.isFull()); + + queue.enQueue("a"); + queue.enQueue("b"); + queue.enQueue("c"); + queue.enQueue("d"); + queue.enQueue("e"); + + Assert.assertTrue(queue.isFull()); + Assert.assertFalse(queue.isEmpty()); + Assert.assertEquals(5, queue.size()); + + Assert.assertEquals("a", queue.deQueue()); + Assert.assertEquals("b", queue.deQueue()); + Assert.assertEquals("c", queue.deQueue()); + Assert.assertEquals("d", queue.deQueue()); + Assert.assertEquals("e", queue.deQueue()); + + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java new file mode 100644 index 0000000000..36ec615d36 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java @@ -0,0 +1,39 @@ +package com.coding.basic.queue; + +import java.util.ArrayList; +import java.util.List; + +/** + * 用Queue来实现Josephus问题 + * 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来 + * @author liuxin + * + */ +public class Josephus { + + public static List execute(int n, int m){ + + Queue queue = new Queue(); + for (int i = 0; i < n; i++){ + queue.enQueue(i); + } + + List result = new ArrayList(); + int i = 0; + + while (!queue.isEmpty()) { + + int x = queue.deQueue(); + + if (++i % m == 0){ + result.add(x); + } else{ + queue.enQueue(x); + } + } + + + return result; + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java new file mode 100644 index 0000000000..7d90318b51 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java @@ -0,0 +1,27 @@ +package com.coding.basic.queue; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class JosephusTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testExecute() { + + Assert.assertEquals("[1, 3, 5, 0, 4, 2, 6]", Josephus.execute(7, 2).toString()); + + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java new file mode 100644 index 0000000000..c4c4b7325e --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java @@ -0,0 +1,61 @@ +package com.coding.basic.queue; + +import java.util.NoSuchElementException; + +public class Queue { + private Node first; + private Node last; + private int size; + + + private static class Node { + private E item; + private Node next; + } + + + public Queue() { + first = null; + last = null; + size = 0; + } + + + public boolean isEmpty() { + return first == null; + } + + public int size() { + return size; + } + + + + public void enQueue(E data) { + Node oldlast = last; + last = new Node(); + last.item = data; + last.next = null; + if (isEmpty()) { + first = last; + } + else{ + oldlast.next = last; + } + size++; + } + + public E deQueue() { + if (isEmpty()) { + throw new NoSuchElementException("Queue underflow"); + } + E item = first.item; + first = first.next; + size--; + if (isEmpty()) { + last = null; + } + return item; + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java new file mode 100644 index 0000000000..bc97df0800 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java @@ -0,0 +1,55 @@ +package com.coding.basic.queue; + +import java.util.NoSuchElementException; +import java.util.Stack; + +public class QueueWithTwoStacks { + private Stack stack1; + private Stack stack2; + + + public QueueWithTwoStacks() { + stack1 = new Stack(); + stack2 = new Stack(); + } + + + private void moveStack1ToStack2() { + while (!stack1.isEmpty()){ + stack2.push(stack1.pop()); + } + + } + + + public boolean isEmpty() { + return stack1.isEmpty() && stack2.isEmpty(); + } + + + + public int size() { + return stack1.size() + stack2.size(); + } + + + + public void enQueue(E item) { + stack1.push(item); + } + + public E deQueue() { + if (isEmpty()) { + throw new NoSuchElementException("Queue is empty"); + } + if (stack2.isEmpty()) { + moveStack1ToStack2(); + } + + return stack2.pop(); + } + + + + } + diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java new file mode 100644 index 0000000000..faf2644ab1 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java @@ -0,0 +1,44 @@ +package com.coding.basic.stack; + +import java.util.Stack; +/** + * 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素 + * finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值 + * @author liuxin + * + */ +public class QuickMinStack { + + private Stack normalStack = new Stack(); + private Stack minNumStack = new Stack(); + + public void push(int data){ + + normalStack.push(data); + + if(minNumStack.isEmpty()){ + minNumStack.push(data); + } else{ + if(minNumStack.peek() >= data) { + minNumStack.push(data); + } + } + + } + public int pop(){ + if(normalStack.isEmpty()){ + throw new RuntimeException("the stack is empty"); + } + int value = normalStack.pop(); + if(value == minNumStack.peek()){ + minNumStack.pop(); + } + return value; + } + public int findMin(){ + if(minNumStack.isEmpty()){ + throw new RuntimeException("the stack is empty"); + } + return minNumStack.peek(); + } +} \ No newline at end of file diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java new file mode 100644 index 0000000000..efe41a9f8f --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java @@ -0,0 +1,39 @@ +package com.coding.basic.stack; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class QuickMinStackTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + QuickMinStack stack = new QuickMinStack(); + stack.push(5); + Assert.assertEquals(5, stack.findMin()); + stack.push(6); + Assert.assertEquals(5, stack.findMin()); + stack.push(4); + Assert.assertEquals(4, stack.findMin()); + stack.push(4); + Assert.assertEquals(4, stack.findMin()); + + stack.pop(); + Assert.assertEquals(4, stack.findMin()); + stack.pop(); + Assert.assertEquals(5, stack.findMin()); + stack.pop(); + Assert.assertEquals(5, stack.findMin()); + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java new file mode 100644 index 0000000000..fedb243604 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java @@ -0,0 +1,24 @@ +package com.coding.basic.stack; + +import com.coding.basic.array.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java new file mode 100644 index 0000000000..7c86d22fe7 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java @@ -0,0 +1,168 @@ +package com.coding.basic.stack; +import java.util.Stack; +public class StackUtil { + + public static void bad_reverse(Stack s) { + if(s == null || s.isEmpty()){ + return; + } + Stack tmpStack = new Stack(); + while(!s.isEmpty()){ + tmpStack.push(s.pop()); + } + + s = tmpStack; + + } + + + + public static void reverse_247565311(Stack s){ + if(s == null || s.isEmpty()) { + return; + } + + int size = s.size(); + Stack tmpStack = new Stack(); + + for(int i=0;ii){ + tmpStack.push(s.pop()); + } + s.push(top); + while(tmpStack.size()>0){ + s.push(tmpStack.pop()); + } + } + } + + + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + if(s == null || s.isEmpty()){ + return; + } + + Stack tmp = new Stack(); + while(!s.isEmpty()){ + tmp.push(s.pop()); + } + while(!tmp.isEmpty()){ + Integer top = tmp.pop(); + addToBottom(s,top); + } + + + } + public static void addToBottom(Stack s, Integer value){ + if(s.isEmpty()){ + s.push(value); + } else{ + Integer top = s.pop(); + addToBottom(s,value); + s.push(top); + } + + } + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s,Object o) { + if(s == null || s.isEmpty()){ + return; + } + Stack tmpStack = new Stack(); + + while(!s.isEmpty()){ + Object value = s.pop(); + if(!value.equals(o)){ + tmpStack.push(value); + } + } + + while(!tmpStack.isEmpty()){ + s.push(tmpStack.pop()); + } + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) { + + if(s == null || s.isEmpty() || s.size() stack = new Stack(); + for(int i=0;i s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + + StackUtil.addToBottom(s, 0); + + Assert.assertEquals("[0, 1, 2, 3]", s.toString()); + + } + @Test + public void testReverse() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); + StackUtil.reverse(s); + Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString()); + } + @Test + public void testReverse_247565311() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + + Assert.assertEquals("[1, 2, 3]", s.toString()); + StackUtil.reverse_247565311(s); + Assert.assertEquals("[3, 2, 1]", s.toString()); + } + @Test + public void testRemove() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + StackUtil.remove(s, 2); + Assert.assertEquals("[1, 3]", s.toString()); + } + + @Test + public void testGetTop() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + { + Object[] values = StackUtil.getTop(s, 3); + Assert.assertEquals(5, values[0]); + Assert.assertEquals(4, values[1]); + Assert.assertEquals(3, values[2]); + } + } + + @Test + public void testIsValidPairs() { + Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])")); + Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})")); + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java new file mode 100644 index 0000000000..7a58fbff56 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java @@ -0,0 +1,53 @@ +package com.coding.basic.stack; + +import java.util.ArrayDeque; +import java.util.Queue; + +public class StackWithTwoQueues { + Queue queue1 = new ArrayDeque<>(); + Queue queue2 = new ArrayDeque<>(); + + public void push(int data) { + //两个栈都为空时,优先考虑queue1 + if (queue1.isEmpty()&&queue2.isEmpty()) { + queue1.add(data); + return; + } + + if (queue1.isEmpty()) { + queue2.add(data); + return; + } + + if (queue2.isEmpty()) { + queue1.add(data); + return; + } + + } + + public int pop() { + + if (queue1.isEmpty()&&queue2.isEmpty()) { + throw new RuntimeException("stack is empty"); + } + + if (queue1.isEmpty()) { + while (queue2.size()>1) { + queue1.add(queue2.poll()); + } + return queue2.poll(); + } + + if (queue2.isEmpty()) { + while (queue1.size()>1) { + queue2.add(queue1.poll()); + } + return queue1.poll(); + } + + throw new RuntimeException("no queue is empty, this is not allowed"); + + + } +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java new file mode 100644 index 0000000000..4541b1f040 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java @@ -0,0 +1,36 @@ +package com.coding.basic.stack; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class StackWithTwoQueuesTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + StackWithTwoQueues stack = new StackWithTwoQueues(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + Assert.assertEquals(4, stack.pop()); + Assert.assertEquals(3, stack.pop()); + + stack.push(5); + Assert.assertEquals(5, stack.pop()); + Assert.assertEquals(2, stack.pop()); + Assert.assertEquals(1, stack.pop()); + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java new file mode 100644 index 0000000000..7f30ce55c8 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java @@ -0,0 +1,5 @@ +package com.coding.basic.stack; + +public class Tail { + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java new file mode 100644 index 0000000000..a532fd6e6c --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java @@ -0,0 +1,117 @@ +package com.coding.basic.stack; + +import java.util.Arrays; + +/** + * 用一个数组实现两个栈 + * 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。 + * @author liuxin + * + */ +public class TwoStackInOneArray { + private Object[] data = new Object[10]; + private int size; + private int top1, top2; + + public TwoStackInOneArray(int n){ + data = new Object[n]; + size = n; + top1 = -1; + top2 = data.length; + } + /** + * 向第一个栈中压入元素 + * @param o + */ + public void push1(Object o){ + ensureCapacity(); + data[++top1] = o; + } + /* + * 向第二个栈压入元素 + */ + public void push2(Object o){ + ensureCapacity(); + data[--top2] = o; + } + public void ensureCapacity(){ + if(top2-top1>1){ + return; + } else{ + + Object[] newArray = new Object[data.length*2]; + System.arraycopy(data, 0, newArray, 0, top1+1); + + int stack2Size = data.length-top2; + int newTop2 = newArray.length-stack2Size; + System.arraycopy(data, top2, newArray, newTop2, stack2Size); + + top2 = newTop2; + data = newArray; + } + } + /** + * 从第一个栈中弹出元素 + * @return + */ + public Object pop1(){ + if(top1 == -1){ + throw new RuntimeException("Stack1 is empty"); + } + Object o = data[top1]; + data[top1] = null; + top1--; + return o; + + } + /** + * 从第二个栈弹出元素 + * @return + */ + public Object pop2(){ + if(top2 == data.length){ + throw new RuntimeException("Stack2 is empty"); + } + Object o = data[top2]; + data[top2] = null; + top2++; + return o; + } + /** + * 获取第一个栈的栈顶元素 + * @return + */ + + public Object peek1(){ + if(top1 == -1){ + throw new RuntimeException("Stack1 is empty"); + } + return data[top1]; + } + + + /** + * 获取第二个栈的栈顶元素 + * @return + */ + + public Object peek2(){ + if(top2 == data.length){ + throw new RuntimeException("Stack2 is empty"); + } + return data[top2]; + } + + public Object[] stack1ToArray(){ + return Arrays.copyOf(data, top1+1); + } + public Object[] stack2ToArray(){ + int size = data.length-top2; + Object [] stack2Data = new Object[size]; + int j=0; + for(int i=data.length-1; i>=top2 ;i--){ + stack2Data[j++] = data[i]; + } + return stack2Data; + } +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java new file mode 100644 index 0000000000..b743d422c6 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java @@ -0,0 +1,65 @@ +package com.coding.basic.stack; + +import java.util.Arrays; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class TwoStackInOneArrayTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test1() { + TwoStackInOneArray stack = new TwoStackInOneArray(10); + stack.push1(1); + stack.push1(2); + stack.push1(3); + stack.push1(4); + stack.push1(5); + + stack.push2(1); + stack.push2(2); + stack.push2(3); + stack.push2(4); + stack.push2(5); + + for(int i=1;i<=5;i++){ + Assert.assertEquals(stack.peek1(), stack.peek2()); + Assert.assertEquals(stack.pop1(), stack.pop2()); + } + + + } + @Test + public void test2() { + TwoStackInOneArray stack = new TwoStackInOneArray(5); + stack.push1(1); + stack.push1(2); + stack.push1(3); + stack.push1(4); + stack.push1(5); + stack.push1(6); + stack.push1(7); + + stack.push2(1); + stack.push2(2); + stack.push2(3); + stack.push2(4); + + + Assert.assertEquals("[1, 2, 3, 4, 5, 6, 7]",Arrays.toString(stack.stack1ToArray())); + Assert.assertEquals("[1, 2, 3, 4]",Arrays.toString(stack.stack2ToArray())); + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java new file mode 100644 index 0000000000..cebef21fa3 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java @@ -0,0 +1,72 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + + +public class InfixExpr { + String expr = null; + + public InfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + + + TokenParser parser = new TokenParser(); + List tokens = parser.parse(this.expr); + + + Stack opStack = new Stack<>(); + Stack numStack = new Stack<>(); + + for(Token token : tokens){ + + if (token.isOperator()){ + + while(!opStack.isEmpty() + && !token.hasHigherPriority(opStack.peek())){ + Token prevOperator = opStack.pop(); + Float f2 = numStack.pop(); + Float f1 = numStack.pop(); + Float result = calculate(prevOperator.toString(), f1,f2); + numStack.push(result); + + } + opStack.push(token); + } + if(token.isNumber()){ + numStack.push(new Float(token.getIntValue())); + } + } + + while(!opStack.isEmpty()){ + Token token = opStack.pop(); + Float f2 = numStack.pop(); + Float f1 = numStack.pop(); + numStack.push(calculate(token.toString(), f1,f2)); + } + + + return numStack.pop().floatValue(); + } + private Float calculate(String op, Float f1, Float f2){ + if(op.equals("+")){ + return f1+f2; + } + if(op.equals("-")){ + return f1-f2; + } + if(op.equals("*")){ + return f1*f2; + } + if(op.equals("/")){ + return f1/f2; + } + throw new RuntimeException(op + " is not supported"); + } + + + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java new file mode 100644 index 0000000000..20e34e8852 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java @@ -0,0 +1,52 @@ +package com.coding.basic.stack.expr; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class InfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); + { + InfixExpr expr = new InfixExpr("2+3*4+5"); + Assert.assertEquals(19.0, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); + Assert.assertEquals(100.0, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("3*20/2"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("20/2*3"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("10-30+50"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("10-2*3+50"); + Assert.assertEquals(54, expr.evaluate(), 0.001f); + } + + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java new file mode 100644 index 0000000000..9e501eda20 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java @@ -0,0 +1,43 @@ +package com.coding.basic.stack.expr; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class InfixToPostfix { + + public static List convert(String expr) { + List inFixTokens = new TokenParser().parse(expr); + + List postFixTokens = new ArrayList<>(); + + Stack opStack = new Stack(); + for(Token token : inFixTokens){ + + if(token.isOperator()){ + + while(!opStack.isEmpty() + && !token.hasHigherPriority(opStack.peek())){ + postFixTokens.add(opStack.pop()); + + } + opStack.push(token); + + } + if(token.isNumber()){ + + postFixTokens.add(token); + + } + } + + while(!opStack.isEmpty()){ + postFixTokens.add(opStack.pop()); + } + + return postFixTokens; + } + + + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java new file mode 100644 index 0000000000..f879f55f14 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java @@ -0,0 +1,41 @@ +package com.coding.basic.stack.expr; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class InfixToPostfixTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testConvert() { + { + List tokens = InfixToPostfix.convert("2+3"); + Assert.assertEquals("[2, 3, +]", tokens.toString()); + } + { + + List tokens = InfixToPostfix.convert("2+3*4"); + Assert.assertEquals("[2, 3, 4, *, +]", tokens.toString()); + } + + { + + List tokens = InfixToPostfix.convert("2-3*4+5"); + Assert.assertEquals("[2, 3, 4, *, -, 5, +]", tokens.toString()); + } + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java new file mode 100644 index 0000000000..c54eb69e2a --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java @@ -0,0 +1,46 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + +public class PostfixExpr { +String expr = null; + + public PostfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + TokenParser parser = new TokenParser(); + List tokens = parser.parse(this.expr); + + + Stack numStack = new Stack<>(); + for(Token token : tokens){ + if(token.isNumber()){ + numStack.push(new Float(token.getIntValue())); + } else{ + Float f2 = numStack.pop(); + Float f1 = numStack.pop(); + numStack.push(calculate(token.toString(),f1,f2)); + } + } + return numStack.pop().floatValue(); + } + + private Float calculate(String op, Float f1, Float f2){ + if(op.equals("+")){ + return f1+f2; + } + if(op.equals("-")){ + return f1-f2; + } + if(op.equals("*")){ + return f1*f2; + } + if(op.equals("/")){ + return f1/f2; + } + throw new RuntimeException(op + " is not supported"); + } +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java new file mode 100644 index 0000000000..c0435a2db5 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java @@ -0,0 +1,41 @@ +package com.coding.basic.stack.expr; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class PostfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + { + PostfixExpr expr = new PostfixExpr("6 5 2 3 + 8 * + 3 + *"); + Assert.assertEquals(288, expr.evaluate(),0.0f); + } + { + //9+(3-1)*3+10/2 + PostfixExpr expr = new PostfixExpr("9 3 1-3*+ 10 2/+"); + Assert.assertEquals(20, expr.evaluate(),0.0f); + } + + { + //10-2*3+50 + PostfixExpr expr = new PostfixExpr("10 2 3 * - 50 +"); + Assert.assertEquals(54, expr.evaluate(),0.0f); + } + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java new file mode 100644 index 0000000000..f811fd6d9a --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java @@ -0,0 +1,52 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + +public class PrefixExpr { + String expr = null; + + public PrefixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + TokenParser parser = new TokenParser(); + List tokens = parser.parse(this.expr); + + Stack exprStack = new Stack<>(); + Stack numStack = new Stack<>(); + for(Token token : tokens){ + exprStack.push(token); + } + + while(!exprStack.isEmpty()){ + Token t = exprStack.pop(); + if(t.isNumber()){ + numStack.push(new Float(t.getIntValue())); + }else{ + Float f1 = numStack.pop(); + Float f2 = numStack.pop(); + numStack.push(calculate(t.toString(),f1,f2)); + + } + } + return numStack.pop().floatValue(); + } + + private Float calculate(String op, Float f1, Float f2){ + if(op.equals("+")){ + return f1+f2; + } + if(op.equals("-")){ + return f1-f2; + } + if(op.equals("*")){ + return f1*f2; + } + if(op.equals("/")){ + return f1/f2; + } + throw new RuntimeException(op + " is not supported"); + } +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java new file mode 100644 index 0000000000..5cec210e75 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java @@ -0,0 +1,45 @@ +package com.coding.basic.stack.expr; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class PrefixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + { + // 2*3+4*5 + PrefixExpr expr = new PrefixExpr("+ * 2 3* 4 5"); + Assert.assertEquals(26, expr.evaluate(),0.001f); + } + { + // 4*2 + 6+9*2/3 -8 + PrefixExpr expr = new PrefixExpr("-++6/*2 9 3 * 4 2 8"); + Assert.assertEquals(12, expr.evaluate(),0.001f); + } + { + //(3+4)*5-6 + PrefixExpr expr = new PrefixExpr("- * + 3 4 5 6"); + Assert.assertEquals(29, expr.evaluate(),0.001f); + } + { + //1+((2+3)*4)-5 + PrefixExpr expr = new PrefixExpr("- + 1 * + 2 3 4 5"); + Assert.assertEquals(16, expr.evaluate(),0.001f); + } + + + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java new file mode 100644 index 0000000000..8579743fe9 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java @@ -0,0 +1,50 @@ +package com.coding.basic.stack.expr; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +class Token { + public static final List OPERATORS = Arrays.asList("+", "-", "*", "/"); + private static final Map priorities = new HashMap<>(); + static { + priorities.put("+", 1); + priorities.put("-", 1); + priorities.put("*", 2); + priorities.put("/", 2); + } + static final int OPERATOR = 1; + static final int NUMBER = 2; + String value; + int type; + public Token(int type, String value){ + this.type = type; + this.value = value; + } + + public boolean isNumber() { + return type == NUMBER; + } + + public boolean isOperator() { + return type == OPERATOR; + } + + public int getIntValue() { + return Integer.valueOf(value).intValue(); + } + public String toString(){ + return value; + } + + public boolean hasHigherPriority(Token t){ + if(!this.isOperator() && !t.isOperator()){ + throw new RuntimeException("numbers can't compare priority"); + } + return priorities.get(this.value) - priorities.get(t.value) > 0; + } + + + +} \ No newline at end of file diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java new file mode 100644 index 0000000000..d3b0f167e1 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java @@ -0,0 +1,57 @@ +package com.coding.basic.stack.expr; + +import java.util.ArrayList; +import java.util.List; + +public class TokenParser { + + + public List parse(String expr) { + List tokens = new ArrayList<>(); + + int i = 0; + + while (i < expr.length()) { + + char c = expr.charAt(i); + + if (isOperator(c)) { + + Token t = new Token(Token.OPERATOR, String.valueOf(c)); + tokens.add(t); + i++; + + } else if (Character.isDigit(c)) { + + int nextOperatorIndex = indexOfNextOperator(i, expr); + String value = expr.substring(i, nextOperatorIndex); + Token t = new Token(Token.NUMBER, value); + tokens.add(t); + i = nextOperatorIndex; + + } else{ + System.out.println("char :["+c+"] is not number or operator,ignore"); + i++; + } + + } + return tokens; + } + + private int indexOfNextOperator(int i, String expr) { + + while (Character.isDigit(expr.charAt(i))) { + i++; + if (i == expr.length()) { + break; + } + } + return i; + + } + + private boolean isOperator(char c) { + String sc = String.valueOf(c); + return Token.OPERATORS.contains(sc); + } +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java new file mode 100644 index 0000000000..399d3e857e --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java @@ -0,0 +1,41 @@ +package com.coding.basic.stack.expr; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TokenParserTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + + TokenParser parser = new TokenParser(); + List tokens = parser.parse("300*20+12*5-20/4"); + + Assert.assertEquals(300, tokens.get(0).getIntValue()); + Assert.assertEquals("*", tokens.get(1).toString()); + Assert.assertEquals(20, tokens.get(2).getIntValue()); + Assert.assertEquals("+", tokens.get(3).toString()); + Assert.assertEquals(12, tokens.get(4).getIntValue()); + Assert.assertEquals("*", tokens.get(5).toString()); + Assert.assertEquals(5, tokens.get(6).getIntValue()); + Assert.assertEquals("-", tokens.get(7).toString()); + Assert.assertEquals(20, tokens.get(8).getIntValue()); + Assert.assertEquals("/", tokens.get(9).toString()); + Assert.assertEquals(4, tokens.get(10).getIntValue()); + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java new file mode 100644 index 0000000000..284e5b0011 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java @@ -0,0 +1,189 @@ +package com.coding.basic.tree; + +import java.util.ArrayList; +import java.util.List; + +import com.coding.basic.queue.Queue; + + +public class BinarySearchTree { + + BinaryTreeNode root; + public BinarySearchTree(BinaryTreeNode root){ + this.root = root; + } + public BinaryTreeNode getRoot(){ + return root; + } + public T findMin(){ + if(root == null){ + return null; + } + return findMin(root).data; + } + public T findMax(){ + if(root == null){ + return null; + } + return findMax(root).data; + } + public int height() { + return height(root); + } + public int size() { + return size(root); + } + public void remove(T e){ + remove(e, root); + } + + private BinaryTreeNode remove(T x, BinaryTreeNode t){ + if(t == null){ + return t; + } + int compareResult = x.compareTo(t.data); + + if(compareResult< 0 ){ + t.left = remove(x,t.left); + + } else if(compareResult > 0){ + t.right = remove(x, t.right); + + } else { + if(t.left != null && t.right != null){ + + t.data = findMin(t.right).data; + t.right = remove(t.data,t.right); + + } else{ + t = (t.left != null) ? t.left : t.right; + } + } + return t; + } + + private BinaryTreeNode findMin(BinaryTreeNode p){ + if (p==null){ + return null; + } else if (p.left == null){ + return p; + } else{ + return findMin(p.left); + } + } + private BinaryTreeNode findMax(BinaryTreeNode p){ + if (p==null){ + return null; + }else if (p.right==null){ + return p; + } else{ + return findMax(p.right); + } + } + private int height(BinaryTreeNode t){ + if (t==null){ + return 0; + }else { + int leftChildHeight=height(t.left); + int rightChildHeight=height(t.right); + if(leftChildHeight > rightChildHeight){ + return leftChildHeight+1; + } else{ + return rightChildHeight+1; + } + } + } + private int size(BinaryTreeNode t){ + if (t == null){ + return 0; + } + return size(t.left) + 1 + size(t.right); + + } + + public List levelVisit(){ + List result = new ArrayList(); + if(root == null){ + return result; + } + Queue> queue = new Queue>(); + BinaryTreeNode node = root; + queue.enQueue(node); + while (!queue.isEmpty()) { + node = queue.deQueue(); + result.add(node.data); + if (node.left != null){ + queue.enQueue(node.left); + } + if (node.right != null){ + queue.enQueue(node.right); + } + } + return result; + } + public boolean isValid(){ + return isValid(root); + } + public T getLowestCommonAncestor(T n1, T n2){ + if (root == null){ + return null; + } + return lowestCommonAncestor(root,n1,n2); + + } + public List getNodesBetween(T n1, T n2){ + List elements = new ArrayList<>(); + getNodesBetween(elements,root,n1,n2); + return elements; + } + + public void getNodesBetween(List elements ,BinaryTreeNode node, T n1, T n2){ + + if (node == null) { + return; + } + + if (n1.compareTo(node.data) < 0) { + getNodesBetween(elements,node.left, n1, n2); + } + + if ((n1.compareTo(node.data) <= 0 ) + && (n2.compareTo(node.data) >= 0 )) { + elements.add(node.data); + } + if (n2.compareTo(node.data)>0) { + getNodesBetween(elements,node.right, n1, n2); + } + } + private T lowestCommonAncestor(BinaryTreeNode node,T n1, T n2){ + if(node == null){ + return null; + } + // 如果n1和n2都比 node的值小, LCA在左孩子 + if (node.data.compareTo(n1) > 0 && node.data.compareTo(n2) >0){ + return lowestCommonAncestor(node.left, n1, n2); + } + + // 如果n1和n2都比 node的值小, LCA在右孩子 + if (node.data.compareTo(n1) < 0 && node.data.compareTo(n2) <0) + return lowestCommonAncestor(node.right, n1, n2); + + return node.data; + } + private boolean isValid(BinaryTreeNode t){ + if(t == null){ + return true; + } + if(t.left != null && findMax(t.left).data.compareTo(t.data) >0){ + return false; + } + if(t.right !=null && findMin(t.right).data.compareTo(t.data) <0){ + return false; + } + if(!isValid(t.left) || !isValid(t.right)){ + return false; + } + return true; + } +} + diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java new file mode 100644 index 0000000000..590e60306c --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java @@ -0,0 +1,108 @@ +package com.coding.basic.tree; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class BinarySearchTreeTest { + + BinarySearchTree tree = null; + + @Before + public void setUp() throws Exception { + BinaryTreeNode root = new BinaryTreeNode(6); + root.left = new BinaryTreeNode(2); + root.right = new BinaryTreeNode(8); + root.left.left = new BinaryTreeNode(1); + root.left.right = new BinaryTreeNode(4); + root.left.right.left = new BinaryTreeNode(3); + root.left.right.right = new BinaryTreeNode(5); + tree = new BinarySearchTree(root); + } + + @After + public void tearDown() throws Exception { + tree = null; + } + + @Test + public void testFindMin() { + Assert.assertEquals(1, tree.findMin().intValue()); + + } + + @Test + public void testFindMax() { + Assert.assertEquals(8, tree.findMax().intValue()); + } + + @Test + public void testHeight() { + Assert.assertEquals(4, tree.height()); + } + + @Test + public void testSize() { + Assert.assertEquals(7, tree.size()); + } + + @Test + public void testRemoveLeaf() { + tree.remove(3); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(4, root.left.right.data.intValue()); + + } + @Test + public void testRemoveMiddleNode1() { + tree.remove(4); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(5, root.left.right.data.intValue()); + Assert.assertEquals(3, root.left.right.left.data.intValue()); + } + @Test + public void testRemoveMiddleNode2() { + tree.remove(2); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(3, root.left.data.intValue()); + Assert.assertEquals(4, root.left.right.data.intValue()); + } + + @Test + public void testLevelVisit() { + List values = tree.levelVisit(); + Assert.assertEquals("[6, 2, 8, 1, 4, 3, 5]", values.toString()); + + } + @Test + public void testLCA(){ + Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 5).intValue()); + Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 4).intValue()); + Assert.assertEquals(6,tree.getLowestCommonAncestor(3, 8).intValue()); + } + @Test + public void testIsValid() { + + Assert.assertTrue(tree.isValid()); + + BinaryTreeNode root = new BinaryTreeNode(6); + root.left = new BinaryTreeNode(2); + root.right = new BinaryTreeNode(8); + root.left.left = new BinaryTreeNode(4); + root.left.right = new BinaryTreeNode(1); + root.left.right.left = new BinaryTreeNode(3); + tree = new BinarySearchTree(root); + + Assert.assertFalse(tree.isValid()); + } + @Test + public void testGetNodesBetween(){ + List numbers = this.tree.getNodesBetween(3, 8); + Assert.assertEquals("[3, 4, 5, 6, 8]",numbers.toString()); + } +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java new file mode 100644 index 0000000000..3f6f4d2b44 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java @@ -0,0 +1,36 @@ +package com.coding.basic.tree; + +public class BinaryTreeNode { + + public T data; + public BinaryTreeNode left; + public BinaryTreeNode right; + + public BinaryTreeNode(T data){ + this.data=data; + } + public T getData() { + return data; + } + public void setData(T data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java new file mode 100644 index 0000000000..f2a6515fa6 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java @@ -0,0 +1,116 @@ +package com.coding.basic.tree; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class BinaryTreeUtil { + /** + * 用递归的方式实现对二叉树的前序遍历, 需要通过BinaryTreeUtilTest测试 + * + * @param root + * @return + */ + public static List preOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + preOrderVisit(root, result); + return result; + } + + /** + * 用递归的方式实现对二叉树的中遍历 + * + * @param root + * @return + */ + public static List inOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + inOrderVisit(root, result); + return result; + } + + /** + * 用递归的方式实现对二叉树的后遍历 + * + * @param root + * @return + */ + public static List postOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + postOrderVisit(root, result); + return result; + } + + public static List preOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + Stack> stack = new Stack>(); + + BinaryTreeNode node = root; + + if(node != null){ + stack.push(node); + } + + while(!stack.isEmpty()){ + node = stack.pop(); + result.add(node.data); + + if(node.right != null){ + stack.push(node.right); + } + + if(node.left != null){ + stack.push(node.right); + } + } + return result; + } + + public static List inOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + BinaryTreeNode node = root; + Stack> stack = new Stack>(); + + while (node != null || !stack.isEmpty()) { + + while (node != null) { + stack.push(node); + node = node.left; + } + BinaryTreeNode currentNode = stack.pop(); + result.add(currentNode.data); + node = currentNode.right; + } + return result; + } + + private static void preOrderVisit(BinaryTreeNode node, List result) { + if (node == null) { + return; + } + result.add(node.getData()); + preOrderVisit(node.getLeft(), result); + preOrderVisit(node.getRight(), result); + } + + private static void inOrderVisit(BinaryTreeNode node, List result) { + if (node == null) { + return; + } + inOrderVisit(node.getLeft(), result); + result.add(node.getData()); + inOrderVisit(node.getRight(), result); + } + + private static void postOrderVisit(BinaryTreeNode node, List result) { + if (node == null) { + return; + } + postOrderVisit(node.getLeft(), result); + postOrderVisit(node.getRight(), result); + result.add(node.getData()); + } + +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java new file mode 100644 index 0000000000..41857e137d --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java @@ -0,0 +1,75 @@ +package com.coding.basic.tree; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class BinaryTreeUtilTest { + + BinaryTreeNode root = null; + @Before + public void setUp() throws Exception { + root = new BinaryTreeNode(1); + root.setLeft(new BinaryTreeNode(2)); + root.setRight(new BinaryTreeNode(5)); + root.getLeft().setLeft(new BinaryTreeNode(3)); + root.getLeft().setRight(new BinaryTreeNode(4)); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testPreOrderVisit() { + + List result = BinaryTreeUtil.preOrderVisit(root); + Assert.assertEquals("[1, 2, 3, 4, 5]", result.toString()); + + + } + @Test + public void testInOrderVisit() { + + + List result = BinaryTreeUtil.inOrderVisit(root); + Assert.assertEquals("[3, 2, 4, 1, 5]", result.toString()); + + } + + @Test + public void testPostOrderVisit() { + + + List result = BinaryTreeUtil.postOrderVisit(root); + Assert.assertEquals("[3, 4, 2, 5, 1]", result.toString()); + + } + + + @Test + public void testInOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.inOrderWithoutRecursion(root); + Assert.assertEquals("[3, 2, 6, 4, 7, 1, 5]", result.toString()); + + } + @Test + public void testPreOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.preOrderWithoutRecursion(root); + Assert.assertEquals("[1, 2, 3, 4, 6, 7, 5]", result.toString()); + + } +} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java new file mode 100644 index 0000000000..85fb8ab2a4 --- /dev/null +++ b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java @@ -0,0 +1,34 @@ +package com.coding.basic.tree; + +import java.io.File; + +public class FileList { + public void list(File f) { + list(f, 0); + } + + public void list(File f, int depth) { + printName(f, depth); + if (f.isDirectory()) { + File[] files = f.listFiles(); + for (File i : files) + list(i, depth + 1); + } + } + + void printName(File f, int depth) { + String name = f.getName(); + for (int i = 0; i < depth; i++) + System.out.print("+"); + if (f.isDirectory()) + System.out.println("Dir: " + name); + else + System.out.println(f.getName() + " " + f.length()); + } + + public static void main(String args[]) { + FileList L = new FileList(); + File f = new File("C:\\coderising\\tmp"); + L.list(f); + } +} diff --git a/students/592146505/data-structure/assignment/pom.xml b/students/592146505/data-structure/assignment/pom.xml new file mode 100644 index 0000000000..5024466d17 --- /dev/null +++ b/students/592146505/data-structure/assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ds-assignment + 0.0.1-SNAPSHOT + jar + + ds-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/DownloadThread.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..900a3ad358 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/DownloadThread.java @@ -0,0 +1,20 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/FileDownloader.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..c3c8a3f27d --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/FileDownloader.java @@ -0,0 +1,73 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn,0,length-1).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/FileDownloaderTest.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..4ff7f46ae0 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/api/Connection.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionException.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionManager.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/api/DownloadListener.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionImpl.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..36a9d2ce15 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,27 @@ +package com.coderising.download.impl; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + @Override + public int getContentLength() { + + return 0; + } + + @Override + public void close() { + + + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..172371dd55 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return null; + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/LoginAction.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/Struts.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/StrutsTest.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/View.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/struts.xml b/students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Course.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Course.java new file mode 100644 index 0000000000..436d092f58 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Course.java @@ -0,0 +1,24 @@ +package com.coderising.ood.course.bad; + +import java.util.List; + +public class Course { + private String id; + private String desc; + private int duration ; + + List prerequisites; + + public List getPrerequisites() { + return prerequisites; + } + + + public boolean equals(Object o){ + if(o == null || !(o instanceof Course)){ + return false; + } + Course c = (Course)o; + return (c != null) && c.id.equals(id); + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseOffering.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseOffering.java new file mode 100644 index 0000000000..ab8c764584 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseOffering.java @@ -0,0 +1,26 @@ +package com.coderising.ood.course.bad; + +import java.util.ArrayList; +import java.util.List; + + +public class CourseOffering { + private Course course; + private String location; + private String teacher; + private int maxStudents; + + List students = new ArrayList(); + + public int getMaxStudents() { + return maxStudents; + } + + public List getStudents() { + return students; + } + + public Course getCourse() { + return course; + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseService.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseService.java new file mode 100644 index 0000000000..8c34bad0c3 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseService.java @@ -0,0 +1,16 @@ +package com.coderising.ood.course.bad; + + + +public class CourseService { + + public void chooseCourse(Student student, CourseOffering sc){ + //如果学生上过该科目的先修科目,并且该课程还未满, 则学生可以加入该课程 + if(student.getCoursesAlreadyTaken().containsAll( + sc.getCourse().getPrerequisites()) + && sc.getMaxStudents() > sc.getStudents().size()){ + sc.getStudents().add(student); + } + + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Student.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Student.java new file mode 100644 index 0000000000..a651923ef5 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Student.java @@ -0,0 +1,14 @@ +package com.coderising.ood.course.bad; + +import java.util.ArrayList; +import java.util.List; + +public class Student { + private String id; + private String name; + private List coursesAlreadyTaken = new ArrayList(); + + public List getCoursesAlreadyTaken() { + return coursesAlreadyTaken; + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Course.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Course.java new file mode 100644 index 0000000000..aefc9692bb --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Course.java @@ -0,0 +1,18 @@ +package com.coderising.ood.course.good; + +import java.util.List; + +public class Course { + private String id; + private String desc; + private int duration ; + + List prerequisites; + + public List getPrerequisites() { + return prerequisites; + } + +} + + diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseOffering.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseOffering.java new file mode 100644 index 0000000000..8660ec8109 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseOffering.java @@ -0,0 +1,34 @@ +package com.coderising.ood.course.good; + +import java.util.ArrayList; +import java.util.List; + +public class CourseOffering { + private Course course; + private String location; + private String teacher; + private int maxStudents; + + List students = new ArrayList(); + + public List getStudents() { + return students; + } + public int getMaxStudents() { + return maxStudents; + } + public Course getCourse() { + return course; + } + + + // 第二步: 把主要逻辑移动到CourseOffering 中 + public void addStudent(Student student){ + + if(student.canAttend(course) + && this.maxStudents > students.size()){ + students.add(student); + } + } + // 第三步: 重构CourseService +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseService.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseService.java new file mode 100644 index 0000000000..22ba4a5450 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseService.java @@ -0,0 +1,14 @@ +package com.coderising.ood.course.good; + + + +public class CourseService { + + public void chooseCourse(Student student, CourseOffering sc){ + //第一步:重构: canAttend , 但是还有问题 + if(student.canAttend(sc.getCourse()) + && sc.getMaxStudents() > sc.getStudents().size()){ + sc.getStudents().add(student); + } + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Student.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Student.java new file mode 100644 index 0000000000..2c7e128b2a --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Student.java @@ -0,0 +1,21 @@ +package com.coderising.ood.course.good; + +import java.util.ArrayList; +import java.util.List; + +public class Student { + private String id; + private String name; + private List coursesAlreadyTaken = new ArrayList(); + + public List getCoursesAlreadyTaken() { + return coursesAlreadyTaken; + } + + public boolean canAttend(Course course){ + return this.coursesAlreadyTaken.containsAll( + course.getPrerequisites()); + } +} + + diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java new file mode 100644 index 0000000000..b6cf28c096 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class DateUtil { + + public static String getCurrentDateAsString() { + + return null; + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/ocp/Logger.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/ocp/Logger.java new file mode 100644 index 0000000000..0357c4d912 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/ocp/Logger.java @@ -0,0 +1,38 @@ +package com.coderising.ood.ocp; + +public class Logger { + + public final int RAW_LOG = 1; + public final int RAW_LOG_WITH_DATE = 2; + public final int EMAIL_LOG = 1; + public final int SMS_LOG = 2; + public final int PRINT_LOG = 3; + + int type = 0; + int method = 0; + + public Logger(int logType, int logMethod){ + this.type = logType; + this.method = logMethod; + } + public void log(String msg){ + + String logMsg = msg; + + if(this.type == RAW_LOG){ + logMsg = msg; + } else if(this.type == RAW_LOG_WITH_DATE){ + String txtDate = DateUtil.getCurrentDateAsString(); + logMsg = txtDate + ": " + msg; + } + + if(this.method == EMAIL_LOG){ + MailUtil.send(logMsg); + } else if(this.method == SMS_LOG){ + SMSUtil.send(logMsg); + } else if(this.method == PRINT_LOG){ + System.out.println(logMsg); + } + } +} + diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java new file mode 100644 index 0000000000..ec54b839c5 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class MailUtil { + + public static void send(String logMsg) { + // TODO Auto-generated method stub + + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java new file mode 100644 index 0000000000..13cf802418 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class SMSUtil { + + public static void send(String logMsg) { + // TODO Auto-generated method stub + + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..781587a846 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..a98917f829 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo R15 +P4955 Vivo X20 \ No newline at end of file diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/Iterator.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/List.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayList.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayList.java new file mode 100644 index 0000000000..4576c016af --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayList.java @@ -0,0 +1,35 @@ +package com.coding.basic.array; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayUtil.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayUtil.java new file mode 100644 index 0000000000..45740e6d57 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coding.basic.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrame.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..994a241a3d --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,57 @@ +package com.coding.basic.linklist; + + +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + private int currentSize; + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + this.currentSize = 0; + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + + + } + + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + + + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..7fd72fc2b4 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,34 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + frame.access(5); + Assert.assertEquals("5,4,0", frame.toString()); + + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/linklist/LinkedList.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/linklist/LinkedList.java new file mode 100644 index 0000000000..f4c7556a2e --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/linklist/LinkedList.java @@ -0,0 +1,125 @@ +package com.coding.basic.linklist; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/CircleQueue.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/CircleQueue.java new file mode 100644 index 0000000000..2e0550c67e --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/CircleQueue.java @@ -0,0 +1,39 @@ +package com.coding.basic.queue; + +/** + * 用数组实现循环队列 + * @author liuxin + * + * @param + */ +public class CircleQueue { + + private final static int DEFAULT_SIZE = 10; + + //用数组来保存循环队列的元素 + private Object[] elementData = new Object[DEFAULT_SIZE] ; + + //队头 + private int front = 0; + //队尾 + private int rear = 0; + + public boolean isEmpty() { + return false; + + } + + public int size() { + return -1; + } + + + + public void enQueue(E data) { + + } + + public E deQueue() { + return null; + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/Josephus.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/Josephus.java new file mode 100644 index 0000000000..6a3ea639b9 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/Josephus.java @@ -0,0 +1,18 @@ +package com.coding.basic.queue; + +import java.util.List; + +/** + * 用Queue来实现Josephus问题 + * 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来 + * 该方法返回一个List, 包含了被杀死人的次序 + * @author liuxin + * + */ +public class Josephus { + + public static List execute(int n, int m){ + return null; + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/JosephusTest.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/JosephusTest.java new file mode 100644 index 0000000000..7d90318b51 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/JosephusTest.java @@ -0,0 +1,27 @@ +package com.coding.basic.queue; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class JosephusTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testExecute() { + + Assert.assertEquals("[1, 3, 5, 0, 4, 2, 6]", Josephus.execute(7, 2).toString()); + + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/Queue.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/Queue.java new file mode 100644 index 0000000000..c4c4b7325e --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/Queue.java @@ -0,0 +1,61 @@ +package com.coding.basic.queue; + +import java.util.NoSuchElementException; + +public class Queue { + private Node first; + private Node last; + private int size; + + + private static class Node { + private E item; + private Node next; + } + + + public Queue() { + first = null; + last = null; + size = 0; + } + + + public boolean isEmpty() { + return first == null; + } + + public int size() { + return size; + } + + + + public void enQueue(E data) { + Node oldlast = last; + last = new Node(); + last.item = data; + last.next = null; + if (isEmpty()) { + first = last; + } + else{ + oldlast.next = last; + } + size++; + } + + public E deQueue() { + if (isEmpty()) { + throw new NoSuchElementException("Queue underflow"); + } + E item = first.item; + first = first.next; + size--; + if (isEmpty()) { + last = null; + } + return item; + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java new file mode 100644 index 0000000000..cef19a8b59 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java @@ -0,0 +1,47 @@ +package com.coding.basic.queue; + +import java.util.Stack; + +/** + * 用两个栈来实现一个队列 + * @author liuxin + * + * @param + */ +public class QueueWithTwoStacks { + private Stack stack1; + private Stack stack2; + + + public QueueWithTwoStacks() { + stack1 = new Stack(); + stack2 = new Stack(); + } + + + + + public boolean isEmpty() { + return false; + } + + + + public int size() { + return -1; + } + + + + public void enQueue(E item) { + + } + + public E deQueue() { + return null; + } + + + + } + diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/QuickMinStack.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/QuickMinStack.java new file mode 100644 index 0000000000..f391d92b8f --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/QuickMinStack.java @@ -0,0 +1,19 @@ +package com.coding.basic.stack; + +/** + * 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素 + * finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值 + * @author liuxin + * + */ +public class QuickMinStack { + public void push(int data){ + + } + public int pop(){ + return -1; + } + public int findMin(){ + return -1; + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/Stack.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/Stack.java new file mode 100644 index 0000000000..fedb243604 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/Stack.java @@ -0,0 +1,24 @@ +package com.coding.basic.stack; + +import com.coding.basic.array.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtil.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtil.java new file mode 100644 index 0000000000..b0ec38161d --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtil.java @@ -0,0 +1,48 @@ +package com.coding.basic.stack; +import java.util.Stack; +public class StackUtil { + + + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + + + + } + + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s,Object o) { + + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) { + return null; + } + /** + * 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz + * 使用堆栈检查字符串s中的括号是不是成对出现的。 + * 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true + * 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; + * @param s + * @return + */ + public static boolean isValidPairs(String s){ + return false; + } + + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtilTest.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtilTest.java new file mode 100644 index 0000000000..76f2cb7668 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtilTest.java @@ -0,0 +1,65 @@ +package com.coding.basic.stack; + +import java.util.Stack; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +public class StackUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + + @Test + public void testReverse() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); + StackUtil.reverse(s); + Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString()); + } + + @Test + public void testRemove() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + StackUtil.remove(s, 2); + Assert.assertEquals("[1, 3]", s.toString()); + } + + @Test + public void testGetTop() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + { + Object[] values = StackUtil.getTop(s, 3); + Assert.assertEquals(5, values[0]); + Assert.assertEquals(4, values[1]); + Assert.assertEquals(3, values[2]); + } + } + + @Test + public void testIsValidPairs() { + Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])")); + Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})")); + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java new file mode 100644 index 0000000000..d0ab4387d2 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java @@ -0,0 +1,16 @@ +package com.coding.basic.stack; + + +public class StackWithTwoQueues { + + + public void push(int data) { + + } + + public int pop() { + return -1; + } + + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java new file mode 100644 index 0000000000..e86d056a24 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java @@ -0,0 +1,57 @@ +package com.coding.basic.stack; + +/** + * 用一个数组实现两个栈 + * 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。 + * @author liuxin + * + */ +public class TwoStackInOneArray { + Object[] data = new Object[10]; + + /** + * 向第一个栈中压入元素 + * @param o + */ + public void push1(Object o){ + + } + /** + * 从第一个栈中弹出元素 + * @return + */ + public Object pop1(){ + return null; + } + + /** + * 获取第一个栈的栈顶元素 + * @return + */ + + public Object peek1(){ + return null; + } + /* + * 向第二个栈压入元素 + */ + public void push2(Object o){ + + } + /** + * 从第二个栈弹出元素 + * @return + */ + public Object pop2(){ + return null; + } + /** + * 获取第二个栈的栈顶元素 + * @return + */ + + public Object peek2(){ + return null; + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExpr.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExpr.java new file mode 100644 index 0000000000..ef85ff007f --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExpr.java @@ -0,0 +1,15 @@ +package com.coding.basic.stack.expr; + +public class InfixExpr { + String expr = null; + + public InfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + return 0.0f; + } + + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java new file mode 100644 index 0000000000..20e34e8852 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java @@ -0,0 +1,52 @@ +package com.coding.basic.stack.expr; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class InfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); + { + InfixExpr expr = new InfixExpr("2+3*4+5"); + Assert.assertEquals(19.0, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); + Assert.assertEquals(100.0, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("3*20/2"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("20/2*3"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("10-30+50"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("10-2*3+50"); + Assert.assertEquals(54, expr.evaluate(), 0.001f); + } + + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java new file mode 100644 index 0000000000..96a2194a67 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java @@ -0,0 +1,14 @@ +package com.coding.basic.stack.expr; + +import java.util.List; + +public class InfixToPostfix { + + public static List convert(String expr) { + + return null; + } + + + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java new file mode 100644 index 0000000000..dcbb18be4b --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java @@ -0,0 +1,18 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + +public class PostfixExpr { +String expr = null; + + public PostfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + return 0.0f; + } + + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java new file mode 100644 index 0000000000..c0435a2db5 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java @@ -0,0 +1,41 @@ +package com.coding.basic.stack.expr; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class PostfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + { + PostfixExpr expr = new PostfixExpr("6 5 2 3 + 8 * + 3 + *"); + Assert.assertEquals(288, expr.evaluate(),0.0f); + } + { + //9+(3-1)*3+10/2 + PostfixExpr expr = new PostfixExpr("9 3 1-3*+ 10 2/+"); + Assert.assertEquals(20, expr.evaluate(),0.0f); + } + + { + //10-2*3+50 + PostfixExpr expr = new PostfixExpr("10 2 3 * - 50 +"); + Assert.assertEquals(54, expr.evaluate(),0.0f); + } + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java new file mode 100644 index 0000000000..956927e2df --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java @@ -0,0 +1,18 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + +public class PrefixExpr { + String expr = null; + + public PrefixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + return 0.0f; + } + + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java new file mode 100644 index 0000000000..5cec210e75 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java @@ -0,0 +1,45 @@ +package com.coding.basic.stack.expr; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class PrefixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + { + // 2*3+4*5 + PrefixExpr expr = new PrefixExpr("+ * 2 3* 4 5"); + Assert.assertEquals(26, expr.evaluate(),0.001f); + } + { + // 4*2 + 6+9*2/3 -8 + PrefixExpr expr = new PrefixExpr("-++6/*2 9 3 * 4 2 8"); + Assert.assertEquals(12, expr.evaluate(),0.001f); + } + { + //(3+4)*5-6 + PrefixExpr expr = new PrefixExpr("- * + 3 4 5 6"); + Assert.assertEquals(29, expr.evaluate(),0.001f); + } + { + //1+((2+3)*4)-5 + PrefixExpr expr = new PrefixExpr("- + 1 * + 2 3 4 5"); + Assert.assertEquals(16, expr.evaluate(),0.001f); + } + + + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/Token.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/Token.java new file mode 100644 index 0000000000..8579743fe9 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/Token.java @@ -0,0 +1,50 @@ +package com.coding.basic.stack.expr; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +class Token { + public static final List OPERATORS = Arrays.asList("+", "-", "*", "/"); + private static final Map priorities = new HashMap<>(); + static { + priorities.put("+", 1); + priorities.put("-", 1); + priorities.put("*", 2); + priorities.put("/", 2); + } + static final int OPERATOR = 1; + static final int NUMBER = 2; + String value; + int type; + public Token(int type, String value){ + this.type = type; + this.value = value; + } + + public boolean isNumber() { + return type == NUMBER; + } + + public boolean isOperator() { + return type == OPERATOR; + } + + public int getIntValue() { + return Integer.valueOf(value).intValue(); + } + public String toString(){ + return value; + } + + public boolean hasHigherPriority(Token t){ + if(!this.isOperator() && !t.isOperator()){ + throw new RuntimeException("numbers can't compare priority"); + } + return priorities.get(this.value) - priorities.get(t.value) > 0; + } + + + +} \ No newline at end of file diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParser.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParser.java new file mode 100644 index 0000000000..d3b0f167e1 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParser.java @@ -0,0 +1,57 @@ +package com.coding.basic.stack.expr; + +import java.util.ArrayList; +import java.util.List; + +public class TokenParser { + + + public List parse(String expr) { + List tokens = new ArrayList<>(); + + int i = 0; + + while (i < expr.length()) { + + char c = expr.charAt(i); + + if (isOperator(c)) { + + Token t = new Token(Token.OPERATOR, String.valueOf(c)); + tokens.add(t); + i++; + + } else if (Character.isDigit(c)) { + + int nextOperatorIndex = indexOfNextOperator(i, expr); + String value = expr.substring(i, nextOperatorIndex); + Token t = new Token(Token.NUMBER, value); + tokens.add(t); + i = nextOperatorIndex; + + } else{ + System.out.println("char :["+c+"] is not number or operator,ignore"); + i++; + } + + } + return tokens; + } + + private int indexOfNextOperator(int i, String expr) { + + while (Character.isDigit(expr.charAt(i))) { + i++; + if (i == expr.length()) { + break; + } + } + return i; + + } + + private boolean isOperator(char c) { + String sc = String.valueOf(c); + return Token.OPERATORS.contains(sc); + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java new file mode 100644 index 0000000000..399d3e857e --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java @@ -0,0 +1,41 @@ +package com.coding.basic.stack.expr; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TokenParserTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + + TokenParser parser = new TokenParser(); + List tokens = parser.parse("300*20+12*5-20/4"); + + Assert.assertEquals(300, tokens.get(0).getIntValue()); + Assert.assertEquals("*", tokens.get(1).toString()); + Assert.assertEquals(20, tokens.get(2).getIntValue()); + Assert.assertEquals("+", tokens.get(3).toString()); + Assert.assertEquals(12, tokens.get(4).getIntValue()); + Assert.assertEquals("*", tokens.get(5).toString()); + Assert.assertEquals(5, tokens.get(6).getIntValue()); + Assert.assertEquals("-", tokens.get(7).toString()); + Assert.assertEquals(20, tokens.get(8).getIntValue()); + Assert.assertEquals("/", tokens.get(9).toString()); + Assert.assertEquals(4, tokens.get(10).getIntValue()); + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTree.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTree.java new file mode 100644 index 0000000000..4536ee7a2b --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTree.java @@ -0,0 +1,55 @@ +package com.coding.basic.tree; + +import java.util.ArrayList; +import java.util.List; + +import com.coding.basic.queue.Queue; + +public class BinarySearchTree { + + BinaryTreeNode root; + public BinarySearchTree(BinaryTreeNode root){ + this.root = root; + } + public BinaryTreeNode getRoot(){ + return root; + } + public T findMin(){ + return null; + } + public T findMax(){ + return null; + } + public int height() { + return -1; + } + public int size() { + return -1; + } + public void remove(T e){ + + } + public List levelVisit(){ + + return null; + } + public boolean isValid(){ + return false; + } + public T getLowestCommonAncestor(T n1, T n2){ + return null; + + } + /** + * 返回所有满足下列条件的节点的值: n1 <= n <= n2 , n 为 + * 该二叉查找树中的某一节点 + * @param n1 + * @param n2 + * @return + */ + public List getNodesBetween(T n1, T n2){ + return null; + } + +} + diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java new file mode 100644 index 0000000000..4a53dbe2f1 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java @@ -0,0 +1,109 @@ +package com.coding.basic.tree; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class BinarySearchTreeTest { + + BinarySearchTree tree = null; + + @Before + public void setUp() throws Exception { + BinaryTreeNode root = new BinaryTreeNode(6); + root.left = new BinaryTreeNode(2); + root.right = new BinaryTreeNode(8); + root.left.left = new BinaryTreeNode(1); + root.left.right = new BinaryTreeNode(4); + root.left.right.left = new BinaryTreeNode(3); + root.left.right.right = new BinaryTreeNode(5); + tree = new BinarySearchTree(root); + } + + @After + public void tearDown() throws Exception { + tree = null; + } + + @Test + public void testFindMin() { + Assert.assertEquals(1, tree.findMin().intValue()); + + } + + @Test + public void testFindMax() { + Assert.assertEquals(8, tree.findMax().intValue()); + } + + @Test + public void testHeight() { + Assert.assertEquals(4, tree.height()); + } + + @Test + public void testSize() { + Assert.assertEquals(7, tree.size()); + } + + @Test + public void testRemoveLeaf() { + tree.remove(3); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(4, root.left.right.data.intValue()); + + } + @Test + public void testRemoveMiddleNode1() { + tree.remove(4); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(5, root.left.right.data.intValue()); + Assert.assertEquals(3, root.left.right.left.data.intValue()); + } + @Test + public void testRemoveMiddleNode2() { + tree.remove(2); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(3, root.left.data.intValue()); + Assert.assertEquals(4, root.left.right.data.intValue()); + } + + @Test + public void testLevelVisit() { + List values = tree.levelVisit(); + Assert.assertEquals("[6, 2, 8, 1, 4, 3, 5]", values.toString()); + + } + @Test + public void testLCA(){ + Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 5).intValue()); + Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 4).intValue()); + Assert.assertEquals(6,tree.getLowestCommonAncestor(3, 8).intValue()); + } + @Test + public void testIsValid() { + + Assert.assertTrue(tree.isValid()); + + BinaryTreeNode root = new BinaryTreeNode(6); + root.left = new BinaryTreeNode(2); + root.right = new BinaryTreeNode(8); + root.left.left = new BinaryTreeNode(4); + root.left.right = new BinaryTreeNode(1); + root.left.right.left = new BinaryTreeNode(3); + tree = new BinarySearchTree(root); + + Assert.assertFalse(tree.isValid()); + } + @Test + public void testGetNodesBetween(){ + List numbers = this.tree.getNodesBetween(3, 8); + System.out.println(numbers.toString()); + + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeNode.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeNode.java new file mode 100644 index 0000000000..c1421cd398 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeNode.java @@ -0,0 +1,35 @@ +package com.coding.basic.tree; + +public class BinaryTreeNode { + + public T data; + public BinaryTreeNode left; + public BinaryTreeNode right; + + public BinaryTreeNode(T data){ + this.data=data; + } + public T getData() { + return data; + } + public void setData(T data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java new file mode 100644 index 0000000000..b033cbe1d5 --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java @@ -0,0 +1,66 @@ +package com.coding.basic.tree; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class BinaryTreeUtil { + /** + * 用递归的方式实现对二叉树的前序遍历, 需要通过BinaryTreeUtilTest测试 + * + * @param root + * @return + */ + public static List preOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + + return result; + } + + /** + * 用递归的方式实现对二叉树的中遍历 + * + * @param root + * @return + */ + public static List inOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + + return result; + } + + /** + * 用递归的方式实现对二叉树的后遍历 + * + * @param root + * @return + */ + public static List postOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + + return result; + } + /** + * 用非递归的方式实现对二叉树的前序遍历 + * @param root + * @return + */ + public static List preOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + + return result; + } + /** + * 用非递归的方式实现对二叉树的中序遍历 + * @param root + * @return + */ + public static List inOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + + return result; + } + +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java new file mode 100644 index 0000000000..41857e137d --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java @@ -0,0 +1,75 @@ +package com.coding.basic.tree; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class BinaryTreeUtilTest { + + BinaryTreeNode root = null; + @Before + public void setUp() throws Exception { + root = new BinaryTreeNode(1); + root.setLeft(new BinaryTreeNode(2)); + root.setRight(new BinaryTreeNode(5)); + root.getLeft().setLeft(new BinaryTreeNode(3)); + root.getLeft().setRight(new BinaryTreeNode(4)); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testPreOrderVisit() { + + List result = BinaryTreeUtil.preOrderVisit(root); + Assert.assertEquals("[1, 2, 3, 4, 5]", result.toString()); + + + } + @Test + public void testInOrderVisit() { + + + List result = BinaryTreeUtil.inOrderVisit(root); + Assert.assertEquals("[3, 2, 4, 1, 5]", result.toString()); + + } + + @Test + public void testPostOrderVisit() { + + + List result = BinaryTreeUtil.postOrderVisit(root); + Assert.assertEquals("[3, 4, 2, 5, 1]", result.toString()); + + } + + + @Test + public void testInOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.inOrderWithoutRecursion(root); + Assert.assertEquals("[3, 2, 6, 4, 7, 1, 5]", result.toString()); + + } + @Test + public void testPreOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.preOrderWithoutRecursion(root); + Assert.assertEquals("[1, 2, 3, 4, 6, 7, 5]", result.toString()); + + } +} diff --git a/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/FileList.java b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/FileList.java new file mode 100644 index 0000000000..6e65192e4a --- /dev/null +++ b/students/592146505/data-structure/assignment/src/main/java/com/coding/basic/tree/FileList.java @@ -0,0 +1,10 @@ +package com.coding.basic.tree; + +import java.io.File; + +public class FileList { + public void list(File f) { + } + + +} diff --git a/students/592146505/ood/ood-assignment/pom.xml b/students/592146505/ood/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/592146505/ood/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..781587a846 --- /dev/null +++ b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 45f8857bffbb3b4256cb1831b74af6df3ed28ea0 Mon Sep 17 00:00:00 2001 From: Lyccccc Date: Mon, 12 Jun 2017 10:27:09 +0800 Subject: [PATCH 006/214] first commit --- students/472779948/helloworld.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/472779948/helloworld.txt diff --git a/students/472779948/helloworld.txt b/students/472779948/helloworld.txt new file mode 100644 index 0000000000..fe51499bb2 --- /dev/null +++ b/students/472779948/helloworld.txt @@ -0,0 +1 @@ +helloworld! \ No newline at end of file From 866444ee2a2c807d7be4024735573b3a0879053c Mon Sep 17 00:00:00 2001 From: nightn Date: Mon, 12 Jun 2017 10:36:31 +0800 Subject: [PATCH 007/214] first commit --- students/276137509/276137509Learning/readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/276137509/276137509Learning/readme.md diff --git a/students/276137509/276137509Learning/readme.md b/students/276137509/276137509Learning/readme.md new file mode 100644 index 0000000000..88093b9187 --- /dev/null +++ b/students/276137509/276137509Learning/readme.md @@ -0,0 +1 @@ +### nightn (杭州-莱顿) 的代码仓库 \ No newline at end of file From 0c42579305e136986ed644c3302e1bcb72ba614c Mon Sep 17 00:00:00 2001 From: nightn Date: Mon, 12 Jun 2017 10:51:44 +0800 Subject: [PATCH 008/214] update readme.md --- students/276137509/276137509Learning/readme.md | 2 +- students/276137509/readme.md | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 students/276137509/readme.md diff --git a/students/276137509/276137509Learning/readme.md b/students/276137509/276137509Learning/readme.md index 88093b9187..7c847014a2 100644 --- a/students/276137509/276137509Learning/readme.md +++ b/students/276137509/276137509Learning/readme.md @@ -1 +1 @@ -### nightn (杭州-莱顿) 的代码仓库 \ No newline at end of file +### This is my first project just for testing \ No newline at end of file diff --git a/students/276137509/readme.md b/students/276137509/readme.md new file mode 100644 index 0000000000..065efcdbbe --- /dev/null +++ b/students/276137509/readme.md @@ -0,0 +1 @@ +#### Nightn (杭州-莱顿) 代码仓库 \ No newline at end of file From 0e40448f7e46700db6f500d8b4a3ed80380daf4c Mon Sep 17 00:00:00 2001 From: maneng Date: Mon, 12 Jun 2017 11:58:00 +0800 Subject: [PATCH 009/214] first pull request --- students/643449856/readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/643449856/readme.md diff --git a/students/643449856/readme.md b/students/643449856/readme.md new file mode 100644 index 0000000000..b8e87ead7c --- /dev/null +++ b/students/643449856/readme.md @@ -0,0 +1 @@ +first pull request \ No newline at end of file From fd03bd64cdf5db36ba0b104c978ebdbe4a5367c4 Mon Sep 17 00:00:00 2001 From: tjc <501917623@qq.com> Date: Mon, 12 Jun 2017 12:05:47 +0800 Subject: [PATCH 010/214] =?UTF-8?q?=E6=B5=8B=E8=AF=95git?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/501917623/src/work/Test/Test.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 students/501917623/src/work/Test/Test.java diff --git a/students/501917623/src/work/Test/Test.java b/students/501917623/src/work/Test/Test.java new file mode 100644 index 0000000000..3da24e1f28 --- /dev/null +++ b/students/501917623/src/work/Test/Test.java @@ -0,0 +1,10 @@ +package work.Test; + +public class Test { + + public static void main(String[] args) { + // TODO Auto-generated method stub + System.out.println("hello world"); + } + +} From 87119062e0aa3f88c40d447fa40a4f5fcdf33437 Mon Sep 17 00:00:00 2001 From: maneng Date: Mon, 12 Jun 2017 12:08:15 +0800 Subject: [PATCH 011/214] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/643449856/ood-assignment/pom.xml | 32 +++ .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 7 files changed, 310 insertions(+) create mode 100644 students/643449856/ood-assignment/pom.xml create mode 100644 students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/643449856/ood-assignment/pom.xml b/students/643449856/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/643449856/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..781587a846 --- /dev/null +++ b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 242c149bdf5f5d227d8853203f46e3b984113f34 Mon Sep 17 00:00:00 2001 From: robert <592146505@qq.com> Date: Mon, 12 Jun 2017 13:20:22 +0800 Subject: [PATCH 012/214] first commit oop --- students/592146505/ood/ood-assignment/pom.xml | 32 +++ .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 7 files changed, 310 insertions(+) create mode 100644 students/592146505/ood/ood-assignment/pom.xml create mode 100644 students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/592146505/ood/ood-assignment/pom.xml b/students/592146505/ood/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/592146505/ood/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..781587a846 --- /dev/null +++ b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/592146505/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From dbeb0fa7489eec36f65b1d54252980ceeb125198 Mon Sep 17 00:00:00 2001 From: robert <592146505@qq.com> Date: Mon, 12 Jun 2017 13:23:35 +0800 Subject: [PATCH 013/214] delete data-structure/answer --- .../592146505/data-structure/answer/pom.xml | 32 --- .../coderising/download/DownloadThread.java | 20 -- .../coderising/download/FileDownloader.java | 73 ------- .../download/FileDownloaderTest.java | 59 ------ .../coderising/download/api/Connection.java | 23 --- .../download/api/ConnectionException.java | 5 - .../download/api/ConnectionManager.java | 10 - .../download/api/DownloadListener.java | 5 - .../download/impl/ConnectionImpl.java | 27 --- .../download/impl/ConnectionManagerImpl.java | 15 -- .../coderising/litestruts/LoginAction.java | 39 ---- .../com/coderising/litestruts/Struts.java | 34 ---- .../com/coderising/litestruts/StrutsTest.java | 43 ---- .../java/com/coderising/litestruts/View.java | 23 --- .../java/com/coderising/litestruts/struts.xml | 11 - .../main/java/com/coding/basic/Iterator.java | 7 - .../src/main/java/com/coding/basic/List.java | 9 - .../com/coding/basic/array/ArrayList.java | 35 ---- .../com/coding/basic/array/ArrayUtil.java | 96 --------- .../coding/basic/linklist/LRUPageFrame.java | 164 --------------- .../basic/linklist/LRUPageFrameTest.java | 34 ---- .../com/coding/basic/linklist/LinkedList.java | 125 ------------ .../com/coding/basic/queue/CircleQueue.java | 47 ----- .../coding/basic/queue/CircleQueueTest.java | 44 ---- .../java/com/coding/basic/queue/Josephus.java | 39 ---- .../com/coding/basic/queue/JosephusTest.java | 27 --- .../java/com/coding/basic/queue/Queue.java | 61 ------ .../basic/queue/QueueWithTwoStacks.java | 55 ----- .../com/coding/basic/stack/QuickMinStack.java | 44 ---- .../coding/basic/stack/QuickMinStackTest.java | 39 ---- .../java/com/coding/basic/stack/Stack.java | 24 --- .../com/coding/basic/stack/StackUtil.java | 168 ---------------- .../com/coding/basic/stack/StackUtilTest.java | 86 -------- .../basic/stack/StackWithTwoQueues.java | 53 ----- .../basic/stack/StackWithTwoQueuesTest.java | 36 ---- .../java/com/coding/basic/stack/Tail.java | 5 - .../basic/stack/TwoStackInOneArray.java | 117 ----------- .../basic/stack/TwoStackInOneArrayTest.java | 65 ------ .../coding/basic/stack/expr/InfixExpr.java | 72 ------- .../basic/stack/expr/InfixExprTest.java | 52 ----- .../basic/stack/expr/InfixToPostfix.java | 43 ---- .../basic/stack/expr/InfixToPostfixTest.java | 41 ---- .../coding/basic/stack/expr/PostfixExpr.java | 46 ----- .../basic/stack/expr/PostfixExprTest.java | 41 ---- .../coding/basic/stack/expr/PrefixExpr.java | 52 ----- .../basic/stack/expr/PrefixExprTest.java | 45 ----- .../com/coding/basic/stack/expr/Token.java | 50 ----- .../coding/basic/stack/expr/TokenParser.java | 57 ------ .../basic/stack/expr/TokenParserTest.java | 41 ---- .../coding/basic/tree/BinarySearchTree.java | 189 ------------------ .../basic/tree/BinarySearchTreeTest.java | 108 ---------- .../com/coding/basic/tree/BinaryTreeNode.java | 36 ---- .../com/coding/basic/tree/BinaryTreeUtil.java | 116 ----------- .../coding/basic/tree/BinaryTreeUtilTest.java | 75 ------- .../java/com/coding/basic/tree/FileList.java | 34 ---- 55 files changed, 2897 deletions(-) delete mode 100644 students/592146505/data-structure/answer/pom.xml delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/View.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/Iterator.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/List.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtilTest.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java delete mode 100644 students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java diff --git a/students/592146505/data-structure/answer/pom.xml b/students/592146505/data-structure/answer/pom.xml deleted file mode 100644 index ac6ba882df..0000000000 --- a/students/592146505/data-structure/answer/pom.xml +++ /dev/null @@ -1,32 +0,0 @@ - - 4.0.0 - - com.coderising - ds-answer - 0.0.1-SNAPSHOT - jar - - ds-answer - http://maven.apache.org - - - UTF-8 - - - - - - junit - junit - 4.12 - - - - - - aliyunmaven - http://maven.aliyun.com/nexus/content/groups/public/ - - - diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java deleted file mode 100644 index 900a3ad358..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.coderising.download; - -import com.coderising.download.api.Connection; - -public class DownloadThread extends Thread{ - - Connection conn; - int startPos; - int endPos; - - public DownloadThread( Connection conn, int startPos, int endPos){ - - this.conn = conn; - this.startPos = startPos; - this.endPos = endPos; - } - public void run(){ - - } -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java deleted file mode 100644 index c3c8a3f27d..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.coderising.download; - -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; - - -public class FileDownloader { - - String url; - - DownloadListener listener; - - ConnectionManager cm; - - - public FileDownloader(String _url) { - this.url = _url; - - } - - public void execute(){ - // 在这里实现你的代码, 注意: 需要用多线程实现下载 - // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 - // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) - // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 - // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 - // 具体的实现思路: - // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 - // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 - // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 - // 3. 把byte数组写入到文件中 - // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 - - // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 - Connection conn = null; - try { - - conn = cm.open(this.url); - - int length = conn.getContentLength(); - - new DownloadThread(conn,0,length-1).start(); - - } catch (ConnectionException e) { - e.printStackTrace(); - }finally{ - if(conn != null){ - conn.close(); - } - } - - - - - } - - public void setListener(DownloadListener listener) { - this.listener = listener; - } - - - - public void setConnectionManager(ConnectionManager ucm){ - this.cm = ucm; - } - - public DownloadListener getListener(){ - return this.listener; - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java deleted file mode 100644 index 4ff7f46ae0..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.coderising.download; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; -import com.coderising.download.impl.ConnectionManagerImpl; - -public class FileDownloaderTest { - boolean downloadFinished = false; - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testDownload() { - - String url = "http://localhost:8080/test.jpg"; - - FileDownloader downloader = new FileDownloader(url); - - - ConnectionManager cm = new ConnectionManagerImpl(); - downloader.setConnectionManager(cm); - - downloader.setListener(new DownloadListener() { - @Override - public void notifyFinished() { - downloadFinished = true; - } - - }); - - - downloader.execute(); - - // 等待多线程下载程序执行完毕 - while (!downloadFinished) { - try { - System.out.println("还没有下载完成,休眠五秒"); - //休眠5秒 - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - System.out.println("下载完成!"); - - - - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java deleted file mode 100644 index 0957eaf7f4..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.coderising.download.api; - -import java.io.IOException; - -public interface Connection { - /** - * 给定开始和结束位置, 读取数据, 返回值是字节数组 - * @param startPos 开始位置, 从0开始 - * @param endPos 结束位置 - * @return - */ - public byte[] read(int startPos,int endPos) throws IOException; - /** - * 得到数据内容的长度 - * @return - */ - public int getContentLength(); - - /** - * 关闭连接 - */ - public void close(); -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java deleted file mode 100644 index 1551a80b3d..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.coderising.download.api; - -public class ConnectionException extends Exception { - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java deleted file mode 100644 index ce045393b1..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.coderising.download.api; - -public interface ConnectionManager { - /** - * 给定一个url , 打开一个连接 - * @param url - * @return - */ - public Connection open(String url) throws ConnectionException; -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java deleted file mode 100644 index bf9807b307..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.coderising.download.api; - -public interface DownloadListener { - public void notifyFinished(); -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java deleted file mode 100644 index 36a9d2ce15..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.coderising.download.impl; - -import java.io.IOException; - -import com.coderising.download.api.Connection; - -public class ConnectionImpl implements Connection{ - - @Override - public byte[] read(int startPos, int endPos) throws IOException { - - return null; - } - - @Override - public int getContentLength() { - - return 0; - } - - @Override - public void close() { - - - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java deleted file mode 100644 index 172371dd55..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.coderising.download.impl; - -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; - -public class ConnectionManagerImpl implements ConnectionManager { - - @Override - public Connection open(String url) throws ConnectionException { - - return null; - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java deleted file mode 100644 index dcdbe226ed..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.coderising.litestruts; - -/** - * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 - * @author liuxin - * - */ -public class LoginAction{ - private String name ; - private String password; - private String message; - - public String getName() { - return name; - } - - public String getPassword() { - return password; - } - - public String execute(){ - if("test".equals(name) && "1234".equals(password)){ - this.message = "login successful"; - return "success"; - } - this.message = "login failed,please check your user/pwd"; - return "fail"; - } - - public void setName(String name){ - this.name = name; - } - public void setPassword(String password){ - this.password = password; - } - public String getMessage(){ - return this.message; - } -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java deleted file mode 100644 index 85e2e22de3..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.coderising.litestruts; - -import java.util.Map; - - - -public class Struts { - - public static View runAction(String actionName, Map parameters) { - - /* - - 0. 读取配置文件struts.xml - - 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) - 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 - ("name"="test" , "password"="1234") , - 那就应该调用 setName和setPassword方法 - - 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" - - 3. 通过反射找到对象的所有getter方法(例如 getMessage), - 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , - 放到View对象的parameters - - 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, - 放到View对象的jsp字段中。 - - */ - - return null; - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java deleted file mode 100644 index b8c81faf3c..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.coderising.litestruts; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.Assert; -import org.junit.Test; - - - - - -public class StrutsTest { - - @Test - public void testLoginActionSuccess() { - - String actionName = "login"; - - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); - Assert.assertEquals("login successful", view.getParameters().get("message")); - } - - @Test - public void testLoginActionFailed() { - String actionName = "login"; - Map params = new HashMap(); - params.put("name","test"); - params.put("password","123456"); //密码和预设的不一致 - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); - Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); - } -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/View.java b/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/View.java deleted file mode 100644 index 07df2a5dab..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/View.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.coderising.litestruts; - -import java.util.Map; - -public class View { - private String jsp; - private Map parameters; - - public String getJsp() { - return jsp; - } - public View setJsp(String jsp) { - this.jsp = jsp; - return this; - } - public Map getParameters() { - return parameters; - } - public View setParameters(Map parameters) { - this.parameters = parameters; - return this; - } -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml b/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml deleted file mode 100644 index e5d9aebba8..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - \ No newline at end of file diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/Iterator.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/Iterator.java deleted file mode 100644 index 06ef6311b2..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/List.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/List.java deleted file mode 100644 index 10d13b5832..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.coding.basic; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java deleted file mode 100644 index 4576c016af..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.coding.basic.array; - -import com.coding.basic.Iterator; -import com.coding.basic.List; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o){ - - } - public void add(int index, Object o){ - - } - - public Object get(int index){ - return null; - } - - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public Iterator iterator(){ - return null; - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java deleted file mode 100644 index 45740e6d57..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.coding.basic.array; - -public class ArrayUtil { - - /** - * 给定一个整形数组a , 对该数组的值进行置换 - 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] - 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * @param origin - * @return - */ - public void reverseArray(int[] origin){ - - } - - /** - * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: - * {1,3,4,5,6,6,5,4,7,6,7,5} - * @param oldArray - * @return - */ - - public int[] removeZero(int[] oldArray){ - return null; - } - - /** - * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 - * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 - * @param array1 - * @param array2 - * @return - */ - - public int[] merge(int[] array1, int[] array2){ - return null; - } - /** - * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 - * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 - * [2,3,6,0,0,0] - * @param oldArray - * @param size - * @return - */ - public int[] grow(int [] oldArray, int size){ - return null; - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 - * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] - * max = 1, 则返回空数组 [] - * @param max - * @return - */ - public int[] fibonacci(int max){ - return null; - } - - /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * @param max - * @return - */ - public int[] getPrimes(int max){ - return null; - } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * @param max - * @return - */ - public int[] getPerfectNumbers(int max){ - return null; - } - - /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" - * @param array - * @param s - * @return - */ - public String join(int[] array, String seperator){ - return null; - } - - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java deleted file mode 100644 index 24b9d8b155..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java +++ /dev/null @@ -1,164 +0,0 @@ -package com.coding.basic.linklist; - - -public class LRUPageFrame { - - private static class Node { - - Node prev; - Node next; - int pageNum; - - Node() { - } - } - - private int capacity; - - private int currentSize; - private Node first;// 链表头 - private Node last;// 链表尾 - - - public LRUPageFrame(int capacity) { - this.currentSize = 0; - this.capacity = capacity; - - } - - /** - * 获取缓存中对象 - * - * @param key - * @return - */ - public void access(int pageNum) { - - Node node = find(pageNum); - //在该队列中存在, 则提到队列头 - if (node != null) { - - moveExistingNodeToHead(node); - - } else{ - - node = new Node(); - node.pageNum = pageNum; - - // 缓存容器是否已经超过大小. - if (currentSize >= capacity) { - removeLast(); - - } - - addNewNodetoHead(node); - - - - - } - } - - private void addNewNodetoHead(Node node) { - - if(isEmpty()){ - - node.prev = null; - node.next = null; - first = node; - last = node; - - } else{ - node.prev = null; - node.next = first; - first.prev = node; - first = node; - } - this.currentSize ++; - } - - private Node find(int data){ - - Node node = first; - while(node != null){ - if(node.pageNum == data){ - return node; - } - node = node.next; - } - return null; - - } - - - - - - - /** - * 删除链表尾部节点 表示 删除最少使用的缓存对象 - */ - private void removeLast() { - Node prev = last.prev; - prev.next = null; - last.prev = null; - last = prev; - this.currentSize --; - } - - /** - * 移动到链表头,表示这个节点是最新使用过的 - * - * @param node - */ - private void moveExistingNodeToHead(Node node) { - - if (node == first) { - - return; - } - else if(node == last){ - //当前节点是链表尾, 需要放到链表头 - Node prevNode = node.prev; - prevNode.next = null; - last.prev = null; - last = prevNode; - - } else{ - //node 在链表的中间, 把node 的前后节点连接起来 - Node prevNode = node.prev; - prevNode.next = node.next; - - Node nextNode = node.next; - nextNode.prev = prevNode; - - - } - - node.prev = null; - node.next = first; - first.prev = node; - first = node; - - } - private boolean isEmpty(){ - return (first == null) && (last == null); - } - - public String toString(){ - StringBuilder buffer = new StringBuilder(); - Node node = first; - while(node != null){ - buffer.append(node.pageNum); - - node = node.next; - if(node != null){ - buffer.append(","); - } - } - return buffer.toString(); - } - - - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java deleted file mode 100644 index 7fd72fc2b4..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.coding.basic.linklist; - -import org.junit.Assert; - -import org.junit.Test; - - -public class LRUPageFrameTest { - - @Test - public void testAccess() { - LRUPageFrame frame = new LRUPageFrame(3); - frame.access(7); - frame.access(0); - frame.access(1); - Assert.assertEquals("1,0,7", frame.toString()); - frame.access(2); - Assert.assertEquals("2,1,0", frame.toString()); - frame.access(0); - Assert.assertEquals("0,2,1", frame.toString()); - frame.access(0); - Assert.assertEquals("0,2,1", frame.toString()); - frame.access(3); - Assert.assertEquals("3,0,2", frame.toString()); - frame.access(0); - Assert.assertEquals("0,3,2", frame.toString()); - frame.access(4); - Assert.assertEquals("4,0,3", frame.toString()); - frame.access(5); - Assert.assertEquals("5,4,0", frame.toString()); - - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java deleted file mode 100644 index f4c7556a2e..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java +++ /dev/null @@ -1,125 +0,0 @@ -package com.coding.basic.linklist; - -import com.coding.basic.Iterator; -import com.coding.basic.List; - -public class LinkedList implements List { - - private Node head; - - public void add(Object o){ - - } - public void add(int index , Object o){ - - } - public Object get(int index){ - return null; - } - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public void addFirst(Object o){ - - } - public void addLast(Object o){ - - } - public Object removeFirst(){ - return null; - } - public Object removeLast(){ - return null; - } - public Iterator iterator(){ - return null; - } - - - private static class Node{ - Object data; - Node next; - - } - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse(){ - - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - - */ - public void removeFirstHalf(){ - - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - */ - public void remove(int i, int length){ - - } - /** - * 假定当前链表和listB均包含已升序排列的整数 - * 从当前链表中取出那些listB所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - */ - public int[] getElements(LinkedList list){ - return null; - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在listB中出现的元素 - - * @param list - */ - - public void subtract(LinkedList list){ - - } - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues(){ - - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * @param min - * @param max - */ - public void removeRange(int min, int max){ - - } - - /** - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - * @param list - */ - public LinkedList intersection( LinkedList list){ - return null; - } -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java deleted file mode 100644 index f169d5f8e4..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.coding.basic.queue; - -public class CircleQueue { - - //用数组来保存循环队列的元素 - private Object[] elementData ; - int size = 0; - //队头 - private int front = 0; - //队尾 - private int rear = 0; - - public CircleQueue(int capacity){ - elementData = new Object[capacity]; - } - public boolean isEmpty() { - return (front == rear) && !isFull(); - - } - - public boolean isFull(){ - return size == elementData.length; - } - public int size() { - return size; - } - - public void enQueue(E data) { - if(isFull()){ - throw new RuntimeException("The queue is full"); - } - rear = (rear+1) % elementData.length; - elementData[rear++] = data; - size++; - } - - public E deQueue() { - if(isEmpty()){ - throw new RuntimeException("The queue is empty"); - } - E data = (E)elementData[front]; - elementData[front] = null; - front = (front+1) % elementData.length; - size --; - return data; - } -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java deleted file mode 100644 index 7307eb77d4..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.coding.basic.queue; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class CircleQueueTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void test() { - CircleQueue queue = new CircleQueue(5); - Assert.assertTrue(queue.isEmpty()); - Assert.assertFalse(queue.isFull()); - - queue.enQueue("a"); - queue.enQueue("b"); - queue.enQueue("c"); - queue.enQueue("d"); - queue.enQueue("e"); - - Assert.assertTrue(queue.isFull()); - Assert.assertFalse(queue.isEmpty()); - Assert.assertEquals(5, queue.size()); - - Assert.assertEquals("a", queue.deQueue()); - Assert.assertEquals("b", queue.deQueue()); - Assert.assertEquals("c", queue.deQueue()); - Assert.assertEquals("d", queue.deQueue()); - Assert.assertEquals("e", queue.deQueue()); - - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java deleted file mode 100644 index 36ec615d36..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.coding.basic.queue; - -import java.util.ArrayList; -import java.util.List; - -/** - * 用Queue来实现Josephus问题 - * 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来 - * @author liuxin - * - */ -public class Josephus { - - public static List execute(int n, int m){ - - Queue queue = new Queue(); - for (int i = 0; i < n; i++){ - queue.enQueue(i); - } - - List result = new ArrayList(); - int i = 0; - - while (!queue.isEmpty()) { - - int x = queue.deQueue(); - - if (++i % m == 0){ - result.add(x); - } else{ - queue.enQueue(x); - } - } - - - return result; - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java deleted file mode 100644 index 7d90318b51..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.coding.basic.queue; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class JosephusTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testExecute() { - - Assert.assertEquals("[1, 3, 5, 0, 4, 2, 6]", Josephus.execute(7, 2).toString()); - - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java deleted file mode 100644 index c4c4b7325e..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.coding.basic.queue; - -import java.util.NoSuchElementException; - -public class Queue { - private Node first; - private Node last; - private int size; - - - private static class Node { - private E item; - private Node next; - } - - - public Queue() { - first = null; - last = null; - size = 0; - } - - - public boolean isEmpty() { - return first == null; - } - - public int size() { - return size; - } - - - - public void enQueue(E data) { - Node oldlast = last; - last = new Node(); - last.item = data; - last.next = null; - if (isEmpty()) { - first = last; - } - else{ - oldlast.next = last; - } - size++; - } - - public E deQueue() { - if (isEmpty()) { - throw new NoSuchElementException("Queue underflow"); - } - E item = first.item; - first = first.next; - size--; - if (isEmpty()) { - last = null; - } - return item; - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java deleted file mode 100644 index bc97df0800..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.coding.basic.queue; - -import java.util.NoSuchElementException; -import java.util.Stack; - -public class QueueWithTwoStacks { - private Stack stack1; - private Stack stack2; - - - public QueueWithTwoStacks() { - stack1 = new Stack(); - stack2 = new Stack(); - } - - - private void moveStack1ToStack2() { - while (!stack1.isEmpty()){ - stack2.push(stack1.pop()); - } - - } - - - public boolean isEmpty() { - return stack1.isEmpty() && stack2.isEmpty(); - } - - - - public int size() { - return stack1.size() + stack2.size(); - } - - - - public void enQueue(E item) { - stack1.push(item); - } - - public E deQueue() { - if (isEmpty()) { - throw new NoSuchElementException("Queue is empty"); - } - if (stack2.isEmpty()) { - moveStack1ToStack2(); - } - - return stack2.pop(); - } - - - - } - diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java deleted file mode 100644 index faf2644ab1..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.coding.basic.stack; - -import java.util.Stack; -/** - * 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素 - * finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值 - * @author liuxin - * - */ -public class QuickMinStack { - - private Stack normalStack = new Stack(); - private Stack minNumStack = new Stack(); - - public void push(int data){ - - normalStack.push(data); - - if(minNumStack.isEmpty()){ - minNumStack.push(data); - } else{ - if(minNumStack.peek() >= data) { - minNumStack.push(data); - } - } - - } - public int pop(){ - if(normalStack.isEmpty()){ - throw new RuntimeException("the stack is empty"); - } - int value = normalStack.pop(); - if(value == minNumStack.peek()){ - minNumStack.pop(); - } - return value; - } - public int findMin(){ - if(minNumStack.isEmpty()){ - throw new RuntimeException("the stack is empty"); - } - return minNumStack.peek(); - } -} \ No newline at end of file diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java deleted file mode 100644 index efe41a9f8f..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.coding.basic.stack; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - -public class QuickMinStackTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void test() { - QuickMinStack stack = new QuickMinStack(); - stack.push(5); - Assert.assertEquals(5, stack.findMin()); - stack.push(6); - Assert.assertEquals(5, stack.findMin()); - stack.push(4); - Assert.assertEquals(4, stack.findMin()); - stack.push(4); - Assert.assertEquals(4, stack.findMin()); - - stack.pop(); - Assert.assertEquals(4, stack.findMin()); - stack.pop(); - Assert.assertEquals(5, stack.findMin()); - stack.pop(); - Assert.assertEquals(5, stack.findMin()); - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java deleted file mode 100644 index fedb243604..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.coding.basic.stack; - -import com.coding.basic.array.ArrayList; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - } - - public Object pop(){ - return null; - } - - public Object peek(){ - return null; - } - public boolean isEmpty(){ - return false; - } - public int size(){ - return -1; - } -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java deleted file mode 100644 index 7c86d22fe7..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java +++ /dev/null @@ -1,168 +0,0 @@ -package com.coding.basic.stack; -import java.util.Stack; -public class StackUtil { - - public static void bad_reverse(Stack s) { - if(s == null || s.isEmpty()){ - return; - } - Stack tmpStack = new Stack(); - while(!s.isEmpty()){ - tmpStack.push(s.pop()); - } - - s = tmpStack; - - } - - - - public static void reverse_247565311(Stack s){ - if(s == null || s.isEmpty()) { - return; - } - - int size = s.size(); - Stack tmpStack = new Stack(); - - for(int i=0;ii){ - tmpStack.push(s.pop()); - } - s.push(top); - while(tmpStack.size()>0){ - s.push(tmpStack.pop()); - } - } - } - - - - /** - * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 - * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 - */ - public static void reverse(Stack s) { - if(s == null || s.isEmpty()){ - return; - } - - Stack tmp = new Stack(); - while(!s.isEmpty()){ - tmp.push(s.pop()); - } - while(!tmp.isEmpty()){ - Integer top = tmp.pop(); - addToBottom(s,top); - } - - - } - public static void addToBottom(Stack s, Integer value){ - if(s.isEmpty()){ - s.push(value); - } else{ - Integer top = s.pop(); - addToBottom(s,value); - s.push(top); - } - - } - /** - * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 - * - * @param o - */ - public static void remove(Stack s,Object o) { - if(s == null || s.isEmpty()){ - return; - } - Stack tmpStack = new Stack(); - - while(!s.isEmpty()){ - Object value = s.pop(); - if(!value.equals(o)){ - tmpStack.push(value); - } - } - - while(!tmpStack.isEmpty()){ - s.push(tmpStack.pop()); - } - } - - /** - * 从栈顶取得len个元素, 原来的栈中元素保持不变 - * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 - * @param len - * @return - */ - public static Object[] getTop(Stack s,int len) { - - if(s == null || s.isEmpty() || s.size() stack = new Stack(); - for(int i=0;i s = new Stack(); - s.push(1); - s.push(2); - s.push(3); - - StackUtil.addToBottom(s, 0); - - Assert.assertEquals("[0, 1, 2, 3]", s.toString()); - - } - @Test - public void testReverse() { - Stack s = new Stack(); - s.push(1); - s.push(2); - s.push(3); - s.push(4); - s.push(5); - Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); - StackUtil.reverse(s); - Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString()); - } - @Test - public void testReverse_247565311() { - Stack s = new Stack(); - s.push(1); - s.push(2); - s.push(3); - - Assert.assertEquals("[1, 2, 3]", s.toString()); - StackUtil.reverse_247565311(s); - Assert.assertEquals("[3, 2, 1]", s.toString()); - } - @Test - public void testRemove() { - Stack s = new Stack(); - s.push(1); - s.push(2); - s.push(3); - StackUtil.remove(s, 2); - Assert.assertEquals("[1, 3]", s.toString()); - } - - @Test - public void testGetTop() { - Stack s = new Stack(); - s.push(1); - s.push(2); - s.push(3); - s.push(4); - s.push(5); - { - Object[] values = StackUtil.getTop(s, 3); - Assert.assertEquals(5, values[0]); - Assert.assertEquals(4, values[1]); - Assert.assertEquals(3, values[2]); - } - } - - @Test - public void testIsValidPairs() { - Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])")); - Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})")); - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java deleted file mode 100644 index 7a58fbff56..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.coding.basic.stack; - -import java.util.ArrayDeque; -import java.util.Queue; - -public class StackWithTwoQueues { - Queue queue1 = new ArrayDeque<>(); - Queue queue2 = new ArrayDeque<>(); - - public void push(int data) { - //两个栈都为空时,优先考虑queue1 - if (queue1.isEmpty()&&queue2.isEmpty()) { - queue1.add(data); - return; - } - - if (queue1.isEmpty()) { - queue2.add(data); - return; - } - - if (queue2.isEmpty()) { - queue1.add(data); - return; - } - - } - - public int pop() { - - if (queue1.isEmpty()&&queue2.isEmpty()) { - throw new RuntimeException("stack is empty"); - } - - if (queue1.isEmpty()) { - while (queue2.size()>1) { - queue1.add(queue2.poll()); - } - return queue2.poll(); - } - - if (queue2.isEmpty()) { - while (queue1.size()>1) { - queue2.add(queue1.poll()); - } - return queue1.poll(); - } - - throw new RuntimeException("no queue is empty, this is not allowed"); - - - } -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java deleted file mode 100644 index 4541b1f040..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.coding.basic.stack; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class StackWithTwoQueuesTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void test() { - StackWithTwoQueues stack = new StackWithTwoQueues(); - stack.push(1); - stack.push(2); - stack.push(3); - stack.push(4); - Assert.assertEquals(4, stack.pop()); - Assert.assertEquals(3, stack.pop()); - - stack.push(5); - Assert.assertEquals(5, stack.pop()); - Assert.assertEquals(2, stack.pop()); - Assert.assertEquals(1, stack.pop()); - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java deleted file mode 100644 index 7f30ce55c8..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.coding.basic.stack; - -public class Tail { - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java deleted file mode 100644 index a532fd6e6c..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.coding.basic.stack; - -import java.util.Arrays; - -/** - * 用一个数组实现两个栈 - * 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。 - * @author liuxin - * - */ -public class TwoStackInOneArray { - private Object[] data = new Object[10]; - private int size; - private int top1, top2; - - public TwoStackInOneArray(int n){ - data = new Object[n]; - size = n; - top1 = -1; - top2 = data.length; - } - /** - * 向第一个栈中压入元素 - * @param o - */ - public void push1(Object o){ - ensureCapacity(); - data[++top1] = o; - } - /* - * 向第二个栈压入元素 - */ - public void push2(Object o){ - ensureCapacity(); - data[--top2] = o; - } - public void ensureCapacity(){ - if(top2-top1>1){ - return; - } else{ - - Object[] newArray = new Object[data.length*2]; - System.arraycopy(data, 0, newArray, 0, top1+1); - - int stack2Size = data.length-top2; - int newTop2 = newArray.length-stack2Size; - System.arraycopy(data, top2, newArray, newTop2, stack2Size); - - top2 = newTop2; - data = newArray; - } - } - /** - * 从第一个栈中弹出元素 - * @return - */ - public Object pop1(){ - if(top1 == -1){ - throw new RuntimeException("Stack1 is empty"); - } - Object o = data[top1]; - data[top1] = null; - top1--; - return o; - - } - /** - * 从第二个栈弹出元素 - * @return - */ - public Object pop2(){ - if(top2 == data.length){ - throw new RuntimeException("Stack2 is empty"); - } - Object o = data[top2]; - data[top2] = null; - top2++; - return o; - } - /** - * 获取第一个栈的栈顶元素 - * @return - */ - - public Object peek1(){ - if(top1 == -1){ - throw new RuntimeException("Stack1 is empty"); - } - return data[top1]; - } - - - /** - * 获取第二个栈的栈顶元素 - * @return - */ - - public Object peek2(){ - if(top2 == data.length){ - throw new RuntimeException("Stack2 is empty"); - } - return data[top2]; - } - - public Object[] stack1ToArray(){ - return Arrays.copyOf(data, top1+1); - } - public Object[] stack2ToArray(){ - int size = data.length-top2; - Object [] stack2Data = new Object[size]; - int j=0; - for(int i=data.length-1; i>=top2 ;i--){ - stack2Data[j++] = data[i]; - } - return stack2Data; - } -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java deleted file mode 100644 index b743d422c6..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.coding.basic.stack; - -import java.util.Arrays; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class TwoStackInOneArrayTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void test1() { - TwoStackInOneArray stack = new TwoStackInOneArray(10); - stack.push1(1); - stack.push1(2); - stack.push1(3); - stack.push1(4); - stack.push1(5); - - stack.push2(1); - stack.push2(2); - stack.push2(3); - stack.push2(4); - stack.push2(5); - - for(int i=1;i<=5;i++){ - Assert.assertEquals(stack.peek1(), stack.peek2()); - Assert.assertEquals(stack.pop1(), stack.pop2()); - } - - - } - @Test - public void test2() { - TwoStackInOneArray stack = new TwoStackInOneArray(5); - stack.push1(1); - stack.push1(2); - stack.push1(3); - stack.push1(4); - stack.push1(5); - stack.push1(6); - stack.push1(7); - - stack.push2(1); - stack.push2(2); - stack.push2(3); - stack.push2(4); - - - Assert.assertEquals("[1, 2, 3, 4, 5, 6, 7]",Arrays.toString(stack.stack1ToArray())); - Assert.assertEquals("[1, 2, 3, 4]",Arrays.toString(stack.stack2ToArray())); - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java deleted file mode 100644 index cebef21fa3..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.List; -import java.util.Stack; - - -public class InfixExpr { - String expr = null; - - public InfixExpr(String expr) { - this.expr = expr; - } - - public float evaluate() { - - - TokenParser parser = new TokenParser(); - List tokens = parser.parse(this.expr); - - - Stack opStack = new Stack<>(); - Stack numStack = new Stack<>(); - - for(Token token : tokens){ - - if (token.isOperator()){ - - while(!opStack.isEmpty() - && !token.hasHigherPriority(opStack.peek())){ - Token prevOperator = opStack.pop(); - Float f2 = numStack.pop(); - Float f1 = numStack.pop(); - Float result = calculate(prevOperator.toString(), f1,f2); - numStack.push(result); - - } - opStack.push(token); - } - if(token.isNumber()){ - numStack.push(new Float(token.getIntValue())); - } - } - - while(!opStack.isEmpty()){ - Token token = opStack.pop(); - Float f2 = numStack.pop(); - Float f1 = numStack.pop(); - numStack.push(calculate(token.toString(), f1,f2)); - } - - - return numStack.pop().floatValue(); - } - private Float calculate(String op, Float f1, Float f2){ - if(op.equals("+")){ - return f1+f2; - } - if(op.equals("-")){ - return f1-f2; - } - if(op.equals("*")){ - return f1*f2; - } - if(op.equals("/")){ - return f1/f2; - } - throw new RuntimeException(op + " is not supported"); - } - - - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java deleted file mode 100644 index 20e34e8852..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.coding.basic.stack.expr; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - -public class InfixExprTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEvaluate() { - //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); - { - InfixExpr expr = new InfixExpr("2+3*4+5"); - Assert.assertEquals(19.0, expr.evaluate(), 0.001f); - } - { - InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); - Assert.assertEquals(100.0, expr.evaluate(), 0.001f); - } - - { - InfixExpr expr = new InfixExpr("3*20/2"); - Assert.assertEquals(30, expr.evaluate(), 0.001f); - } - - { - InfixExpr expr = new InfixExpr("20/2*3"); - Assert.assertEquals(30, expr.evaluate(), 0.001f); - } - - { - InfixExpr expr = new InfixExpr("10-30+50"); - Assert.assertEquals(30, expr.evaluate(), 0.001f); - } - { - InfixExpr expr = new InfixExpr("10-2*3+50"); - Assert.assertEquals(54, expr.evaluate(), 0.001f); - } - - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java deleted file mode 100644 index 9e501eda20..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -public class InfixToPostfix { - - public static List convert(String expr) { - List inFixTokens = new TokenParser().parse(expr); - - List postFixTokens = new ArrayList<>(); - - Stack opStack = new Stack(); - for(Token token : inFixTokens){ - - if(token.isOperator()){ - - while(!opStack.isEmpty() - && !token.hasHigherPriority(opStack.peek())){ - postFixTokens.add(opStack.pop()); - - } - opStack.push(token); - - } - if(token.isNumber()){ - - postFixTokens.add(token); - - } - } - - while(!opStack.isEmpty()){ - postFixTokens.add(opStack.pop()); - } - - return postFixTokens; - } - - - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java deleted file mode 100644 index f879f55f14..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.List; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class InfixToPostfixTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testConvert() { - { - List tokens = InfixToPostfix.convert("2+3"); - Assert.assertEquals("[2, 3, +]", tokens.toString()); - } - { - - List tokens = InfixToPostfix.convert("2+3*4"); - Assert.assertEquals("[2, 3, 4, *, +]", tokens.toString()); - } - - { - - List tokens = InfixToPostfix.convert("2-3*4+5"); - Assert.assertEquals("[2, 3, 4, *, -, 5, +]", tokens.toString()); - } - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java deleted file mode 100644 index c54eb69e2a..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.List; -import java.util.Stack; - -public class PostfixExpr { -String expr = null; - - public PostfixExpr(String expr) { - this.expr = expr; - } - - public float evaluate() { - TokenParser parser = new TokenParser(); - List tokens = parser.parse(this.expr); - - - Stack numStack = new Stack<>(); - for(Token token : tokens){ - if(token.isNumber()){ - numStack.push(new Float(token.getIntValue())); - } else{ - Float f2 = numStack.pop(); - Float f1 = numStack.pop(); - numStack.push(calculate(token.toString(),f1,f2)); - } - } - return numStack.pop().floatValue(); - } - - private Float calculate(String op, Float f1, Float f2){ - if(op.equals("+")){ - return f1+f2; - } - if(op.equals("-")){ - return f1-f2; - } - if(op.equals("*")){ - return f1*f2; - } - if(op.equals("/")){ - return f1/f2; - } - throw new RuntimeException(op + " is not supported"); - } -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java deleted file mode 100644 index c0435a2db5..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.coding.basic.stack.expr; - - - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class PostfixExprTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEvaluate() { - { - PostfixExpr expr = new PostfixExpr("6 5 2 3 + 8 * + 3 + *"); - Assert.assertEquals(288, expr.evaluate(),0.0f); - } - { - //9+(3-1)*3+10/2 - PostfixExpr expr = new PostfixExpr("9 3 1-3*+ 10 2/+"); - Assert.assertEquals(20, expr.evaluate(),0.0f); - } - - { - //10-2*3+50 - PostfixExpr expr = new PostfixExpr("10 2 3 * - 50 +"); - Assert.assertEquals(54, expr.evaluate(),0.0f); - } - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java deleted file mode 100644 index f811fd6d9a..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.List; -import java.util.Stack; - -public class PrefixExpr { - String expr = null; - - public PrefixExpr(String expr) { - this.expr = expr; - } - - public float evaluate() { - TokenParser parser = new TokenParser(); - List tokens = parser.parse(this.expr); - - Stack exprStack = new Stack<>(); - Stack numStack = new Stack<>(); - for(Token token : tokens){ - exprStack.push(token); - } - - while(!exprStack.isEmpty()){ - Token t = exprStack.pop(); - if(t.isNumber()){ - numStack.push(new Float(t.getIntValue())); - }else{ - Float f1 = numStack.pop(); - Float f2 = numStack.pop(); - numStack.push(calculate(t.toString(),f1,f2)); - - } - } - return numStack.pop().floatValue(); - } - - private Float calculate(String op, Float f1, Float f2){ - if(op.equals("+")){ - return f1+f2; - } - if(op.equals("-")){ - return f1-f2; - } - if(op.equals("*")){ - return f1*f2; - } - if(op.equals("/")){ - return f1/f2; - } - throw new RuntimeException(op + " is not supported"); - } -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java deleted file mode 100644 index 5cec210e75..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.coding.basic.stack.expr; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - -public class PrefixExprTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEvaluate() { - { - // 2*3+4*5 - PrefixExpr expr = new PrefixExpr("+ * 2 3* 4 5"); - Assert.assertEquals(26, expr.evaluate(),0.001f); - } - { - // 4*2 + 6+9*2/3 -8 - PrefixExpr expr = new PrefixExpr("-++6/*2 9 3 * 4 2 8"); - Assert.assertEquals(12, expr.evaluate(),0.001f); - } - { - //(3+4)*5-6 - PrefixExpr expr = new PrefixExpr("- * + 3 4 5 6"); - Assert.assertEquals(29, expr.evaluate(),0.001f); - } - { - //1+((2+3)*4)-5 - PrefixExpr expr = new PrefixExpr("- + 1 * + 2 3 4 5"); - Assert.assertEquals(16, expr.evaluate(),0.001f); - } - - - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java deleted file mode 100644 index 8579743fe9..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -class Token { - public static final List OPERATORS = Arrays.asList("+", "-", "*", "/"); - private static final Map priorities = new HashMap<>(); - static { - priorities.put("+", 1); - priorities.put("-", 1); - priorities.put("*", 2); - priorities.put("/", 2); - } - static final int OPERATOR = 1; - static final int NUMBER = 2; - String value; - int type; - public Token(int type, String value){ - this.type = type; - this.value = value; - } - - public boolean isNumber() { - return type == NUMBER; - } - - public boolean isOperator() { - return type == OPERATOR; - } - - public int getIntValue() { - return Integer.valueOf(value).intValue(); - } - public String toString(){ - return value; - } - - public boolean hasHigherPriority(Token t){ - if(!this.isOperator() && !t.isOperator()){ - throw new RuntimeException("numbers can't compare priority"); - } - return priorities.get(this.value) - priorities.get(t.value) > 0; - } - - - -} \ No newline at end of file diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java deleted file mode 100644 index d3b0f167e1..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.ArrayList; -import java.util.List; - -public class TokenParser { - - - public List parse(String expr) { - List tokens = new ArrayList<>(); - - int i = 0; - - while (i < expr.length()) { - - char c = expr.charAt(i); - - if (isOperator(c)) { - - Token t = new Token(Token.OPERATOR, String.valueOf(c)); - tokens.add(t); - i++; - - } else if (Character.isDigit(c)) { - - int nextOperatorIndex = indexOfNextOperator(i, expr); - String value = expr.substring(i, nextOperatorIndex); - Token t = new Token(Token.NUMBER, value); - tokens.add(t); - i = nextOperatorIndex; - - } else{ - System.out.println("char :["+c+"] is not number or operator,ignore"); - i++; - } - - } - return tokens; - } - - private int indexOfNextOperator(int i, String expr) { - - while (Character.isDigit(expr.charAt(i))) { - i++; - if (i == expr.length()) { - break; - } - } - return i; - - } - - private boolean isOperator(char c) { - String sc = String.valueOf(c); - return Token.OPERATORS.contains(sc); - } -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java deleted file mode 100644 index 399d3e857e..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.coding.basic.stack.expr; - -import static org.junit.Assert.*; - -import java.util.List; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -public class TokenParserTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void test() { - - TokenParser parser = new TokenParser(); - List tokens = parser.parse("300*20+12*5-20/4"); - - Assert.assertEquals(300, tokens.get(0).getIntValue()); - Assert.assertEquals("*", tokens.get(1).toString()); - Assert.assertEquals(20, tokens.get(2).getIntValue()); - Assert.assertEquals("+", tokens.get(3).toString()); - Assert.assertEquals(12, tokens.get(4).getIntValue()); - Assert.assertEquals("*", tokens.get(5).toString()); - Assert.assertEquals(5, tokens.get(6).getIntValue()); - Assert.assertEquals("-", tokens.get(7).toString()); - Assert.assertEquals(20, tokens.get(8).getIntValue()); - Assert.assertEquals("/", tokens.get(9).toString()); - Assert.assertEquals(4, tokens.get(10).getIntValue()); - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java deleted file mode 100644 index 284e5b0011..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java +++ /dev/null @@ -1,189 +0,0 @@ -package com.coding.basic.tree; - -import java.util.ArrayList; -import java.util.List; - -import com.coding.basic.queue.Queue; - - -public class BinarySearchTree { - - BinaryTreeNode root; - public BinarySearchTree(BinaryTreeNode root){ - this.root = root; - } - public BinaryTreeNode getRoot(){ - return root; - } - public T findMin(){ - if(root == null){ - return null; - } - return findMin(root).data; - } - public T findMax(){ - if(root == null){ - return null; - } - return findMax(root).data; - } - public int height() { - return height(root); - } - public int size() { - return size(root); - } - public void remove(T e){ - remove(e, root); - } - - private BinaryTreeNode remove(T x, BinaryTreeNode t){ - if(t == null){ - return t; - } - int compareResult = x.compareTo(t.data); - - if(compareResult< 0 ){ - t.left = remove(x,t.left); - - } else if(compareResult > 0){ - t.right = remove(x, t.right); - - } else { - if(t.left != null && t.right != null){ - - t.data = findMin(t.right).data; - t.right = remove(t.data,t.right); - - } else{ - t = (t.left != null) ? t.left : t.right; - } - } - return t; - } - - private BinaryTreeNode findMin(BinaryTreeNode p){ - if (p==null){ - return null; - } else if (p.left == null){ - return p; - } else{ - return findMin(p.left); - } - } - private BinaryTreeNode findMax(BinaryTreeNode p){ - if (p==null){ - return null; - }else if (p.right==null){ - return p; - } else{ - return findMax(p.right); - } - } - private int height(BinaryTreeNode t){ - if (t==null){ - return 0; - }else { - int leftChildHeight=height(t.left); - int rightChildHeight=height(t.right); - if(leftChildHeight > rightChildHeight){ - return leftChildHeight+1; - } else{ - return rightChildHeight+1; - } - } - } - private int size(BinaryTreeNode t){ - if (t == null){ - return 0; - } - return size(t.left) + 1 + size(t.right); - - } - - public List levelVisit(){ - List result = new ArrayList(); - if(root == null){ - return result; - } - Queue> queue = new Queue>(); - BinaryTreeNode node = root; - queue.enQueue(node); - while (!queue.isEmpty()) { - node = queue.deQueue(); - result.add(node.data); - if (node.left != null){ - queue.enQueue(node.left); - } - if (node.right != null){ - queue.enQueue(node.right); - } - } - return result; - } - public boolean isValid(){ - return isValid(root); - } - public T getLowestCommonAncestor(T n1, T n2){ - if (root == null){ - return null; - } - return lowestCommonAncestor(root,n1,n2); - - } - public List getNodesBetween(T n1, T n2){ - List elements = new ArrayList<>(); - getNodesBetween(elements,root,n1,n2); - return elements; - } - - public void getNodesBetween(List elements ,BinaryTreeNode node, T n1, T n2){ - - if (node == null) { - return; - } - - if (n1.compareTo(node.data) < 0) { - getNodesBetween(elements,node.left, n1, n2); - } - - if ((n1.compareTo(node.data) <= 0 ) - && (n2.compareTo(node.data) >= 0 )) { - elements.add(node.data); - } - if (n2.compareTo(node.data)>0) { - getNodesBetween(elements,node.right, n1, n2); - } - } - private T lowestCommonAncestor(BinaryTreeNode node,T n1, T n2){ - if(node == null){ - return null; - } - // 如果n1和n2都比 node的值小, LCA在左孩子 - if (node.data.compareTo(n1) > 0 && node.data.compareTo(n2) >0){ - return lowestCommonAncestor(node.left, n1, n2); - } - - // 如果n1和n2都比 node的值小, LCA在右孩子 - if (node.data.compareTo(n1) < 0 && node.data.compareTo(n2) <0) - return lowestCommonAncestor(node.right, n1, n2); - - return node.data; - } - private boolean isValid(BinaryTreeNode t){ - if(t == null){ - return true; - } - if(t.left != null && findMax(t.left).data.compareTo(t.data) >0){ - return false; - } - if(t.right !=null && findMin(t.right).data.compareTo(t.data) <0){ - return false; - } - if(!isValid(t.left) || !isValid(t.right)){ - return false; - } - return true; - } -} - diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java deleted file mode 100644 index 590e60306c..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.coding.basic.tree; - -import java.util.List; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class BinarySearchTreeTest { - - BinarySearchTree tree = null; - - @Before - public void setUp() throws Exception { - BinaryTreeNode root = new BinaryTreeNode(6); - root.left = new BinaryTreeNode(2); - root.right = new BinaryTreeNode(8); - root.left.left = new BinaryTreeNode(1); - root.left.right = new BinaryTreeNode(4); - root.left.right.left = new BinaryTreeNode(3); - root.left.right.right = new BinaryTreeNode(5); - tree = new BinarySearchTree(root); - } - - @After - public void tearDown() throws Exception { - tree = null; - } - - @Test - public void testFindMin() { - Assert.assertEquals(1, tree.findMin().intValue()); - - } - - @Test - public void testFindMax() { - Assert.assertEquals(8, tree.findMax().intValue()); - } - - @Test - public void testHeight() { - Assert.assertEquals(4, tree.height()); - } - - @Test - public void testSize() { - Assert.assertEquals(7, tree.size()); - } - - @Test - public void testRemoveLeaf() { - tree.remove(3); - BinaryTreeNode root= tree.getRoot(); - Assert.assertEquals(4, root.left.right.data.intValue()); - - } - @Test - public void testRemoveMiddleNode1() { - tree.remove(4); - BinaryTreeNode root= tree.getRoot(); - Assert.assertEquals(5, root.left.right.data.intValue()); - Assert.assertEquals(3, root.left.right.left.data.intValue()); - } - @Test - public void testRemoveMiddleNode2() { - tree.remove(2); - BinaryTreeNode root= tree.getRoot(); - Assert.assertEquals(3, root.left.data.intValue()); - Assert.assertEquals(4, root.left.right.data.intValue()); - } - - @Test - public void testLevelVisit() { - List values = tree.levelVisit(); - Assert.assertEquals("[6, 2, 8, 1, 4, 3, 5]", values.toString()); - - } - @Test - public void testLCA(){ - Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 5).intValue()); - Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 4).intValue()); - Assert.assertEquals(6,tree.getLowestCommonAncestor(3, 8).intValue()); - } - @Test - public void testIsValid() { - - Assert.assertTrue(tree.isValid()); - - BinaryTreeNode root = new BinaryTreeNode(6); - root.left = new BinaryTreeNode(2); - root.right = new BinaryTreeNode(8); - root.left.left = new BinaryTreeNode(4); - root.left.right = new BinaryTreeNode(1); - root.left.right.left = new BinaryTreeNode(3); - tree = new BinarySearchTree(root); - - Assert.assertFalse(tree.isValid()); - } - @Test - public void testGetNodesBetween(){ - List numbers = this.tree.getNodesBetween(3, 8); - Assert.assertEquals("[3, 4, 5, 6, 8]",numbers.toString()); - } -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java deleted file mode 100644 index 3f6f4d2b44..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.coding.basic.tree; - -public class BinaryTreeNode { - - public T data; - public BinaryTreeNode left; - public BinaryTreeNode right; - - public BinaryTreeNode(T data){ - this.data=data; - } - public T getData() { - return data; - } - public void setData(T data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java deleted file mode 100644 index f2a6515fa6..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.coding.basic.tree; - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -public class BinaryTreeUtil { - /** - * 用递归的方式实现对二叉树的前序遍历, 需要通过BinaryTreeUtilTest测试 - * - * @param root - * @return - */ - public static List preOrderVisit(BinaryTreeNode root) { - List result = new ArrayList(); - preOrderVisit(root, result); - return result; - } - - /** - * 用递归的方式实现对二叉树的中遍历 - * - * @param root - * @return - */ - public static List inOrderVisit(BinaryTreeNode root) { - List result = new ArrayList(); - inOrderVisit(root, result); - return result; - } - - /** - * 用递归的方式实现对二叉树的后遍历 - * - * @param root - * @return - */ - public static List postOrderVisit(BinaryTreeNode root) { - List result = new ArrayList(); - postOrderVisit(root, result); - return result; - } - - public static List preOrderWithoutRecursion(BinaryTreeNode root) { - - List result = new ArrayList(); - Stack> stack = new Stack>(); - - BinaryTreeNode node = root; - - if(node != null){ - stack.push(node); - } - - while(!stack.isEmpty()){ - node = stack.pop(); - result.add(node.data); - - if(node.right != null){ - stack.push(node.right); - } - - if(node.left != null){ - stack.push(node.right); - } - } - return result; - } - - public static List inOrderWithoutRecursion(BinaryTreeNode root) { - - List result = new ArrayList(); - BinaryTreeNode node = root; - Stack> stack = new Stack>(); - - while (node != null || !stack.isEmpty()) { - - while (node != null) { - stack.push(node); - node = node.left; - } - BinaryTreeNode currentNode = stack.pop(); - result.add(currentNode.data); - node = currentNode.right; - } - return result; - } - - private static void preOrderVisit(BinaryTreeNode node, List result) { - if (node == null) { - return; - } - result.add(node.getData()); - preOrderVisit(node.getLeft(), result); - preOrderVisit(node.getRight(), result); - } - - private static void inOrderVisit(BinaryTreeNode node, List result) { - if (node == null) { - return; - } - inOrderVisit(node.getLeft(), result); - result.add(node.getData()); - inOrderVisit(node.getRight(), result); - } - - private static void postOrderVisit(BinaryTreeNode node, List result) { - if (node == null) { - return; - } - postOrderVisit(node.getLeft(), result); - postOrderVisit(node.getRight(), result); - result.add(node.getData()); - } - -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java deleted file mode 100644 index 41857e137d..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.coding.basic.tree; - -import java.util.List; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class BinaryTreeUtilTest { - - BinaryTreeNode root = null; - @Before - public void setUp() throws Exception { - root = new BinaryTreeNode(1); - root.setLeft(new BinaryTreeNode(2)); - root.setRight(new BinaryTreeNode(5)); - root.getLeft().setLeft(new BinaryTreeNode(3)); - root.getLeft().setRight(new BinaryTreeNode(4)); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testPreOrderVisit() { - - List result = BinaryTreeUtil.preOrderVisit(root); - Assert.assertEquals("[1, 2, 3, 4, 5]", result.toString()); - - - } - @Test - public void testInOrderVisit() { - - - List result = BinaryTreeUtil.inOrderVisit(root); - Assert.assertEquals("[3, 2, 4, 1, 5]", result.toString()); - - } - - @Test - public void testPostOrderVisit() { - - - List result = BinaryTreeUtil.postOrderVisit(root); - Assert.assertEquals("[3, 4, 2, 5, 1]", result.toString()); - - } - - - @Test - public void testInOrderVisitWithoutRecursion() { - BinaryTreeNode node = root.getLeft().getRight(); - node.setLeft(new BinaryTreeNode(6)); - node.setRight(new BinaryTreeNode(7)); - - List result = BinaryTreeUtil.inOrderWithoutRecursion(root); - Assert.assertEquals("[3, 2, 6, 4, 7, 1, 5]", result.toString()); - - } - @Test - public void testPreOrderVisitWithoutRecursion() { - BinaryTreeNode node = root.getLeft().getRight(); - node.setLeft(new BinaryTreeNode(6)); - node.setRight(new BinaryTreeNode(7)); - - List result = BinaryTreeUtil.preOrderWithoutRecursion(root); - Assert.assertEquals("[1, 2, 3, 4, 6, 7, 5]", result.toString()); - - } -} diff --git a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java b/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java deleted file mode 100644 index 85fb8ab2a4..0000000000 --- a/students/592146505/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.coding.basic.tree; - -import java.io.File; - -public class FileList { - public void list(File f) { - list(f, 0); - } - - public void list(File f, int depth) { - printName(f, depth); - if (f.isDirectory()) { - File[] files = f.listFiles(); - for (File i : files) - list(i, depth + 1); - } - } - - void printName(File f, int depth) { - String name = f.getName(); - for (int i = 0; i < depth; i++) - System.out.print("+"); - if (f.isDirectory()) - System.out.println("Dir: " + name); - else - System.out.println(f.getName() + " " + f.length()); - } - - public static void main(String args[]) { - FileList L = new FileList(); - File f = new File("C:\\coderising\\tmp"); - L.list(f); - } -} From 1fadf8d274a4c4ed8754e9d1806063fa23323159 Mon Sep 17 00:00:00 2001 From: fangzuo Date: Mon, 12 Jun 2017 13:51:54 +0800 Subject: [PATCH 014/214] =?UTF-8?q?1452302762=20ood=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1452302762/ood/ood-assignment/pom.xml | 32 +++++++++ .../com/coderising/ood/srp/Configuration.java | 22 +++++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 +++ .../java/com/coderising/ood/srp/Constant.java | 7 ++ .../java/com/coderising/ood/srp/DBUtil.java | 25 +++++++ .../java/com/coderising/ood/srp/FileUtil.java | 23 +++++++ .../java/com/coderising/ood/srp/MailUtil.java | 66 +++++++++++++++++++ .../java/com/coderising/ood/srp/Product.java | 30 +++++++++ .../com/coderising/ood/srp/PromotionMail.java | 13 ++++ .../coderising/ood/srp/product_promotion.txt | 4 ++ 10 files changed, 231 insertions(+) create mode 100644 students/1452302762/ood/ood-assignment/pom.xml create mode 100644 students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Constant.java create mode 100644 students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java create mode 100644 students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/1452302762/ood/ood-assignment/pom.xml b/students/1452302762/ood/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/1452302762/ood/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..20657ee745 --- /dev/null +++ b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,22 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public static String getProperty(String key) { + return configurations.get(key); + } + +} diff --git a/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Constant.java b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Constant.java new file mode 100644 index 0000000000..02807c7a3d --- /dev/null +++ b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Constant.java @@ -0,0 +1,7 @@ +package com.coderising.ood.srp; + +public class Constant { + + public static final String NAME_KEY = "NAME"; + public static final String EMAIL_KEY = "EMAIL"; +} diff --git a/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java new file mode 100644 index 0000000000..3d386ee7f4 --- /dev/null +++ b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class FileUtil { + public static String[] readFile(File file) throws IOException + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + return data; + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } +} diff --git a/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..22389dcb59 --- /dev/null +++ b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,66 @@ +package com.coderising.ood.srp; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class MailUtil { + private static String smtpHost = Configuration.getProperty(ConfigurationKeys.SMTP_SERVER);; + private static String altSmtpHost = Configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); ; + private static String fromAddress = Configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN); ; + private static String toAddress = null; + private static String subject = null; + private static String message = null; + protected void setMessage(String name,String productDesc) throws IOException{ + if(toAddress.length()>0){ + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + } + } + private static List loadMailingList(String productID) throws Exception { + String sendMailQuery = "Select name from subscriptions where product_id= '" + productID +"and send_mail=1 "; + System.out.println("loadQuery set"); + return DBUtil.query(sendMailQuery); + } + protected static void configureEMail(HashMap userInfo) throws IOException{ + toAddress =userInfo.get(Constant.EMAIL_KEY); + } + public static void sendEMails(boolean debug,String productID) throws Exception{ + System.out.println("开始发送邮件"); + List mailingList=loadMailingList(productID); + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try{ + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + } + else { + System.out.println("没有邮件发送"); + + } + } + private static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + } +} diff --git a/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..2ba503eae4 --- /dev/null +++ b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,30 @@ +package com.coderising.ood.srp; + + +public class Product { + private String productID ; + private String productDesc; + + public Product(String productID, String productDesc) { + this.productID = productID; + this.productDesc = productDesc; + } + public Product(String[] data) { + this.productID =data[0]; + this.productDesc = data[1]; + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + } + public String getProductID() { + return productID; + } + public void setProductID(String productID) { + this.productID = productID; + } + public String getProductDesc() { + return productDesc; + } + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } +} diff --git a/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..f70095e45c --- /dev/null +++ b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,13 @@ +package com.coderising.ood.srp; + +import java.io.File; + +public class PromotionMail { + + public static void main(String[] args) throws Exception { + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + Product product=new Product(FileUtil.readFile(f)); + MailUtil.sendEMails(emailDebug,product.getProductID()); + } +} diff --git a/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/1452302762/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From d98b9ee45a9cd8ffe9e09a2198761bc36852b4a7 Mon Sep 17 00:00:00 2001 From: zhanglei <383117348@qq.com> Date: Mon, 12 Jun 2017 14:11:22 +0800 Subject: [PATCH 015/214] =?UTF-8?q?383117348=20=E7=AC=AC=E4=B8=80=E6=AC=A1?= =?UTF-8?q?ood=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coderising/ood/srp/Configuration.java | 5 +- .../coderising/ood/srp/ConfigurationKeys.java | 2 +- .../com/coderising/ood/srp/Constants.java | 17 ++ .../java/com/coderising/ood/srp/FileUtil.java | 61 +++++ .../java/com/coderising/ood/srp/MailUtil.java | 54 ++++- .../com/coderising/ood/srp/PromotionMail.java | 224 +++++------------- .../coderising/ood/srp/UserInfoService.java | 14 ++ .../ood/srp/UserInfoServiceImpl.java | 26 ++ 8 files changed, 229 insertions(+), 174 deletions(-) create mode 100644 students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/Constants.java create mode 100644 students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java create mode 100644 students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfoService.java create mode 100644 students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfoServiceImpl.java diff --git a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java index 819e85fb8a..715b5a23db 100644 --- a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java +++ b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -4,18 +4,19 @@ public class Configuration { - static Map configurations = new HashMap(); + static Map configurations = new HashMap(); static{ configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + configurations.put(ConfigurationKeys.IS_EMAIL_DEBUG, false); } /** * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 * @param key * @return */ - public String getProperty(String key) { + public Object getProperty(String key) { return configurations.get(key); } diff --git a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java index 868a03ff83..ddc77c7566 100644 --- a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java +++ b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -5,5 +5,5 @@ public class ConfigurationKeys { public static final String SMTP_SERVER = "smtp.server"; public static final String ALT_SMTP_SERVER = "alt.smtp.server"; public static final String EMAIL_ADMIN = "email.admin"; - + public static final String IS_EMAIL_DEBUG = "is_email_debug"; } diff --git a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/Constants.java b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/Constants.java new file mode 100644 index 0000000000..ef6fe915c1 --- /dev/null +++ b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/Constants.java @@ -0,0 +1,17 @@ +package com.coderising.ood.srp; + +public class Constants { + + public static final String sendMailQuery = null; + public static final String smtpHost = null; + public static final String altSmtpHost = null; + public static final String fromAddress = null; + public static final String toAddress = null; + public static final String subject = null; + public static final String message = null; + public static final String productID = null; + public static final String productDesc = null; + + public static final String NAME_KEY = "NAME"; + public static final String EMAIL_KEY = "EMAIL"; +} diff --git a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java new file mode 100644 index 0000000000..275edf2ab2 --- /dev/null +++ b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java @@ -0,0 +1,61 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class FileUtil { + + /** + * 根据文件路径获取文件,如果文件不存在,抛出异常 + * @param path + * @return + * @throws FileNotFoundException + */ + public static File readFile(String path) throws FileNotFoundException { + File file = new File(path); + if (!file.exists()) { + throw new FileNotFoundException("文件不存在"); + } + return file; + } + + /** + * 根据正则,将文件中的数据解析成字符串数组形式返回 + * + * @param file + * @param regex + * @return + */ + public static List parseToString(File file, String regex) { + List list = new ArrayList(); + BufferedReader br = null; + try { + if (file != null && file.exists()) { + + br = new BufferedReader(new FileReader(file)); + String temp = null; + while ((temp = br.readLine()) != null) { + String[] strs = temp.split(regex); + list.add(strs); + } + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + return list; + } +} diff --git a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java index 373f3ee306..d5ed2e536e 100644 --- a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java +++ b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -2,17 +2,51 @@ public class MailUtil { - public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, - boolean debug) { - //假装发了一封邮件 - StringBuilder buffer = new StringBuilder(); - buffer.append("From:").append(fromAddress).append("\n"); - buffer.append("To:").append(toAddress).append("\n"); - buffer.append("Subject:").append(subject).append("\n"); - buffer.append("Content:").append(message).append("\n"); - System.out.println(buffer.toString()); - + private static String fromAddress = ""; + private static String smtpHost = ""; + private static String altSmtpHost = ""; + private static boolean debug = false; + + private static Configuration config = new Configuration(); + + private static void ConfigureEmail() { + + altSmtpHost = (String) config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + fromAddress = (String) config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + smtpHost = (String) config.getProperty(ConfigurationKeys.SMTP_SERVER); + debug = (Boolean) config.getProperty(ConfigurationKeys.IS_EMAIL_DEBUG); } + /** + * 发送单条邮件 + * @param toAddress + * @param subject + * @param message + */ + public static void sendEmail(String toAddress,String subject,String message) { + ConfigureEmail(); + if(debug){ + System.out.println("测试环境"); + }else{ + System.out.println("正式环境"); + } + try { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + } catch (Exception e) { + try { + System.out.println("备用SMTP服务器: "); + MailUtil.sendEmail(toAddress, subject, message); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } } diff --git a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index 94bfcbaf54..d00b438768 100644 --- a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -1,199 +1,101 @@ package com.coderising.ood.srp; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; import java.io.IOException; -import java.io.Serializable; -import java.util.HashMap; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Map; public class PromotionMail { - - protected String sendMailQuery = null; - - - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; - protected String toAddress = null; protected String subject = null; protected String message = null; - protected String productID = null; - protected String productDesc = null; - - private static Configuration config; - - - - private static final String NAME_KEY = "NAME"; - private static final String EMAIL_KEY = "EMAIL"; - - - public static void main(String[] args) throws Exception { - - File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); - boolean emailDebug = false; + private static List products = new ArrayList(); - PromotionMail pe = new PromotionMail(f, emailDebug); + private static String filePath = "E:\\coding2017\\students\\383117348\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"; - } + private UserInfoService uis = new UserInfoServiceImpl(); - - public PromotionMail(File file, boolean mailDebug) throws Exception { - - //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + public PromotionMail(File file) throws Exception { + // 读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 readFile(file); - - - config = new Configuration(); - - setSMTPHost(); - setAltSMTPHost(); - - - setFromAddress(); - - - setLoadQuery(); - - sendEMails(mailDebug, loadMailingList()); - - - } - - - - - protected void setProductID(String productID) - { - this.productID = productID; - - } - - protected String getproductID() - { - return productID; - } - - protected void setLoadQuery() throws Exception { - - sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + productID +"' " - + "and send_mail=1 "; - - - System.out.println("loadQuery set"); } - - protected void setSMTPHost() - { - smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); - } - - - protected void setAltSMTPHost() - { - altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); - - } - - - protected void setFromAddress() - { - fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); - } - - protected void setMessage(HashMap userInfo) throws IOException - { - - String name = (String) userInfo.get(NAME_KEY); - - subject = "您关注的产品降价了"; - message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; - - - + public static void main(String[] args) throws Exception { + File f = FileUtil.readFile(filePath); + PromotionMail pe = new PromotionMail(f); + // 遍历每条产品信息 + for (String[] data : products) { + List> list = pe.loadUserInfoList(data[0]); + if (list != null && list.size() > 0) { + pe.sendEMails(list, data[1]); + } + } } - - protected void readFile(File file) throws IOException // @02C - { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - setProductID(data[0]); - setProductDesc(data[1]); - - System.out.println("产品ID = " + productID + "\n"); - System.out.println("产品描述 = " + productDesc + "\n"); - - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); + /** + * 读取产品文件,获得所有的产品信息 + * + * @param file + */ + protected void readFile(File file) { + List datas = FileUtil.parseToString(file, " "); + if (datas != null && datas.size() > 0) { + products = datas; + for (String[] data : products) { + System.out.print("产品ID = " + data[0] + "\n"); + System.out.println("产品描述 = " + data[1] + "\n"); + } } } - private void setProductDesc(String desc) { - this.productDesc = desc; + /** + * 根据产品id获取需要发送的用户信息,暂未考虑sql注入的安全问题 + * + * @return + * @throws Exception + */ + protected List> loadUserInfoList(String productID) throws Exception { + return uis.getList(productID); } - - protected void configureEMail(HashMap userInfo) throws IOException - { - toAddress = (String) userInfo.get(EMAIL_KEY); - if (toAddress.length() > 0) - setMessage(userInfo); + /** + * 设置发送的消息体 + * + * @param name + * @throws IOException + */ + protected void setMessage(String name, String productDesc) throws IOException { + this.subject = "您关注的产品降价了"; + this.message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; } - protected List loadMailingList() throws Exception { - return DBUtil.query(this.sendMailQuery); - } - - - protected void sendEMails(boolean debug, List mailingList) throws IOException - { + /** + * 发送邮件 + * + * @param mailingList + * @throws IOException + */ + protected void sendEMails(List> mailingList, String productDesc) throws IOException { System.out.println("开始发送邮件"); - if (mailingList != null) { - Iterator iter = mailingList.iterator(); + Iterator> iter = mailingList.iterator(); while (iter.hasNext()) { - configureEMail((HashMap) iter.next()); - try - { - if (toAddress.length() > 0) - MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } - catch (Exception e) - { - - try { - MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) - { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } + Map map = iter.next(); + String toAddress = (String) map.get("EMAIL"); + String name = (String) map.get("NAME"); + setMessage(name, productDesc); + if (toAddress != null && toAddress.length() > 0) + MailUtil.sendEmail(toAddress, subject, message); } - - - } - - else { + } else { System.out.println("没有邮件发送"); - } } + } diff --git a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfoService.java b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfoService.java new file mode 100644 index 0000000000..9dea3e28d5 --- /dev/null +++ b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfoService.java @@ -0,0 +1,14 @@ +package com.coderising.ood.srp; + +import java.util.List; +import java.util.Map; + +public interface UserInfoService { + /** + * 根据产品id获取订阅信息用户 + * @param productID + * @return + */ + public List> getList(String productID); + +} diff --git a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfoServiceImpl.java b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfoServiceImpl.java new file mode 100644 index 0000000000..07427ac138 --- /dev/null +++ b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfoServiceImpl.java @@ -0,0 +1,26 @@ +package com.coderising.ood.srp; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class UserInfoServiceImpl implements UserInfoService { + + public List> getList(String productID) { + // TODO Auto-generated method stub + List> list = new ArrayList>(); + String sql = "Select name from subscriptions " + "where product_id= '" + productID + "' " + + "and send_mail=1 "; + + System.out.println("loadQuery set"); + try { + list = DBUtil.query(sql); + } catch (Exception e) { + e.printStackTrace(); + } + + return list; + } + + +} From 24220567ed94b370a10b18a724859dbfc0b1fbb3 Mon Sep 17 00:00:00 2001 From: zhanglei <383117348@qq.com> Date: Mon, 12 Jun 2017 14:15:24 +0800 Subject: [PATCH 016/214] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/coderising/ood/srp/Constants.java | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/Constants.java diff --git a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/Constants.java b/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/Constants.java deleted file mode 100644 index ef6fe915c1..0000000000 --- a/students/383117348/ood-assignment/src/main/java/com/coderising/ood/srp/Constants.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.coderising.ood.srp; - -public class Constants { - - public static final String sendMailQuery = null; - public static final String smtpHost = null; - public static final String altSmtpHost = null; - public static final String fromAddress = null; - public static final String toAddress = null; - public static final String subject = null; - public static final String message = null; - public static final String productID = null; - public static final String productDesc = null; - - public static final String NAME_KEY = "NAME"; - public static final String EMAIL_KEY = "EMAIL"; -} From 2cb061f35c970ec2d691b60c375d7e0aaad68f9f Mon Sep 17 00:00:00 2001 From: ximan Date: Mon, 12 Jun 2017 16:09:02 +0800 Subject: [PATCH 017/214] reconsitution --- students/582161208/ood-assignment/pom.xml | 47 +++++++ .../com/coderising/ood/srp/Configuration.java | 22 +++ .../coderising/ood/srp/ConfigurationKeys.java | 8 ++ .../java/com/coderising/ood/srp/DBUtil.java | 24 ++++ .../java/com/coderising/ood/srp/MailInfo.java | 20 +++ .../java/com/coderising/ood/srp/MailUtil.java | 15 +++ .../java/com/coderising/ood/srp/Product.java | 12 ++ .../com/coderising/ood/srp/PromotionMail.java | 127 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 9 files changed, 279 insertions(+) create mode 100644 students/582161208/ood-assignment/pom.xml create mode 100644 students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/MailInfo.java create mode 100644 students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/582161208/ood-assignment/pom.xml b/students/582161208/ood-assignment/pom.xml new file mode 100644 index 0000000000..a24289ffac --- /dev/null +++ b/students/582161208/ood-assignment/pom.xml @@ -0,0 +1,47 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + org.projectlombok + lombok + 1.14.8 + + + org.apache.commons + commons-lang3 + 3.1 + + + commons-collections + commons-collections + 3.2 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..3985daf8b5 --- /dev/null +++ b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,22 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + return configurations.get(key); + } + +} diff --git a/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..0a96deeb98 --- /dev/null +++ b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,8 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; +} diff --git a/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..cf0b00a917 --- /dev/null +++ b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,24 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + return userList; + } +} diff --git a/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/MailInfo.java b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/MailInfo.java new file mode 100644 index 0000000000..ea7f061510 --- /dev/null +++ b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/MailInfo.java @@ -0,0 +1,20 @@ +package com.coderising.ood.srp; + +import lombok.Data; + +@Data +public class MailInfo { + + private String smtpHost; + + private String altSmtpHost; + + private String fromAddress; + + private String toAddress; + + private String subject; + + private String message; + +} diff --git a/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..06005966e7 --- /dev/null +++ b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,15 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + // 假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + } +} diff --git a/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..ffe0653df0 --- /dev/null +++ b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,12 @@ +package com.coderising.ood.srp; + +import lombok.Data; + +@Data +public class Product { + + private String productId; + + private String productDesc; + +} diff --git a/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..b0b93d9673 --- /dev/null +++ b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,127 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; + +public class PromotionMail { + + protected static String sendMailQuery = ""; + private static Configuration config; + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + public void sendMails(File file, boolean mailDebug) { + try { + config = new Configuration(); + sendEMails(file, mailDebug); + } catch (IOException e) { + System.out.println(e.getMessage()); + } + } + + private static MailInfo setMailInfo() { + MailInfo mailInfo = new MailInfo(); + mailInfo.setSmtpHost(config.getProperty(ConfigurationKeys.SMTP_SERVER)); + mailInfo.setAltSmtpHost(config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + mailInfo.setFromAddress(config.getProperty(ConfigurationKeys.EMAIL_ADMIN)); + return mailInfo; + } + + private static MailInfo setMessage(HashMap userInfo, MailInfo mailInfo, Product product) throws IOException { + String name = (String) userInfo.get(NAME_KEY); + mailInfo.setSubject("您关注的产品降价了"); + mailInfo.setMessage("尊敬的 " + name + ", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!"); + return mailInfo; + } + + private static Product readFile(File file) throws IOException { + Product product = setProductInfo(file); + System.out.println("产品ID = " + product.getProductId() + "\n"); + System.out.println("产品描述 = " + product.getProductDesc() + "\n"); + sendMailQuery = "Select name from subscriptions " + "where product_id= '" + product.getProductId() + "' " + + "and send_mail=1 "; + System.out.println("loadQuery set"); + return product; + } + + private static Product setProductInfo(File file) throws IOException { + Product product = null; + BufferedReader br = null; + try { + product = new Product(); + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + product.setProductId(data[0]); + product.setProductDesc(data[1]); + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + return product; + } + + private static MailInfo configureEMail(HashMap userInfo, MailInfo mailInfo, Product product) throws IOException { + String toAddress = (String) userInfo.get(EMAIL_KEY); + mailInfo.setToAddress(toAddress); + if (toAddress.length() > 0) + return setMessage(userInfo, mailInfo, product); + return mailInfo; + } + + private static void sendEMails(File file, boolean debug) throws IOException { + + Product product = readFile(file); + + MailInfo mailInfo = setMailInfo(); + + System.out.println("开始发送邮件"); + + List mailingList = DBUtil.query(sendMailQuery); + if (CollectionUtils.isNotEmpty(mailingList)) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + MailInfo newMail = configureEMail((HashMap) iter.next(), mailInfo, product); + try { + if (StringUtils.isNotBlank(newMail.getToAddress())) + sendMail(mailInfo, debug); + } catch (Exception e) { + try { + sendMail(mailInfo, debug); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + } else { + System.out.println("没有邮件发送"); + } + + } + + private static void sendMail(MailInfo mailInfo, Boolean debug) { + MailUtil.sendEmail(mailInfo.getToAddress(), mailInfo.getFromAddress(), mailInfo.getSubject(), + mailInfo.getMessage(), mailInfo.getSmtpHost(), debug); + } + + public static void main(String[] args) throws Exception { + // File f = new + // File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + File file = new File("/Users/dianping/Desktop/product_promotion.txt"); + boolean emailDebug = false; + PromotionMail pe = new PromotionMail(); + pe.sendMails(file, emailDebug); + } + +} diff --git a/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From ba6ceb38c44033b9d5bee6dd5ace449b88f31370 Mon Sep 17 00:00:00 2001 From: ximan Date: Mon, 12 Jun 2017 16:29:22 +0800 Subject: [PATCH 018/214] fix --- .../java/com/coderising/ood/srp/MailInfo.java | 13 +- .../java/com/coderising/ood/srp/Product.java | 12 -- .../com/coderising/ood/srp/ProductInfo.java | 17 +++ .../com/coderising/ood/srp/PromotionMail.java | 138 ++++++++++++------ 4 files changed, 123 insertions(+), 57 deletions(-) delete mode 100644 students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java diff --git a/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/MailInfo.java b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/MailInfo.java index ea7f061510..8a82cad4f2 100644 --- a/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/MailInfo.java +++ b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/MailInfo.java @@ -2,19 +2,24 @@ import lombok.Data; +/** + * 邮件基本信息 + * + * @author ida 2017/6/12 + */ @Data public class MailInfo { private String smtpHost; - + private String altSmtpHost; - + private String fromAddress; - + private String toAddress; private String subject; - + private String message; } diff --git a/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java deleted file mode 100644 index ffe0653df0..0000000000 --- a/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.coderising.ood.srp; - -import lombok.Data; - -@Data -public class Product { - - private String productId; - - private String productDesc; - -} diff --git a/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java new file mode 100644 index 0000000000..0d77a94f91 --- /dev/null +++ b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java @@ -0,0 +1,17 @@ +package com.coderising.ood.srp; + +import lombok.Data; + +/** + * 产品信息 + * + * @author ida 2017/6/12 + */ +@Data +public class ProductInfo { + + /** 产品id */ + private String productId; + /** 产品描述 */ + private String productDesc; +} diff --git a/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index b0b93d9673..0df36f30c9 100644 --- a/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/582161208/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -11,14 +11,26 @@ import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; +/** + * 邮件发送处理类 + * + * @author ida 2017/6/12 + */ public class PromotionMail { protected static String sendMailQuery = ""; + private static Configuration config; private static final String NAME_KEY = "NAME"; private static final String EMAIL_KEY = "EMAIL"; + /** + * 发送邮件公有方法 + * + * @param file + * @param mailDebug + */ public void sendMails(File file, boolean mailDebug) { try { config = new Configuration(); @@ -28,6 +40,48 @@ public void sendMails(File file, boolean mailDebug) { } } + /** + * 发送邮件 + * + * @param file + * @param debug + * @throws IOException + */ + private static void sendEMails(File file, boolean debug) throws IOException { + + ProductInfo product = readFile(file); + + MailInfo mailInfo = setMailInfo(); + + System.out.println("开始发送邮件"); + + List mailingList = DBUtil.query(sendMailQuery); + if (CollectionUtils.isNotEmpty(mailingList)) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + MailInfo newMail = configureEMail((HashMap) iter.next(), mailInfo, product); + try { + if (StringUtils.isNotBlank(newMail.getToAddress())) + sendMail(mailInfo, debug); + } catch (Exception e) { + try { + sendMail(mailInfo, debug); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + } else { + System.out.println("没有邮件发送"); + } + + } + + /** + * 设置邮件部分信息 + * + * @return + */ private static MailInfo setMailInfo() { MailInfo mailInfo = new MailInfo(); mailInfo.setSmtpHost(config.getProperty(ConfigurationKeys.SMTP_SERVER)); @@ -36,15 +90,32 @@ private static MailInfo setMailInfo() { return mailInfo; } - private static MailInfo setMessage(HashMap userInfo, MailInfo mailInfo, Product product) throws IOException { + /** + * 设置邮件message + * + * @param userInfo + * @param mailInfo + * @param product + * @return + * @throws IOException + */ + private static MailInfo setMessage(HashMap userInfo, MailInfo mailInfo, ProductInfo product) + throws IOException { String name = (String) userInfo.get(NAME_KEY); mailInfo.setSubject("您关注的产品降价了"); mailInfo.setMessage("尊敬的 " + name + ", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!"); return mailInfo; } - private static Product readFile(File file) throws IOException { - Product product = setProductInfo(file); + /** + * 读取文件 + * + * @param file + * @return + * @throws IOException + */ + private static ProductInfo readFile(File file) throws IOException { + ProductInfo product = setProductInfo(file); System.out.println("产品ID = " + product.getProductId() + "\n"); System.out.println("产品描述 = " + product.getProductDesc() + "\n"); sendMailQuery = "Select name from subscriptions " + "where product_id= '" + product.getProductId() + "' " @@ -53,11 +124,18 @@ private static Product readFile(File file) throws IOException { return product; } - private static Product setProductInfo(File file) throws IOException { - Product product = null; + /** + * 设置peoduct信息 + * + * @param file + * @return + * @throws IOException + */ + private static ProductInfo setProductInfo(File file) throws IOException { + ProductInfo product = null; BufferedReader br = null; try { - product = new Product(); + product = new ProductInfo(); br = new BufferedReader(new FileReader(file)); String temp = br.readLine(); String[] data = temp.split(" "); @@ -71,7 +149,17 @@ private static Product setProductInfo(File file) throws IOException { return product; } - private static MailInfo configureEMail(HashMap userInfo, MailInfo mailInfo, Product product) throws IOException { + /** + * 读取邮件数据 + * + * @param userInfo + * @param mailInfo + * @param product + * @return + * @throws IOException + */ + private static MailInfo configureEMail(HashMap userInfo, MailInfo mailInfo, ProductInfo product) + throws IOException { String toAddress = (String) userInfo.get(EMAIL_KEY); mailInfo.setToAddress(toAddress); if (toAddress.length() > 0) @@ -79,46 +167,14 @@ private static MailInfo configureEMail(HashMap userInfo, MailInfo mailInfo return mailInfo; } - private static void sendEMails(File file, boolean debug) throws IOException { - - Product product = readFile(file); - - MailInfo mailInfo = setMailInfo(); - - System.out.println("开始发送邮件"); - - List mailingList = DBUtil.query(sendMailQuery); - if (CollectionUtils.isNotEmpty(mailingList)) { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) { - MailInfo newMail = configureEMail((HashMap) iter.next(), mailInfo, product); - try { - if (StringUtils.isNotBlank(newMail.getToAddress())) - sendMail(mailInfo, debug); - } catch (Exception e) { - try { - sendMail(mailInfo, debug); - } catch (Exception e2) { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } - } - - } else { - System.out.println("没有邮件发送"); - } - - } - private static void sendMail(MailInfo mailInfo, Boolean debug) { MailUtil.sendEmail(mailInfo.getToAddress(), mailInfo.getFromAddress(), mailInfo.getSubject(), mailInfo.getMessage(), mailInfo.getSmtpHost(), debug); } public static void main(String[] args) throws Exception { - // File f = new - // File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); - File file = new File("/Users/dianping/Desktop/product_promotion.txt"); + File file = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); +// File file = new File("/Users/myhome/Desktop/product_promotion.txt"); boolean emailDebug = false; PromotionMail pe = new PromotionMail(); pe.sendMails(file, emailDebug); From 9adb2b1e16ba04711a0122cf9dabe8b104e886ea Mon Sep 17 00:00:00 2001 From: nanusl Date: Mon, 12 Jun 2017 16:44:02 +0800 Subject: [PATCH 019/214] =?UTF-8?q?=E4=BA=A4=E4=BD=9C=E4=B8=9A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../549739951/src/main/java/srp/Main.java | 58 +++++++++++++ .../src/main/java/srp/config/Config.java | 21 +++++ .../src/main/java/srp/config/Constant.java | 26 ++++++ .../549739951/src/main/java/srp/dao/DB.java | 86 +++++++++++++++++++ .../src/main/java/srp/model/Email.java | 71 +++++++++++++++ .../src/main/java/srp/model/Product.java | 58 +++++++++++++ .../src/main/java/srp/model/User.java | 58 +++++++++++++ .../main/java/srp/service/MailService.java | 36 ++++++++ .../main/java/srp/service/ProductService.java | 35 ++++++++ .../java/srp/service/PromotionService.java | 46 ++++++++++ .../main/java/srp/service/UserService.java | 36 ++++++++ .../src/main/java/srp/util/MailUtil.java | 25 ++++++ .../src/main/java/srp/util/RandomUtils.java | 61 +++++++++++++ .../src/main/resources/product_promotion.txt | 4 + 14 files changed, 621 insertions(+) create mode 100644 students/549739951/src/main/java/srp/Main.java create mode 100644 students/549739951/src/main/java/srp/config/Config.java create mode 100644 students/549739951/src/main/java/srp/config/Constant.java create mode 100644 students/549739951/src/main/java/srp/dao/DB.java create mode 100644 students/549739951/src/main/java/srp/model/Email.java create mode 100644 students/549739951/src/main/java/srp/model/Product.java create mode 100644 students/549739951/src/main/java/srp/model/User.java create mode 100644 students/549739951/src/main/java/srp/service/MailService.java create mode 100644 students/549739951/src/main/java/srp/service/ProductService.java create mode 100644 students/549739951/src/main/java/srp/service/PromotionService.java create mode 100644 students/549739951/src/main/java/srp/service/UserService.java create mode 100644 students/549739951/src/main/java/srp/util/MailUtil.java create mode 100644 students/549739951/src/main/java/srp/util/RandomUtils.java create mode 100644 students/549739951/src/main/resources/product_promotion.txt diff --git a/students/549739951/src/main/java/srp/Main.java b/students/549739951/src/main/java/srp/Main.java new file mode 100644 index 0000000000..617f64e3ee --- /dev/null +++ b/students/549739951/src/main/java/srp/Main.java @@ -0,0 +1,58 @@ +package srp; + +import srp.model.Product; +import srp.model.User; +import srp.service.ProductService; +import srp.service.PromotionService; +import srp.service.UserService; +import srp.util.RandomUtils; + +import java.util.List; + +/** + * @version V1.0 + * @Title: Main + * @Package: srp + * @Description: 主程序 + * @author: 南来 + * @date: 2017-06-12 9:22 + */ +public class Main { + + /** + * //TODO 写的crud项目太多,作业越写越懵逼,最后我也不知道写成啥了,也不知道是否符合SRP原则。。。总之……欢迎老师和同学们批评指正! + */ + public static void main(String[] args) throws InterruptedException { + //模拟业务场景 + for (; ; ) { + start(); + Thread.sleep(Long.parseLong(RandomUtils.randomNumber(3))); + } + } + + private static void start() { + //region 模拟自动装配 + UserService userService = new UserService(); + ProductService productService = new ProductService(); + PromotionService promotionService = new PromotionService(); + //endregion + + // 1 遍历商品 是否降价 + List products = productService.getProduct(); + if (null != products) { + for (Product product : products) { + // 2 商品降价 + if (product.getDown()) { + // 3 获取所有关注这个产品的用户 + List watchProductUsers = userService.getWatchProductUsers(product.getId()); + if (null != watchProductUsers && watchProductUsers.size() > 0) + // 4 发送促销邮件 + for (User user : watchProductUsers) + promotionService.promotionMail(user.getName(), user.getEmail(), product.getDesc()); + } + } + } + + //TODO 以上代码有明显线程问题。。。大家无视就好。。。。。¯\_(ツ)_/¯ + } +} diff --git a/students/549739951/src/main/java/srp/config/Config.java b/students/549739951/src/main/java/srp/config/Config.java new file mode 100644 index 0000000000..df81a97584 --- /dev/null +++ b/students/549739951/src/main/java/srp/config/Config.java @@ -0,0 +1,21 @@ +package srp.config; + +/** + * @version V1.0 + * @Title: Config + * @Package: srp.config + * @Description: 配置对象 模拟读取配置文件 //todo 或者应该封装成对象? + * @author: 南来 + * @date: 2017-06-12 9:21 + */ +public class Config { + /** + * 主邮件服务器 + */ + public static final String smtpHost = "smtp.server"; + /** + * 备用邮件服务器 + */ + public static final String altSmtpHost = "alt.smtp.server"; + +} diff --git a/students/549739951/src/main/java/srp/config/Constant.java b/students/549739951/src/main/java/srp/config/Constant.java new file mode 100644 index 0000000000..33983c7c05 --- /dev/null +++ b/students/549739951/src/main/java/srp/config/Constant.java @@ -0,0 +1,26 @@ +package srp.config; + +/** + * @version V1.0 + * @Title: Constant + * @Package: srp.config + * @Description: 存放项目常量,避免写死代码,便于后期维护。 + * @author: 南来 + * @date: 2017-06-12 9:17 + */ +public class Constant { + /** + * from address + */ + public static final String EMAIL_ADMIN = "email.admin"; + + /** + * 促销邮件主题 + */ + public static final String SUBJECT = "您关注的产品降价了"; + + /** + * 促销邮件内容 + */ + public static final String MESSAGE = "尊敬的 %s, 您关注的产品 %s 降价了,欢迎购买!"; +} diff --git a/students/549739951/src/main/java/srp/dao/DB.java b/students/549739951/src/main/java/srp/dao/DB.java new file mode 100644 index 0000000000..0b273333b6 --- /dev/null +++ b/students/549739951/src/main/java/srp/dao/DB.java @@ -0,0 +1,86 @@ +package srp.dao; + +import srp.model.Product; +import srp.model.User; +import srp.util.RandomUtils; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * @version V1.0 + * @Title: DB + * @Package: srp.dao + * @Description: 伪dao层 持续输出数据 没有细分每个对象单独dao层的部分 + * @author: 南来 + * @date: 2017-06-12 9:20 + */ +public class DB { + + /** + * 模拟在数据库中查询用户 + * + * @return 所有用户 + */ + public List getUsers() { + List users = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + User user = new User(); + user.setName(RandomUtils.randomName()); + user.setEmail(RandomUtils.randomMail()); + //region 模拟关注商品 + try { + List products = getProducts(); + if (null != products && products.size() > 0) + user.setWatchProductId(RandomUtils.randomOne(products).getId()); + } catch (IOException e) { + e.printStackTrace(); + } + //endregion + users.add(user); + } + return users; + } + + /** + * @param productId 商品id + * @return 所有关注该商品的用户集 + */ + public List getWatchProductUsers(String productId) { + if (0 == productId.length()) return null; + List users = getUsers(); + List temp = new ArrayList<>(); + for (User user : users) { + if (null != user.getWatchProductId() && productId.equals(user.getWatchProductId())) + temp.add(user); + } + return temp; + } + + /** + * 模拟在数据库中查询商品 + * + * @return 所有商品 + */ + public List getProducts() throws IOException { + List products = new ArrayList<>(); + try (BufferedReader br = new BufferedReader(new FileReader(new File("D:\\product_promotion.txt")))) { + String temp; + while (null != (temp = br.readLine())) { + String[] data = temp.split(" "); + Product product = new Product(); + product.setId(data[0]); + product.setDesc(data[1]); + //region 模拟降价 + product.setDown(RandomUtils.randomBoolean()); + //endregion + products.add(product); + } + } + return products; + } +} diff --git a/students/549739951/src/main/java/srp/model/Email.java b/students/549739951/src/main/java/srp/model/Email.java new file mode 100644 index 0000000000..a316b6cd57 --- /dev/null +++ b/students/549739951/src/main/java/srp/model/Email.java @@ -0,0 +1,71 @@ +package srp.model; + +/** + * @version V1.0 + * @Title: Email + * @Package: srp.model + * @Description: Email 对象 + * @author: 南来 + * @date: 2017-06-12 10:32 + */ +public class Email { + + /** + * from address + */ + private String from; + /** + * to address + */ + private String to; + /** + * 主题 + */ + private String subject; + /** + * 内容 + */ + private String content; + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public String getTo() { + return to; + } + + public void setTo(String to) { + this.to = to; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + @Override + public String toString() { + return "Email{" + + "from='" + from + '\'' + + ", to='" + to + '\'' + + ", subject='" + subject + '\'' + + ", content='" + content + '\'' + + '}'; + } +} diff --git a/students/549739951/src/main/java/srp/model/Product.java b/students/549739951/src/main/java/srp/model/Product.java new file mode 100644 index 0000000000..6a6cfab5ab --- /dev/null +++ b/students/549739951/src/main/java/srp/model/Product.java @@ -0,0 +1,58 @@ +package srp.model; + +/** + * @version V1.0 + * @Title: Product + * @Package: srp.model + * @Description: 商品对象 + * @author: 南来 + * @date: 2017-06-12 9:46 + */ +public class Product { + + /** + * 商品主键 + */ + private String Id; + /** + * 商品描述 + */ + private String Desc; + /** + * 是否降价 + */ + private boolean down; + + public String getId() { + return Id; + } + + public void setId(String id) { + Id = id; + } + + public String getDesc() { + return Desc; + } + + public void setDesc(String desc) { + Desc = desc; + } + + public boolean getDown() { + return down; + } + + public void setDown(boolean down) { + this.down = down; + } + + @Override + public String toString() { + return "Product{" + + "Id='" + Id + '\'' + + ", Desc='" + Desc + '\'' + + ", down='" + down + '\'' + + '}'; + } +} diff --git a/students/549739951/src/main/java/srp/model/User.java b/students/549739951/src/main/java/srp/model/User.java new file mode 100644 index 0000000000..f45c818979 --- /dev/null +++ b/students/549739951/src/main/java/srp/model/User.java @@ -0,0 +1,58 @@ +package srp.model; + +/** + * @version V1.0 + * @Title: User + * @Package: srp.model + * @Description: 用户对象 + * @author: 南来 + * @date: 2017-06-12 10:07 + */ +public class User { + + /** + * 用户名 + */ + private String name; + /** + * e-mail + */ + private String email; + /** + * 关注的商品Id + */ + private String watchProductId; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getWatchProductId() { + return watchProductId; + } + + public void setWatchProductId(String watchProductId) { + this.watchProductId = watchProductId; + } + + @Override + public String toString() { + return "User{" + + "name='" + name + '\'' + + ", email='" + email + '\'' + + ", watchProductId='" + watchProductId + '\'' + + '}'; + } +} diff --git a/students/549739951/src/main/java/srp/service/MailService.java b/students/549739951/src/main/java/srp/service/MailService.java new file mode 100644 index 0000000000..60c79117db --- /dev/null +++ b/students/549739951/src/main/java/srp/service/MailService.java @@ -0,0 +1,36 @@ +package srp.service; + +import srp.model.Email; +import srp.util.MailUtil; + +import static srp.config.Config.altSmtpHost; +import static srp.config.Config.smtpHost; + +/** + * @version V1.0 + * @Title: MailService + * @Package: srp.service + * @Description: e-mail服务类 省去接口部分 + * @author: 南来 + * @date: 2017-06-12 10:26 + */ +public class MailService { + + /** + * 负责发送邮件对象 + * + * @param email email对象 + */ + public void send(Email email) { + if (MailUtil.send(email, smtpHost)) { + System.out.println(String.format("ServerHost: %s , 邮件内容: %s ", smtpHost, email)); + } else { + System.out.println("主邮件服务器发送失败,尝试使用备用服务器发送……"); + if (MailUtil.send(email, altSmtpHost)) { + System.out.println(String.format("ServerHost: %s , 邮件内容: %s ", altSmtpHost, email)); + } else { + System.err.println("使用备用服务器发送失败……(╯°Д°)╯︵┻━┻"); + } + } + } +} diff --git a/students/549739951/src/main/java/srp/service/ProductService.java b/students/549739951/src/main/java/srp/service/ProductService.java new file mode 100644 index 0000000000..bcc10ee362 --- /dev/null +++ b/students/549739951/src/main/java/srp/service/ProductService.java @@ -0,0 +1,35 @@ +package srp.service; + +import srp.dao.DB; +import srp.model.Product; + +import java.io.IOException; +import java.util.List; + +/** + * @version V1.0 + * @Title: ProductService + * @Package: srp.service + * @Description: 商品service + * @author: 南来 + * @date: 2017-06-12 11:11 + */ +public class ProductService { + + //模拟自动装配 + private DB db = new DB(); + + /** + * 获取所有商品 + * + * @return 所有商品 + */ + public List getProduct() { + try { + return db.getProducts(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } +} diff --git a/students/549739951/src/main/java/srp/service/PromotionService.java b/students/549739951/src/main/java/srp/service/PromotionService.java new file mode 100644 index 0000000000..f888d3d673 --- /dev/null +++ b/students/549739951/src/main/java/srp/service/PromotionService.java @@ -0,0 +1,46 @@ +package srp.service; + +import srp.config.Constant; +import srp.model.Email; + +/** + * @version V1.0 + * @Title: PromotionService + * @Package: srp.service + * @Description: Promotion服务类 省去接口部分 + * @author: 南来 + * @date: 2017-06-12 10:24 + */ +public class PromotionService { + + private MailService mailService = new MailService(); + + /** + * 发送促销邮件 + * + * @param name + * @param toAddress 邮箱地址 + * @param desc + */ + public void promotionMail(String name, String toAddress, String desc) { + mailService.send(build(name, toAddress, desc)); + } + + /** + * 构建Email对象 + * + * @param name 姓名 + * @param toAddress 邮箱地址 + * @param desc 商品描述 + * @return Email对象 + */ + private Email build(String name, String toAddress, String desc) { + Email email = new Email(); + email.setFrom(Constant.EMAIL_ADMIN); + email.setTo(toAddress); + email.setSubject(Constant.SUBJECT); + email.setContent(String.format(Constant.MESSAGE, name, desc)); + return email; + } + +} diff --git a/students/549739951/src/main/java/srp/service/UserService.java b/students/549739951/src/main/java/srp/service/UserService.java new file mode 100644 index 0000000000..96f9d42bfb --- /dev/null +++ b/students/549739951/src/main/java/srp/service/UserService.java @@ -0,0 +1,36 @@ +package srp.service; + +import srp.dao.DB; +import srp.model.User; + +import java.util.List; + +/** + * @version V1.0 + * @Title: UserService + * @Package: srp.service + * @Description: User服务类 省去接口部分 + * @author: 南来 + * @date: 2017-06-12 10:25 + */ +public class UserService { + //模拟自动装配 + private DB db = new DB(); + + /** + * 获取所有用户 + * + * @return 所有用户 + */ + protected List getUser() { + return db.getUsers(); + } + + /** + * @param productId 商品id + * @return 所有关注该商品的用户集 + */ + public List getWatchProductUsers(String productId) { + return db.getWatchProductUsers(productId); + } +} diff --git a/students/549739951/src/main/java/srp/util/MailUtil.java b/students/549739951/src/main/java/srp/util/MailUtil.java new file mode 100644 index 0000000000..5bb87b37d7 --- /dev/null +++ b/students/549739951/src/main/java/srp/util/MailUtil.java @@ -0,0 +1,25 @@ +package srp.util; + +import srp.model.Email; + +/** + * @version V1.0 + * @Title: MailUtil + * @Package: srp.util + * @Description: 邮件工具类 + * @author: 南来 + * @date: 2017-06-12 10:50 + */ +public class MailUtil { + + /** + * 模拟发送 随机失败 + * + * @param email email + * @param serverHost 邮件服务配置 + * @return 成功失败 + */ + public static boolean send(Email email, String serverHost) { + return RandomUtils.randomBoolean(); + } +} diff --git a/students/549739951/src/main/java/srp/util/RandomUtils.java b/students/549739951/src/main/java/srp/util/RandomUtils.java new file mode 100644 index 0000000000..caa37fb99a --- /dev/null +++ b/students/549739951/src/main/java/srp/util/RandomUtils.java @@ -0,0 +1,61 @@ +package srp.util; + +import java.io.UnsupportedEncodingException; +import java.util.List; +import java.util.Random; + +/** + * @version V1.0 + * @Title: RandomUtils + * @Package: srp.util + * @Description: 随机工具类 + * @author: 南来 + * @date: 2017-06-12 13:04 + */ +public class RandomUtils { + + public static boolean randomBoolean() { + return new Random().nextBoolean(); + } + + public static Exception randomException() { + if (RandomUtils.randomBoolean()) + return new RuntimeException(); + return null; + } + + public static String randomMail() { + return randomNumber(9) + "@qq.com"; + } + + public static T randomOne(List list) { + return list.get(new Random().nextInt(list.size())); + } + + public static String randomNumber(int length) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < length; i++) + sb.append(new Random().nextInt(10)); + return sb.toString(); + } + + public static String randomName() { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 3; i++) { + int highCode, lowCode; + + highCode = (176 + Math.abs(new Random().nextInt(39))); + lowCode = (161 + Math.abs(new Random().nextInt(93))); + + byte[] b = new byte[]{(Integer.valueOf(highCode)).byteValue() + , (Integer.valueOf(lowCode)).byteValue()}; + + try { + sb.append(new String(b, "GBK")); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + } + return sb.toString(); + } +} diff --git a/students/549739951/src/main/resources/product_promotion.txt b/students/549739951/src/main/resources/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/549739951/src/main/resources/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 67d0e7d5a2829f07f7c5c52dcff384c4232c94a9 Mon Sep 17 00:00:00 2001 From: tianxianhu <329866097@qq.com> Date: Mon, 12 Jun 2017 17:23:02 +0800 Subject: [PATCH 020/214] initial homework environment --- students/329866097/.gitignore | 80 +++++++ students/329866097/README.md | 1 + students/329866097/pom.xml | 45 ++++ .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 198 ++++++++++++++++++ .../src/main/resources/product_promotion.txt | 4 + 9 files changed, 403 insertions(+) create mode 100644 students/329866097/.gitignore create mode 100644 students/329866097/README.md create mode 100644 students/329866097/pom.xml create mode 100644 students/329866097/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/329866097/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/329866097/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/329866097/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/329866097/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/329866097/src/main/resources/product_promotion.txt diff --git a/students/329866097/.gitignore b/students/329866097/.gitignore new file mode 100644 index 0000000000..bf58d0a162 --- /dev/null +++ b/students/329866097/.gitignore @@ -0,0 +1,80 @@ +###################### +# 解决java产生文件 +###################### +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +###################### +# 解决maven产生的文件 +###################### + +target/ +**/target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties + +###################### +# 解决各类编辑器自动产生的文件 +###################### + +*.iml + +## Directory-based project format: +.idea/ +# if you remove the above rule, at least ignore the following: + +# User-specific stuff: +# .idea/workspace.xml +# .idea/tasks.xml +# .idea/dictionaries + +# Sensitive or high-churn files: +# .idea/dataSources.ids +# .idea/dataSources.xml +# .idea/sqlDataSources.xml +# .idea/dynamic.xml +# .idea/uiDesigner.xml + +# Gradle: +# .idea/gradle.xml +# .idea/libraries + +# Mongo Explorer plugin: +# .idea/mongoSettings.xml + +## File-based project format: +*.ipr +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ +/target/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties diff --git a/students/329866097/README.md b/students/329866097/README.md new file mode 100644 index 0000000000..87f2e553da --- /dev/null +++ b/students/329866097/README.md @@ -0,0 +1 @@ +Mr.Who 作业提交 \ No newline at end of file diff --git a/students/329866097/pom.xml b/students/329866097/pom.xml new file mode 100644 index 0000000000..b1b8d4d410 --- /dev/null +++ b/students/329866097/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + com.coderising + ood-assignment-txh + 1.0-SNAPSHOT + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + \ No newline at end of file diff --git a/students/329866097/src/main/java/com/coderising/ood/srp/Configuration.java b/students/329866097/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/329866097/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/329866097/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/329866097/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/329866097/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/329866097/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/329866097/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/329866097/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/329866097/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/329866097/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/329866097/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/329866097/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/329866097/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..d32df49c79 --- /dev/null +++ b/students/329866097/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,198 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("src/main/resources/product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/329866097/src/main/resources/product_promotion.txt b/students/329866097/src/main/resources/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/329866097/src/main/resources/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 92191804f605251d5d8b3b394b582738d5ec5b54 Mon Sep 17 00:00:00 2001 From: renxin Date: Mon, 12 Jun 2017 17:30:23 +0800 Subject: [PATCH 021/214] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=B8=8A=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/335402763/pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 students/335402763/pom.xml diff --git a/students/335402763/pom.xml b/students/335402763/pom.xml new file mode 100644 index 0000000000..d73cd62750 --- /dev/null +++ b/students/335402763/pom.xml @@ -0,0 +1,6 @@ + + 4.0.0 + com.coderising + 335402763Learning + 0.0.1-SNAPSHOT + \ No newline at end of file From a0c616972623b4a600ee2ad1d180bf8a4125eda9 Mon Sep 17 00:00:00 2001 From: renxin Date: Mon, 12 Jun 2017 17:32:32 +0800 Subject: [PATCH 022/214] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=8E=9F=E5=A7=8Bcod?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 6 files changed, 278 insertions(+) create mode 100644 students/335402763/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/335402763/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/335402763/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/335402763/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/335402763/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/335402763/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/Configuration.java b/students/335402763/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..927c7155cc --- /dev/null +++ b/students/335402763/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/335402763/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..868a03ff83 --- /dev/null +++ b/students/335402763/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/335402763/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..65383e4dba --- /dev/null +++ b/students/335402763/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/335402763/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..373f3ee306 --- /dev/null +++ b/students/335402763/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/335402763/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..94bfcbaf54 --- /dev/null +++ b/students/335402763/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/335402763/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/335402763/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From b684e959f56984552b1073c5e518b2119a38a703 Mon Sep 17 00:00:00 2001 From: gongxun Date: Mon, 12 Jun 2017 19:02:26 +0800 Subject: [PATCH 023/214] =?UTF-8?q?=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../first/ood/srp/Configuration.java | 28 +++++++++ .../first/ood/srp/ConfigurationKeys.java | 10 ++++ students/785396327/first/ood/srp/DBUtil.java | 38 ++++++++++++ students/785396327/first/ood/srp/Email.java | 59 +++++++++++++++++++ .../785396327/first/ood/srp/EmailParser.java | 54 +++++++++++++++++ .../785396327/first/ood/srp/FileParser.java | 44 ++++++++++++++ .../785396327/first/ood/srp/MailSender.java | 22 +++++++ .../785396327/first/ood/srp/MailUtil.java | 16 +++++ .../first/ood/srp/PromotionMail.java | 26 ++++++++ .../785396327/first/ood/srp/SendMailTest.java | 11 ++++ .../785396327/first/ood/srp/StringUtils.java | 17 ++++++ .../first/ood/srp/product_promotion.txt | 4 ++ 12 files changed, 329 insertions(+) create mode 100644 students/785396327/first/ood/srp/Configuration.java create mode 100644 students/785396327/first/ood/srp/ConfigurationKeys.java create mode 100644 students/785396327/first/ood/srp/DBUtil.java create mode 100644 students/785396327/first/ood/srp/Email.java create mode 100644 students/785396327/first/ood/srp/EmailParser.java create mode 100644 students/785396327/first/ood/srp/FileParser.java create mode 100644 students/785396327/first/ood/srp/MailSender.java create mode 100644 students/785396327/first/ood/srp/MailUtil.java create mode 100644 students/785396327/first/ood/srp/PromotionMail.java create mode 100644 students/785396327/first/ood/srp/SendMailTest.java create mode 100644 students/785396327/first/ood/srp/StringUtils.java create mode 100644 students/785396327/first/ood/srp/product_promotion.txt diff --git a/students/785396327/first/ood/srp/Configuration.java b/students/785396327/first/ood/srp/Configuration.java new file mode 100644 index 0000000000..2d4130423e --- /dev/null +++ b/students/785396327/first/ood/srp/Configuration.java @@ -0,0 +1,28 @@ +package first.ood.srp; + +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap(); + + + static { + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/785396327/first/ood/srp/ConfigurationKeys.java b/students/785396327/first/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..28de2ced0a --- /dev/null +++ b/students/785396327/first/ood/srp/ConfigurationKeys.java @@ -0,0 +1,10 @@ +package first.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + public static final String NAME_KEY = "NAME"; + public static final String EMAIL_KEY = "EMAIL"; +} diff --git a/students/785396327/first/ood/srp/DBUtil.java b/students/785396327/first/ood/srp/DBUtil.java new file mode 100644 index 0000000000..463b464df4 --- /dev/null +++ b/students/785396327/first/ood/srp/DBUtil.java @@ -0,0 +1,38 @@ +package first.ood.srp; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * + * @param sql + * @return + */ + public static List> query(String sql) { +// validateSQL(sql, params); + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + Map userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } + + private static void validateSQL(String sql, Object[] params) { + if (StringUtils.isEmpty(sql)) + throw new RuntimeException("empty sql"); + String[] sqlFaction = sql.split("\\?"); + if (sqlFaction.length - 1 != params.length) + throw new RuntimeException("wrong number of parameters"); + + } +} diff --git a/students/785396327/first/ood/srp/Email.java b/students/785396327/first/ood/srp/Email.java new file mode 100644 index 0000000000..417e9aba6d --- /dev/null +++ b/students/785396327/first/ood/srp/Email.java @@ -0,0 +1,59 @@ +package first.ood.srp; + +/** + * Created by gongxun on 2017/6/12. + */ +public class Email { + protected String smtpHost; + protected String altSmtpHost; + protected String fromAddress; + protected String toAddress; + protected String subject; + protected String message; + + public Email() { + + } + + public Email(String smtpHost, String altSmtpHost, String fromAddress, String toAddress, String subject, String message) { + this.smtpHost = smtpHost; + this.altSmtpHost = altSmtpHost; + this.fromAddress = fromAddress; + this.toAddress = toAddress; + this.subject = subject; + this.message = message; + } + + protected void setSMTPHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + protected void setAltSMTPHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + + } + + protected void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + protected void setMessage(String message) { + this.message = message; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } +} diff --git a/students/785396327/first/ood/srp/EmailParser.java b/students/785396327/first/ood/srp/EmailParser.java new file mode 100644 index 0000000000..1129beaf34 --- /dev/null +++ b/students/785396327/first/ood/srp/EmailParser.java @@ -0,0 +1,54 @@ +package first.ood.srp; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * Created by gongxun on 2017/6/12. + */ +public class EmailParser { + + public List parseEmailList(String filepath, String loadQuery) { + List mailList = new ArrayList(); + Email email = parseCommonInfo(filepath); + List> individualInfo = getIndividualInfo(loadQuery); + for (HashMap map : individualInfo) { + PromotionMail promotionMail = new PromotionMail(email); + promotionMail.setToAddress(parseToAddress(map)); + promotionMail.setMessage(parseMessage(map, promotionMail)); + promotionMail.setSubject("您关注的产品降价了"); + mailList.add(promotionMail); + } + return mailList; + } + + private String parseMessage(HashMap map, PromotionMail promotionMail) { + String name = map.get(ConfigurationKeys.NAME_KEY); + String message = "尊敬的 " + name + ", 您关注的产品 " + promotionMail.getProductDesc() + " 降价了,欢迎购买!"; + return message; + } + + private String parseToAddress(HashMap map) { + return map.get(ConfigurationKeys.EMAIL_KEY); + } + + private PromotionMail parseCommonInfo(String filepath) { + Email email = new Email(); + + FileParser fileParser = new FileParser(filepath); +// email.setProductID(fileParser.parseProductID()); +// email.setProductDesc(fileParser.parseProductDesc()); + + Configuration configuration = new Configuration(); +// promotionMail.setSMTPHost(configuration.getProperty(ConfigurationKeys.SMTP_SERVER)); +// promotionMail.setFromAddress(configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN)); +// promotionMail.setAltSMTPHost(configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); +// return promotionMail; + return null; + } + + private List> getIndividualInfo(String loadQuery) { + return DBUtil.query(loadQuery); + } +} diff --git a/students/785396327/first/ood/srp/FileParser.java b/students/785396327/first/ood/srp/FileParser.java new file mode 100644 index 0000000000..52827cdd32 --- /dev/null +++ b/students/785396327/first/ood/srp/FileParser.java @@ -0,0 +1,44 @@ +package first.ood.srp; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +/** + * Created by gongxun on 2017/6/12. + */ +public class FileParser { + private String[] data; + + public FileParser(String filePath) { + try { + if (StringUtils.isEmpty(filePath)) + throw new RuntimeException("init file parser must contains a legal file"); + readFile(filePath); + } catch (IOException e) { + throw new RuntimeException("parse file cause errors"); + } + } + + private void readFile(String filePath) throws IOException + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(filePath)); + String temp = br.readLine(); + data = temp.split(" "); + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + public String parseProductID() { + return data[0]; + } + + public String parseProductDesc() { + return data[1]; + } +} diff --git a/students/785396327/first/ood/srp/MailSender.java b/students/785396327/first/ood/srp/MailSender.java new file mode 100644 index 0000000000..90d7f42bdb --- /dev/null +++ b/students/785396327/first/ood/srp/MailSender.java @@ -0,0 +1,22 @@ +package first.ood.srp; + +import java.util.Iterator; +import java.util.List; + +/** + * Created by gongxun on 2017/6/12. + */ +public class MailSender { + + private void sendMail(PromotionMail mail, boolean isDebug) { + MailUtil.sendEmail(mail.toAddress, mail.fromAddress, mail.subject, mail.message, StringUtils.isEmpty(mail.smtpHost) == true ? mail.smtpHost : mail.altSmtpHost, isDebug); + } + + public void sendMailList(List mailList, boolean isDebug) { + if (mailList != null) { + for (Iterator iterator = mailList.iterator(); iterator.hasNext(); ) { + sendMail(iterator.next(), isDebug); + } + } + } +} diff --git a/students/785396327/first/ood/srp/MailUtil.java b/students/785396327/first/ood/srp/MailUtil.java new file mode 100644 index 0000000000..2ec9de8c42 --- /dev/null +++ b/students/785396327/first/ood/srp/MailUtil.java @@ -0,0 +1,16 @@ +package first.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } +} diff --git a/students/785396327/first/ood/srp/PromotionMail.java b/students/785396327/first/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..1a604b7fe3 --- /dev/null +++ b/students/785396327/first/ood/srp/PromotionMail.java @@ -0,0 +1,26 @@ +package first.ood.srp; + +public class PromotionMail extends Email { + private String productID = null; + private String productDesc = null; + + public PromotionMail(Email email) { + super(email.smtpHost, email.altSmtpHost, email.fromAddress, email.toAddress, email.subject, email.message); + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getproductID() { + return productID; + } + + public void setProductDesc(String desc) { + this.productDesc = desc; + } + + public String getProductDesc() { + return this.productDesc; + } +} diff --git a/students/785396327/first/ood/srp/SendMailTest.java b/students/785396327/first/ood/srp/SendMailTest.java new file mode 100644 index 0000000000..69681f6c6d --- /dev/null +++ b/students/785396327/first/ood/srp/SendMailTest.java @@ -0,0 +1,11 @@ +package first.ood.srp; + +/** + * Created by gongxun on 2017/6/12. + */ +public class SendMailTest { + + public static void main(String[] args) { + String loadQuery = "Select name from subscriptions where product_id= ? and send_mail=1"; + } +} diff --git a/students/785396327/first/ood/srp/StringUtils.java b/students/785396327/first/ood/srp/StringUtils.java new file mode 100644 index 0000000000..f5321161bb --- /dev/null +++ b/students/785396327/first/ood/srp/StringUtils.java @@ -0,0 +1,17 @@ +package first.ood.srp; + +/** + * Created by gongxun on 2017/6/12. + */ +public class StringUtils { + + /** + * 判断文件路径是否为空 + * + * @param filePath + * @return + */ + public static boolean isEmpty(String filePath) { + return filePath == null || filePath.trim().isEmpty(); + } +} diff --git a/students/785396327/first/ood/srp/product_promotion.txt b/students/785396327/first/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/785396327/first/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 2d6c31eee79df9a75c1d714818e7d57efcd0c703 Mon Sep 17 00:00:00 2001 From: yanghuichi Date: Mon, 12 Jun 2017 20:42:32 +0800 Subject: [PATCH 024/214] Create 183549495 test --- students/183549495 | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/183549495 diff --git a/students/183549495 b/students/183549495 new file mode 100644 index 0000000000..f29dee6099 --- /dev/null +++ b/students/183549495 @@ -0,0 +1 @@ +测试 From b4cc663aa72d27b8ea586016674e2e548175b83a Mon Sep 17 00:00:00 2001 From: blidingdark Date: Mon, 12 Jun 2017 20:52:05 +0800 Subject: [PATCH 025/214] first --- students/844620174/readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/844620174/readme.md diff --git a/students/844620174/readme.md b/students/844620174/readme.md new file mode 100644 index 0000000000..3a15fb45aa --- /dev/null +++ b/students/844620174/readme.md @@ -0,0 +1 @@ +# ϴ \ No newline at end of file From 4244648f33630e189540847edf367f3b883c6c47 Mon Sep 17 00:00:00 2001 From: blidingdark Date: Mon, 12 Jun 2017 20:56:39 +0800 Subject: [PATCH 026/214] =?UTF-8?q?=E7=BC=96=E7=A0=81=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E8=A7=A3=E5=86=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/844620174/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/students/844620174/readme.md b/students/844620174/readme.md index 3a15fb45aa..7e9b389003 100644 --- a/students/844620174/readme.md +++ b/students/844620174/readme.md @@ -1 +1 @@ -# ϴ \ No newline at end of file +# 新上传的内容 \ No newline at end of file From 662bc536e17759dcc4cd7bf9b89543d17b792200 Mon Sep 17 00:00:00 2001 From: blidingdark Date: Mon, 12 Jun 2017 20:58:23 +0800 Subject: [PATCH 027/214] new line --- students/844620174/readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/students/844620174/readme.md b/students/844620174/readme.md index 7e9b389003..74ff74c6b3 100644 --- a/students/844620174/readme.md +++ b/students/844620174/readme.md @@ -1 +1,2 @@ -# 新上传的内容 \ No newline at end of file +# 新上传的内容 +123 \ No newline at end of file From 6fbbcb65e8c5cdc954f4a426ebf6d334784a7061 Mon Sep 17 00:00:00 2001 From: blidingdark Date: Mon, 12 Jun 2017 21:13:58 +0800 Subject: [PATCH 028/214] =?UTF-8?q?=E5=A2=9E=E5=8A=A0readmem=E5=86=85?= =?UTF-8?q?=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/844620174/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/students/844620174/readme.md b/students/844620174/readme.md index 74ff74c6b3..54f3009fc3 100644 --- a/students/844620174/readme.md +++ b/students/844620174/readme.md @@ -1,2 +1,2 @@ # 新上传的内容 -123 \ No newline at end of file +### Git 命令行使用 \ No newline at end of file From ceefee416d14e81c0aaf4b7d970ac7ff2d77e877 Mon Sep 17 00:00:00 2001 From: blidingdark Date: Mon, 12 Jun 2017 21:17:27 +0800 Subject: [PATCH 029/214] =?UTF-8?q?git=20=E4=BD=BF=E7=94=A8=E6=8C=87?= =?UTF-8?q?=E5=8D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/844620174/readme.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/students/844620174/readme.md b/students/844620174/readme.md index 54f3009fc3..3e7261a69d 100644 --- a/students/844620174/readme.md +++ b/students/844620174/readme.md @@ -1,2 +1,14 @@ # 新上传的内容 -### Git 命令行使用 \ No newline at end of file +### Git 命令行使用 +git 拉取主仓库步骤 +1. fork +2. clone +3. 上传代码 + +git 上传代码分为三步 +1. 新增文件 add +1. 本地提交 commit +1. 远程提交 push + +更新代码 +使用 git pull \ No newline at end of file From c5a5ff45171f85d67f12407a8c47bc40dae6313a Mon Sep 17 00:00:00 2001 From: blidingdark Date: Mon, 12 Jun 2017 21:20:04 +0800 Subject: [PATCH 030/214] =?UTF-8?q?git=E6=8C=87=E5=8D=972.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/844620174/readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/students/844620174/readme.md b/students/844620174/readme.md index 3e7261a69d..ead67b8547 100644 --- a/students/844620174/readme.md +++ b/students/844620174/readme.md @@ -10,5 +10,5 @@ git 上传代码分为三步 1. 本地提交 commit 1. 远程提交 push -更新代码 -使用 git pull \ No newline at end of file +更新代码 +使用 git pull 命令 \ No newline at end of file From 68efe80768238d3a81eaac6762076b6c4df3f30e Mon Sep 17 00:00:00 2001 From: luoziyihao Date: Mon, 12 Jun 2017 21:44:50 +0800 Subject: [PATCH 031/214] add 1204187480 --- students/1204187480/.gitignore | 21 ++ students/1204187480/code/homework/.gitignore | 21 ++ .../code/homework/coderising/pom.xml | 26 ++ .../com/coderising/jvm/clz/AccessFlag.java | 7 + .../com/coderising/jvm/clz/ClassFile.java | 7 + .../com/coderising/jvm/clz/ClassIndex.java | 10 + .../jvm/loader/ClassFileLoader.java | 114 ++++++ .../jvm/loader/ClassFileParser.java | 7 + .../coderising/litestruts/LoginAction.java | 39 ++ .../com/coderising/litestruts/Struts.java | 121 ++++++ .../com/coderising/litestruts/StrutsTest.java | 43 +++ .../java/com/coderising/litestruts/View.java | 23 ++ .../litestruts/parser/ActionConfig.java | 45 +++ .../parser/DefaultStrutsParser.java | 52 +++ .../coderising/litestruts/parser/Result.java | 22 ++ .../litestruts/parser/StrutsConfig.java | 23 ++ .../litestruts/parser/StrutsParser.java | 8 + .../coderising/src/main/resources/struts.xml | 11 + .../java/com/coderising/api/ComputeTest.java | 21 ++ .../java/com/coderising/api/CycleTest.java | 25 ++ .../java/com/coderising/api/FileTest.java | 32 ++ .../java/com/coderising/api/ObjectTest.java | 18 + .../java/com/coderising/api/StrmanTest.java | 17 + .../jvm/test/ClassFileloaderTest.java | 354 ++++++++++++++++++ .../com/coderising/jvm/test/EmployeeV1.java | 28 ++ .../litestruts/parser/StructsParserTest.java | 14 + .../1204187480/code/homework/coding/pom.xml | 12 + .../java/com/coding/basic/BinaryTreeNode.java | 32 ++ .../main/java/com/coding/basic/Iterator.java | 7 + .../src/main/java/com/coding/basic/List.java | 10 + .../src/main/java/com/coding/basic/Queue.java | 24 ++ .../src/main/java/com/coding/basic/Stack.java | 32 ++ .../com/coding/basic/array/ArrayList.java | 111 ++++++ .../com/coding/basic/array/ArrayUtil.java | 252 +++++++++++++ .../coding/basic/linklist/LRUPageFrame.java | 169 +++++++++ .../basic/linklist/LRUPageFrameTest.java | 34 ++ .../com/coding/basic/linklist/LinkedList.java | 351 +++++++++++++++++ .../test/java/com/coding/api/ArraysTest.java | 22 ++ .../test/java/com/coding/api/SystemTest.java | 24 ++ .../java/com/coding/basic/LinkedListTest.java | 202 ++++++++++ .../com/coding/basic/array/ArrayListTest.java | 37 ++ .../com/coding/basic/array/ArrayUtilTest.java | 76 ++++ .../1204187480/code/homework/common/pom.xml | 13 + .../com/coding/common/util/BeanUtils.java | 144 +++++++ .../com/coding/common/util/ByteUtils.java | 21 ++ .../com/coding/common/util/FileUtils2.java | 69 ++++ .../java/com/coding/common/util/IOUtils2.java | 24 ++ .../com/coding/common/util/StringUtils2.java | 34 ++ .../test/java/com/coding/api/ArraysTest.java | 22 ++ .../test/java/com/coding/api/SystemTest.java | 24 ++ .../com/coding/common/util/ByteUtilsTest.java | 20 + .../coding/common/util/FileUtils2Test.java | 35 ++ .../code/homework/parent-dependencies/pom.xml | 169 +++++++++ .../1204187480/code/homework/parent/pom.xml | 23 ++ students/1204187480/code/homework/pom.xml | 18 + ...0\210\350\256\241\347\256\227\346\234\272" | 0 students/1204187480/note/todo/homework.md | 8 + 57 files changed, 3128 insertions(+) create mode 100644 students/1204187480/.gitignore create mode 100644 students/1204187480/code/homework/.gitignore create mode 100644 students/1204187480/code/homework/coderising/pom.xml create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/clz/AccessFlag.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/clz/ClassFile.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/clz/ClassIndex.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/loader/ClassFileParser.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/LoginAction.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/Struts.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/StrutsTest.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/View.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/ActionConfig.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/DefaultStrutsParser.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/Result.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsConfig.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsParser.java create mode 100644 students/1204187480/code/homework/coderising/src/main/resources/struts.xml create mode 100644 students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/ComputeTest.java create mode 100644 students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/CycleTest.java create mode 100644 students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/FileTest.java create mode 100644 students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/ObjectTest.java create mode 100644 students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/StrmanTest.java create mode 100644 students/1204187480/code/homework/coderising/src/test/java/com/coderising/jvm/test/ClassFileloaderTest.java create mode 100644 students/1204187480/code/homework/coderising/src/test/java/com/coderising/jvm/test/EmployeeV1.java create mode 100644 students/1204187480/code/homework/coderising/src/test/java/com/coderising/litestruts/parser/StructsParserTest.java create mode 100644 students/1204187480/code/homework/coding/pom.xml create mode 100644 students/1204187480/code/homework/coding/src/main/java/com/coding/basic/BinaryTreeNode.java create mode 100644 students/1204187480/code/homework/coding/src/main/java/com/coding/basic/Iterator.java create mode 100644 students/1204187480/code/homework/coding/src/main/java/com/coding/basic/List.java create mode 100644 students/1204187480/code/homework/coding/src/main/java/com/coding/basic/Queue.java create mode 100644 students/1204187480/code/homework/coding/src/main/java/com/coding/basic/Stack.java create mode 100644 students/1204187480/code/homework/coding/src/main/java/com/coding/basic/array/ArrayList.java create mode 100644 students/1204187480/code/homework/coding/src/main/java/com/coding/basic/array/ArrayUtil.java create mode 100644 students/1204187480/code/homework/coding/src/main/java/com/coding/basic/linklist/LRUPageFrame.java create mode 100644 students/1204187480/code/homework/coding/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java create mode 100644 students/1204187480/code/homework/coding/src/main/java/com/coding/basic/linklist/LinkedList.java create mode 100644 students/1204187480/code/homework/coding/src/test/java/com/coding/api/ArraysTest.java create mode 100644 students/1204187480/code/homework/coding/src/test/java/com/coding/api/SystemTest.java create mode 100644 students/1204187480/code/homework/coding/src/test/java/com/coding/basic/LinkedListTest.java create mode 100644 students/1204187480/code/homework/coding/src/test/java/com/coding/basic/array/ArrayListTest.java create mode 100644 students/1204187480/code/homework/coding/src/test/java/com/coding/basic/array/ArrayUtilTest.java create mode 100644 students/1204187480/code/homework/common/pom.xml create mode 100755 students/1204187480/code/homework/common/src/main/java/com/coding/common/util/BeanUtils.java create mode 100644 students/1204187480/code/homework/common/src/main/java/com/coding/common/util/ByteUtils.java create mode 100644 students/1204187480/code/homework/common/src/main/java/com/coding/common/util/FileUtils2.java create mode 100644 students/1204187480/code/homework/common/src/main/java/com/coding/common/util/IOUtils2.java create mode 100644 students/1204187480/code/homework/common/src/main/java/com/coding/common/util/StringUtils2.java create mode 100644 students/1204187480/code/homework/common/src/test/java/com/coding/api/ArraysTest.java create mode 100644 students/1204187480/code/homework/common/src/test/java/com/coding/api/SystemTest.java create mode 100644 students/1204187480/code/homework/common/src/test/java/com/coding/common/util/ByteUtilsTest.java create mode 100644 students/1204187480/code/homework/common/src/test/java/com/coding/common/util/FileUtils2Test.java create mode 100644 students/1204187480/code/homework/parent-dependencies/pom.xml create mode 100644 students/1204187480/code/homework/parent/pom.xml create mode 100644 students/1204187480/code/homework/pom.xml create mode 100644 "students/1204187480/note/homework/cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244-\346\274\253\350\260\210\350\256\241\347\256\227\346\234\272" create mode 100644 students/1204187480/note/todo/homework.md diff --git a/students/1204187480/.gitignore b/students/1204187480/.gitignore new file mode 100644 index 0000000000..2a5296f902 --- /dev/null +++ b/students/1204187480/.gitignore @@ -0,0 +1,21 @@ +*.class +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders +.idea/ +*.iml +rebel.* +.rebel.* + +target diff --git a/students/1204187480/code/homework/.gitignore b/students/1204187480/code/homework/.gitignore new file mode 100644 index 0000000000..2a5296f902 --- /dev/null +++ b/students/1204187480/code/homework/.gitignore @@ -0,0 +1,21 @@ +*.class +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders +.idea/ +*.iml +rebel.* +.rebel.* + +target diff --git a/students/1204187480/code/homework/coderising/pom.xml b/students/1204187480/code/homework/coderising/pom.xml new file mode 100644 index 0000000000..d7d922d4d8 --- /dev/null +++ b/students/1204187480/code/homework/coderising/pom.xml @@ -0,0 +1,26 @@ + + 4.0.0 + coderising + + com.coding + parent + 1.0-SNAPSHOT + ../parent/pom.xml + + + + 2.1 + + + + + + commons-digester + commons-digester + ${commons-digester.version} + + + + + \ No newline at end of file diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/clz/AccessFlag.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/clz/AccessFlag.java new file mode 100644 index 0000000000..6b60e0bed2 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/clz/AccessFlag.java @@ -0,0 +1,7 @@ +package com.coderising.jvm.clz; + +/** + * Created by luoziyihao on 5/23/17. + */ +public class AccessFlag { +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/clz/ClassFile.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/clz/ClassFile.java new file mode 100644 index 0000000000..855bf9c7e0 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/clz/ClassFile.java @@ -0,0 +1,7 @@ +package com.coderising.jvm.clz; + +/** + * Created by luoziyihao on 5/23/17. + */ +public class ClassFile { +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/clz/ClassIndex.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/clz/ClassIndex.java new file mode 100644 index 0000000000..7b177aa12f --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/clz/ClassIndex.java @@ -0,0 +1,10 @@ +package com.coderising.jvm.clz; + +/** + * Created by luoziyihao on 5/23/17. + */ +public class ClassIndex { + + String minorVersion; + String majorVersion; +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..1c5f8196e8 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/loader/ClassFileLoader.java @@ -0,0 +1,114 @@ +package com.coderising.jvm.loader; + + +import com.coding.common.util.FileUtils2; +import org.apache.commons.lang3.StringUtils; +import strman.Strman; + +import java.io.File; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; + +import static com.coding.common.util.FileUtils2.getCanonicalPath; +import static org.apache.commons.lang3.StringUtils.replace; +import static org.apache.commons.lang3.StringUtils.substringAfter; + +/** + * Created by luoziyihao on 4/27/17. + */ +public class ClassFileLoader { + + private List clzPaths; + + private Map clzContext; + + public void addClassPath(String path) { + if (clzPaths == null) { + clzPaths = new ArrayList<>(5); + } + if (StringUtils.isBlank(path)) { + return; + } + File file = new File(path); + if (!file.isDirectory()) { + return; + } + String canonicalName = getCanonicalPath(file); + if (clzPaths.contains(canonicalName)) { + return; + } + clzPaths.add(getCanonicalPath(file)); + } + + + private static final String SPLIT = ";"; + + public String getClassPath() { + StringBuilder classPath = new StringBuilder(); + + for (String e : clzPaths) { + classPath.append(e) + .append(SPLIT); + } + if (classPath.length() > 1) { + classPath.deleteCharAt(classPath.length() - 1); + } + return classPath.toString(); + } + + private static final String CLZ_SUFFIX = ".class"; + + public byte[] readBinaryCode(String className) { + if (StringUtils.isBlank(className)) { + throw new IllegalStateException("className is blank"); + } + byte[] binaryCode = getClzContext().get(Strman.append(className, CLZ_SUFFIX)); + if (binaryCode == null) { + throw new IllegalStateException( + Strman.format("className={0} is not found in classpath", className)); + } + return binaryCode; + } + + private Map getClzContext() { + if (clzContext == null) { + clzContext = createClzContextWithClzPaths(clzPaths); + } + return clzContext; + } + + private Map createClzContextWithClzPaths(List clzPaths) { + Map clzContext = new ConcurrentHashMap<>(60); + for (String e : clzPaths) { + File file = new File(e); + if (file.isDirectory()) { + List files = FileUtils2.listAllFiles(file); + clzContext = addClassElements(clzContext, e, files); + } + } + return clzContext; + } + + private Map addClassElements(Map clzContext, String classpath, List files) { + for (File classFile : files) { + String filePath = getCanonicalPath(classFile); + String canonicalName = getCanonicalName(classpath, filePath); + byte[] bytes = FileUtils2.getBytes(classFile); + clzContext.put(canonicalName, bytes); + } + return clzContext; + } + + /** + * 将classpath 下的文件路径转成 a.b.c.class 的格式 + */ + private static final String POINT = "."; + + private String getCanonicalName(String classpath, String filePath) { + String tmp = replace(substringAfter(filePath, classpath), File.separator, POINT); + if (tmp.startsWith(POINT)) { + tmp = StringUtils.removeStart(tmp, POINT); + } + return tmp; + } +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/loader/ClassFileParser.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/loader/ClassFileParser.java new file mode 100644 index 0000000000..9c77821341 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/jvm/loader/ClassFileParser.java @@ -0,0 +1,7 @@ +package com.coderising.jvm.loader; + +/** + * Created by luoziyihao on 5/23/17. + */ +public class ClassFileParser { +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/LoginAction.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/Struts.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..0b238b2db0 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,121 @@ +package com.coderising.litestruts; + +import com.coderising.litestruts.parser.ActionConfig; +import com.coderising.litestruts.parser.DefaultStrutsParser; +import com.coderising.litestruts.parser.StrutsConfig; +import com.coderising.litestruts.parser.StrutsParser; +import com.coding.common.util.BeanUtils; + +import java.util.Map; + + +public class Struts { + + private static StrutsParser strutsParser = new DefaultStrutsParser(); + + private static final String STRUTS_CONFIG_PATH = "struts.xml"; + private static final BeanUtils beanUtils = new BeanUtils(); + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + /** + * 0. 读取配置文件struts.xml + */ + StrutsConfig strutsConfig = strutsParser.parser(STRUTS_CONFIG_PATH); + ActionConfig actionConfig = strutsConfig.getActions().get(actionName); + /** + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + */ + Object action = setPropertiesForAction(actionConfig, actionName, parameters); + + /** + * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + */ + String resultName = doExecute(action); + /** + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + */ + View view = createViewAndSetParameters(action); + /** + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + */ + setViewValue(view, resultName, actionConfig); + return view; + } + + private static void setViewValue(View view, String resultName, ActionConfig config) { + view.setJsp(config.getResults().get(resultName).getView()); + } + + private static View createViewAndSetParameters(Object action) { + View view = new View(); + view.setParameters(beanUtils.describe(action)); + return view; + } + + private static String doExecute(Object action) { + return (String) beanUtils.invokeWithNoParamter("execute", action); + } + + private static Object setPropertiesForAction(ActionConfig actionConfig, String actionName, Map parameters) { + Object action = createInstance(findActionClass(actionConfig.getClassName())); + for (Map.Entry entry : parameters.entrySet()) { + setProperty(entry.getKey(), entry.getValue(), action); + } + return action; + } + + /** + * todo 校验 key 是否存在 + * + * @param key + * @param value + * @param action + */ + private static void setProperty(String key, String value, Object action) { + beanUtils.setPara(value, key, action); + } + + private static Object createInstance(Class classValue) { + try { + return classValue.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + throw new IllegalStateException(e); + } + } + + private static Class findActionClass(String className) { + try { + return Class.forName(className); + } catch (ClassNotFoundException e) { + throw new IllegalStateException(e); + } + } + +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/StrutsTest.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/View.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/ActionConfig.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/ActionConfig.java new file mode 100644 index 0000000000..4aaba284fb --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/ActionConfig.java @@ -0,0 +1,45 @@ +package com.coderising.litestruts.parser; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class ActionConfig { + private String name; + private String className; + private Map results = new HashMap<>(10); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public Map getResults() { + return results; + } + + public void setResults(Map results) { + this.results = results; + } + + public void addResult(Result result) { + this.results.put(result.getName(), result); + } + + +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/DefaultStrutsParser.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/DefaultStrutsParser.java new file mode 100644 index 0000000000..ea58507738 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/DefaultStrutsParser.java @@ -0,0 +1,52 @@ +package com.coderising.litestruts.parser; + +import com.alibaba.fastjson.JSON; +import org.apache.commons.digester.Digester; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xml.sax.SAXException; + +import java.io.File; +import java.io.IOException; + +/** + * 解析 struts.xml 文件 + * @apiNote 借鉴 http://www.everycoding.com/coding/78.html; http://blog.csdn.net/caihaijiang/article/details/5944955 + * Created by luoziyihao on 3/5/17. + */ +public class DefaultStrutsParser implements StrutsParser { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Override + public StrutsConfig parser(String filePathInClasspath) { + String path = this.getClass().getClassLoader().getResource(filePathInClasspath).getPath(); + File input = new File(path); + Digester digester = new Digester(); + // 创建 StrutsConfig 对象 + digester.addObjectCreate("struts", StrutsConfig.class); + // 将 struts 节点上的attribute属性映射到 StrutsConfig 对象的属性上 + digester.addSetProperties("struts"); + digester.addObjectCreate("struts/action", ActionConfig.class); + // 将 struts/action 节点上的attribute属性映射到 Action 对象的属性上, 并自定义属性映射 + digester.addSetProperties("struts/action" + , new String[]{"name", "class"}, new String[]{"name", "className"}); + digester.addObjectCreate("struts/action/result", Result.class); + digester.addSetProperties("struts/action/result" + , new String[]{"name"}, new String[]{"name"}); + // 将 struts/action/result 节点上的body属性映射到 Result 对象的属性上 + digester.addCallMethod("struts/action/result", "setView", 0); + // 对应struts/action/result 生成的对象添加到 Action中 + digester.addSetNext("struts/action/result", "addResult"); + // 对应struts/action 生成的对象添加到 Struts中 + digester.addSetNext("struts/action", "addAction"); + + try { + StrutsConfig strutsConfig = (StrutsConfig) digester.parse(input); + logger.debug("strutsConfig={}", JSON.toJSONString(strutsConfig)); + return strutsConfig; + } catch (IOException | SAXException e) { + throw new IllegalStateException(e); + } + + } +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/Result.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/Result.java new file mode 100644 index 0000000000..c402418e6b --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/Result.java @@ -0,0 +1,22 @@ +package com.coderising.litestruts.parser; + +public class Result { + private String name; + private String view; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getView() { + return view; + } + + public void setView(String view) { + this.view = view; + } +} \ No newline at end of file diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsConfig.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsConfig.java new file mode 100644 index 0000000000..88f769157e --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsConfig.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts.parser; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class StrutsConfig { + public Map actions = new HashMap<>(10); + + public Map getActions() { + return actions; + } + + public void setActions(Map actions) { + this.actions = actions; + } + + public void addAction(ActionConfig action) { + this.actions.put(action.getName(), action); + } +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsParser.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsParser.java new file mode 100644 index 0000000000..ab7358c777 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsParser.java @@ -0,0 +1,8 @@ +package com.coderising.litestruts.parser; + +/** + * Created by luoziyihao on 3/5/17. + */ +public interface StrutsParser { + StrutsConfig parser(String filePathInClasspath); +} diff --git a/students/1204187480/code/homework/coderising/src/main/resources/struts.xml b/students/1204187480/code/homework/coderising/src/main/resources/struts.xml new file mode 100644 index 0000000000..876156eb4d --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/ComputeTest.java b/students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/ComputeTest.java new file mode 100644 index 0000000000..f02816a555 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/ComputeTest.java @@ -0,0 +1,21 @@ +package com.coderising.api; + +import org.junit.Test; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class ComputeTest { + + @Test + public void testDivisionExactly(){ + System.out.println( 7 >> 1); + System.out.println( -5 >> 2); + System.out.println( -5 << 2); + } + + @Test + public void testSqrt() { + System.out.println(Math.sqrt(10)); + } +} diff --git a/students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/CycleTest.java b/students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/CycleTest.java new file mode 100644 index 0000000000..6abb5d925d --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/CycleTest.java @@ -0,0 +1,25 @@ +package com.coderising.api; + +import org.junit.Test; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class CycleTest { + + /** + * checkIndex will be excuted in each cycle + */ + @Test + public void testForSize() { + int[] arr = new int[]{1, 2, 3, 4, 54}; + for (int i = 0; checkIndex(i, arr); i++ ) { + + } + } + + private boolean checkIndex(int i, int[] arr) { + System.out.println(i); + return i < arr.length; + } +} diff --git a/students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/FileTest.java b/students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/FileTest.java new file mode 100644 index 0000000000..bd918a011c --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/FileTest.java @@ -0,0 +1,32 @@ +package com.coderising.api; + +import lombok.extern.slf4j.Slf4j; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; + +/** + * Created by luoziyihao on 4/28/17. + */ +@Slf4j +public class FileTest { + + @Test + public void testFile() { + File file = new File("./hahah"); + Assert.assertFalse(file.isDirectory()); + + } + + @Test + public void testAbsolutePath() throws IOException { + File file = new File("../src"); + log.info("isDirectory={}", file.isDirectory()); + log.info(file.getAbsolutePath()); + log.info(file.getCanonicalPath()); + log.info(file.getName()); + log.info(file.getPath()); + } +} diff --git a/students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/ObjectTest.java b/students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/ObjectTest.java new file mode 100644 index 0000000000..06cd373de5 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/ObjectTest.java @@ -0,0 +1,18 @@ +package com.coderising.api; + +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; + +/** + * Created by luoziyihao on 5/2/17. + */ +@Slf4j +public class ObjectTest { + @Test + public void test(){ + Object a[] = {1}; + log.info(a.getClass().getName()); + log.info(a.getClass().getCanonicalName()); + log.info(a.getClass().getSimpleName()); + } +} diff --git a/students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/StrmanTest.java b/students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/StrmanTest.java new file mode 100644 index 0000000000..6831eb0ad6 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/test/java/com/coderising/api/StrmanTest.java @@ -0,0 +1,17 @@ +package com.coderising.api; + +import org.junit.Test; +import strman.Strman; + +/** + * Created by luoziyihao on 5/3/17. + */ +public class StrmanTest { + + @Test + public void testFormat() { + + System.out.println(Strman.format("className is not found in classpath, className={1}", ",333 ","cccc")); + + } +} diff --git a/students/1204187480/code/homework/coderising/src/test/java/com/coderising/jvm/test/ClassFileloaderTest.java b/students/1204187480/code/homework/coderising/src/test/java/com/coderising/jvm/test/ClassFileloaderTest.java new file mode 100644 index 0000000000..b7c5bab54e --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/test/java/com/coderising/jvm/test/ClassFileloaderTest.java @@ -0,0 +1,354 @@ +package com.coderising.jvm.test; + +import java.io.File; +import java.util.List; + +import ch.qos.logback.core.encoder.ByteArrayUtil; +import com.coding.common.util.ByteUtils; +import com.coding.common.util.FileUtils2; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +//import com.coderising.jvm.clz.ClassFile; +//import com.coderising.jvm.clz.ClassIndex; +//import com.coderising.jvm.cmd.BiPushCmd; +//import com.coderising.jvm.cmd.ByteCodeCommand; +//import com.coderising.jvm.cmd.OneOperandCmd; +//import com.coderising.jvm.cmd.TwoOperandCmd; +//import com.coderising.jvm.constant.ClassInfo; +//import com.coderising.jvm.constant.ConstantPool; +//import com.coderising.jvm.constant.MethodRefInfo; +//import com.coderising.jvm.constant.NameAndTypeInfo; +//import com.coderising.jvm.constant.UTF8Info; +//import com.coderising.jvm.field.Field; +import com.coderising.jvm.loader.ClassFileLoader; + +import static com.coding.common.util.FileUtils2.getCanonicalPath; +//import com.coderising.jvm.method.Method; + + + + + +public class ClassFileloaderTest { + + + private static final String FULL_QUALIFIED_CLASS_NAME = "com/coderising/jvm/test/EmployeeV1"; + + static String path1 = "target/classes"; + static String path2 = "target/test-classes"; + +// static ClassFile clzFile = null; +// static { +// ClassFileLoader loader = new ClassFileLoader(); +// loader.addClassPath(path1); +// String className = "com.coderising.jvm.test.EmployeeV1"; +// +// clzFile = loader.loadClass(className); +// +// } + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + private void addClassPath(ClassFileLoader loader) { + loader.addClassPath(path1); + loader.addClassPath(path2); + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + addClassPath(loader); + String clzPath = loader.getClassPath(); + + Assert.assertEquals(getCanonicalPath(new File(path1))+";"+getCanonicalPath(new File(path2)),clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + addClassPath(loader); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + addClassPath(loader); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = this.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + private String byteToHexString(byte[] codes ){ + return ByteUtils.byteToHexString(codes); + } + +// add comment for behind test +// /** +// * ---------------------------------------------------------------------- +// */ +// +// +// @Test +// public void testVersion(){ +// +// Assert.assertEquals(0, clzFile.getMinorVersion()); +// Assert.assertEquals(52, clzFile.getMajorVersion()); +// +// } +// +// @Test +// public void testConstantPool(){ +// +// +// ConstantPool pool = clzFile.getConstantPool(); +// +// Assert.assertEquals(53, pool.getSize()); +// +// { +// ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(1); +// Assert.assertEquals(2, clzInfo.getUtf8Index()); +// +// UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(2); +// Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, utf8Info.getValue()); +// } +// { +// ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(3); +// Assert.assertEquals(4, clzInfo.getUtf8Index()); +// +// UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(4); +// Assert.assertEquals("java/lang/Object", utf8Info.getValue()); +// } +// { +// UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(5); +// Assert.assertEquals("name", utf8Info.getValue()); +// +// utf8Info = (UTF8Info) pool.getConstantInfo(6); +// Assert.assertEquals("Ljava/lang/String;", utf8Info.getValue()); +// +// utf8Info = (UTF8Info) pool.getConstantInfo(7); +// Assert.assertEquals("age", utf8Info.getValue()); +// +// utf8Info = (UTF8Info) pool.getConstantInfo(8); +// Assert.assertEquals("I", utf8Info.getValue()); +// +// utf8Info = (UTF8Info) pool.getConstantInfo(9); +// Assert.assertEquals("", utf8Info.getValue()); +// +// utf8Info = (UTF8Info) pool.getConstantInfo(10); +// Assert.assertEquals("(Ljava/lang/String;I)V", utf8Info.getValue()); +// +// utf8Info = (UTF8Info) pool.getConstantInfo(11); +// Assert.assertEquals("Code", utf8Info.getValue()); +// } +// +// { +// MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(12); +// Assert.assertEquals(3, methodRef.getClassInfoIndex()); +// Assert.assertEquals(13, methodRef.getNameAndTypeIndex()); +// } +// +// { +// NameAndTypeInfo nameAndType = (NameAndTypeInfo) pool.getConstantInfo(13); +// Assert.assertEquals(9, nameAndType.getIndex1()); +// Assert.assertEquals(14, nameAndType.getIndex2()); +// } +// //抽查几个吧 +// { +// MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(45); +// Assert.assertEquals(1, methodRef.getClassInfoIndex()); +// Assert.assertEquals(46, methodRef.getNameAndTypeIndex()); +// } +// +// { +// UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(53); +// Assert.assertEquals("EmployeeV1.java", utf8Info.getValue()); +// } +// } +// @Test +// public void testClassIndex(){ +// +// ClassIndex clzIndex = clzFile.getClzIndex(); +// ClassInfo thisClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); +// ClassInfo superClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex()); +// +// +// Assert.assertEquals(FULL_QUALIFIED_CLASS_NAME, thisClassInfo.getClassName()); +// Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); +// } +// +// /** +// * 下面是第三次JVM课应实现的测试用例 +// */ +// @Test +// public void testReadFields(){ +// +// List fields = clzFile.getFields(); +// Assert.assertEquals(2, fields.size()); +// { +// Field f = fields.get(0); +// Assert.assertEquals("name:Ljava/lang/String;", f.toString()); +// } +// { +// Field f = fields.get(1); +// Assert.assertEquals("age:I", f.toString()); +// } +// } +// @Test +// public void testMethods(){ +// +// List methods = clzFile.getMethods(); +// ConstantPool pool = clzFile.getConstantPool(); +// +// { +// Method m = methods.get(0); +// assertMethodEquals(pool,m, +// "", +// "(Ljava/lang/String;I)V", +// "2ab7000c2a2bb5000f2a1cb50011b1"); +// +// } +// { +// Method m = methods.get(1); +// assertMethodEquals(pool,m, +// "setName", +// "(Ljava/lang/String;)V", +// "2a2bb5000fb1"); +// +// } +// { +// Method m = methods.get(2); +// assertMethodEquals(pool,m, +// "setAge", +// "(I)V", +// "2a1bb50011b1"); +// } +// { +// Method m = methods.get(3); +// assertMethodEquals(pool,m, +// "sayHello", +// "()V", +// "b2001c1222b60024b1"); +// +// } +// { +// Method m = methods.get(4); +// assertMethodEquals(pool,m, +// "main", +// "([Ljava/lang/String;)V", +// "bb000159122b101db7002d4c2bb6002fb1"); +// } +// } +// +// private void assertMethodEquals(ConstantPool pool,Method m , String expectedName, String expectedDesc,String expectedCode){ +// String methodName = pool.getUTF8String(m.getNameIndex()); +// String methodDesc = pool.getUTF8String(m.getDescriptorIndex()); +// String code = m.getCodeAttr().getCode(); +// Assert.assertEquals(expectedName, methodName); +// Assert.assertEquals(expectedDesc, methodDesc); +// Assert.assertEquals(expectedCode, code); +// } +// +// @Test +// public void testByteCodeCommand(){ +// { +// Method initMethod = this.clzFile.getMethod("", "(Ljava/lang/String;I)V"); +// ByteCodeCommand [] cmds = initMethod.getCmds(); +// +// assertOpCodeEquals("0: aload_0", cmds[0]); +// assertOpCodeEquals("1: invokespecial #12", cmds[1]); +// assertOpCodeEquals("4: aload_0", cmds[2]); +// assertOpCodeEquals("5: aload_1", cmds[3]); +// assertOpCodeEquals("6: putfield #15", cmds[4]); +// assertOpCodeEquals("9: aload_0", cmds[5]); +// assertOpCodeEquals("10: iload_2", cmds[6]); +// assertOpCodeEquals("11: putfield #17", cmds[7]); +// assertOpCodeEquals("14: return", cmds[8]); +// } +// +// { +// Method setNameMethod = this.clzFile.getMethod("setName", "(Ljava/lang/String;)V"); +// ByteCodeCommand [] cmds = setNameMethod.getCmds(); +// +// assertOpCodeEquals("0: aload_0", cmds[0]); +// assertOpCodeEquals("1: aload_1", cmds[1]); +// assertOpCodeEquals("2: putfield #15", cmds[2]); +// assertOpCodeEquals("5: return", cmds[3]); +// +// } +// +// { +// Method sayHelloMethod = this.clzFile.getMethod("sayHello", "()V"); +// ByteCodeCommand [] cmds = sayHelloMethod.getCmds(); +// +// assertOpCodeEquals("0: getstatic #28", cmds[0]); +// assertOpCodeEquals("3: ldc #34", cmds[1]); +// assertOpCodeEquals("5: invokevirtual #36", cmds[2]); +// assertOpCodeEquals("8: return", cmds[3]); +// +// } +// +// { +// Method mainMethod = this.clzFile.getMainMethod(); +// +// ByteCodeCommand [] cmds = mainMethod.getCmds(); +// +// assertOpCodeEquals("0: new #1", cmds[0]); +// assertOpCodeEquals("3: dup", cmds[1]); +// assertOpCodeEquals("4: ldc #43", cmds[2]); +// assertOpCodeEquals("6: bipush 29", cmds[3]); +// assertOpCodeEquals("8: invokespecial #45", cmds[4]); +// assertOpCodeEquals("11: astore_1", cmds[5]); +// assertOpCodeEquals("12: aload_1", cmds[6]); +// assertOpCodeEquals("13: invokevirtual #47", cmds[7]); +// assertOpCodeEquals("16: return", cmds[8]); +// } +// +// } +// +// private void assertOpCodeEquals(String expected, ByteCodeCommand cmd){ +// +// String acctual = cmd.getOffset()+": "+cmd.getReadableCodeText(); +// +// if(cmd instanceof OneOperandCmd){ +// if(cmd instanceof BiPushCmd){ +// acctual += " " + ((OneOperandCmd)cmd).getOperand(); +// } else{ +// acctual += " #" + ((OneOperandCmd)cmd).getOperand(); +// } +// } +// if(cmd instanceof TwoOperandCmd){ +// acctual += " #" + ((TwoOperandCmd)cmd).getIndex(); +// } +// Assert.assertEquals(expected, acctual); +// } + +} diff --git a/students/1204187480/code/homework/coderising/src/test/java/com/coderising/jvm/test/EmployeeV1.java b/students/1204187480/code/homework/coderising/src/test/java/com/coderising/jvm/test/EmployeeV1.java new file mode 100644 index 0000000000..12e3d7efdd --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/test/java/com/coderising/jvm/test/EmployeeV1.java @@ -0,0 +1,28 @@ +package com.coderising.jvm.test; + +public class EmployeeV1 { + + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + public void setAge(int age){ + this.age = age; + } + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + public static void main(String[] args){ + EmployeeV1 p = new EmployeeV1("Andy",29); + p.sayHello(); + + } +} \ No newline at end of file diff --git a/students/1204187480/code/homework/coderising/src/test/java/com/coderising/litestruts/parser/StructsParserTest.java b/students/1204187480/code/homework/coderising/src/test/java/com/coderising/litestruts/parser/StructsParserTest.java new file mode 100644 index 0000000000..72e841a230 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/test/java/com/coderising/litestruts/parser/StructsParserTest.java @@ -0,0 +1,14 @@ +package com.coderising.litestruts.parser; + +import org.junit.Test; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class StructsParserTest { + @Test + public void parser() throws Exception { + new DefaultStrutsParser().parser("struts.xml"); + } + +} \ No newline at end of file diff --git a/students/1204187480/code/homework/coding/pom.xml b/students/1204187480/code/homework/coding/pom.xml new file mode 100644 index 0000000000..08acfc3528 --- /dev/null +++ b/students/1204187480/code/homework/coding/pom.xml @@ -0,0 +1,12 @@ + + 4.0.0 + coding + + com.coding + parent + 1.0-SNAPSHOT + ../parent/pom.xml + + + \ No newline at end of file diff --git a/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/BinaryTreeNode.java b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/Iterator.java b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/List.java b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..ef939ae2cc --- /dev/null +++ b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/List.java @@ -0,0 +1,10 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); + public Iterator iterator(); +} diff --git a/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/Queue.java b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 0000000000..e333496198 --- /dev/null +++ b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,24 @@ +package com.coding.basic; + +import com.coding.basic.linklist.LinkedList; + +public class Queue { + + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + return elementData.remove(0); + } + + public boolean isEmpty(){ + return size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/Stack.java b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 0000000000..7336dccfe9 --- /dev/null +++ b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/Stack.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +import com.coding.basic.array.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if (isEmpty()) { + throw new IllegalStateException("the stack is empty"); + } + return elementData.remove(elementData.size() - 1); + } + + public Object peek(){ + if (isEmpty()) { + throw new IllegalStateException("the stack is empty"); + } + return elementData.get(elementData.size() - 1); + } + + public boolean isEmpty(){ + return size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/array/ArrayList.java b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/array/ArrayList.java new file mode 100644 index 0000000000..cbe1f87a05 --- /dev/null +++ b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/array/ArrayList.java @@ -0,0 +1,111 @@ +package com.coding.basic.array; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + private Iterator iterator = new ArrayListIterator(); + + private int length() { + return elementData.length; + } + + private static final int ENLARGE_LENGTH = 100; + + private Object[] enlarge(Object[] origin) { + return Arrays.copyOf(origin, origin.length + ENLARGE_LENGTH); + } + + private void enLargeElementData() { + if (size == length()) { + elementData = enlarge(elementData); + } + } + + public void add(Object o) { + enLargeElementData(); + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + checkForAdd(index); + enLargeElementData(); + // 备份 index 处及后面的数据 + Object[] elementsBehindIndex = backBehindElements(elementData, index); + // 给index处 设值 + elementData[index] = o; + // 追加 备份的数据 + appendElement(elementData, index, elementsBehindIndex); + size++; + } + + private void appendElement(Object[] origin, int pos, Object[] append) { + System.arraycopy(append, 0, origin, pos, append.length); + } + + private Object[] backBehindElements(Object[] elementData, int index) { + int backSize = size - index; + Object[] back = new Object[backSize]; + System.arraycopy(elementData, index, back, 0, backSize); + return back; + } + + public Object get(int index) { + checkIndex(index); + return elementData[index]; + } + + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new ArrayIndexOutOfBoundsException(String.format("index=%s, size=%s", index, size)); + } + } + + private void checkForAdd(int index) { + if (index < 0 || index > size) { + throw new ArrayIndexOutOfBoundsException(String.format("index=%s, size=%s", index, size)); + } + } + + public Object remove(int index) { + checkIndex(index); + Object[] back = backBehindElements(elementData, index + 1); + System.arraycopy(back, 0, elementData, index, back.length); + Object ret = elementData[index]; + elementData[index] = null; + size--; + return ret; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return iterator; + } + + private class ArrayListIterator implements Iterator { + + int next = 0; + + @Override + public boolean hasNext() { + return next < size; + } + + @Override + public Object next() { + return elementData[next++]; + } + } + +} diff --git a/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/array/ArrayUtil.java b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/array/ArrayUtil.java new file mode 100644 index 0000000000..42ec6efe57 --- /dev/null +++ b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/array/ArrayUtil.java @@ -0,0 +1,252 @@ +package com.coding.basic.array; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ArrayUtil { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + int length = origin.length; + int mid = length >> 1; + for (int i = 0; i < mid; i++) { + int hIndex = length - 1 -i; + int l = origin[i]; + int h = origin[hIndex]; + origin[hIndex] = l; + origin[i] = h; + } + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + int removeValue = 0; + return removeValue(oldArray, removeValue); + } + + private int[] removeValue(int[] oldArray, int removeValue) { + int length = oldArray.length; + int[] dest = new int[length]; + int j = 0; + for(int i = 0; i < length; i++) { + int v = oldArray[i]; + if (v != removeValue) { + dest[j++] = v; + } + } + + int[] retArray = new int[j]; + System.arraycopy(dest, 0, retArray, 0, j); + return retArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * todo 数组 a1, b1 自身去重 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int length1 = array1.length; + int length2 = array2.length; + int length = length1 + length2; + int[] newArray = new int[length]; + + return findAndSetLeastWithOutDuplicate(array1, array2, 0, 0, 0, 0, newArray); + } + + /** + * todo 优化递归出口判断, 优化三个条件判断为一个 + * @param array1 + * @param array2 + * @param i + * @param j + * @param k + * @param duplicate + * @param newArray + * @return + */ + private int[] findAndSetLeastWithOutDuplicate(int[] array1, int[] array2, int i, int j, int k, int duplicate, int[] newArray) { + + if (i == array1.length && j < array2.length) { + System.arraycopy(array2, j, newArray, k, array2.length - j); + return copyLastValues(newArray, duplicate); + } + if (j == array2.length && i < array1.length) { + System.arraycopy(array1, i, newArray, k, array1.length - i); + return copyLastValues(newArray, duplicate); + } + if (j == array2.length && i == array1.length) { + return copyLastValues(newArray, duplicate); + } + + int v1 = array1[i]; + int v2 = array2[j]; + if (v1 < v2) { + newArray [k] = v1; + return findAndSetLeastWithOutDuplicate(array1, array2, ++i, j, ++k, duplicate, newArray); + } else if (v1 > v2){ + newArray [k] = v2; + return findAndSetLeastWithOutDuplicate(array1, array2, i, ++j, ++k, duplicate, newArray); + } else { + newArray [k] = v2; + return findAndSetLeastWithOutDuplicate(array1, array2, ++i, ++j, ++k, ++duplicate, newArray); + } + + } + + private int[] copyLastValues(int[] newArray, int duplicate) { + int[] retArray = new int[newArray.length - duplicate]; + System.arraycopy(newArray, 0, retArray, 0, retArray.length); + return retArray; + } + + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + if (max <= 1) { + return new int[]{}; + } + int [] newArray = new int[max]; + newArray[0] = 1; + return fibonacciN(1, 1, 1, max, newArray); + } + + private int[] fibonacciN(int size, int current, int next, int max, int[] newArray) { + if (next >= max) { + int[] retArray = new int[size]; + System.arraycopy(newArray, 0, retArray, 0, size); + return retArray; + } else { + newArray[++size - 1] = next; + return fibonacciN(size, next, current + next, max, newArray); + } + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * todo 使用已有的质数序列 优化质数验证 + * @param max + * @return + */ + public int[] getPrimes(int max){ + if (max <= 2) { + return new int[]{}; + } + + int[] newArray = new int[max]; + int j = 0; + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + newArray[j++] = i; + } + } + int[] retArray = new int[j]; + System.arraycopy(newArray, 0, retArray, 0, j); + return retArray; + } + + private boolean isPrime(int number) { + int limit = Double.valueOf(Math.sqrt(number)).intValue(); + for(int i = 2; i <= limit; i++) { + if (number % i == 0 ) { + return false; + } + } + return true; + + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] newArray = new int[48]; // 经过不少数学家研究,到2013年2月6日为止,一共找到了48个完全数。 + int j = 0; + for (int i = 2; i < max; i++) { + if (isPerfectNumber(i)) { + if (j >= newArray.length) { + newArray = this.grow(newArray, 1); + } + newArray[j++] = i; + + } + } + int[] retArray = new int[j]; + System.arraycopy(newArray, 0, retArray, 0, j); + return retArray; + } + + private boolean isPerfectNumber(int number) { + int sum = 0; + for (int i = 1; i < number; i++) { + if (number % i == 0) { + sum += i; + } + } + + return sum == number; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder builder = new StringBuilder(20); + int length = array.length; + for (int i = 0; i< length; i++) { + builder.append(array[i]).append(seperator); + } + builder.deleteCharAt(builder.length() - seperator.length()); + return builder.toString(); + } + + +} diff --git a/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/linklist/LRUPageFrame.java b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..96c2e2cdab --- /dev/null +++ b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,169 @@ +package com.coding.basic.linklist; + +import lombok.extern.slf4j.Slf4j; + +/** + * 定长链表 + * 命中后更新 pageNumber的位置 + * 随时要考虑 first, node 的变化 + */ +@Slf4j +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + public Node(Node next, int pageNum) { + this.next = next; + this.pageNum = pageNum; + } + + public Node(int pageNum) { + this.pageNum = pageNum; + } + + Node() { + } + + public String debug() { + return new StringBuilder(). + append("\n##########################pre: ") + .append(prev) + .append("\n##########################node: ") + .append(this) + .append("\n##########################next: ") + .append(next) + .append("\n##########################pageNum: ") + .append(pageNum) + + .toString(); + } + } + + + private int capacity; + + private int currentSize; + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + this.currentSize = 0; + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * 新的对象应该放在前面 + * + * @param pageNum + * @return + */ + public void access(int pageNum) { + if (capacity == 0) { + return; + } + + // 如果已经存在, 删除已经存在的 + removeContainedNode(pageNum); + + /** + * 向前追加 + */ + // 如果 first 为空, first=last=newNode + if (first == null && last == null) { + first = last = new Node(pageNum); + // 如果不为空 , first=newNode, first.next.pre = first + } else { + first = new Node(first, pageNum); + first.next.prev = first; + } + // 修改 size + currentSize++; + debugContent("addNewNode"); + + // 如果 size = capacity + 1, 去除last (额外考虑 capacity 为 1 的情况), last.next=null + if (currentSize == capacity + 1) { + last = last.prev; + last.next = null; + currentSize--; + debugContent("rmSpareNode"); + } + } + + private Node removeContainedNode(int pageNum) { + Node node = first; + while (node != null) { + + if (node.pageNum == pageNum) { + Node nodePre = node.prev; + Node nodeNext = node.next; + if (nodePre == null) { // 说明在第一个节点就 hit了 + first = nodeNext; + first.prev = null; + } else { + nodePre.next = nodeNext; + if (nodeNext != null) { + nodeNext.prev = nodePre; + } else { + last = nodePre; // 如果 nodeNext 为空, 说明原先 last 是 node, 现在是 nodePre + } + } + currentSize--; + return node; + } + node = node.next; + } + debugContent("removeContainedNode"); + return null; + } + + private void debugContent(String tag) { + log.debug("tag={}, currentSize={}, toString={}", tag, currentSize, debug()); + } + + + public String toString() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer.append(node.pageNum); + + node = node.next; + if (node != null) { + buffer.append(","); + } + } + return buffer.toString(); + } + + public String debug() { + StringBuilder buffer = new StringBuilder(); + Node node = first; + while (node != null) { + buffer + .append(node.debug()) + .append("\n##########################last: ") + .append(last) + .append("\n##########################capacity: ") + .append(capacity) + .append("\n##########################toString: ") + .append(toString()) + + ; + + node = node.next; + if (node != null) { + buffer.append("\n,"); + } + } + return buffer.toString() + "\n"; + } + + +} diff --git a/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..7fd72fc2b4 --- /dev/null +++ b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,34 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + frame.access(5); + Assert.assertEquals("5,4,0", frame.toString()); + + } + +} diff --git a/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/linklist/LinkedList.java b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/linklist/LinkedList.java new file mode 100644 index 0000000000..d9c4ee3c7b --- /dev/null +++ b/students/1204187480/code/homework/coding/src/main/java/com/coding/basic/linklist/LinkedList.java @@ -0,0 +1,351 @@ +package com.coding.basic.linklist; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class LinkedList implements List { + + private Node head; + private int size = 0; + + public void add(Object o) { + Node newNode = new Node(o, null); + if (head == null) { + head = newNode; + } else { + node(size - 1).next = newNode; + } + size++; + } + + public void add(int index, Object o) { + checkForAdd(index); + if (index == size) { + add(o); + } else { + Node newNode = new Node(o, null); + if (index == 0) { + addFirst(o); + } else { + Node preNode = node(index - 1); + Node now = preNode.next; + preNode.next = newNode; + newNode.next = now; + size++; + } + } + + } + + private Node node(int index) { + Node x = head; + for (int i = 0; i < index; i++) { + x = x.next; + } + return x; + } + + public Object get(int index) { + checkIndex(index); + return node(index).data; + } + + /** + * 让被删除的引用的持有者指向下一个节点 + * + * @param index + * @return + */ + public Object remove(int index) { + final Object ret; + checkIndex(index); + if (index == 0) { + Node removeNode = head; + ret = head.data; + head = removeNode.next; + } else { + Node pre = node(index - 1); + Node removeNode = pre.next; + ret = removeNode.data; + pre.next = removeNode.next; + } + size--; + return ret; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + head = new Node(o, head); + ; + size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + if (size == 0) { + return null; + } else { + return remove(0); + } + } + + public Object removeLast() { + return remove(size - 1); + } + + public LinkedListIterator iterator() { + return new LinkedListIterator(head); + } + + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new ArrayIndexOutOfBoundsException(String.format("index=%s, size=%s", index, size)); + } + } + + private void checkForAdd(int index) { + if (index < 0 || index > size) { + throw new ArrayIndexOutOfBoundsException(String.format("index=%s, size=%s", index, size)); + } + } + + + @Override + public String toString() { + Iterator iterator = iterator(); + StringBuilder builder = new StringBuilder("["); + while ((iterator.hasNext())) { + builder.append(iterator.next()).append(','); + } + if (size() > 0) { + builder.deleteCharAt(builder.length() - 1); + } + return builder + .append(']') + .toString(); + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse() { + if (size == 0) { + return; + } + Object[] datas = new Object[size]; + int i = 0; + // 迭代链表的数据生成数组 + Iterator iterator = iterator(); + while (iterator.hasNext()) { + datas[i++] = iterator.next(); + } + // 遍历数组越生成新的 链表 + Node newHead = new Node(datas[--i], null); + Node next = newHead; + for (int j = --i; j >= 0; j--) { + next.next = new Node(datas[j], null); + next = next.next; + + } + this.head = newHead; + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + */ + public void removeFirstHalf() { + removeFirstSize(size >> 1); + } + + public void removeFirstSize(int firstSize) { + firstSize = firstSize > size() ? size() : firstSize; + LinkedListIterator iterator = iterator(); + int i = 1; + while (i++ <= firstSize) { + iterator.nextNode(); + } + if (size > 0) { + head = iterator.nextNode(); + size = size() - firstSize; + } + } + + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * + * @param i + * @param length + */ + public void remove(int i, int length) { + if (i == 0) { + removeFirstSize(length); + return; + } + if (i >= size || length == 0) { + return; + } + + int lastLenth = size - i; + length = length <= lastLenth ? length : lastLenth; + Node pre = node(i - 1); + int j = 0; + + Node next = pre; + while (j++ < length) { + next = next.next; + } + pre.next = next.next; + size = size - length; + + + } + + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * + * @param list + */ + public int[] getElements(LinkedList list) { + if (size() == 0) { + return new int[0]; + } + Iterator iterator = list.iterator(); + Node fromNode = iterator().nextNode(); + int fromIndex = 0; + + int[] retArray = new int[list.size()]; + int retIndex = 0; + while (iterator.hasNext()) { + int index = (int) iterator.next(); + Node node = node(fromNode, fromIndex, index); + fromIndex = index; + fromNode = node; + if (node == null) { + return retArray; + } else { + retArray[retIndex++] = (int)node.data; + } + } + return retArray; + } + + private Node node(Node fromNode, int fromIndex, int index) { + Node next = fromNode; + int nextIndex = fromIndex; + while (next != null && nextIndex < index) { + next = next.next; + nextIndex++; + } + if (nextIndex == index) { + return next; + } else { + return null; + } + } + + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + * + * @param list + */ + + public void subtract(LinkedList list) { + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues() { + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * + * @param min + * @param max + */ + public void removeRange(int min, int max) { + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * + * @param list + */ + public LinkedList intersection(LinkedList list) { + return null; + } + + private static class Node { + Object data; + Node next; + + public Node() { + } + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + private class LinkedListIterator implements Iterator { + + private Node next; + + public LinkedListIterator() { + } + + private LinkedListIterator(Node next) { + this.next = next; + } + + @Override + public boolean hasNext() { + return next != null; + } + + @Override + public Object next() { + if (next == null) { + throw new IndexOutOfBoundsException("there is no node in list"); + } + Node ret = next; + next = next.next; + return ret.data; + } + + + private Node nextNode() { + if (next == null) { + throw new IndexOutOfBoundsException("there is no node in list"); + } + Node ret = next; + next = next.next; + return ret; + } + } +} diff --git a/students/1204187480/code/homework/coding/src/test/java/com/coding/api/ArraysTest.java b/students/1204187480/code/homework/coding/src/test/java/com/coding/api/ArraysTest.java new file mode 100644 index 0000000000..eb41a7e262 --- /dev/null +++ b/students/1204187480/code/homework/coding/src/test/java/com/coding/api/ArraysTest.java @@ -0,0 +1,22 @@ +package com.coding.api; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; + +/** + * Created by luoziyihao on 2/25/17. + */ +public class ArraysTest { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + + @Test + public void testCopyOf(){ + Object[] a = new Object[]{1, 2, 3, 4}; + Object[] b = Arrays.copyOf(a, 10); + logger.info("a={}, b={}", Arrays.toString(a), Arrays.toString(b)); + } +} diff --git a/students/1204187480/code/homework/coding/src/test/java/com/coding/api/SystemTest.java b/students/1204187480/code/homework/coding/src/test/java/com/coding/api/SystemTest.java new file mode 100644 index 0000000000..efc4022378 --- /dev/null +++ b/students/1204187480/code/homework/coding/src/test/java/com/coding/api/SystemTest.java @@ -0,0 +1,24 @@ +package com.coding.api; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; + +/** + * Created by luoziyihao on 2/25/17. + */ +public class SystemTest { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Test + public void testArrayCopy() { + int[] a = new int[]{1, 2, 3, 4, 5, 6, 7}; + int[] b = new int[]{11, 22, 33, 44, 55, 66, 77}; + System.arraycopy(a, 2, b, 4, 3); + logger.info("b={}", Arrays.toString(b)); + + } +} diff --git a/students/1204187480/code/homework/coding/src/test/java/com/coding/basic/LinkedListTest.java b/students/1204187480/code/homework/coding/src/test/java/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..9e951354ef --- /dev/null +++ b/students/1204187480/code/homework/coding/src/test/java/com/coding/basic/LinkedListTest.java @@ -0,0 +1,202 @@ +package com.coding.basic; + +import com.coding.basic.linklist.LinkedList; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by luoziyihao on 3/23/17. + */ +public class LinkedListTest { + + @Test + public void add() throws Exception { + + } + + @Test + public void add1() throws Exception { + + } + + @Test + public void get() throws Exception { + + } + + @Test + public void remove() throws Exception { + + } + + @Test + public void size() throws Exception { + + } + + @Test + public void addFirst() throws Exception { + + } + + @Test + public void addLast() throws Exception { + + } + + @Test + public void removeFirst() throws Exception { + + } + + @Test + public void removeLast() throws Exception { + + } + + @Test + public void removeFirstHalf() throws Exception { + LinkedList linkedList = createAndFillLinkedList(0); + linkedList.removeFirstHalf(); + Assert.assertEquals("[]", linkedList.toString()); + } + + @Test + public void removeFirstHalf1() throws Exception { + LinkedList linkedList = createAndFillLinkedList(1); + linkedList.removeFirstHalf(); + Assert.assertEquals("[1]", linkedList.toString()); + } + + @Test + public void removeFirstHalf2() throws Exception { + LinkedList linkedList = createAndFillLinkedList(2); + linkedList.removeFirstHalf(); + Assert.assertEquals("[2]", linkedList.toString()); + } + + @Test + public void removeFirstHalf3() throws Exception { + LinkedList linkedList = createAndFillLinkedList(3); + linkedList.removeFirstHalf(); + Assert.assertEquals("[2,3]", linkedList.toString()); + } + + private LinkedList createAndFillLinkedList() { + return createAndFillLinkedList(4); + } + + private LinkedList createAndFillLinkedList(int length) { + return createAndFillLinkedList(1, length); + } + + private LinkedList createAndFillLinkedList(int start, int length) { + LinkedList linkedList = new LinkedList(); + for (int i = start; i <= length; i++) { + linkedList.add(i); + } + return linkedList; + } + + @Test + public void remove1() throws Exception { + LinkedList list = createAndFillLinkedList(4); + list.remove(0, 0); + Assert.assertEquals("[1,2,3,4]", list.toString()); + } + + @Test + public void remove2() throws Exception { + LinkedList list = createAndFillLinkedList(4); + list.remove(0, 1); + Assert.assertEquals("[2,3,4]", list.toString()); + } + + @Test + public void remove3() throws Exception { + LinkedList list = createAndFillLinkedList(4); + list.remove(1, 0); + Assert.assertEquals("[1,2,3,4]", list.toString()); + } + + @Test + public void remove4() throws Exception { + LinkedList list = createAndFillLinkedList(4); + list.remove(1, 1); + Assert.assertEquals("[1,3,4]", list.toString()); + } + + @Test + public void remove5() throws Exception { + LinkedList list = createAndFillLinkedList(4); + list.remove(1, 3); + Assert.assertEquals("[1]", list.toString()); + } + + @Test + public void remove6() throws Exception { + LinkedList list = createAndFillLinkedList(4); + list.remove(1, 4); + Assert.assertEquals("[1]", list.toString()); + } + + @Test + public void remove7() throws Exception { + LinkedList list = createAndFillLinkedList(4); + list.remove(1, 5); + Assert.assertEquals("[1]", list.toString()); + } + + @Test + public void getElements() throws Exception { +// LinkedList listA = createAndFillLinkedList(0, 8); +// LinkedList listB = createAndFillLinkedList(4, 4); +// Assert.assertEquals("[4,5,6,7]", Arrays.toString(listA.getElements(listB))); + + } + + @Test + public void subtract() throws Exception { + + } + + @Test + public void removeDuplicateValues() throws Exception { + + } + + @Test + public void removeRange() throws Exception { + + } + + @Test + public void intersection() throws Exception { + + } + + @Test + public void iterator() throws Exception { + + List linkedList = new LinkedList(); + linkedList.add("1"); + linkedList.add("2"); + linkedList.add("3"); + linkedList.add("4"); + Assert.assertEquals("[1,2,3,4]", linkedList.toString()); + } + + @Test + public void reverse() throws Exception { + LinkedList linkedList = new LinkedList(); + linkedList.add("1"); + linkedList.add("2"); + linkedList.add("3"); + linkedList.add("4"); + linkedList.reverse(); + Assert.assertEquals("[4,3,2,1]", linkedList.toString()); + } + +} \ No newline at end of file diff --git a/students/1204187480/code/homework/coding/src/test/java/com/coding/basic/array/ArrayListTest.java b/students/1204187480/code/homework/coding/src/test/java/com/coding/basic/array/ArrayListTest.java new file mode 100644 index 0000000000..e70c52a725 --- /dev/null +++ b/students/1204187480/code/homework/coding/src/test/java/com/coding/basic/array/ArrayListTest.java @@ -0,0 +1,37 @@ +package com.coding.basic.array; + +import com.coding.basic.List; +import com.coding.basic.array.ArrayList; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Created by luoziyihao on 2/25/17. + */ +public class ArrayListTest { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + + private List list = new ArrayList(); + + @Before + public void before() { + + } + + @Test + public void add() throws Exception { + list.add(1); + } + + @Test + public void get() throws Exception { + add(); + logger.info("{}", list.get(0)); + } + +} \ No newline at end of file diff --git a/students/1204187480/code/homework/coding/src/test/java/com/coding/basic/array/ArrayUtilTest.java b/students/1204187480/code/homework/coding/src/test/java/com/coding/basic/array/ArrayUtilTest.java new file mode 100644 index 0000000000..04c8b51547 --- /dev/null +++ b/students/1204187480/code/homework/coding/src/test/java/com/coding/basic/array/ArrayUtilTest.java @@ -0,0 +1,76 @@ +package com.coding.basic.array; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.*; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class ArrayUtilTest { + + private ArrayUtil creatArrayUtil(){ + return new ArrayUtil(); + } + + @Test + public void reverseArray() throws Exception { + int[] origin = new int[]{1, 2, 3}; + int[] destArray = new int[]{3, 2, 1}; + creatArrayUtil().reverseArray(origin); + Assert.assertArrayEquals(destArray, origin); + } + + @Test + public void removeZero() throws Exception { + int[] origin = new int[]{1, 2, 3, 0, 10}; + int[] destArray = new int[]{1, 2, 3, 10}; + int[] retArray = creatArrayUtil().removeZero(origin); + Assert.assertArrayEquals(destArray, retArray); + } + + @Test + public void merge() throws Exception { + int[] a = new int[]{1, 2, 3}; + int[] b = new int[]{2, 3}; + + int[] newArray = creatArrayUtil().merge(a, b); + info(newArray); + assertArrayEquals(new int[]{1, 2, 3}, newArray); + } + + @Test + public void grow() throws Exception { + assertArrayEquals(new int[]{1, 2, 0, 0}, creatArrayUtil().grow(new int[]{1, 2}, 2)); + } + + @Test + public void fibonacci() throws Exception { + assertArrayEquals(new int[]{1, 1, 2, 3, 5, 8}, creatArrayUtil().fibonacci(10)); + } + + @Test + public void getPrimes() throws Exception { + int max = Double.valueOf(Math.pow(2, 4)).intValue(); + assertArrayEquals(new int[]{2, 3, 5, 7, 11, 13}, creatArrayUtil().getPrimes(max)); + } + + @Test + public void getPerfectNumbers() throws Exception { + int max = Double.valueOf(Math.pow(2, 8)).intValue(); + assertArrayEquals(new int[]{6, 28}, creatArrayUtil().getPerfectNumbers(max)); + + } + + @Test + public void join() throws Exception { + assertEquals("1_2_3_10", creatArrayUtil().join(new int[]{1, 2, 3, 10}, "_")); + } + + private void info(int[] array) { + System.out.println(Arrays.toString(array)); + } +} \ No newline at end of file diff --git a/students/1204187480/code/homework/common/pom.xml b/students/1204187480/code/homework/common/pom.xml new file mode 100644 index 0000000000..3cbad444b6 --- /dev/null +++ b/students/1204187480/code/homework/common/pom.xml @@ -0,0 +1,13 @@ + + 4.0.0 + common + jar + + com.coding + parent-dependencies + 1.0-SNAPSHOT + ../parent-dependencies/pom.xml + + + \ No newline at end of file diff --git a/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/BeanUtils.java b/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/BeanUtils.java new file mode 100755 index 0000000000..67dd8fd1f1 --- /dev/null +++ b/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/BeanUtils.java @@ -0,0 +1,144 @@ +package com.coding.common.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.math.BigDecimal; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by luoziyihao on 5/25/16. + */ +public class BeanUtils { + + + public static final String SET = "set"; + public static final String GET = "get"; + // 日志输出类 + private final Logger log = LoggerFactory.getLogger(this.getClass()); + private final StringUtils2 stringUtils = new StringUtils2(); + + public Object setInvoke(Object para, String methodName, Object obj) { + Method method = null; + Object returnObj = null; + try { + + method = obj.getClass().getMethod(methodName, para.getClass()); + returnObj = method.invoke(obj, para); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + return returnObj; + } + + public Object getInvoke(String methodName, Object obj) { + Method method = null; + Object returnObj = null; + try { + method = obj.getClass().getMethod(methodName); + returnObj = method.invoke(obj); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + return returnObj; + } + + public Object invokeWithNoParamter(String methodName, Object obj) { + Method method; + Object returnObj = null; + try { + method = obj.getClass().getMethod(methodName); + returnObj = method.invoke(obj); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + return returnObj; + } + + + + public Object getPara(String paraName, Object object) { + if (stringUtils.isSpaceOrNull(paraName)) { + throw new RuntimeException("paraname is null or space"); + } + String methodName = new StringBuilder().append(GET).append(stringUtils.toUpperCase(paraName, 0)).toString(); + return getInvoke(methodName, object); + } + + + + public Object setPara(Object para, String paraName, Object object) { + if (stringUtils.isSpaceOrNull(paraName)) { + throw new RuntimeException("paraname is null or space"); + } + String methodName = new StringBuilder().append(SET).append(stringUtils.toUpperCase(paraName, 0)).toString(); + return setInvoke(para, methodName, object); + } + + public T get(String paraName, Object object, Class clazz) { + return (T) getPara(paraName, object); + } + + public Integer getInt(String paraName, Object object) { + return (Integer) getPara(paraName, object); + } + + public Long getLong(String paraName, Object object) { + return (Long) getPara(paraName, object); + } + + public Double getDouble(String paraName, Object object) { + return (Double) getPara(paraName, object); + } + + public BigDecimal getBigDecimal(String paraName, Object object) { + return (BigDecimal) getPara(paraName, object); + } + + public String getString(String paraName, Object object) { + return getPara(paraName, object).toString(); + } + + public Date getDate(String paraName, Object object) { + return (Date) getPara(paraName, object); + } + + public Long getLongByString(String paraName, Object object) { + return Long.parseLong(getString(paraName, object)); + } + + public Integer getIntByString(String paraName, Object object) { + return Integer.parseInt(getString(paraName, object)); + } + + public Double getDoubleByString(String paraName, Object object) { + return Double.parseDouble(getString(paraName, object)); + } + + public BigDecimal getBigDecimalByString(String paraName, Object object) { + return new BigDecimal(getString(paraName, object)); + } + + private final static String GETTER_PRE = "get"; + public Map describe(Object model) { + Method[] methods = model.getClass().getDeclaredMethods(); //获取实体类的所有属性,返回Field数组 + Map properties = new HashMap<>(); + for (Method method : methods) { + String methodName = method.getName(); + if (methodName.startsWith(GETTER_PRE)) { + try { + Object o = method.invoke(model); + String valueName = stringUtils.toLowwerCase(methodName.substring(GETTER_PRE.length()), 0); + properties.put(valueName, o); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new IllegalStateException(e); + } + } + } + return properties; + } +} diff --git a/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/ByteUtils.java b/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/ByteUtils.java new file mode 100644 index 0000000000..4126765ff1 --- /dev/null +++ b/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/ByteUtils.java @@ -0,0 +1,21 @@ +package com.coding.common.util; + +/** + * Created by luoziyihao on 5/2/17. + */ +public abstract class ByteUtils { + + public static String byteToHexString(byte[] codes ){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i listAllFiles(File directory) { + Preconditions.checkNotNull(directory); + Preconditions.checkArgument(directory.isDirectory() + , "file=%s is not directory", directory.getPath()); + return listAllFiles(new ArrayList<>(), directory); + } + + private static List listAllFiles(List files, File directory) { + File[] fileArr = directory.listFiles(); + if (fileArr == null) { + return files; + } + for (File file : fileArr) { + if (file.isDirectory()) { + files = listAllFiles(files, file); + } else { + files.add(file); + } + } + return files; + } + + + public static String getCanonicalPath(File file) { + Preconditions.checkNotNull(file); + try { + return file.getCanonicalPath(); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + + public static byte[] getBytes(File classFile) throws IllegalStateException { + // byteArrayOutputStream, 可写的动长数组 + ByteArrayOutputStream baos = null; + BufferedInputStream bis = null; + try { + baos = new ByteArrayOutputStream(); + bis = new BufferedInputStream(new FileInputStream(classFile)); + int intTmp = bis.read(); + while (intTmp != -1) { + baos.write(intTmp); + intTmp = bis.read(); + } + return baos.toByteArray(); + } catch (IOException e) { + throw new IllegalStateException(e); + } finally { + IOUtils2.close(baos); + IOUtils2.close(bis); + } + } + + +} diff --git a/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/IOUtils2.java b/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/IOUtils2.java new file mode 100644 index 0000000000..a302a6c199 --- /dev/null +++ b/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/IOUtils2.java @@ -0,0 +1,24 @@ +package com.coding.common.util; + +import java.io.Closeable; +import java.io.IOException; +import java.io.OutputStream; + +/** + * Created by luoziyihao on 5/2/17. + */ +public abstract class IOUtils2 { + + public static void close(Closeable closeable) { + if (closeable != null) { + try { + closeable.close(); + } catch (IOException e) { + throw new IllegalStateException(e); + + } + } + } + + +} diff --git a/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/StringUtils2.java b/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/StringUtils2.java new file mode 100644 index 0000000000..0883351690 --- /dev/null +++ b/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/StringUtils2.java @@ -0,0 +1,34 @@ +package com.coding.common.util; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class StringUtils2 { + + /** + * 改变指定位置的 char的大小写 + */ + public String toUpperCase(String str, int index) { + char[] chars = str.toCharArray(); + if (index + 1 > chars.length) { + throw new RuntimeException("the char at the index don't exist"); + } + chars[index] = Character.toUpperCase(chars[index]); + return new String(chars); + } + + /** + * 改变指定位置的 char的大小写 + */ + public String toLowwerCase(String str, int index) { + char[] chars = str.toCharArray(); + if (index + 1 > chars.length ) {throw new RuntimeException("the char at the index don't exist");} + chars[index] = Character.toLowerCase(chars[index]); + return new String(chars); + } + + public boolean isSpaceOrNull(String paraName) { + return (paraName == null || paraName.trim().isEmpty()); + } + +} diff --git a/students/1204187480/code/homework/common/src/test/java/com/coding/api/ArraysTest.java b/students/1204187480/code/homework/common/src/test/java/com/coding/api/ArraysTest.java new file mode 100644 index 0000000000..eb41a7e262 --- /dev/null +++ b/students/1204187480/code/homework/common/src/test/java/com/coding/api/ArraysTest.java @@ -0,0 +1,22 @@ +package com.coding.api; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; + +/** + * Created by luoziyihao on 2/25/17. + */ +public class ArraysTest { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + + @Test + public void testCopyOf(){ + Object[] a = new Object[]{1, 2, 3, 4}; + Object[] b = Arrays.copyOf(a, 10); + logger.info("a={}, b={}", Arrays.toString(a), Arrays.toString(b)); + } +} diff --git a/students/1204187480/code/homework/common/src/test/java/com/coding/api/SystemTest.java b/students/1204187480/code/homework/common/src/test/java/com/coding/api/SystemTest.java new file mode 100644 index 0000000000..efc4022378 --- /dev/null +++ b/students/1204187480/code/homework/common/src/test/java/com/coding/api/SystemTest.java @@ -0,0 +1,24 @@ +package com.coding.api; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; + +/** + * Created by luoziyihao on 2/25/17. + */ +public class SystemTest { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Test + public void testArrayCopy() { + int[] a = new int[]{1, 2, 3, 4, 5, 6, 7}; + int[] b = new int[]{11, 22, 33, 44, 55, 66, 77}; + System.arraycopy(a, 2, b, 4, 3); + logger.info("b={}", Arrays.toString(b)); + + } +} diff --git a/students/1204187480/code/homework/common/src/test/java/com/coding/common/util/ByteUtilsTest.java b/students/1204187480/code/homework/common/src/test/java/com/coding/common/util/ByteUtilsTest.java new file mode 100644 index 0000000000..62dd4907cc --- /dev/null +++ b/students/1204187480/code/homework/common/src/test/java/com/coding/common/util/ByteUtilsTest.java @@ -0,0 +1,20 @@ +package com.coding.common.util; + +import org.junit.Test; + +/** + * Created by luoziyihao on 5/2/17. + */ +public class ByteUtilsTest { + + @Test + public void testByteToHexString(){ + byte[] bytes = new byte[]{ + 1, + (byte) 255 + }; + System.out.println(ByteUtils.byteToHexString(bytes)); + System.out.println(Integer.toHexString(255)); + } + +} \ No newline at end of file diff --git a/students/1204187480/code/homework/common/src/test/java/com/coding/common/util/FileUtils2Test.java b/students/1204187480/code/homework/common/src/test/java/com/coding/common/util/FileUtils2Test.java new file mode 100644 index 0000000000..cb58cf99c5 --- /dev/null +++ b/students/1204187480/code/homework/common/src/test/java/com/coding/common/util/FileUtils2Test.java @@ -0,0 +1,35 @@ +package com.coding.common.util; + +import org.junit.Test; + +import java.io.File; +import java.util.List; + +/** + * Created by luoziyihao on 4/28/17. + */ +public class FileUtils2Test { + @Test + public void getCanonicalPath() throws Exception { + System.out.println(FileUtils2.getCanonicalPath(new File(""))); + } + + @Test + public void getBytes() throws Exception { + byte[] bytes = FileUtils2.getBytes(new File("pom.xml")); + System.out.println(new String(bytes)); + System.out.println(ByteUtils.byteToHexString(bytes)); + + } + + + @Test + public void listAllFiles() throws Exception { + String currentPath = new File("").getCanonicalPath(); + List files = FileUtils2.listAllFiles(new File(currentPath )); + for (File file : files) { + System.out.println(file.getCanonicalPath()); + } + } + +} \ No newline at end of file diff --git a/students/1204187480/code/homework/parent-dependencies/pom.xml b/students/1204187480/code/homework/parent-dependencies/pom.xml new file mode 100644 index 0000000000..b961e90c59 --- /dev/null +++ b/students/1204187480/code/homework/parent-dependencies/pom.xml @@ -0,0 +1,169 @@ + + 4.0.0 + + com.coding + parent-dependencies + pom + 1.0-SNAPSHOT + https://github.com/luoziyihao/coding2017 + + + + alimaven + aliyun maven + http://maven.aliyun.com/nexus/content/groups/public/ + + true + + + true + + + + + + alimaven + aliyun maven + http://maven.aliyun.com/nexus/content/groups/public/ + + true + + + true + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/libs-snapshot + + true + + + + + + + UTF-8 + UTF-8 + 1.8 + UTF-8 + 1.8 + 1.8 + 3.0 + 1.1.7 + 1.1.7 + 1.2 + 1.2.17 + 4.12 + 3.4 + 4.1 + 2.5 + 1.9.2 + 19.0 + 1.1.6 + 1.16.10 + 1.2.22 + 0.2.0 + 2.9.4 + + + + + + + ch.qos.logback + logback-classic + ${logback-classic.version} + + + + commons-logging + commons-logging + ${commons-logging.version} + + + log4j + log4j + ${log4j.version} + + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + commons-io + commons-io + ${commons-io.version} + + + commons-beanutils + commons-beanutils + ${commons-beanutils.version} + + + com.google.guava + guava + ${guava.version} + + + org.projectlombok + lombok + ${lombok.version} + + + joda-time + joda-time + ${joda-time.version} + + + io.reactivex + rxjava + ${rxjava.version} + + + com.alibaba + fastjson + ${fastjson.version} + + + com.shekhargulati + strman + ${strman.version} + + + + + + junit + junit + ${junit.version} + + + + + ${project.artifactId} + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + + \ No newline at end of file diff --git a/students/1204187480/code/homework/parent/pom.xml b/students/1204187480/code/homework/parent/pom.xml new file mode 100644 index 0000000000..e6a94a2e4f --- /dev/null +++ b/students/1204187480/code/homework/parent/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + parent + pom + manage the dependencies for modules + + com.coding + parent-dependencies + 1.0-SNAPSHOT + ../parent-dependencies/pom.xml + + + + + com.coding + common + 1.0-SNAPSHOT + + + + \ No newline at end of file diff --git a/students/1204187480/code/homework/pom.xml b/students/1204187480/code/homework/pom.xml new file mode 100644 index 0000000000..28ac74f159 --- /dev/null +++ b/students/1204187480/code/homework/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + com.coding + coding2017 + 1.0-SNAPSHOT + pom + + parent-dependencies + common + parent + coding + coderising + + + + diff --git "a/students/1204187480/note/homework/cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244-\346\274\253\350\260\210\350\256\241\347\256\227\346\234\272" "b/students/1204187480/note/homework/cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244-\346\274\253\350\260\210\350\256\241\347\256\227\346\234\272" new file mode 100644 index 0000000000..e69de29bb2 diff --git a/students/1204187480/note/todo/homework.md b/students/1204187480/note/todo/homework.md new file mode 100644 index 0000000000..5f111a9ea6 --- /dev/null +++ b/students/1204187480/note/todo/homework.md @@ -0,0 +1,8 @@ +# 0326 操作系统中的lru算法 + +ClassFileLoader + +LRUPageFrame + +深入理解java虚拟机 第6章 + From f0447b8a16bbe95a71ffc12e1970ef8c8d1a20c1 Mon Sep 17 00:00:00 2001 From: luoziyihao Date: Mon, 12 Jun 2017 21:53:50 +0800 Subject: [PATCH 032/214] add ood src: --- .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../src/main/resources/product_promotion.txt | 4 + 6 files changed, 278 insertions(+) create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/1204187480/code/homework/coderising/src/main/resources/product_promotion.txt diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/Configuration.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..781587a846 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/1204187480/code/homework/coderising/src/main/resources/product_promotion.txt b/students/1204187480/code/homework/coderising/src/main/resources/product_promotion.txt new file mode 100644 index 0000000000..a98917f829 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/resources/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo R15 +P4955 Vivo X20 \ No newline at end of file From e80ed54717dc7af1d2728d77150bfb29f11f1773 Mon Sep 17 00:00:00 2001 From: leozhaopeng <14zhaopeng@gmail.com> Date: Mon, 12 Jun 2017 21:57:55 +0800 Subject: [PATCH 033/214] =?UTF-8?q?git=E6=8F=90=E4=BA=A4=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=EF=BC=8C=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/309229350/readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/309229350/readme.md diff --git a/students/309229350/readme.md b/students/309229350/readme.md new file mode 100644 index 0000000000..48b857086e --- /dev/null +++ b/students/309229350/readme.md @@ -0,0 +1 @@ +#测试git,第一次提交 From 4280fc089c9452b1a52ec7a02303d0c039c2ff75 Mon Sep 17 00:00:00 2001 From: EightWolf <675554906@qq.com> Date: Mon, 12 Jun 2017 22:00:19 +0800 Subject: [PATCH 034/214] this is a test --- students/675554906/readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/675554906/readme.md diff --git a/students/675554906/readme.md b/students/675554906/readme.md new file mode 100644 index 0000000000..a51b977d60 --- /dev/null +++ b/students/675554906/readme.md @@ -0,0 +1 @@ +第一次提交 仅为学习 \ No newline at end of file From 060700d899c23d957d4b6d3394fd6e9c772bfad8 Mon Sep 17 00:00:00 2001 From: gongxun Date: Mon, 12 Jun 2017 22:06:25 +0800 Subject: [PATCH 035/214] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 + .../785396327/first/ood/srp/BeanUtils.java | 12 ++++ students/785396327/first/ood/srp/DBUtil.java | 11 +++- students/785396327/first/ood/srp/Email.java | 13 ----- .../785396327/first/ood/srp/EmailParser.java | 55 +++++++++++-------- .../first/ood/srp/PromotionMail.java | 8 +-- .../785396327/first/ood/srp/SendMailTest.java | 9 +++ 7 files changed, 66 insertions(+), 45 deletions(-) create mode 100644 students/785396327/first/ood/srp/BeanUtils.java diff --git a/.gitignore b/.gitignore index f1e9957cfa..4b65de1971 100644 --- a/.gitignore +++ b/.gitignore @@ -280,6 +280,9 @@ target liuxin/.DS_Store liuxin/src/.DS_Store +students/* +!students/785396327 + diff --git a/students/785396327/first/ood/srp/BeanUtils.java b/students/785396327/first/ood/srp/BeanUtils.java new file mode 100644 index 0000000000..617c96666e --- /dev/null +++ b/students/785396327/first/ood/srp/BeanUtils.java @@ -0,0 +1,12 @@ +package first.ood.srp; + +/** + * Created by IBM on 2017/6/12. + */ +public class BeanUtils { + + public static void copyProperties(Object dst, Object src) { + //拷贝方法暂不实现 + } + +} diff --git a/students/785396327/first/ood/srp/DBUtil.java b/students/785396327/first/ood/srp/DBUtil.java index 463b464df4..473b8b1ca4 100644 --- a/students/785396327/first/ood/srp/DBUtil.java +++ b/students/785396327/first/ood/srp/DBUtil.java @@ -13,8 +13,8 @@ public class DBUtil { * @param sql * @return */ - public static List> query(String sql) { -// validateSQL(sql, params); + public static List> query(String sql, Object[] params) { + formateSQL(sql, params); List userList = new ArrayList(); for (int i = 1; i <= 3; i++) { @@ -27,12 +27,17 @@ public static List> query(String sql) { return userList; } - private static void validateSQL(String sql, Object[] params) { + private static String formateSQL(String sql, Object[] params) { if (StringUtils.isEmpty(sql)) throw new RuntimeException("empty sql"); String[] sqlFaction = sql.split("\\?"); if (sqlFaction.length - 1 != params.length) throw new RuntimeException("wrong number of parameters"); + for (int i = 0; i < params.length; i++) { + sql = sql.replaceFirst("\\?", "'" + params[i].toString() + "'"); + } + return sql; } + } diff --git a/students/785396327/first/ood/srp/Email.java b/students/785396327/first/ood/srp/Email.java index 417e9aba6d..11a2c408ae 100644 --- a/students/785396327/first/ood/srp/Email.java +++ b/students/785396327/first/ood/srp/Email.java @@ -11,19 +11,6 @@ public class Email { protected String subject; protected String message; - public Email() { - - } - - public Email(String smtpHost, String altSmtpHost, String fromAddress, String toAddress, String subject, String message) { - this.smtpHost = smtpHost; - this.altSmtpHost = altSmtpHost; - this.fromAddress = fromAddress; - this.toAddress = toAddress; - this.subject = subject; - this.message = message; - } - protected void setSMTPHost(String smtpHost) { this.smtpHost = smtpHost; } diff --git a/students/785396327/first/ood/srp/EmailParser.java b/students/785396327/first/ood/srp/EmailParser.java index 1129beaf34..4c14b8a66f 100644 --- a/students/785396327/first/ood/srp/EmailParser.java +++ b/students/785396327/first/ood/srp/EmailParser.java @@ -9,18 +9,10 @@ */ public class EmailParser { - public List parseEmailList(String filepath, String loadQuery) { - List mailList = new ArrayList(); - Email email = parseCommonInfo(filepath); - List> individualInfo = getIndividualInfo(loadQuery); - for (HashMap map : individualInfo) { - PromotionMail promotionMail = new PromotionMail(email); - promotionMail.setToAddress(parseToAddress(map)); - promotionMail.setMessage(parseMessage(map, promotionMail)); - promotionMail.setSubject("您关注的产品降价了"); - mailList.add(promotionMail); - } - return mailList; + public List parseEmailList(String filepath, String loadQuery, Object[] params) { + PromotionMail email = packageInfoFromConfig(); + packageInfoFromFile(email, filepath); + return packageInfoFromDB(loadQuery, email, params); } private String parseMessage(HashMap map, PromotionMail promotionMail) { @@ -33,22 +25,39 @@ private String parseToAddress(HashMap map) { return map.get(ConfigurationKeys.EMAIL_KEY); } - private PromotionMail parseCommonInfo(String filepath) { - Email email = new Email(); + private List packageInfoFromDB(String loadQuery, PromotionMail email, Object[] params) { + List> individualInfo = getIndividualInfo(loadQuery, params); + List mailList = new ArrayList(); + for (HashMap map : individualInfo) { + PromotionMail completeMail = new PromotionMail(); + BeanUtils.copyProperties(completeMail, email); + completeMail.setToAddress(parseToAddress(map)); + completeMail.setMessage(parseMessage(map, completeMail)); + completeMail.setSubject("您关注的产品降价了"); + mailList.add(completeMail); + } + return mailList; + } + + private PromotionMail packageInfoFromFile(PromotionMail email, String filepath) { FileParser fileParser = new FileParser(filepath); -// email.setProductID(fileParser.parseProductID()); -// email.setProductDesc(fileParser.parseProductDesc()); + email.setProductDesc(fileParser.parseProductDesc()); + email.setProductID(fileParser.parseProductID()); + return email; + } + + private PromotionMail packageInfoFromConfig() { + PromotionMail email = new PromotionMail(); Configuration configuration = new Configuration(); -// promotionMail.setSMTPHost(configuration.getProperty(ConfigurationKeys.SMTP_SERVER)); -// promotionMail.setFromAddress(configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN)); -// promotionMail.setAltSMTPHost(configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); -// return promotionMail; - return null; + email.setSMTPHost(configuration.getProperty(ConfigurationKeys.SMTP_SERVER)); + email.setFromAddress(configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN)); + email.setAltSMTPHost(configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + return email; } - private List> getIndividualInfo(String loadQuery) { - return DBUtil.query(loadQuery); + private List> getIndividualInfo(String loadQuery, Object[] params) { + return DBUtil.query(loadQuery, params); } } diff --git a/students/785396327/first/ood/srp/PromotionMail.java b/students/785396327/first/ood/srp/PromotionMail.java index 1a604b7fe3..48096fa54a 100644 --- a/students/785396327/first/ood/srp/PromotionMail.java +++ b/students/785396327/first/ood/srp/PromotionMail.java @@ -1,12 +1,8 @@ package first.ood.srp; public class PromotionMail extends Email { - private String productID = null; - private String productDesc = null; - - public PromotionMail(Email email) { - super(email.smtpHost, email.altSmtpHost, email.fromAddress, email.toAddress, email.subject, email.message); - } + private String productID; + private String productDesc; public void setProductID(String productID) { this.productID = productID; diff --git a/students/785396327/first/ood/srp/SendMailTest.java b/students/785396327/first/ood/srp/SendMailTest.java index 69681f6c6d..d82df14609 100644 --- a/students/785396327/first/ood/srp/SendMailTest.java +++ b/students/785396327/first/ood/srp/SendMailTest.java @@ -1,5 +1,7 @@ package first.ood.srp; +import java.util.List; + /** * Created by gongxun on 2017/6/12. */ @@ -7,5 +9,12 @@ public class SendMailTest { public static void main(String[] args) { String loadQuery = "Select name from subscriptions where product_id= ? and send_mail=1"; + String filepath = "D:\\workspace\\IDEA\\homework\\coding2017_section2\\coding2017\\students\\785396327\\first\\ood\\srp\\product_promotion.txt"; + boolean isDebug = false; + + EmailParser emailParser = new EmailParser(); + List promotionMails = emailParser.parseEmailList(filepath, loadQuery); + MailSender mailSender = new MailSender(); + mailSender.sendMailList(promotionMails, isDebug); } } From c46230c4f1587df2c2bc4b1939f3e3328d12ae4d Mon Sep 17 00:00:00 2001 From: gongxun Date: Mon, 12 Jun 2017 22:11:58 +0800 Subject: [PATCH 036/214] update --- students/785396327/first/ood/srp/EmailParser.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/students/785396327/first/ood/srp/EmailParser.java b/students/785396327/first/ood/srp/EmailParser.java index 4c14b8a66f..06782d2c09 100644 --- a/students/785396327/first/ood/srp/EmailParser.java +++ b/students/785396327/first/ood/srp/EmailParser.java @@ -9,10 +9,10 @@ */ public class EmailParser { - public List parseEmailList(String filepath, String loadQuery, Object[] params) { + public List parseEmailList(String filepath, String loadQuery) { PromotionMail email = packageInfoFromConfig(); packageInfoFromFile(email, filepath); - return packageInfoFromDB(loadQuery, email, params); + return packageInfoFromDB(loadQuery, email); } private String parseMessage(HashMap map, PromotionMail promotionMail) { @@ -26,8 +26,8 @@ private String parseToAddress(HashMap map) { } - private List packageInfoFromDB(String loadQuery, PromotionMail email, Object[] params) { - List> individualInfo = getIndividualInfo(loadQuery, params); + private List packageInfoFromDB(String loadQuery, PromotionMail email) { + List> individualInfo = getIndividualInfo(loadQuery, new Object[]{email.getproductID()}); List mailList = new ArrayList(); for (HashMap map : individualInfo) { PromotionMail completeMail = new PromotionMail(); From 793257d2362df0ac5eb765533560737066e16589 Mon Sep 17 00:00:00 2001 From: cheungchan <1377699408@qq.com> Date: Mon, 12 Jun 2017 22:27:47 +0800 Subject: [PATCH 037/214] =?UTF-8?q?=E5=A2=9E=E5=8A=A0readmee?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/1377699408/README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 students/1377699408/README.md diff --git a/students/1377699408/README.md b/students/1377699408/README.md new file mode 100644 index 0000000000..bf3d9bbc98 --- /dev/null +++ b/students/1377699408/README.md @@ -0,0 +1,2 @@ +## 1377699408的文件夹 +测试 From 219d3c7e9e92d7cba1a50d23bf2c510e8065a63d Mon Sep 17 00:00:00 2001 From: luoziyihao Date: Mon, 12 Jun 2017 22:31:26 +0800 Subject: [PATCH 038/214] design fro promotionMail --- .../ood/srp/optimize/EmailSendClaim.java | 17 +++++++++++++++++ .../srp/optimize/EmailSendableBehavior.java | 12 ++++++++++++ .../coderising/ood/srp/optimize/Product.java | 9 +++++++++ .../ood/srp/optimize/ProductParser.java | 19 +++++++++++++++++++ .../ood/srp/optimize/PromotionMail.java | 11 +++++++++++ .../ood/srp/optimize/SmptPropeties.java | 10 ++++++++++ .../com/coderising/ood/srp/optimize/User.java | 9 +++++++++ .../ood/srp/optimize/UserService.java | 13 +++++++++++++ 8 files changed, 100 insertions(+) create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/EmailSendClaim.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/EmailSendableBehavior.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/Product.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/ProductParser.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMail.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/SmptPropeties.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/User.java create mode 100644 students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/UserService.java diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/EmailSendClaim.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/EmailSendClaim.java new file mode 100644 index 0000000000..e61704ec60 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/EmailSendClaim.java @@ -0,0 +1,17 @@ +package com.coderising.ood.srp.optimize; + +/** + * Created by luoziyihao on 6/12/17. + */ +public class EmailSendClaim { + private String toAddress; + private String fromAddress; + private String subject; + private String message; + private String smtpHost; + private String debug; + + public void setMessage(Product product, User user) { + this.message = null; + } +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/EmailSendableBehavior.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/EmailSendableBehavior.java new file mode 100644 index 0000000000..aede0fdc66 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/EmailSendableBehavior.java @@ -0,0 +1,12 @@ +package com.coderising.ood.srp.optimize; + +import java.util.List; + +/** + * Created by luoziyihao on 6/12/17. + */ +public class EmailSendableBehavior { + void send(List emailSendClaimList) { + + } +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/Product.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/Product.java new file mode 100644 index 0000000000..652897a7f3 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/Product.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp.optimize; + +/** + * Created by luoziyihao on 6/12/17. + */ +public class Product { + private static String productID; + private static String productDesc; +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/ProductParser.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/ProductParser.java new file mode 100644 index 0000000000..7860dd6047 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/ProductParser.java @@ -0,0 +1,19 @@ +package com.coderising.ood.srp.optimize; + +import java.util.List; + +/** + * Created by luoziyihao on 6/12/17. + */ +public class ProductParser { + + private String productFilePath; + + public void setProductFilePath(String productFilePath) { + this.productFilePath = productFilePath; + } + + public List parse(){ + return null; + } +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMail.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMail.java new file mode 100644 index 0000000000..4288925d1e --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMail.java @@ -0,0 +1,11 @@ +package com.coderising.ood.srp.optimize; + +/** + * Created by luoziyihao on 6/12/17. + */ +public class PromotionMail { + + public static void main(String args[]){ + + } +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/SmptPropeties.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/SmptPropeties.java new file mode 100644 index 0000000000..fd6d7be8d7 --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/SmptPropeties.java @@ -0,0 +1,10 @@ +package com.coderising.ood.srp.optimize; + +/** + * Created by luoziyihao on 6/12/17. + */ +public class SmptPropeties { + private String smtpHost; + private String altSmtpHost; + private String fromAddress; +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/User.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/User.java new file mode 100644 index 0000000000..45f7739a7b --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/User.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp.optimize; + +/** + * Created by luoziyihao on 6/12/17. + */ +public class User { + private String name; + private String email; +} diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/UserService.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/UserService.java new file mode 100644 index 0000000000..5178489e0f --- /dev/null +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/UserService.java @@ -0,0 +1,13 @@ +package com.coderising.ood.srp.optimize; + +import java.util.List; + +/** + * Created by luoziyihao on 6/12/17. + */ +public class UserService { + + List loadMailingList(){ + return null; + } +} From bde7e7b6f698ad4c594b4e2c703d2b1ba0bd1869 Mon Sep 17 00:00:00 2001 From: luoziyihao Date: Mon, 12 Jun 2017 22:34:50 +0800 Subject: [PATCH 039/214] design fro promotionMail2 --- .../optimize/{PromotionMail.java => PromotionMailApp.java} | 2 +- .../{EmailSendClaim.java => PromotionMailClaim.java} | 2 +- ...lSendableBehavior.java => PromotionMailableBehavior.java} | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) rename students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/{PromotionMail.java => PromotionMailApp.java} (81%) rename students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/{EmailSendClaim.java => PromotionMailClaim.java} (91%) rename students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/{EmailSendableBehavior.java => PromotionMailableBehavior.java} (53%) diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMail.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailApp.java similarity index 81% rename from students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMail.java rename to students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailApp.java index 4288925d1e..ef89a79d77 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMail.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailApp.java @@ -3,7 +3,7 @@ /** * Created by luoziyihao on 6/12/17. */ -public class PromotionMail { +public class PromotionMailApp { public static void main(String args[]){ diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/EmailSendClaim.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailClaim.java similarity index 91% rename from students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/EmailSendClaim.java rename to students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailClaim.java index e61704ec60..06d447b817 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/EmailSendClaim.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailClaim.java @@ -3,7 +3,7 @@ /** * Created by luoziyihao on 6/12/17. */ -public class EmailSendClaim { +public class PromotionMailClaim { private String toAddress; private String fromAddress; private String subject; diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/EmailSendableBehavior.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailableBehavior.java similarity index 53% rename from students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/EmailSendableBehavior.java rename to students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailableBehavior.java index aede0fdc66..548f69722a 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/EmailSendableBehavior.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailableBehavior.java @@ -5,8 +5,9 @@ /** * Created by luoziyihao on 6/12/17. */ -public class EmailSendableBehavior { - void send(List emailSendClaimList) { +public class PromotionMailableBehavior { + + void send(List emailSendClaimList) { } } From 251022e66ff6a536a6ead1d31d8152a64dddfcfb Mon Sep 17 00:00:00 2001 From: palmshe Date: Mon, 12 Jun 2017 22:57:15 +0800 Subject: [PATCH 040/214] =?UTF-8?q?=E6=8C=89=E5=8D=95=E4=B8=80=E8=B4=A3?= =?UTF-8?q?=E4=BB=BB=E5=8E=9F=E5=88=99=E8=BF=9B=E8=A1=8C=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coderising/ood/srp/Configuration.java | 2 +- .../com/coderising/ood/srp/DataGenerator.java | 64 ++++++ .../com/coderising/ood/srp/InitDataUtils.java | 61 ------ .../java/com/coderising/ood/srp/MailUtil.java | 39 +++- .../java/com/coderising/ood/srp/Main.java | 34 +++ .../com/coderising/ood/srp/PromotionMail.java | 207 +----------------- 6 files changed, 142 insertions(+), 265 deletions(-) create mode 100644 students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/DataGenerator.java delete mode 100644 students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/InitDataUtils.java create mode 100644 students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/Main.java diff --git a/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java index f328c1816a..1faff3f68d 100644 --- a/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java +++ b/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -15,7 +15,7 @@ public class Configuration { * @param key * @return */ - public String getProperty(String key) { + public static String getProperty(String key) { return configurations.get(key); } diff --git a/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/DataGenerator.java b/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/DataGenerator.java new file mode 100644 index 0000000000..6e9f1c2e84 --- /dev/null +++ b/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/DataGenerator.java @@ -0,0 +1,64 @@ +/** + * 版权 (c) 2017 palmshe.com + * 保留所有权利。 + */ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @Description: + * @author palmshe + * @date 2017年6月11日 下午10:24:46 + */ +public class DataGenerator { + + /** + * @Description:获取商品 + * @param file + * @return + * @throws IOException + */ + public static Map generateGoods(File file) throws IOException{ + Map good= new HashMap(); + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + good.put(PromotionMail.ID_KEY, data[0]); + good.put(PromotionMail.DESC_KEY, data[1]); + + System.out.println("产品ID = " + data[0] + "\n"); + System.out.println("产品描述 = " + data[1] + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + return good; + } + + /** + * @Description:获取客户 + * @param good + * @return + * @throws Exception + */ + public static List loadMailingList(Map good) throws Exception { + String id= (String)good.get(PromotionMail.ID_KEY); + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + id +"' " + + "and send_mail=1 "; + + return DBUtil.query(sendMailQuery); + } +} diff --git a/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/InitDataUtils.java b/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/InitDataUtils.java deleted file mode 100644 index 35cdb939f6..0000000000 --- a/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/InitDataUtils.java +++ /dev/null @@ -1,61 +0,0 @@ -/** - * 版权 (c) 2017 palmshe.com - * 保留所有权利。 - */ -package com.coderising.ood.srp; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * @Description: 加载所需要的数据,以便PromotionMail取用 - * @author palmshe - * @date 2017年6月11日 下午10:24:46 - */ -public class InitDataUtils { - - protected static final String GOOD_NAME= "good_name"; - protected static final String GOOD_ID= "good_id"; - - /** - * @Description:生产商品 - * @param file - * @return - * @throws IOException - */ - public static List generateGoods(File file) throws IOException{ - List goods= new ArrayList(); - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - for (int i = 0; i < 1; i++) { - Map goodMaps= new HashMap(); - goodMaps.put(GOOD_ID, data[0]); - goodMaps.put(GOOD_NAME, data[1]); - goods.add(goodMaps); - } - -// setProductID(data[0]); -// setProductDesc(data[1]); - -// System.out.println("产品ID = " + productID + "\n"); -// System.out.println("产品描述 = " + productDesc + "\n"); - - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); - } - - return goods; - } -} diff --git a/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java index 9f9e749af7..e31d8a9b75 100644 --- a/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java +++ b/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -2,17 +2,40 @@ public class MailUtil { - public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, - boolean debug) { - //假装发了一封邮件 + private static String fromAddress; + private static String smtpServer; + private static String altSmtpServer; + + public MailUtil(String fromAddress, String smtpServer, String altSmtpServer){ + this.fromAddress= fromAddress; + this.smtpServer= smtpServer; + this.altSmtpServer= altSmtpServer; + } + + /** + * @Description:发送邮件 + * @param pm + * @param debug + */ + public void sendEmail(PromotionMail pm, boolean debug){ + try { + sendEmail(fromAddress, pm.toAddress, pm.subject, pm.message, smtpServer, debug); + } catch (Exception e1) { + try { + sendEmail(fromAddress, pm.toAddress, pm.subject, pm.message, altSmtpServer, debug); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + private void sendEmail(String from, String to, String subject, String message, String server, boolean debug){ +// 假装发了一封邮件 StringBuilder buffer = new StringBuilder(); - buffer.append("From:").append(fromAddress).append("\n"); - buffer.append("To:").append(toAddress).append("\n"); + buffer.append("From:").append(from).append("\n"); + buffer.append("To:").append(to).append("\n"); buffer.append("Subject:").append(subject).append("\n"); buffer.append("Content:").append(message).append("\n"); System.out.println(buffer.toString()); - } - - } diff --git a/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/Main.java b/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/Main.java new file mode 100644 index 0000000000..8029d24a4c --- /dev/null +++ b/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/Main.java @@ -0,0 +1,34 @@ +/** + * 版权 (c) 2017 palmshe.com + * 保留所有权利。 + */ +package com.coderising.ood.srp; + +import java.io.File; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +/** + * @Description: + * @author palmshe + * @date 2017年6月12日 下午10:07:23 + */ +public class Main { + public static void main(String[] args) { + try { + File f = new File("E:\\Workspace-Sourcetree\\coding2017\\students\\2842295913\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"); + MailUtil maiUtil= new MailUtil(Configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN), Configuration.getProperty(ConfigurationKeys.SMTP_SERVER), Configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + Map good = DataGenerator.generateGoods(f); + List users= DataGenerator.loadMailingList(good); + if (!users.isEmpty()) { + Iterator it= users.iterator(); + while (it.hasNext()) { + maiUtil.sendEmail(new PromotionMail((Map)it.next(), good), true); + } + } + } catch (Exception e) { + System.out.println("构造发送邮件数据失败:"+ e.getMessage()); + } + } +} diff --git a/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index f293eb1f45..a773d878ee 100644 --- a/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -1,209 +1,26 @@ package com.coderising.ood.srp; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; import java.util.Map; public class PromotionMail { - - protected String sendMailQuery = null; - - - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; protected String toAddress = null; protected String subject = null; protected String message = null; - protected String productID = null; protected String productDesc = null; - private static Configuration config; - - - - private static final String NAME_KEY = "NAME"; - private static final String EMAIL_KEY = "EMAIL"; - - - public static void main(String[] args) throws Exception { - - File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); - boolean emailDebug = false; - - PromotionMail pe = new PromotionMail(f, emailDebug); - - } - - - public PromotionMail(File file, boolean mailDebug) throws Exception { - - //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - readFile(file); - - - config = new Configuration(); - - setSMTPHost(); - setAltSMTPHost(); - - - setFromAddress(); - - - setLoadQuery(); - - sendEMails(mailDebug, loadMailingList()); - - - } - - - - - private void setProductID(String productID) - { - this.productID = productID; - - } - - protected String getproductID() - { - return productID; - } - - protected void setLoadQuery() throws Exception { - - sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + productID +"' " - + "and send_mail=1 "; - - - System.out.println("loadQuery set"); - } - - - protected void setSMTPHost() - { - smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); - } - - - protected void setAltSMTPHost() - { - altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); - - } - - - protected void setFromAddress() - { - fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); - } - - protected void setMessage(HashMap userInfo) throws IOException - { - - String name = (String) userInfo.get(NAME_KEY); - - subject = "您关注的产品降价了"; - message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; - - - - } - - - protected void readFile(File file) throws IOException // @02C - { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - setProductID(data[0]); - setProductDesc(data[1]); - - System.out.println("产品ID = " + productID + "\n"); - System.out.println("产品描述 = " + productDesc + "\n"); - - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); - } - } - - private void setProductDesc(String desc) { - this.productDesc = desc; - } - - - protected void configureEMail(HashMap userInfo) throws IOException - { - toAddress = (String) userInfo.get(EMAIL_KEY); - if (toAddress.length() > 0) - setMessage(userInfo); - } - - protected List loadMailingList() throws Exception { - return DBUtil.query(this.sendMailQuery); - } - - - protected void sendEMails(boolean debug, List mailingList) throws IOException - { - - System.out.println("开始发送邮件"); - - - if (mailingList != null) { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) { - configureEMail((HashMap) iter.next()); - try - { - if (toAddress.length() > 0) - MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } - catch (Exception e) - { - - try { - MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) - { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } - } - - - } - - else { - System.out.println("没有邮件发送"); - - } - - } - - /** - * @Description:设置商品信息 - * @param goods - */ - public void setGoods(List goods){ - Map goodMap= (HashMap)goods.get(0); - setProductDesc((String)goodMap.get(InitDataUtils.GOOD_NAME)); - setProductID((String)goodMap.get(InitDataUtils.GOOD_ID)); + protected static final String NAME_KEY = "NAME"; + protected static final String EMAIL_KEY = "EMAIL"; + protected static final String ID_KEY = "ID"; + protected static final String DESC_KEY = "DESC"; + + public PromotionMail(Map user, Map good){ + String name = (String)user.get(NAME_KEY); + this.productDesc= (String)good.get(DESC_KEY); + this.productID= (String)good.get(ID_KEY); + this.toAddress= (String)user.get(EMAIL_KEY); + this.subject = "您关注的产品降价了"; + this.message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; } } From 7f7332d57fdbf085027ffafcbae8ad2a5a968a01 Mon Sep 17 00:00:00 2001 From: fade <511739113@qq.com> Date: Mon, 12 Jun 2017 23:39:04 +0800 Subject: [PATCH 041/214] =?UTF-8?q?=E6=8F=90=E4=BA=A4=20=E5=8E=9F=E7=89=88?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../511739113/6.11/ood-assignment/pom.xml | 32 +++ .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 7 files changed, 310 insertions(+) create mode 100644 students/511739113/6.11/ood-assignment/pom.xml create mode 100644 students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/511739113/6.11/ood-assignment/pom.xml b/students/511739113/6.11/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/511739113/6.11/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..781587a846 --- /dev/null +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 37e47d3afa21b0f22a12f0c848fb4f5035887499 Mon Sep 17 00:00:00 2001 From: peng Date: Mon, 12 Jun 2017 23:39:21 +0800 Subject: [PATCH 042/214] work init --- students/1395844061/.gitignore | 27 +++++++++++++++++++ students/1395844061/README.md | 1 + students/1395844061/build.gradle | 18 +++++++++++++ students/1395844061/course-pro-1/build.gradle | 14 ++++++++++ students/1395844061/settings.gradle | 2 ++ 5 files changed, 62 insertions(+) create mode 100644 students/1395844061/.gitignore create mode 100644 students/1395844061/README.md create mode 100644 students/1395844061/build.gradle create mode 100644 students/1395844061/course-pro-1/build.gradle create mode 100644 students/1395844061/settings.gradle diff --git a/students/1395844061/.gitignore b/students/1395844061/.gitignore new file mode 100644 index 0000000000..15df76b3b5 --- /dev/null +++ b/students/1395844061/.gitignore @@ -0,0 +1,27 @@ +# Created by .ignore support plugin (hsz.mobi) +### Java template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +1395844061.iml +src/ diff --git a/students/1395844061/README.md b/students/1395844061/README.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/students/1395844061/README.md @@ -0,0 +1 @@ + diff --git a/students/1395844061/build.gradle b/students/1395844061/build.gradle new file mode 100644 index 0000000000..db1cbf8def --- /dev/null +++ b/students/1395844061/build.gradle @@ -0,0 +1,18 @@ +group 'peng-test' +version '1.0-SNAPSHOT' + +apply plugin: 'java' + +sourceCompatibility = 1.5 + +repositories { + maven{ + url "http://maven.oschina.net/content/groups/public/" + } +} + +dependencies { + + compile 'org.htmlparser:htmlparser:2.1' + testCompile group: 'junit', name: 'junit', version: '4.11' +} diff --git a/students/1395844061/course-pro-1/build.gradle b/students/1395844061/course-pro-1/build.gradle new file mode 100644 index 0000000000..939cf6ccbe --- /dev/null +++ b/students/1395844061/course-pro-1/build.gradle @@ -0,0 +1,14 @@ +group 'peng-test' +version '1.0-SNAPSHOT' + +apply plugin: 'java' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.12' +} diff --git a/students/1395844061/settings.gradle b/students/1395844061/settings.gradle new file mode 100644 index 0000000000..f66e41c2f9 --- /dev/null +++ b/students/1395844061/settings.gradle @@ -0,0 +1,2 @@ +include 'course-pro-1' + From 56965fe72c76a7dd73a0e538e623f91d910c1d1f Mon Sep 17 00:00:00 2001 From: peng Date: Mon, 12 Jun 2017 23:44:23 +0800 Subject: [PATCH 043/214] work init --- .../com/coderising/ood/srp/Configuration.java | 33 +++ .../coderising/ood/srp/ConfigurationKeys.java | 16 ++ .../java/com/coderising/ood/srp/DBUtil.java | 31 +++ .../java/com/coderising/ood/srp/MailUtil.java | 27 +++ .../com/coderising/ood/srp/PromotionMail.java | 203 ++++++++++++++++++ 5 files changed, 310 insertions(+) create mode 100644 students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/Configuration.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..cd447ad225 --- /dev/null +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,33 @@ +package com.coderising.ood.srp; + +import java.util.HashMap; +import java.util.Map; + +/** + * Configuration + * + * @author Chenpz + * @package com.coderising.ood.srp + * @date 2017/6/12/23:30 + */ +public class Configuration { + + static Map configurations = new HashMap<>(); + + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + + + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } +} diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..7c3aa420b9 --- /dev/null +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,16 @@ +package com.coderising.ood.srp; + +/** + * ConfigurationKeys + * + * @author Chenpz + * @package com.coderising.ood.srp + * @date 2017/6/12/23:31 + */ +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..437a74cb40 --- /dev/null +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,31 @@ +package com.coderising.ood.srp; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * DBUtil + * + * @author Chenpz + * @package com.coderising.ood.srp + * @date 2017/6/12/23:32 + */ +public class DBUtil { + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + return userList; + } +} diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..83b3ef844b --- /dev/null +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,27 @@ +package com.coderising.ood.srp; + +/** + * MailUtil + * + * @author Chenpz + * @package com.coderising.ood.srp + * @date 2017/6/12/23:28 + */ +public final class MailUtil { + + + private MailUtil(){ + throw new RuntimeException("illegal called!"); + } + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + } +} diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..331537f5d9 --- /dev/null +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,203 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +/** + * PromotionMail + * + * @author Chenpz + * @package com.coderising.ood.srp + * @date 2017/6/12/23:33 + */ +public class PromotionMail { + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} From 029e31e55bdf64c11fbe2cb4cd885e2bb5791ab4 Mon Sep 17 00:00:00 2001 From: peng Date: Mon, 12 Jun 2017 23:45:27 +0800 Subject: [PATCH 044/214] add gitignore file --- students/1395844061/.gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/students/1395844061/.gitignore b/students/1395844061/.gitignore index 15df76b3b5..d41523dfd3 100644 --- a/students/1395844061/.gitignore +++ b/students/1395844061/.gitignore @@ -24,4 +24,3 @@ hs_err_pid* 1395844061.iml -src/ From 58298121161fba45621585d7b40e13113c10e985 Mon Sep 17 00:00:00 2001 From: onlyLYJ <382266293@qq.com> Date: Sun, 11 Jun 2017 22:12:13 +0800 Subject: [PATCH 045/214] Merge pull request #6 from onlyliuxin/master season 2 --- .../com/coderising/ood/srp/MailSender.java | 52 ++++++++++++++++++ .../com/coderising/ood/srp/PromotionMail.java | 40 ++++++++++++++ .../src/com/coderising/ood/srp/bean/Mail.java | 53 +++++++++++++++++++ .../com/coderising/ood/srp/bean/Product.java | 38 +++++++++++++ .../coderising/ood/srp/bean/Subscriber.java | 27 ++++++++++ .../ood/srp/config/Configuration.java | 38 +++++++++++++ .../ood/srp/config/ServerConfig.java | 32 +++++++++++ .../com/coderising/ood/srp/dao/MailDAO.java | 39 ++++++++++++++ .../coderising/ood/srp/dao/ProductDAO.java | 49 +++++++++++++++++ .../com/coderising/ood/srp/dao/UserDAO.java | 31 +++++++++++ .../ood/srp/data/product_promotion.txt | 4 ++ .../com/coderising/ood/srp/util/DBUtil.java | 16 ++++++ .../com/coderising/ood/srp/util/MailUtil.java | 32 +++++++++++ students/382266293/src/pom.xml | 32 +++++++++++ 14 files changed, 483 insertions(+) create mode 100644 students/382266293/src/com/coderising/ood/srp/MailSender.java create mode 100644 students/382266293/src/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/382266293/src/com/coderising/ood/srp/bean/Mail.java create mode 100644 students/382266293/src/com/coderising/ood/srp/bean/Product.java create mode 100644 students/382266293/src/com/coderising/ood/srp/bean/Subscriber.java create mode 100644 students/382266293/src/com/coderising/ood/srp/config/Configuration.java create mode 100644 students/382266293/src/com/coderising/ood/srp/config/ServerConfig.java create mode 100644 students/382266293/src/com/coderising/ood/srp/dao/MailDAO.java create mode 100644 students/382266293/src/com/coderising/ood/srp/dao/ProductDAO.java create mode 100644 students/382266293/src/com/coderising/ood/srp/dao/UserDAO.java create mode 100644 students/382266293/src/com/coderising/ood/srp/data/product_promotion.txt create mode 100644 students/382266293/src/com/coderising/ood/srp/util/DBUtil.java create mode 100644 students/382266293/src/com/coderising/ood/srp/util/MailUtil.java create mode 100644 students/382266293/src/pom.xml diff --git a/students/382266293/src/com/coderising/ood/srp/MailSender.java b/students/382266293/src/com/coderising/ood/srp/MailSender.java new file mode 100644 index 0000000000..6d52844059 --- /dev/null +++ b/students/382266293/src/com/coderising/ood/srp/MailSender.java @@ -0,0 +1,52 @@ +package com.coderising.ood.srp; + +import com.coderising.ood.srp.bean.Mail; +import com.coderising.ood.srp.bean.Product; +import com.coderising.ood.srp.config.Configuration; +import com.coderising.ood.srp.config.ServerConfig; +import com.coderising.ood.srp.dao.MailDAO; +import com.coderising.ood.srp.dao.ProductDAO; +import com.coderising.ood.srp.util.MailUtil; + +import java.io.File; +import java.util.List; + +/** + * Created by onlyLYJ on 2017/6/12. + */ +public abstract class MailSender { + + protected static ServerConfig sc = new Configuration().config().getServerConfig(); + protected static MailDAO mailDAO = new MailDAO(); + protected static ProductDAO productDAO = new ProductDAO(); + protected boolean debug; + + protected List prepareMails(File productFile) throws Exception { + + List products = productDAO.list(productFile); + List mailingList = mailDAO.loadMailingList(products); + setMailContext(mailingList); + + return mailingList; + } + + protected abstract void setMailContext(List mailingList); + + protected void setDebug(boolean debug) { + this.debug = debug; + } + + protected void sendEMails(List mailingList) { + + if (mailDAO.isValide(mailingList)) { + System.out.println("开始发送邮件"); + + for (Mail mail : mailingList) { + MailUtil.send(mail, sc); + } + } + + } + + +} diff --git a/students/382266293/src/com/coderising/ood/srp/PromotionMail.java b/students/382266293/src/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..abde88278d --- /dev/null +++ b/students/382266293/src/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,40 @@ +package com.coderising.ood.srp; + +import com.coderising.ood.srp.bean.Mail; + +import java.io.File; +import java.util.List; + +public class PromotionMail extends MailSender { + + private static final String EMAIL_ADMIN = "email.admin"; + + public static void main(String[] args) throws Exception { + + File f = new File("E:\\git\\coding2017\\students\\382266293\\src\\com\\coderising\\ood\\srp\\data\\product_promotion.txt"); + MailSender pm = new PromotionMail(); + pm.setDebug(false); + List mailList = pm.prepareMails(f); + pm.sendEMails(mailList); + + } + + @Override + protected void setMailContext(List mailingList) { + + String subject = "您关注的产品降价了"; + + for (Mail mail : mailingList) { + mail.setFromAddress(EMAIL_ADMIN); + mail.setSubject(subject); + String name = mail.getSubscriber().getName(); + String productDesc = mail.getProduct().getProductDesc(); + String message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + mail.setSubject(subject); + mail.setMessage(message); + } + + } + + +} diff --git a/students/382266293/src/com/coderising/ood/srp/bean/Mail.java b/students/382266293/src/com/coderising/ood/srp/bean/Mail.java new file mode 100644 index 0000000000..6a612dbd48 --- /dev/null +++ b/students/382266293/src/com/coderising/ood/srp/bean/Mail.java @@ -0,0 +1,53 @@ +package com.coderising.ood.srp.bean; + +/** + * Created by onlyLYJ on 2017/6/12. + */ +public class Mail { + + private String fromAddress; + private String subject; + private String message; + private Product product; + private Subscriber subscriber; + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public Product getProduct() { + return product; + } + + public void setProduct(Product product) { + this.product = product; + } + + public Subscriber getSubscriber() { + return subscriber; + } + + public void setSubscriber(Subscriber subscriber) { + this.subscriber = subscriber; + } +} diff --git a/students/382266293/src/com/coderising/ood/srp/bean/Product.java b/students/382266293/src/com/coderising/ood/srp/bean/Product.java new file mode 100644 index 0000000000..783f2033c4 --- /dev/null +++ b/students/382266293/src/com/coderising/ood/srp/bean/Product.java @@ -0,0 +1,38 @@ +package com.coderising.ood.srp.bean; + +import java.util.List; + +/** + * Created by onlyLYJ on 2017/6/12. + */ +public class Product { + + private String productID; + private String productDesc; + private List subscribers; + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + + public List getSubscribers() { + return subscribers; + } + + public void setSubscribers(List subscribers) { + this.subscribers = subscribers; + } + +} diff --git a/students/382266293/src/com/coderising/ood/srp/bean/Subscriber.java b/students/382266293/src/com/coderising/ood/srp/bean/Subscriber.java new file mode 100644 index 0000000000..048c1eaf34 --- /dev/null +++ b/students/382266293/src/com/coderising/ood/srp/bean/Subscriber.java @@ -0,0 +1,27 @@ +package com.coderising.ood.srp.bean; + +/** + * Created by onlyLYJ on 2017/6/12. + */ +public class Subscriber { + + private String name; + private String mailAddress; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getMailAddress() { + return mailAddress; + } + + public void setMailAddress(String mailAddress) { + this.mailAddress = mailAddress; + } + +} diff --git a/students/382266293/src/com/coderising/ood/srp/config/Configuration.java b/students/382266293/src/com/coderising/ood/srp/config/Configuration.java new file mode 100644 index 0000000000..2ed21e3af8 --- /dev/null +++ b/students/382266293/src/com/coderising/ood/srp/config/Configuration.java @@ -0,0 +1,38 @@ +package com.coderising.ood.srp.config; + +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + private static final String SMTP_SERVER = "smtp.server"; + private static final String ALT_SMTP_SERVER = "alt.smtp.server"; + + + private static Map configurations = null; + + public Configuration config() { + + configurations = new HashMap<>(); + configurations.put(SMTP_SERVER, "smtp.163.com"); + configurations.put(ALT_SMTP_SERVER, "smtp1.163.com"); + + return this; + } + + public ServerConfig getServerConfig() { + + ServerConfig sc = new ServerConfig(); + + sc.setSmtpHost(getProperty(SMTP_SERVER)); + sc.setAltSmtpHost(getProperty(ALT_SMTP_SERVER)); + + return sc; + } + + private String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/382266293/src/com/coderising/ood/srp/config/ServerConfig.java b/students/382266293/src/com/coderising/ood/srp/config/ServerConfig.java new file mode 100644 index 0000000000..4b4b374b73 --- /dev/null +++ b/students/382266293/src/com/coderising/ood/srp/config/ServerConfig.java @@ -0,0 +1,32 @@ +package com.coderising.ood.srp.config; + +/** + * Created by onlyLYJ on 2017/6/12. + */ +public class ServerConfig { + + private String smtpHost; + private String altSmtpHost; + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + + +} + + + + diff --git a/students/382266293/src/com/coderising/ood/srp/dao/MailDAO.java b/students/382266293/src/com/coderising/ood/srp/dao/MailDAO.java new file mode 100644 index 0000000000..9dd5ee2259 --- /dev/null +++ b/students/382266293/src/com/coderising/ood/srp/dao/MailDAO.java @@ -0,0 +1,39 @@ +package com.coderising.ood.srp.dao; + +import com.coderising.ood.srp.bean.Mail; +import com.coderising.ood.srp.bean.Product; +import com.coderising.ood.srp.bean.Subscriber; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by onlyLYJ on 2017/6/12. + */ +public class MailDAO { + + public List loadMailingList(List products) throws Exception { + + List mails = new ArrayList<>(); + + for (Product p : products) { + List subscribers = new UserDAO().list(p); + + for (Subscriber s : subscribers) { + Mail mail = new Mail(); + mail.setProduct(p); + mail.setSubscriber(s); + mails.add(mail); + } + } + return mails; + } + + public boolean isValide(List mailingList) { + if (null == mailingList || mailingList.isEmpty()) { + throw new RuntimeException("没有邮件发送"); + } + return true; + } + +} diff --git a/students/382266293/src/com/coderising/ood/srp/dao/ProductDAO.java b/students/382266293/src/com/coderising/ood/srp/dao/ProductDAO.java new file mode 100644 index 0000000000..9aa998ebc1 --- /dev/null +++ b/students/382266293/src/com/coderising/ood/srp/dao/ProductDAO.java @@ -0,0 +1,49 @@ +package com.coderising.ood.srp.dao; + +import com.coderising.ood.srp.bean.Product; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by onlyLYJ on 2017/6/12. + */ +public class ProductDAO { + + public List list(File productFile) throws IOException // @02C + { + + System.out.println(productFile); + List products = new ArrayList<>(); + + System.out.println("开始导入产品清单"); + try (BufferedReader br = new BufferedReader(new FileReader(productFile))) { + String temp = null; + while (null != (temp = br.readLine())) { + String[] data = temp.split(" "); + + String productID = data[0]; + String productDesc = data[1]; + + Product p = new Product(); + p.setProductID(productID); + p.setProductDesc(productDesc); + System.out.println("产品ID = " + productID); + System.out.println("产品描述 = " + productDesc + "\r\n"); + + products.add(p); + } + } catch (IOException e) { + throw new IOException(e.getMessage()); + } + + + return products; + } + + +} diff --git a/students/382266293/src/com/coderising/ood/srp/dao/UserDAO.java b/students/382266293/src/com/coderising/ood/srp/dao/UserDAO.java new file mode 100644 index 0000000000..58bdbd07e9 --- /dev/null +++ b/students/382266293/src/com/coderising/ood/srp/dao/UserDAO.java @@ -0,0 +1,31 @@ +package com.coderising.ood.srp.dao; + +import com.coderising.ood.srp.bean.Product; +import com.coderising.ood.srp.bean.Subscriber; +import com.coderising.ood.srp.util.DBUtil; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by onlyLYJ on 2017/6/12. + */ +public class UserDAO { + + public List list(Product p) throws Exception { + + DBUtil.setLoadQuery(p); + + List subscribers = new ArrayList<>(); + for (int i = 1; i <= 3; i++) { + Subscriber s = new Subscriber(); + s.setName("user" + i); + s.setMailAddress(s.getName() + "@amazon.com"); + subscribers.add(s); + + } + return subscribers; + + } + +} diff --git a/students/382266293/src/com/coderising/ood/srp/data/product_promotion.txt b/students/382266293/src/com/coderising/ood/srp/data/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/382266293/src/com/coderising/ood/srp/data/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/382266293/src/com/coderising/ood/srp/util/DBUtil.java b/students/382266293/src/com/coderising/ood/srp/util/DBUtil.java new file mode 100644 index 0000000000..a5885fe70e --- /dev/null +++ b/students/382266293/src/com/coderising/ood/srp/util/DBUtil.java @@ -0,0 +1,16 @@ +package com.coderising.ood.srp.util; + +import com.coderising.ood.srp.bean.Product; + +public class DBUtil { + + public static void setLoadQuery(Product p) { + + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + p.getProductID() + "' " + + "and send_mail=1 "; + + System.out.println("loadQuery set"); + } + +} diff --git a/students/382266293/src/com/coderising/ood/srp/util/MailUtil.java b/students/382266293/src/com/coderising/ood/srp/util/MailUtil.java new file mode 100644 index 0000000000..a28e8aaadb --- /dev/null +++ b/students/382266293/src/com/coderising/ood/srp/util/MailUtil.java @@ -0,0 +1,32 @@ +package com.coderising.ood.srp.util; + +import com.coderising.ood.srp.bean.Mail; +import com.coderising.ood.srp.config.ServerConfig; + +public class MailUtil { + + public static void send(Mail mail, ServerConfig sc) { + try { + sendEmail(mail, sc.getSmtpHost()); + } catch (Exception e) { + try { + sendEmail(mail, sc.getAltSmtpHost()); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + public static void sendEmail(Mail mail, String SmtpHost) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(mail.getFromAddress()).append("\n"); + buffer.append("To:").append(mail.getSubscriber().getMailAddress()).append("\n"); + buffer.append("Subject:").append(mail.getSubject()).append("\n"); + buffer.append("Content:").append(mail.getMessage()).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/382266293/src/pom.xml b/students/382266293/src/pom.xml new file mode 100644 index 0000000000..2bf55e64c9 --- /dev/null +++ b/students/382266293/src/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + From 61b06dc82cf9883ec3242e787df9ef4959fc0ca4 Mon Sep 17 00:00:00 2001 From: onlyLYJ <382266293@qq.com> Date: Tue, 13 Jun 2017 00:06:40 +0800 Subject: [PATCH 046/214] Merge pull request #6 from onlyliuxin/master season 2 --- .../src/com/coderising/ood/srp/config/Configuration.java | 1 - 1 file changed, 1 deletion(-) diff --git a/students/382266293/src/com/coderising/ood/srp/config/Configuration.java b/students/382266293/src/com/coderising/ood/srp/config/Configuration.java index 2ed21e3af8..5f0d181d16 100644 --- a/students/382266293/src/com/coderising/ood/srp/config/Configuration.java +++ b/students/382266293/src/com/coderising/ood/srp/config/Configuration.java @@ -31,7 +31,6 @@ public ServerConfig getServerConfig() { } private String getProperty(String key) { - return configurations.get(key); } From 473b854ec1fc1d5de0f692d49a04747e4b8cb0c1 Mon Sep 17 00:00:00 2001 From: onlyLYJ <382266293@qq.com> Date: Tue, 13 Jun 2017 00:14:16 +0800 Subject: [PATCH 047/214] week 1 done --- .../coderising => }/ood/srp/MailSender.java | 18 +++++++++--------- .../coderising => }/ood/srp/PromotionMail.java | 4 ++-- .../coderising => }/ood/srp/bean/Mail.java | 2 +- .../coderising => }/ood/srp/bean/Product.java | 2 +- .../ood/srp/bean/Subscriber.java | 2 +- .../ood/srp/config/Configuration.java | 2 +- .../ood/srp/config/ServerConfig.java | 2 +- .../coderising => }/ood/srp/dao/MailDAO.java | 8 ++++---- .../ood/srp/dao/ProductDAO.java | 4 ++-- .../coderising => }/ood/srp/dao/UserDAO.java | 8 ++++---- .../ood/srp/data/product_promotion.txt | 0 .../coderising => }/ood/srp/util/DBUtil.java | 4 ++-- .../coderising => }/ood/srp/util/MailUtil.java | 6 +++--- 13 files changed, 31 insertions(+), 31 deletions(-) rename students/382266293/src/{com/coderising => }/ood/srp/MailSender.java (75%) rename students/382266293/src/{com/coderising => }/ood/srp/PromotionMail.java (93%) rename students/382266293/src/{com/coderising => }/ood/srp/bean/Mail.java (96%) rename students/382266293/src/{com/coderising => }/ood/srp/bean/Product.java (95%) rename students/382266293/src/{com/coderising => }/ood/srp/bean/Subscriber.java (92%) rename students/382266293/src/{com/coderising => }/ood/srp/config/Configuration.java (95%) rename students/382266293/src/{com/coderising => }/ood/srp/config/ServerConfig.java (92%) rename students/382266293/src/{com/coderising => }/ood/srp/dao/MailDAO.java (82%) rename students/382266293/src/{com/coderising => }/ood/srp/dao/ProductDAO.java (93%) rename students/382266293/src/{com/coderising => }/ood/srp/dao/UserDAO.java (75%) rename students/382266293/src/{com/coderising => }/ood/srp/data/product_promotion.txt (100%) rename students/382266293/src/{com/coderising => }/ood/srp/util/DBUtil.java (78%) rename students/382266293/src/{com/coderising => }/ood/srp/util/MailUtil.java (87%) diff --git a/students/382266293/src/com/coderising/ood/srp/MailSender.java b/students/382266293/src/ood/srp/MailSender.java similarity index 75% rename from students/382266293/src/com/coderising/ood/srp/MailSender.java rename to students/382266293/src/ood/srp/MailSender.java index 6d52844059..83e3fbef85 100644 --- a/students/382266293/src/com/coderising/ood/srp/MailSender.java +++ b/students/382266293/src/ood/srp/MailSender.java @@ -1,12 +1,12 @@ -package com.coderising.ood.srp; - -import com.coderising.ood.srp.bean.Mail; -import com.coderising.ood.srp.bean.Product; -import com.coderising.ood.srp.config.Configuration; -import com.coderising.ood.srp.config.ServerConfig; -import com.coderising.ood.srp.dao.MailDAO; -import com.coderising.ood.srp.dao.ProductDAO; -import com.coderising.ood.srp.util.MailUtil; +package ood.srp; + +import ood.srp.bean.Mail; +import ood.srp.bean.Product; +import ood.srp.config.Configuration; +import ood.srp.config.ServerConfig; +import ood.srp.dao.MailDAO; +import ood.srp.dao.ProductDAO; +import ood.srp.util.MailUtil; import java.io.File; import java.util.List; diff --git a/students/382266293/src/com/coderising/ood/srp/PromotionMail.java b/students/382266293/src/ood/srp/PromotionMail.java similarity index 93% rename from students/382266293/src/com/coderising/ood/srp/PromotionMail.java rename to students/382266293/src/ood/srp/PromotionMail.java index abde88278d..c3d6520a67 100644 --- a/students/382266293/src/com/coderising/ood/srp/PromotionMail.java +++ b/students/382266293/src/ood/srp/PromotionMail.java @@ -1,6 +1,6 @@ -package com.coderising.ood.srp; +package ood.srp; -import com.coderising.ood.srp.bean.Mail; +import ood.srp.bean.Mail; import java.io.File; import java.util.List; diff --git a/students/382266293/src/com/coderising/ood/srp/bean/Mail.java b/students/382266293/src/ood/srp/bean/Mail.java similarity index 96% rename from students/382266293/src/com/coderising/ood/srp/bean/Mail.java rename to students/382266293/src/ood/srp/bean/Mail.java index 6a612dbd48..91e1363140 100644 --- a/students/382266293/src/com/coderising/ood/srp/bean/Mail.java +++ b/students/382266293/src/ood/srp/bean/Mail.java @@ -1,4 +1,4 @@ -package com.coderising.ood.srp.bean; +package ood.srp.bean; /** * Created by onlyLYJ on 2017/6/12. diff --git a/students/382266293/src/com/coderising/ood/srp/bean/Product.java b/students/382266293/src/ood/srp/bean/Product.java similarity index 95% rename from students/382266293/src/com/coderising/ood/srp/bean/Product.java rename to students/382266293/src/ood/srp/bean/Product.java index 783f2033c4..3b879b274f 100644 --- a/students/382266293/src/com/coderising/ood/srp/bean/Product.java +++ b/students/382266293/src/ood/srp/bean/Product.java @@ -1,4 +1,4 @@ -package com.coderising.ood.srp.bean; +package ood.srp.bean; import java.util.List; diff --git a/students/382266293/src/com/coderising/ood/srp/bean/Subscriber.java b/students/382266293/src/ood/srp/bean/Subscriber.java similarity index 92% rename from students/382266293/src/com/coderising/ood/srp/bean/Subscriber.java rename to students/382266293/src/ood/srp/bean/Subscriber.java index 048c1eaf34..a2a2641622 100644 --- a/students/382266293/src/com/coderising/ood/srp/bean/Subscriber.java +++ b/students/382266293/src/ood/srp/bean/Subscriber.java @@ -1,4 +1,4 @@ -package com.coderising.ood.srp.bean; +package ood.srp.bean; /** * Created by onlyLYJ on 2017/6/12. diff --git a/students/382266293/src/com/coderising/ood/srp/config/Configuration.java b/students/382266293/src/ood/srp/config/Configuration.java similarity index 95% rename from students/382266293/src/com/coderising/ood/srp/config/Configuration.java rename to students/382266293/src/ood/srp/config/Configuration.java index 5f0d181d16..59bf7bfbd1 100644 --- a/students/382266293/src/com/coderising/ood/srp/config/Configuration.java +++ b/students/382266293/src/ood/srp/config/Configuration.java @@ -1,4 +1,4 @@ -package com.coderising.ood.srp.config; +package ood.srp.config; import java.util.HashMap; import java.util.Map; diff --git a/students/382266293/src/com/coderising/ood/srp/config/ServerConfig.java b/students/382266293/src/ood/srp/config/ServerConfig.java similarity index 92% rename from students/382266293/src/com/coderising/ood/srp/config/ServerConfig.java rename to students/382266293/src/ood/srp/config/ServerConfig.java index 4b4b374b73..2c8c52a79a 100644 --- a/students/382266293/src/com/coderising/ood/srp/config/ServerConfig.java +++ b/students/382266293/src/ood/srp/config/ServerConfig.java @@ -1,4 +1,4 @@ -package com.coderising.ood.srp.config; +package ood.srp.config; /** * Created by onlyLYJ on 2017/6/12. diff --git a/students/382266293/src/com/coderising/ood/srp/dao/MailDAO.java b/students/382266293/src/ood/srp/dao/MailDAO.java similarity index 82% rename from students/382266293/src/com/coderising/ood/srp/dao/MailDAO.java rename to students/382266293/src/ood/srp/dao/MailDAO.java index 9dd5ee2259..c80eb7f7bc 100644 --- a/students/382266293/src/com/coderising/ood/srp/dao/MailDAO.java +++ b/students/382266293/src/ood/srp/dao/MailDAO.java @@ -1,8 +1,8 @@ -package com.coderising.ood.srp.dao; +package ood.srp.dao; -import com.coderising.ood.srp.bean.Mail; -import com.coderising.ood.srp.bean.Product; -import com.coderising.ood.srp.bean.Subscriber; +import ood.srp.bean.Mail; +import ood.srp.bean.Product; +import ood.srp.bean.Subscriber; import java.util.ArrayList; import java.util.List; diff --git a/students/382266293/src/com/coderising/ood/srp/dao/ProductDAO.java b/students/382266293/src/ood/srp/dao/ProductDAO.java similarity index 93% rename from students/382266293/src/com/coderising/ood/srp/dao/ProductDAO.java rename to students/382266293/src/ood/srp/dao/ProductDAO.java index 9aa998ebc1..39312cf24a 100644 --- a/students/382266293/src/com/coderising/ood/srp/dao/ProductDAO.java +++ b/students/382266293/src/ood/srp/dao/ProductDAO.java @@ -1,6 +1,6 @@ -package com.coderising.ood.srp.dao; +package ood.srp.dao; -import com.coderising.ood.srp.bean.Product; +import ood.srp.bean.Product; import java.io.BufferedReader; import java.io.File; diff --git a/students/382266293/src/com/coderising/ood/srp/dao/UserDAO.java b/students/382266293/src/ood/srp/dao/UserDAO.java similarity index 75% rename from students/382266293/src/com/coderising/ood/srp/dao/UserDAO.java rename to students/382266293/src/ood/srp/dao/UserDAO.java index 58bdbd07e9..91ebd5cd23 100644 --- a/students/382266293/src/com/coderising/ood/srp/dao/UserDAO.java +++ b/students/382266293/src/ood/srp/dao/UserDAO.java @@ -1,8 +1,8 @@ -package com.coderising.ood.srp.dao; +package ood.srp.dao; -import com.coderising.ood.srp.bean.Product; -import com.coderising.ood.srp.bean.Subscriber; -import com.coderising.ood.srp.util.DBUtil; +import ood.srp.bean.Product; +import ood.srp.bean.Subscriber; +import ood.srp.util.DBUtil; import java.util.ArrayList; import java.util.List; diff --git a/students/382266293/src/com/coderising/ood/srp/data/product_promotion.txt b/students/382266293/src/ood/srp/data/product_promotion.txt similarity index 100% rename from students/382266293/src/com/coderising/ood/srp/data/product_promotion.txt rename to students/382266293/src/ood/srp/data/product_promotion.txt diff --git a/students/382266293/src/com/coderising/ood/srp/util/DBUtil.java b/students/382266293/src/ood/srp/util/DBUtil.java similarity index 78% rename from students/382266293/src/com/coderising/ood/srp/util/DBUtil.java rename to students/382266293/src/ood/srp/util/DBUtil.java index a5885fe70e..9d14f02cba 100644 --- a/students/382266293/src/com/coderising/ood/srp/util/DBUtil.java +++ b/students/382266293/src/ood/srp/util/DBUtil.java @@ -1,6 +1,6 @@ -package com.coderising.ood.srp.util; +package ood.srp.util; -import com.coderising.ood.srp.bean.Product; +import ood.srp.bean.Product; public class DBUtil { diff --git a/students/382266293/src/com/coderising/ood/srp/util/MailUtil.java b/students/382266293/src/ood/srp/util/MailUtil.java similarity index 87% rename from students/382266293/src/com/coderising/ood/srp/util/MailUtil.java rename to students/382266293/src/ood/srp/util/MailUtil.java index a28e8aaadb..d41af6667e 100644 --- a/students/382266293/src/com/coderising/ood/srp/util/MailUtil.java +++ b/students/382266293/src/ood/srp/util/MailUtil.java @@ -1,7 +1,7 @@ -package com.coderising.ood.srp.util; +package ood.srp.util; -import com.coderising.ood.srp.bean.Mail; -import com.coderising.ood.srp.config.ServerConfig; +import ood.srp.bean.Mail; +import ood.srp.config.ServerConfig; public class MailUtil { From 91b94b790c9806dc5380cdb3f7cdb299461292a7 Mon Sep 17 00:00:00 2001 From: dtwj03 Date: Tue, 13 Jun 2017 00:56:49 +0800 Subject: [PATCH 048/214] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=AF=B4=E6=98=8E?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/469880403/readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/469880403/readme.md diff --git a/students/469880403/readme.md b/students/469880403/readme.md new file mode 100644 index 0000000000..8fc153b34f --- /dev/null +++ b/students/469880403/readme.md @@ -0,0 +1 @@ +说明文件 \ No newline at end of file From b8a284b0b3d597f196d131b7cbc5470f5a9d40b1 Mon Sep 17 00:00:00 2001 From: fade <511739113@qq.com> Date: Tue, 13 Jun 2017 02:06:26 +0800 Subject: [PATCH 049/214] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coderising/ood/srp/ConfigurationKeys.java | 9 - .../java/com/coderising/ood/srp/MailUtil.java | 18 -- .../com/coderising/ood/srp/PromotionMail.java | 207 +++--------------- .../com/coderising/ood/srp/bean/Message.java | 37 ++++ .../com/coderising/ood/srp/bean/Product.java | 40 ++++ .../coderising/ood/srp/bean/ServerBean.java | 61 ++++++ .../ood/srp/{ => config}/Configuration.java | 14 +- .../ood/srp/config/ConfigurationKeys.java | 21 ++ .../com/coderising/ood/srp/jdbc/UserJDBC.java | 29 +++ .../ood/srp/service/MessageService.java | 83 +++++++ .../ood/srp/service/UserService.java | 35 +++ .../coderising/ood/srp/{ => util}/DBUtil.java | 9 +- .../com/coderising/ood/srp/util/FileUtil.java | 52 +++++ .../com/coderising/ood/srp/util/MailUtil.java | 25 +++ 14 files changed, 429 insertions(+), 211 deletions(-) delete mode 100644 students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java delete mode 100644 students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Message.java create mode 100644 students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Product.java create mode 100644 students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/bean/ServerBean.java rename students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/{ => config}/Configuration.java (64%) create mode 100644 students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java create mode 100644 students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/jdbc/UserJDBC.java create mode 100644 students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/service/MessageService.java create mode 100644 students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/service/UserService.java rename students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/{ => util}/DBUtil.java (74%) create mode 100644 students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/util/FileUtil.java create mode 100644 students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/util/MailUtil.java diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java deleted file mode 100644 index 8695aed644..0000000000 --- a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.coderising.ood.srp; - -public class ConfigurationKeys { - - public static final String SMTP_SERVER = "smtp.server"; - public static final String ALT_SMTP_SERVER = "alt.smtp.server"; - public static final String EMAIL_ADMIN = "email.admin"; - -} diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java deleted file mode 100644 index 9f9e749af7..0000000000 --- a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.coderising.ood.srp; - -public class MailUtil { - - public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, - boolean debug) { - //假装发了一封邮件 - StringBuilder buffer = new StringBuilder(); - buffer.append("From:").append(fromAddress).append("\n"); - buffer.append("To:").append(toAddress).append("\n"); - buffer.append("Subject:").append(subject).append("\n"); - buffer.append("Content:").append(message).append("\n"); - System.out.println(buffer.toString()); - - } - - -} diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index 781587a846..bf42021028 100644 --- a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -1,199 +1,46 @@ package com.coderising.ood.srp; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.Serializable; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; +import com.coderising.ood.srp.bean.Product; +import com.coderising.ood.srp.service.MessageService; +import com.coderising.ood.srp.service.UserService; +import com.coderising.ood.srp.util.FileUtil; + +/** + * 模拟 商品促销通知系统 + *

标题:

+ *

描述:

+ * @autho zx + * @time 2017年6月12日 下午11:43:57 +*/ public class PromotionMail { - - - protected String sendMailQuery = null; - - - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; - protected String toAddress = null; - protected String subject = null; - protected String message = null; - - protected String productID = null; - protected String productDesc = null; - - private static Configuration config; - + /** 模拟注入userService */ + private UserService userService = new UserService(); - private static final String NAME_KEY = "NAME"; - private static final String EMAIL_KEY = "EMAIL"; - + /** 模拟注入messageService */ + private MessageService messageService = new MessageService(); public static void main(String[] args) throws Exception { - File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); boolean emailDebug = false; - - PromotionMail pe = new PromotionMail(f, emailDebug); - + new PromotionMail(f, emailDebug); } - + /** + * 商品促销通知系统 + *

标题:

+ *

描述:

+ * @param file + * @param mailDebug + * @throws Exception + */ public PromotionMail(File file, boolean mailDebug) throws Exception { - //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - readFile(file); - - - config = new Configuration(); - - setSMTPHost(); - setAltSMTPHost(); - - - setFromAddress(); - - - setLoadQuery(); - - sendEMails(mailDebug, loadMailingList()); - - - } - - - - - protected void setProductID(String productID) - { - this.productID = productID; - - } - - protected String getproductID() - { - return productID; - } - - protected void setLoadQuery() throws Exception { - - sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + productID +"' " - + "and send_mail=1 "; - - - System.out.println("loadQuery set"); + Product product = FileUtil.readFile(file); + messageService.sendEMails(mailDebug, userService.queryUserInfo(product.getProductId()),product); } - - protected void setSMTPHost() - { - smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); - } - - protected void setAltSMTPHost() - { - altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); - - } - - - protected void setFromAddress() - { - fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); - } - - protected void setMessage(HashMap userInfo) throws IOException - { - - String name = (String) userInfo.get(NAME_KEY); - - subject = "您关注的产品降价了"; - message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; - - - - } - - - protected void readFile(File file) throws IOException // @02C - { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - setProductID(data[0]); - setProductDesc(data[1]); - - System.out.println("产品ID = " + productID + "\n"); - System.out.println("产品描述 = " + productDesc + "\n"); - - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); - } - } - - private void setProductDesc(String desc) { - this.productDesc = desc; - } - - - protected void configureEMail(HashMap userInfo) throws IOException - { - toAddress = (String) userInfo.get(EMAIL_KEY); - if (toAddress.length() > 0) - setMessage(userInfo); - } - - protected List loadMailingList() throws Exception { - return DBUtil.query(this.sendMailQuery); - } - - - protected void sendEMails(boolean debug, List mailingList) throws IOException - { - - System.out.println("开始发送邮件"); - - - if (mailingList != null) { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) { - configureEMail((HashMap) iter.next()); - try - { - if (toAddress.length() > 0) - MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } - catch (Exception e) - { - - try { - MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) - { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } - } - - - } - - else { - System.out.println("没有邮件发送"); - - } - - } } diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Message.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Message.java new file mode 100644 index 0000000000..01cd33b04c --- /dev/null +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Message.java @@ -0,0 +1,37 @@ +package com.coderising.ood.srp.bean; + +/** + * 推送的消息 实体 + *

标题:

+ *

描述:

+ * @autho zx + * @time 2017年6月13日 上午2:01:17 +*/ +public class Message extends ServerBean{ + + /** */ + private static final long serialVersionUID = 3850050693864793038L; + + /** 标题 */ + private String subject; + + /** 消息 */ + private String message; + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + +} diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Product.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Product.java new file mode 100644 index 0000000000..ed841c4f72 --- /dev/null +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Product.java @@ -0,0 +1,40 @@ +package com.coderising.ood.srp.bean; + +import java.io.Serializable; + +/** + * 商品类 + *

标题:

+ *

描述:

+ * @autho zx + * @time 2017年6月13日 上午12:29:56 +*/ +public class Product implements Serializable{ + + /** */ + private static final long serialVersionUID = 2966621699675433678L; + + /** 商品Id */ + private String productId; + + /** 商品描述 */ + private String productDesc; + + public String getProductId() { + return productId; + } + + public void setProductId(String productId) { + this.productId = productId; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + + +} diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/bean/ServerBean.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/bean/ServerBean.java new file mode 100644 index 0000000000..948f501f01 --- /dev/null +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/bean/ServerBean.java @@ -0,0 +1,61 @@ +package com.coderising.ood.srp.bean; + +import java.io.Serializable; + +/** + * 服务配置 + *

标题:

+ *

描述:

+ * @autho zx + * @time 2017年6月13日 上午1:22:38 +*/ +public class ServerBean implements Serializable{ + + /** */ + private static final long serialVersionUID = -1842399098772577584L; + + /** 服务器地址 */ + private String smtpHost; + + /** 备用服务器地址 */ + private String altSmtpHost; + + /** 发送地址 */ + private String fromAddress; + + /** 接受地址 */ + private String toAddress; + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + +} diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/config/Configuration.java similarity index 64% rename from students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java rename to students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/config/Configuration.java index f328c1816a..45cbf41c11 100644 --- a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/config/Configuration.java @@ -1,22 +1,30 @@ -package com.coderising.ood.srp; +package com.coderising.ood.srp.config; import java.util.HashMap; import java.util.Map; +/** + * 配置类,模拟从配置文件中读取参数 + *

标题:

+ *

描述:

+ * @autho zx + * @time 2017年6月12日 下午11:44:36 +*/ public class Configuration { - static Map configurations = new HashMap<>(); + private static Map configurations = new HashMap(); + static{ configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); } + /** * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 * @param key * @return */ public String getProperty(String key) { - return configurations.get(key); } diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java new file mode 100644 index 0000000000..0e8c889f2e --- /dev/null +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java @@ -0,0 +1,21 @@ +package com.coderising.ood.srp.config; + +/** + * 常量类 存放配置文件key + *

标题:

+ *

描述:

+ * @autho zx + * @time 2017年6月12日 下午11:45:43 +*/ +public class ConfigurationKeys { + + /** 服务器地址 */ + public static final String SMTP_SERVER = "smtp.server"; + + /** 备用服务器地址 */ + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + + /** email地址 */ + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/jdbc/UserJDBC.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/jdbc/UserJDBC.java new file mode 100644 index 0000000000..b0382b21a8 --- /dev/null +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/jdbc/UserJDBC.java @@ -0,0 +1,29 @@ +package com.coderising.ood.srp.jdbc; + +import java.util.List; + +import com.coderising.ood.srp.util.DBUtil; + +/** + * 用户 jdbc + *

标题:

+ *

描述:

+ * @autho zx + * @time 2017年6月13日 上午12:51:00 +*/ +public class UserJDBC { + + /** + * 根据商品Id 获取用户信息 + *

方法名称:

+ *

方法说明:

+ * @param productId + * @return + * @autho zx + * @time 2017年6月13日 上午12:51:14 + */ + public List selectUserId(String sql){ + return DBUtil.query(sql); + } + +} diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/service/MessageService.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/service/MessageService.java new file mode 100644 index 0000000000..61202c0a6f --- /dev/null +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/service/MessageService.java @@ -0,0 +1,83 @@ +package com.coderising.ood.srp.service; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +import com.coderising.ood.srp.bean.Message; +import com.coderising.ood.srp.bean.Product; +import com.coderising.ood.srp.config.Configuration; +import com.coderising.ood.srp.config.ConfigurationKeys; +import com.coderising.ood.srp.util.MailUtil; + +/** + * 推送消息 + *

标题:

+ *

描述:

+ * @autho zx + * @time 2017年6月13日 上午1:10:27 +*/ +public class MessageService { + + private static final String EMAIL_KEY = "EMAIL"; + + private static final String NAME_KEY = "NAME"; + + private Configuration configuration = new Configuration(); + + /** + * 推送消息 + *

方法名称:

+ *

方法说明:

+ * @param debug + * @param mailingList + * @param product + * @throws IOException + * @autho zx + * @time 2017年6月13日 上午1:59:31 + */ + public void sendEMails(boolean debug, List mailingList,Product product) throws IOException{ + System.out.println("开始发送邮件"); + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + Message message = configureEMail((HashMap) iter.next(),product.getProductDesc()); + try { + if (message!=null) + MailUtil.sendEmail(message, debug); + }catch (Exception e){ + try { + MailUtil.sendEmail(message, debug); + } catch (Exception e2){ + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + }else { + System.out.println("没有邮件发送"); + } + } + + private Message configureEMail(HashMap userInfo,String productDesc) throws IOException{ + String toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + return getMessage(userInfo,productDesc,toAddress); + return null; + } + + private Message getMessage(HashMap userInfo,String productDesc,String toAddress) throws IOException{ + String name = (String) userInfo.get(NAME_KEY); + Message messageBean = new Message(); + String subject = "您关注的产品降价了"; + String message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + messageBean.setMessage(message); + messageBean.setSubject(subject); + messageBean.setToAddress(toAddress); + messageBean.setAltSmtpHost(configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + messageBean.setSmtpHost(configuration.getProperty(ConfigurationKeys.SMTP_SERVER)); + messageBean.setFromAddress(configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN)); + return messageBean; + } + +} diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/service/UserService.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/service/UserService.java new file mode 100644 index 0000000000..99161f9349 --- /dev/null +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/service/UserService.java @@ -0,0 +1,35 @@ +package com.coderising.ood.srp.service; + +import java.util.List; + +import com.coderising.ood.srp.jdbc.UserJDBC; + +/** + * 用户 service + *

标题:

+ *

描述:

+ * @autho zx + * @time 2017年6月13日 上午12:53:30 +*/ +public class UserService { + + /** 模拟注入userJDBC */ + private UserJDBC userJDBC = new UserJDBC(); + + /** + * 获取用户信息 + *

方法名称:

+ *

方法说明:

+ * @param productId + * @return + * @autho zx + * @time 2017年6月13日 上午12:56:45 + */ + public List queryUserInfo(String productId){ + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productId +"' " + + "and send_mail=1 "; + return userJDBC.selectUserId(sendMailQuery); + } + +} diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/util/DBUtil.java similarity index 74% rename from students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java rename to students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/util/DBUtil.java index 82e9261d18..e466fcb0d4 100644 --- a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/util/DBUtil.java @@ -1,8 +1,15 @@ -package com.coderising.ood.srp; +package com.coderising.ood.srp.util; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +/** + * 模拟获取数据库 + *

标题:

+ *

描述:

+ * @autho zx + * @time 2017年6月12日 下午11:47:21 +*/ public class DBUtil { /** diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/util/FileUtil.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/util/FileUtil.java new file mode 100644 index 0000000000..01eeef3227 --- /dev/null +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/util/FileUtil.java @@ -0,0 +1,52 @@ +package com.coderising.ood.srp.util; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import com.coderising.ood.srp.bean.Product; + +/** + * file工具类 + *

标题:

+ *

描述:

+ * @autho zx + * @time 2017年6月13日 上午2:04:59 +*/ +public class FileUtil { + + /** + * 读取商品信息 + *

方法名称:

+ *

方法说明:

+ * @param file + * @return + * @throws IOException + * @autho zx + * @time 2017年6月13日 上午12:39:06 + */ + public static Product readFile(File file) throws IOException{ + BufferedReader br = null; + Product product = new Product(); + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + String productId = data[0]; + String productDesc = data[1]; + product.setProductId(productId); + product.setProductDesc(productDesc); + + System.out.println("产品ID = " + productId + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + return product; + } + +} diff --git a/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/util/MailUtil.java b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/util/MailUtil.java new file mode 100644 index 0000000000..a7ecb7f213 --- /dev/null +++ b/students/511739113/6.11/ood-assignment/src/main/java/com/coderising/ood/srp/util/MailUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp.util; + +import com.coderising.ood.srp.bean.Message; + +/** + * 消息推送工具类 + *

标题:

+ *

描述:

+ * @autho zhangxu + * @time 2017年6月13日 上午2:03:46 +*/ +public class MailUtil { + + public static void sendEmail(Message message,boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(message.getFromAddress()).append("\n"); + buffer.append("To:").append(message.getToAddress()).append("\n"); + buffer.append("Subject:").append(message.getSubject()).append("\n"); + buffer.append("Content:").append(message.getMessage()).append("\n"); + System.out.println(buffer.toString()); + } + + +} From 426445742fdbd728a8f5759077ba2855d22397bb Mon Sep 17 00:00:00 2001 From: luoziyihao Date: Tue, 13 Jun 2017 11:10:50 +0800 Subject: [PATCH 050/214] ok ProductParser --- .../coderising/ood/srp/optimize/Product.java | 23 +++++++++++-- .../ood/srp/optimize/ProductParser.java | 33 ++++++++++++++++--- .../ood/srp/optimize/PromotionMailApp.java | 6 +++- .../com/coding/common/util/FileUtils2.java | 6 +++- .../java/com/coding/common/util/IOUtils2.java | 17 ++++++++-- .../com/coding/common/util/IOUtils2Test.java | 26 +++++++++++++++ .../common/src/test/resources/test.json | 4 +++ 7 files changed, 103 insertions(+), 12 deletions(-) create mode 100644 students/1204187480/code/homework/common/src/test/java/com/coding/common/util/IOUtils2Test.java create mode 100644 students/1204187480/code/homework/common/src/test/resources/test.json diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/Product.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/Product.java index 652897a7f3..ffb440712f 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/Product.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/Product.java @@ -1,9 +1,28 @@ package com.coderising.ood.srp.optimize; +import lombok.Setter; + /** * Created by luoziyihao on 6/12/17. */ + public class Product { - private static String productID; - private static String productDesc; + private String productID; + private String productDesc; + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } } diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/ProductParser.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/ProductParser.java index 7860dd6047..dd3198c42d 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/ProductParser.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/ProductParser.java @@ -1,19 +1,42 @@ package com.coderising.ood.srp.optimize; import java.util.List; +import java.util.stream.Collectors; + +import static com.coding.common.util.FileUtils2.openStream; +import static com.coding.common.util.IOUtils2.readToStringList; /** * Created by luoziyihao on 6/12/17. */ public class ProductParser { - private String productFilePath; + private String productClassPath; - public void setProductFilePath(String productFilePath) { - this.productFilePath = productFilePath; + public void setProductClassPath(String productClassPath) { + this.productClassPath = productClassPath; } - public List parse(){ - return null; + public List parse() { + List stringList = readToStringList(openStream(productClassPath)); + return stringList.stream() + .map(String::trim) + .filter(s -> !s.isEmpty()) + .filter(s -> s.contains(SPLIT_STRING)) + .map(this::parseLine) + .collect(Collectors.toList()); + + } + + private static final String SPLIT_STRING = " "; + + private Product parseLine(String s) { + int index = s.indexOf(SPLIT_STRING); + String productID = s.substring(0, index); + String productDesc = s.substring(index); + Product product = new Product(); + product.setProductDesc(productDesc); + product.setProductID(productID); + return product; } } diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailApp.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailApp.java index ef89a79d77..8b9061e7e5 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailApp.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailApp.java @@ -1,11 +1,15 @@ package com.coderising.ood.srp.optimize; +import java.util.List; + /** * Created by luoziyihao on 6/12/17. */ public class PromotionMailApp { public static void main(String args[]){ - + ProductParser productParser = new ProductParser(); + productParser.setProductClassPath("product_promotion.txt"); + List products = productParser.parse(); } } diff --git a/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/FileUtils2.java b/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/FileUtils2.java index 1d4aef10f5..08e42fb615 100644 --- a/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/FileUtils2.java +++ b/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/FileUtils2.java @@ -3,6 +3,7 @@ import com.google.common.base.Preconditions; import java.io.*; +import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -44,7 +45,7 @@ public static String getCanonicalPath(File file) { } - public static byte[] getBytes(File classFile) throws IllegalStateException { + public static byte[] getBytes(File classFile) { // byteArrayOutputStream, 可写的动长数组 ByteArrayOutputStream baos = null; BufferedInputStream bis = null; @@ -65,5 +66,8 @@ public static byte[] getBytes(File classFile) throws IllegalStateException { } } + public static InputStream openStream(String classPath) { + return ClassLoader.getSystemResourceAsStream(classPath); + } } diff --git a/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/IOUtils2.java b/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/IOUtils2.java index a302a6c199..889c6cefb6 100644 --- a/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/IOUtils2.java +++ b/students/1204187480/code/homework/common/src/main/java/com/coding/common/util/IOUtils2.java @@ -1,8 +1,8 @@ package com.coding.common.util; -import java.io.Closeable; -import java.io.IOException; -import java.io.OutputStream; +import java.io.*; +import java.util.List; +import java.util.stream.Collectors; /** * Created by luoziyihao on 5/2/17. @@ -21,4 +21,15 @@ public static void close(Closeable closeable) { } + public static List readToStringList(InputStream inputStream) { + BufferedReader br = null; + try { + br = new BufferedReader(new InputStreamReader(inputStream)); + return br.lines().collect(Collectors.toList()); + } finally { + close(br); + } + + } + } diff --git a/students/1204187480/code/homework/common/src/test/java/com/coding/common/util/IOUtils2Test.java b/students/1204187480/code/homework/common/src/test/java/com/coding/common/util/IOUtils2Test.java new file mode 100644 index 0000000000..6870919037 --- /dev/null +++ b/students/1204187480/code/homework/common/src/test/java/com/coding/common/util/IOUtils2Test.java @@ -0,0 +1,26 @@ +package com.coding.common.util; + +import org.junit.Test; + +import java.util.List; + +import static com.coding.common.util.FileUtils2.openStream; +import static com.coding.common.util.IOUtils2.readToStringList; +import static org.junit.Assert.*; + +/** + *

+ *

+ * + * @author raoxiang + * @version 6/13/17 + * @since 1.8 + */ +public class IOUtils2Test { + @Test + public void readToStringListTest() throws Exception { + List poms = readToStringList(openStream("test.json")); + System.out.println(poms); + } + +} \ No newline at end of file diff --git a/students/1204187480/code/homework/common/src/test/resources/test.json b/students/1204187480/code/homework/common/src/test/resources/test.json new file mode 100644 index 0000000000..ccc0682ac2 --- /dev/null +++ b/students/1204187480/code/homework/common/src/test/resources/test.json @@ -0,0 +1,4 @@ +{ + "key1": "value1" + , "key2": "value2" +} \ No newline at end of file From f0b779eaddc44cbfda1e408ea577b1ad2591205c Mon Sep 17 00:00:00 2001 From: jyp Date: Tue, 13 Jun 2017 11:41:20 +0800 Subject: [PATCH 051/214] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 第一次 --- .../data-structure/build.gradle | 30 ++++ .../gradle/wrapper/gradle-wrapper.properties | 6 + .../data-structure/data-structure/gradlew | 169 ++++++++++++++++++ .../data-structure/data-structure/gradlew.bat | 84 +++++++++ .../data-structure/settings.gradle | 19 ++ .../data-structure/src/main/java/Library.java | 11 ++ .../src/test/java/LibraryTest.java | 15 ++ students/992331664/ood/ood/build.gradle | 30 ++++ .../gradle/wrapper/gradle-wrapper.properties | 6 + students/992331664/ood/ood/gradlew | 169 ++++++++++++++++++ students/992331664/ood/ood/gradlew.bat | 84 +++++++++ students/992331664/ood/ood/settings.gradle | 19 ++ .../ood/ood/src/main/java/Library.java | 11 ++ .../ood/ood/src/test/java/LibraryTest.java | 15 ++ 14 files changed, 668 insertions(+) create mode 100644 students/992331664/data-structure/data-structure/build.gradle create mode 100644 students/992331664/data-structure/data-structure/gradle/wrapper/gradle-wrapper.properties create mode 100644 students/992331664/data-structure/data-structure/gradlew create mode 100644 students/992331664/data-structure/data-structure/gradlew.bat create mode 100644 students/992331664/data-structure/data-structure/settings.gradle create mode 100644 students/992331664/data-structure/data-structure/src/main/java/Library.java create mode 100644 students/992331664/data-structure/data-structure/src/test/java/LibraryTest.java create mode 100644 students/992331664/ood/ood/build.gradle create mode 100644 students/992331664/ood/ood/gradle/wrapper/gradle-wrapper.properties create mode 100644 students/992331664/ood/ood/gradlew create mode 100644 students/992331664/ood/ood/gradlew.bat create mode 100644 students/992331664/ood/ood/settings.gradle create mode 100644 students/992331664/ood/ood/src/main/java/Library.java create mode 100644 students/992331664/ood/ood/src/test/java/LibraryTest.java diff --git a/students/992331664/data-structure/data-structure/build.gradle b/students/992331664/data-structure/data-structure/build.gradle new file mode 100644 index 0000000000..588e5e86aa --- /dev/null +++ b/students/992331664/data-structure/data-structure/build.gradle @@ -0,0 +1,30 @@ +/* + * This build file was auto generated by running the Gradle 'init' task + * by 'gant' at '17-6-13 上午11:30' with Gradle 3.0 + * + * This generated file contains a sample Java project to get you started. + * For more details take a look at the Java Quickstart chapter in the Gradle + * user guide available at https://docs.gradle.org/3.0/userguide/tutorial_java_projects.html + */ + +// Apply the java plugin to add support for Java +apply plugin: 'java' + +// In this section you declare where to find the dependencies of your project +repositories { + // Use 'jcenter' for resolving your dependencies. + // You can declare any Maven/Ivy/file repository here. + jcenter() +} + +// In this section you declare the dependencies for your production and test code +dependencies { + // The production code uses the SLF4J logging API at compile time + compile 'org.slf4j:slf4j-api:1.7.21' + + // Declare the dependency for your favourite test framework you want to use in your tests. + // TestNG is also supported by the Gradle Test task. Just change the + // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add + // 'test.useTestNG()' to your build script. + testCompile 'junit:junit:4.12' +} diff --git a/students/992331664/data-structure/data-structure/gradle/wrapper/gradle-wrapper.properties b/students/992331664/data-structure/data-structure/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..d4aaec595d --- /dev/null +++ b/students/992331664/data-structure/data-structure/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Tue Jun 13 11:30:56 CST 2017 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-3.0-bin.zip diff --git a/students/992331664/data-structure/data-structure/gradlew b/students/992331664/data-structure/data-structure/gradlew new file mode 100644 index 0000000000..9aa616c273 --- /dev/null +++ b/students/992331664/data-structure/data-structure/gradlew @@ -0,0 +1,169 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/students/992331664/data-structure/data-structure/gradlew.bat b/students/992331664/data-structure/data-structure/gradlew.bat new file mode 100644 index 0000000000..f9553162f1 --- /dev/null +++ b/students/992331664/data-structure/data-structure/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/students/992331664/data-structure/data-structure/settings.gradle b/students/992331664/data-structure/data-structure/settings.gradle new file mode 100644 index 0000000000..e5c7e27790 --- /dev/null +++ b/students/992331664/data-structure/data-structure/settings.gradle @@ -0,0 +1,19 @@ +/* + * This settings file was auto generated by the Gradle buildInit task + * by 'gant' at '17-6-13 上午11:30' with Gradle 3.0 + * + * The settings file is used to specify which projects to include in your build. + * In a single project build this file can be empty or even removed. + * + * Detailed information about configuring a multi-project build in Gradle can be found + * in the user guide at https://docs.gradle.org/3.0/userguide/multi_project_builds.html + */ + +/* +// To declare projects as part of a multi-project build use the 'include' method +include 'shared' +include 'api' +include 'services:webservice' +*/ + +rootProject.name = 'data-structure' diff --git a/students/992331664/data-structure/data-structure/src/main/java/Library.java b/students/992331664/data-structure/data-structure/src/main/java/Library.java new file mode 100644 index 0000000000..3a465d0ed5 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/Library.java @@ -0,0 +1,11 @@ +/* + * This Java source file was auto generated by running 'gradle buildInit --type java-library' + * by 'gant' at '17-6-13 上午11:30' with Gradle 3.0 + * + * @author gant, @date 17-6-13 上午11:30 + */ +public class Library { + public boolean someLibraryMethod() { + return true; + } +} diff --git a/students/992331664/data-structure/data-structure/src/test/java/LibraryTest.java b/students/992331664/data-structure/data-structure/src/test/java/LibraryTest.java new file mode 100644 index 0000000000..da00ff25a0 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/test/java/LibraryTest.java @@ -0,0 +1,15 @@ +import org.junit.Test; +import static org.junit.Assert.*; + +/* + * This Java source file was auto generated by running 'gradle init --type java-library' + * by 'gant' at '17-6-13 上午11:30' with Gradle 3.0 + * + * @author gant, @date 17-6-13 上午11:30 + */ +public class LibraryTest { + @Test public void testSomeLibraryMethod() { + Library classUnderTest = new Library(); + assertTrue("someLibraryMethod should return 'true'", classUnderTest.someLibraryMethod()); + } +} diff --git a/students/992331664/ood/ood/build.gradle b/students/992331664/ood/ood/build.gradle new file mode 100644 index 0000000000..588e5e86aa --- /dev/null +++ b/students/992331664/ood/ood/build.gradle @@ -0,0 +1,30 @@ +/* + * This build file was auto generated by running the Gradle 'init' task + * by 'gant' at '17-6-13 上午11:30' with Gradle 3.0 + * + * This generated file contains a sample Java project to get you started. + * For more details take a look at the Java Quickstart chapter in the Gradle + * user guide available at https://docs.gradle.org/3.0/userguide/tutorial_java_projects.html + */ + +// Apply the java plugin to add support for Java +apply plugin: 'java' + +// In this section you declare where to find the dependencies of your project +repositories { + // Use 'jcenter' for resolving your dependencies. + // You can declare any Maven/Ivy/file repository here. + jcenter() +} + +// In this section you declare the dependencies for your production and test code +dependencies { + // The production code uses the SLF4J logging API at compile time + compile 'org.slf4j:slf4j-api:1.7.21' + + // Declare the dependency for your favourite test framework you want to use in your tests. + // TestNG is also supported by the Gradle Test task. Just change the + // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add + // 'test.useTestNG()' to your build script. + testCompile 'junit:junit:4.12' +} diff --git a/students/992331664/ood/ood/gradle/wrapper/gradle-wrapper.properties b/students/992331664/ood/ood/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..5a2cbbeeab --- /dev/null +++ b/students/992331664/ood/ood/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Tue Jun 13 11:30:26 CST 2017 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-3.0-bin.zip diff --git a/students/992331664/ood/ood/gradlew b/students/992331664/ood/ood/gradlew new file mode 100644 index 0000000000..9aa616c273 --- /dev/null +++ b/students/992331664/ood/ood/gradlew @@ -0,0 +1,169 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/students/992331664/ood/ood/gradlew.bat b/students/992331664/ood/ood/gradlew.bat new file mode 100644 index 0000000000..f9553162f1 --- /dev/null +++ b/students/992331664/ood/ood/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/students/992331664/ood/ood/settings.gradle b/students/992331664/ood/ood/settings.gradle new file mode 100644 index 0000000000..f98d8fb6d8 --- /dev/null +++ b/students/992331664/ood/ood/settings.gradle @@ -0,0 +1,19 @@ +/* + * This settings file was auto generated by the Gradle buildInit task + * by 'gant' at '17-6-13 上午11:30' with Gradle 3.0 + * + * The settings file is used to specify which projects to include in your build. + * In a single project build this file can be empty or even removed. + * + * Detailed information about configuring a multi-project build in Gradle can be found + * in the user guide at https://docs.gradle.org/3.0/userguide/multi_project_builds.html + */ + +/* +// To declare projects as part of a multi-project build use the 'include' method +include 'shared' +include 'api' +include 'services:webservice' +*/ + +rootProject.name = 'ood' diff --git a/students/992331664/ood/ood/src/main/java/Library.java b/students/992331664/ood/ood/src/main/java/Library.java new file mode 100644 index 0000000000..3a465d0ed5 --- /dev/null +++ b/students/992331664/ood/ood/src/main/java/Library.java @@ -0,0 +1,11 @@ +/* + * This Java source file was auto generated by running 'gradle buildInit --type java-library' + * by 'gant' at '17-6-13 上午11:30' with Gradle 3.0 + * + * @author gant, @date 17-6-13 上午11:30 + */ +public class Library { + public boolean someLibraryMethod() { + return true; + } +} diff --git a/students/992331664/ood/ood/src/test/java/LibraryTest.java b/students/992331664/ood/ood/src/test/java/LibraryTest.java new file mode 100644 index 0000000000..da00ff25a0 --- /dev/null +++ b/students/992331664/ood/ood/src/test/java/LibraryTest.java @@ -0,0 +1,15 @@ +import org.junit.Test; +import static org.junit.Assert.*; + +/* + * This Java source file was auto generated by running 'gradle init --type java-library' + * by 'gant' at '17-6-13 上午11:30' with Gradle 3.0 + * + * @author gant, @date 17-6-13 上午11:30 + */ +public class LibraryTest { + @Test public void testSomeLibraryMethod() { + Library classUnderTest = new Library(); + assertTrue("someLibraryMethod should return 'true'", classUnderTest.someLibraryMethod()); + } +} From a98b1029eb11c8f2c136f693677990f67c9e5f8c Mon Sep 17 00:00:00 2001 From: orajavac Date: Tue, 13 Jun 2017 11:41:23 +0800 Subject: [PATCH 052/214] 20170613_1140 readme.md --- students/562768642/readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/562768642/readme.md diff --git a/students/562768642/readme.md b/students/562768642/readme.md new file mode 100644 index 0000000000..97979001f4 --- /dev/null +++ b/students/562768642/readme.md @@ -0,0 +1 @@ +最新提交 \ No newline at end of file From 4a2ca08c0124785c92915d7a9b8b2515161c40a1 Mon Sep 17 00:00:00 2001 From: orajavac Date: Tue, 13 Jun 2017 11:45:39 +0800 Subject: [PATCH 053/214] =?UTF-8?q?20170613=5F1145=20=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=B8=80=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/562768642/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/students/562768642/readme.md b/students/562768642/readme.md index 97979001f4..f363197d69 100644 --- a/students/562768642/readme.md +++ b/students/562768642/readme.md @@ -1 +1 @@ -最新提交 \ No newline at end of file +最新提交,测试一下 \ No newline at end of file From 9d909c569f737d238c3272f854a7f374ba96486a Mon Sep 17 00:00:00 2001 From: orajavac Date: Tue, 13 Jun 2017 11:52:56 +0800 Subject: [PATCH 054/214] =?UTF-8?q?20170613=5F1152=20=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=8F=90=E4=BA=A4java=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coding2017/ood/srp/Configuration.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/srp/Configuration.java diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/srp/Configuration.java b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/Configuration.java new file mode 100644 index 0000000000..465fe66675 --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.github.orajavac.coding2017.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} From 82d9a01304f0be5a824719a6136a9beff425e006 Mon Sep 17 00:00:00 2001 From: jyp Date: Tue, 13 Jun 2017 11:59:35 +0800 Subject: [PATCH 055/214] =?UTF-8?q?=E5=87=86=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data-structure/src/main/java/Library.java | 11 -- .../src/test/java/LibraryTest.java | 15 -- .../ood/ood/src/main/java/Library.java | 11 -- .../com/coderising/ood/srp/Configuration.java | 23 +++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 27 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 162 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + .../ood/ood/src/test/java/LibraryTest.java | 15 -- 10 files changed, 243 insertions(+), 52 deletions(-) delete mode 100644 students/992331664/data-structure/data-structure/src/main/java/Library.java delete mode 100644 students/992331664/data-structure/data-structure/src/test/java/LibraryTest.java delete mode 100644 students/992331664/ood/ood/src/main/java/Library.java create mode 100644 students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/product_promotion.txt delete mode 100644 students/992331664/ood/ood/src/test/java/LibraryTest.java diff --git a/students/992331664/data-structure/data-structure/src/main/java/Library.java b/students/992331664/data-structure/data-structure/src/main/java/Library.java deleted file mode 100644 index 3a465d0ed5..0000000000 --- a/students/992331664/data-structure/data-structure/src/main/java/Library.java +++ /dev/null @@ -1,11 +0,0 @@ -/* - * This Java source file was auto generated by running 'gradle buildInit --type java-library' - * by 'gant' at '17-6-13 上午11:30' with Gradle 3.0 - * - * @author gant, @date 17-6-13 上午11:30 - */ -public class Library { - public boolean someLibraryMethod() { - return true; - } -} diff --git a/students/992331664/data-structure/data-structure/src/test/java/LibraryTest.java b/students/992331664/data-structure/data-structure/src/test/java/LibraryTest.java deleted file mode 100644 index da00ff25a0..0000000000 --- a/students/992331664/data-structure/data-structure/src/test/java/LibraryTest.java +++ /dev/null @@ -1,15 +0,0 @@ -import org.junit.Test; -import static org.junit.Assert.*; - -/* - * This Java source file was auto generated by running 'gradle init --type java-library' - * by 'gant' at '17-6-13 上午11:30' with Gradle 3.0 - * - * @author gant, @date 17-6-13 上午11:30 - */ -public class LibraryTest { - @Test public void testSomeLibraryMethod() { - Library classUnderTest = new Library(); - assertTrue("someLibraryMethod should return 'true'", classUnderTest.someLibraryMethod()); - } -} diff --git a/students/992331664/ood/ood/src/main/java/Library.java b/students/992331664/ood/ood/src/main/java/Library.java deleted file mode 100644 index 3a465d0ed5..0000000000 --- a/students/992331664/ood/ood/src/main/java/Library.java +++ /dev/null @@ -1,11 +0,0 @@ -/* - * This Java source file was auto generated by running 'gradle buildInit --type java-library' - * by 'gant' at '17-6-13 上午11:30' with Gradle 3.0 - * - * @author gant, @date 17-6-13 上午11:30 - */ -public class Library { - public boolean someLibraryMethod() { - return true; - } -} diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/Configuration.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..7597905da1 --- /dev/null +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,27 @@ +package com.coderising.ood.srp; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * + * @param sql + * @return + */ + public static List query(String sql) { + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..d98b32e7fc --- /dev/null +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,162 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + protected String sendMailQuery = null; + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + // 读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + setFromAddress(); + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + } + + protected void setProductID(String productID) { + this.productID = productID; + + } + + protected String getproductID() { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + "where product_id= '" + productID + "' " + + "and send_mail=1 "; + + System.out.println("loadQuery set"); + } + + protected void setSMTPHost() { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + protected void setAltSMTPHost() { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + protected void setFromAddress() { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + + } + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + protected void configureEMail(HashMap userInfo) throws IOException { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + protected void sendEMails(boolean debug, List mailingList) throws IOException { + + System.out.println("开始发送邮件"); + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } catch (Exception e) { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/992331664/ood/ood/src/test/java/LibraryTest.java b/students/992331664/ood/ood/src/test/java/LibraryTest.java deleted file mode 100644 index da00ff25a0..0000000000 --- a/students/992331664/ood/ood/src/test/java/LibraryTest.java +++ /dev/null @@ -1,15 +0,0 @@ -import org.junit.Test; -import static org.junit.Assert.*; - -/* - * This Java source file was auto generated by running 'gradle init --type java-library' - * by 'gant' at '17-6-13 上午11:30' with Gradle 3.0 - * - * @author gant, @date 17-6-13 上午11:30 - */ -public class LibraryTest { - @Test public void testSomeLibraryMethod() { - Library classUnderTest = new Library(); - assertTrue("someLibraryMethod should return 'true'", classUnderTest.someLibraryMethod()); - } -} From 9b48654b4b35e9cad8f4835869d6161d84fa0968 Mon Sep 17 00:00:00 2001 From: yangdd1205 Date: Tue, 13 Jun 2017 13:10:44 +0800 Subject: [PATCH 056/214] srp --- students/1049843090/ood/build.gradle | 28 +++ students/1049843090/ood/settings.gradle | 2 + .../com/coderising/ood/srp/Configuration.java | 23 +++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 181 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 8 files changed, 290 insertions(+) create mode 100644 students/1049843090/ood/build.gradle create mode 100644 students/1049843090/ood/settings.gradle create mode 100644 students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/1049843090/ood/build.gradle b/students/1049843090/ood/build.gradle new file mode 100644 index 0000000000..adca958b35 --- /dev/null +++ b/students/1049843090/ood/build.gradle @@ -0,0 +1,28 @@ +group 'com.yangdd' +version '1.0-SNAPSHOT' +description = '面向对象设计' + +apply plugin: 'java' + +sourceCompatibility = 1.8 + +repositories { + mavenLocal() + mavenCentral() +} + +//项目布局,下面是Java plugin的默认布局 +sourceSets { + main.java.srcDir('src/main/java') + main.resources.srcDir('src/main/resources') + test.java.srcDir('src/test/java') + test.resources.srcDir('src/test/resources') +} + +dependencies { + testCompile('junit:junit:4.12') +} + +tasks.withType(JavaCompile) { + options.encoding = 'UTF-8' +} \ No newline at end of file diff --git a/students/1049843090/ood/settings.gradle b/students/1049843090/ood/settings.gradle new file mode 100644 index 0000000000..4ffc61b06a --- /dev/null +++ b/students/1049843090/ood/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'ood' + diff --git a/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/Configuration.java b/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..d1209d44e4 --- /dev/null +++ b/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package java.com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} \ No newline at end of file diff --git a/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..5f4b048948 --- /dev/null +++ b/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package java.com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} \ No newline at end of file diff --git a/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/DBUtil.java b/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..299aa8bc46 --- /dev/null +++ b/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package java.com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} \ No newline at end of file diff --git a/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/MailUtil.java b/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..3c75104aeb --- /dev/null +++ b/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package java.com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} \ No newline at end of file diff --git a/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/PromotionMail.java b/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..1a3d4a6c15 --- /dev/null +++ b/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,181 @@ +package java.com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + protected void setProductID(String productID) { + this.productID = productID; + + } + + protected String getproductID() { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID + "' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } catch (Exception e) { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } else { + System.out.println("没有邮件发送"); + + } + + } +} \ No newline at end of file diff --git a/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/product_promotion.txt b/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From a9a1e31781560531d48394707cc8d2e0b0f714b6 Mon Sep 17 00:00:00 2001 From: mddonly <275677638@qq.com> Date: Tue, 13 Jun 2017 13:20:49 +0800 Subject: [PATCH 057/214] first --- students/275677638/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/275677638/README.md diff --git a/students/275677638/README.md b/students/275677638/README.md new file mode 100644 index 0000000000..2e9f085533 --- /dev/null +++ b/students/275677638/README.md @@ -0,0 +1 @@ +愿意自荐代码的,可以每个人一个目录 以自己的QQ号命名 ,把自荐的代码放到里边去 From 6e88a41a17fd2046eef43d80a38d6043fd3d9ca8 Mon Sep 17 00:00:00 2001 From: mddonly <275677638@qq.com> Date: Tue, 13 Jun 2017 13:52:05 +0800 Subject: [PATCH 058/214] new line --- students/275677638/1.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 students/275677638/1.txt diff --git a/students/275677638/1.txt b/students/275677638/1.txt new file mode 100644 index 0000000000..e69de29bb2 From b20f426ea7c341b6fa90b91e072ce67f3179a8da Mon Sep 17 00:00:00 2001 From: mddonly <275677638@qq.com> Date: Tue, 13 Jun 2017 13:52:45 +0800 Subject: [PATCH 059/214] mod --- students/275677638/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/students/275677638/README.md b/students/275677638/README.md index 2e9f085533..aa7a31b814 100644 --- a/students/275677638/README.md +++ b/students/275677638/README.md @@ -1 +1,3 @@ 愿意自荐代码的,可以每个人一个目录 以自己的QQ号命名 ,把自荐的代码放到里边去 + +diff From 35fd5945ccc8d665bafca3c0208dccbc248cb2a1 Mon Sep 17 00:00:00 2001 From: orajavac Date: Tue, 13 Jun 2017 15:13:18 +0800 Subject: [PATCH 060/214] =?UTF-8?q?20170613=5F1513=20=E5=88=A0=E9=99=A4rea?= =?UTF-8?q?dme.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/562768642/readme.md | 1 - .../coding2017/ood/srp/ConfigurationKeys.java | 9 + .../orajavac/coding2017/ood/srp/DBUtil.java | 25 +++ .../orajavac/coding2017/ood/srp/MailUtil.java | 18 ++ .../coding2017/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coding2017/ood/srp/product_promotion.txt | 4 + 6 files changed, 255 insertions(+), 1 deletion(-) delete mode 100644 students/562768642/readme.md create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/srp/ConfigurationKeys.java create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/srp/DBUtil.java create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/srp/MailUtil.java create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/srp/PromotionMail.java create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/srp/product_promotion.txt diff --git a/students/562768642/readme.md b/students/562768642/readme.md deleted file mode 100644 index f363197d69..0000000000 --- a/students/562768642/readme.md +++ /dev/null @@ -1 +0,0 @@ -最新提交,测试一下 \ No newline at end of file diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/srp/ConfigurationKeys.java b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..94eb56e6e4 --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.github.orajavac.coding2017.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/srp/DBUtil.java b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/DBUtil.java new file mode 100644 index 0000000000..c4d5f8a65d --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.github.orajavac.coding2017.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/srp/MailUtil.java b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/MailUtil.java new file mode 100644 index 0000000000..c9614ebfc5 --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.github.orajavac.coding2017.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/srp/PromotionMail.java b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..db982d9f34 --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.github.orajavac.coding2017.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/srp/product_promotion.txt b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From f022b287b3e4bc9472649078496f1a4b24d23fbb Mon Sep 17 00:00:00 2001 From: guozheng5 <840145455@qq.com> Date: Tue, 13 Jun 2017 15:15:56 +0800 Subject: [PATCH 061/214] test --- students/840145455/readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/840145455/readme.md diff --git a/students/840145455/readme.md b/students/840145455/readme.md new file mode 100644 index 0000000000..6afb392c20 --- /dev/null +++ b/students/840145455/readme.md @@ -0,0 +1 @@ +愿意自荐代码的,可以每个人一个目录 以自己的QQ号命名 ,把自荐的代码放到里边去 From 57ec996333fbabdd4fc210f3a6701b96941e8d0f Mon Sep 17 00:00:00 2001 From: orajavac Date: Tue, 13 Jun 2017 15:17:51 +0800 Subject: [PATCH 062/214] =?UTF-8?q?20170613=5F1517=20=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/github/orajavac/coding2017/ood/srp/Configuration.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/srp/Configuration.java b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/Configuration.java index 465fe66675..bf9902bf99 100644 --- a/students/562768642/src/com/github/orajavac/coding2017/ood/srp/Configuration.java +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/Configuration.java @@ -9,7 +9,7 @@ public class Configuration { configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); - } + } /** * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 * @param key From eb26a759ece77b182554690ec3f6bb7f44b8c94c Mon Sep 17 00:00:00 2001 From: yuyingzhi Date: Tue, 13 Jun 2017 15:38:15 +0800 Subject: [PATCH 063/214] first ood-assignment finish --- .../first_OOP_homework/ood-assignment/pom.xml | 32 ++++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 ++ .../java/com/coderising/ood/srp/DBUtil.java | 24 ++++ .../java/com/coderising/ood/srp/Email.java | 80 ++++++++++++++ .../java/com/coderising/ood/srp/MailUtil.java | 15 +++ .../java/com/coderising/ood/srp/Message.java | 19 ++++ .../java/com/coderising/ood/srp/Product.java | 43 ++++++++ .../com/coderising/ood/srp/PromotionMail.java | 103 ++++++++++++++++++ .../java/com/coderising/ood/srp/TestMain.java | 16 +++ .../java/com/coderising/ood/srp/User.java | 21 ++++ .../coderising/ood/srp/product_promotion.txt | 4 + .../81681981/first_OOP_homework/readme.txt | 11 ++ 12 files changed, 377 insertions(+) create mode 100644 students/81681981/first_OOP_homework/ood-assignment/pom.xml create mode 100644 students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/Email.java create mode 100644 students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/Message.java create mode 100644 students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/TestMain.java create mode 100644 students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/User.java create mode 100644 students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt create mode 100644 students/81681981/first_OOP_homework/readme.txt diff --git a/students/81681981/first_OOP_homework/ood-assignment/pom.xml b/students/81681981/first_OOP_homework/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/81681981/first_OOP_homework/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..0af0155e33 --- /dev/null +++ b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,24 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + User user = new User(); + user.setUserName("User" + i); + user.setMail(i+"aa@bb.com"); + userList.add(user); + } + + return userList; + } +} diff --git a/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/Email.java b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/Email.java new file mode 100644 index 0000000000..b6d62acbeb --- /dev/null +++ b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/Email.java @@ -0,0 +1,80 @@ +package com.coderising.ood.srp; + +import java.util.HashMap; +import java.util.Map; +/* + * email + */ + +public class Email { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String subject = null; + protected String message = null; + + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + + public String getSmtpHost() { + return smtpHost; + } + + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + + public String getAltSmtpHost() { + return altSmtpHost; + } + + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + + + public String getFromAddress() { + return fromAddress; + } + + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + protected void setSMTPHost() + { + smtpHost = this.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = this.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = this.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + +} diff --git a/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..f0adff0ab8 --- /dev/null +++ b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,15 @@ +package com.coderising.ood.srp; + + +public class MailUtil { + public static void sendEmail(String toAddress,Message mes,boolean debug) { + Email email = new Email(); + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(email.getFromAddress()).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(mes.getSubject()).append("\n"); + buffer.append("Content:").append(mes.getMessageDesc()).append("\n"); + System.out.println(buffer.toString()); + } +} diff --git a/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/Message.java b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/Message.java new file mode 100644 index 0000000000..59d916286b --- /dev/null +++ b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/Message.java @@ -0,0 +1,19 @@ +package com.coderising.ood.srp; + +public class Message { + private String subject; + private String messageDesc; + public String getSubject() { + return subject; + } + public void setSubject(String subject) { + this.subject = subject; + } + public String getMessageDesc() { + return messageDesc; + } + public void setMessageDesc(String messageDesc) { + this.messageDesc = messageDesc; + } + +} diff --git a/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..236f61d132 --- /dev/null +++ b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,43 @@ +package com.coderising.ood.srp; + +import java.util.ArrayList; +import java.util.List; + +public class Product { + + protected String productID = null; + protected String productDesc = null; + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + + public String getProductID() { + return productID; + } + + //获得该产品的所有订阅者 + protected List getLoadQuery(String productID){ + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + System.out.println("loadQuery set"); + return DBUtil.query(sendMailQuery); + } +} diff --git a/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..b0641c9789 --- /dev/null +++ b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,103 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class PromotionMail { + + protected String sendMailQuery = null; + + protected String toAddress = null; + + protected String subject = null; + protected String message = null; + /** + * 1.读产品信息 2.获取发送地址 3.组织内容 4.发送 + * + * */ + // 1.获取产品信息 + protected List readFile(File file) throws IOException // @02C + { + List productlist = new ArrayList(); + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + Product product = new Product(); + product.setProductID(data[0]); + product.setProductDesc(data[1]); + productlist.add(product); + System.out.println("产品ID = " + data[0] + "\n"); + System.out.println("产品描述 = " + data[1] + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + + return productlist; + } + + // 获取产品信息 + public PromotionMail(File file, boolean mailDebug) throws Exception { + + // 读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + List productlist = readFile(file); + if (productlist != null) { + for (int i = 0; i < productlist.size(); i++) { + Product product = (Product) productlist.get(i); + if(product != null){ + this.consistAndSend(product); + } + } + + } + } + + //获取某产品的所有订阅用户,并推送对应内容 + public void consistAndSend(Product product){ + List toAddressList = product.getLoadQuery(product + .getproductID());// 获取该产品的订阅者 + if (toAddressList != null){ + for (int j = 0; j < toAddressList.size(); j++) { + User user = (User) toAddressList.get(j); + Message mes = this.setMessage(user.getUserName(), + product.getProductDesc()); + this.sendEMails(true, mes, user.getMail()); + } + } + } + + + protected Message setMessage(String name, String productDesc){ + Message mes = new Message(); + String subject = "您关注的产品降价了"; + String message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + + " 降价了,欢迎购买!"; + mes.setSubject(subject); + mes.setMessageDesc(message); + return mes; + } + + protected void sendEMails(boolean debug, Message mes, String toAddress){ + System.out.println("开始发送邮件"); + if (null != toAddress && !toAddress.equals("")) { + try{ + MailUtil.sendEmail(toAddress, mes, debug); + + }catch(Exception e){ + + } + } else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/TestMain.java b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/TestMain.java new file mode 100644 index 0000000000..88b43c18ac --- /dev/null +++ b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/TestMain.java @@ -0,0 +1,16 @@ +package com.coderising.ood.srp; + +import java.io.File; + +public class TestMain { + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + + } +} diff --git a/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/User.java b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/User.java new file mode 100644 index 0000000000..cb03ec5d2c --- /dev/null +++ b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/User.java @@ -0,0 +1,21 @@ +package com.coderising.ood.srp; + +import java.util.List; + +public class User { + private String userName; + private String mail; + public String getUserName() { + return userName; + } + public void setUserName(String userName) { + this.userName = userName; + } + public String getMail() { + return mail; + } + public void setMail(String mail) { + this.mail = mail; + } + +} diff --git a/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/81681981/first_OOP_homework/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/81681981/first_OOP_homework/readme.txt b/students/81681981/first_OOP_homework/readme.txt new file mode 100644 index 0000000000..0ddf999fd4 --- /dev/null +++ b/students/81681981/first_OOP_homework/readme.txt @@ -0,0 +1,11 @@ +ҵ˵ + +༰ +1.conifgurationKeys.java ʼͷϢ +2.User.java ӦûϢû䣩 +3.޸DBUtil.java ݿ࣬Ѽֵ ޸ΪUser +4.Email.java άʼ͵ǰϢʼ⣬ʼݣsmtphost,altSmtpHost,fromAddress,subject,message +5.Product.java άƷϢû +6.Message.javaάҪ͵ +7.޸PromotionMail.java ܣȡƷϢ֯Ҫ͵ݷװΪMessage󣬻ȡòƷӦĶûMailUtil.java ʼ +8.MailUtil.java \ No newline at end of file From 24e2243834b12893c5fd68579f66cdb3cee4ee86 Mon Sep 17 00:00:00 2001 From: yangdd1205 Date: Tue, 13 Jun 2017 16:24:13 +0800 Subject: [PATCH 064/214] update package name --- .../{java => }/com/coderising/ood/srp/Configuration.java | 2 +- .../{java => }/com/coderising/ood/srp/ConfigurationKeys.java | 2 +- .../main/java/{java => }/com/coderising/ood/srp/DBUtil.java | 2 +- .../java/{java => }/com/coderising/ood/srp/MailUtil.java | 2 +- .../{java => }/com/coderising/ood/srp/PromotionMail.java | 5 ++--- .../{java => }/com/coderising/ood/srp/product_promotion.txt | 0 6 files changed, 6 insertions(+), 7 deletions(-) rename students/1049843090/ood/src/main/java/{java => }/com/coderising/ood/srp/Configuration.java (93%) rename students/1049843090/ood/src/main/java/{java => }/com/coderising/ood/srp/ConfigurationKeys.java (85%) rename students/1049843090/ood/src/main/java/{java => }/com/coderising/ood/srp/DBUtil.java (92%) rename students/1049843090/ood/src/main/java/{java => }/com/coderising/ood/srp/MailUtil.java (93%) rename students/1049843090/ood/src/main/java/{java => }/com/coderising/ood/srp/PromotionMail.java (96%) rename students/1049843090/ood/src/main/java/{java => }/com/coderising/ood/srp/product_promotion.txt (100%) diff --git a/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/Configuration.java b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/Configuration.java similarity index 93% rename from students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/Configuration.java rename to students/1049843090/ood/src/main/java/com/coderising/ood/srp/Configuration.java index d1209d44e4..70759b547a 100644 --- a/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/Configuration.java +++ b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/Configuration.java @@ -1,4 +1,4 @@ -package java.com.coderising.ood.srp; +package com.coderising.ood.srp; import java.util.HashMap; import java.util.Map; diff --git a/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java similarity index 85% rename from students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/ConfigurationKeys.java rename to students/1049843090/ood/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java index 5f4b048948..c57f37c4ce 100644 --- a/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/ConfigurationKeys.java +++ b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -1,4 +1,4 @@ -package java.com.coderising.ood.srp; +package com.coderising.ood.srp; public class ConfigurationKeys { diff --git a/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/DBUtil.java b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/DBUtil.java similarity index 92% rename from students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/DBUtil.java rename to students/1049843090/ood/src/main/java/com/coderising/ood/srp/DBUtil.java index 299aa8bc46..4b8452ded2 100644 --- a/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/DBUtil.java +++ b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -1,4 +1,4 @@ -package java.com.coderising.ood.srp; +package com.coderising.ood.srp; import java.util.ArrayList; import java.util.HashMap; import java.util.List; diff --git a/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/MailUtil.java b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/MailUtil.java similarity index 93% rename from students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/MailUtil.java rename to students/1049843090/ood/src/main/java/com/coderising/ood/srp/MailUtil.java index 3c75104aeb..9e77ef1968 100644 --- a/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/MailUtil.java +++ b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -1,4 +1,4 @@ -package java.com.coderising.ood.srp; +package com.coderising.ood.srp; public class MailUtil { diff --git a/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/PromotionMail.java b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java similarity index 96% rename from students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/PromotionMail.java rename to students/1049843090/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java index 1a3d4a6c15..55c1f2558c 100644 --- a/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -1,10 +1,9 @@ -package java.com.coderising.ood.srp; +package com.coderising.ood.srp; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; -import java.io.Serializable; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -34,7 +33,7 @@ public class PromotionMail { public static void main(String[] args) throws Exception { - File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + File f = new File("E:\\git\\coding2017-Q2\\students\\1049843090\\ood\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"); boolean emailDebug = false; PromotionMail pe = new PromotionMail(f, emailDebug); diff --git a/students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/product_promotion.txt b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/product_promotion.txt similarity index 100% rename from students/1049843090/ood/src/main/java/java/com/coderising/ood/srp/product_promotion.txt rename to students/1049843090/ood/src/main/java/com/coderising/ood/srp/product_promotion.txt From f0d88f2a1d087778a09f049f73e4041e92bcee4e Mon Sep 17 00:00:00 2001 From: Xujie Date: Tue, 13 Jun 2017 17:19:22 +0800 Subject: [PATCH 065/214] =?UTF-8?q?=E5=88=9B=E5=BB=BA617314917=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 创建文件夹,并添加README.md --- students/617314917/readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/617314917/readme.md diff --git a/students/617314917/readme.md b/students/617314917/readme.md new file mode 100644 index 0000000000..d620ba5b0a --- /dev/null +++ b/students/617314917/readme.md @@ -0,0 +1 @@ +这是“广州-许洁”提交代码的qq命名文件夹。 \ No newline at end of file From 24f5e9c38be98d6d36157e595b95ca580de9c119 Mon Sep 17 00:00:00 2001 From: Horace He Date: Tue, 13 Jun 2017 18:51:26 +0800 Subject: [PATCH 066/214] first commit homework --- students/108847244/ood/ood-assignment/pom.xml | 32 +++ .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 7 files changed, 310 insertions(+) create mode 100644 students/108847244/ood/ood-assignment/pom.xml create mode 100644 students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/108847244/ood/ood-assignment/pom.xml b/students/108847244/ood/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/108847244/ood/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..781587a846 --- /dev/null +++ b/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/108847244/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From e51fcd95b99647b826623c2dfc9832cf6c8b2c7f Mon Sep 17 00:00:00 2001 From: KevinSmile Date: Tue, 13 Jun 2017 19:29:02 +0800 Subject: [PATCH 067/214] create readme --- students/279069328/readme.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 students/279069328/readme.md diff --git a/students/279069328/readme.md b/students/279069328/readme.md new file mode 100644 index 0000000000..ce14d38509 --- /dev/null +++ b/students/279069328/readme.md @@ -0,0 +1,3 @@ +# Coding 2017 + +@KevinSmile \ No newline at end of file From 6ea33cd859287af627c1c8cf61ea0cbe1e005a64 Mon Sep 17 00:00:00 2001 From: luoziyihao Date: Tue, 13 Jun 2017 21:35:13 +0800 Subject: [PATCH 068/214] =?UTF-8?q?optimize=20promotioMail=20v1=20todo=20?= =?UTF-8?q?=E5=A3=AE=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ood/srp/optimize/ProductParser.java | 8 +- .../ood/srp/optimize/PromotionMailApp.java | 15 +++- .../ood/srp/optimize/PromotionMailClaim.java | 87 ++++++++++++++++++- .../optimize/PromotionMailableBehavior.java | 29 ++++++- .../ood/srp/optimize/SmptPropeties.java | 30 ++++++- .../com/coderising/ood/srp/optimize/User.java | 16 ++++ .../ood/srp/optimize/UserService.java | 12 ++- 7 files changed, 177 insertions(+), 20 deletions(-) diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/ProductParser.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/ProductParser.java index dd3198c42d..0c1cbba2e4 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/ProductParser.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/ProductParser.java @@ -11,13 +11,7 @@ */ public class ProductParser { - private String productClassPath; - - public void setProductClassPath(String productClassPath) { - this.productClassPath = productClassPath; - } - - public List parse() { + public List parse(String productClassPath) { List stringList = readToStringList(openStream(productClassPath)); return stringList.stream() .map(String::trim) diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailApp.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailApp.java index 8b9061e7e5..ce49ae101b 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailApp.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailApp.java @@ -7,9 +7,16 @@ */ public class PromotionMailApp { - public static void main(String args[]){ - ProductParser productParser = new ProductParser(); - productParser.setProductClassPath("product_promotion.txt"); - List products = productParser.parse(); + public static void main(String args[]) { + List products = new ProductParser().parse("product_promotion.txt"); + + List users = new UserService().loadMailingList(); + + List promotionMailClaims = new PromotionMailClaim() + .load(products, users, new SmptPropeties(), true); + + new PromotionMailableBehavior().send(promotionMailClaims); + } + } diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailClaim.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailClaim.java index 06d447b817..7d2011a5df 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailClaim.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailClaim.java @@ -1,5 +1,8 @@ package com.coderising.ood.srp.optimize; +import java.util.ArrayList; +import java.util.List; + /** * Created by luoziyihao on 6/12/17. */ @@ -9,9 +12,87 @@ public class PromotionMailClaim { private String subject; private String message; private String smtpHost; - private String debug; + private String altSmtpHost; + private Boolean mailDebug; + + private PromotionMailClaim init(Product product, User user, SmptPropeties smptPropeties, Boolean mailDebug) { + this.toAddress = user.getEmail(); + this.fromAddress = smptPropeties.getFromAddress(); + this.subject = "您关注的产品降价了"; + this.message = "尊敬的 " + user.getName() + ", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!"; + this.smtpHost = smptPropeties.getSmtpHost(); + this.altSmtpHost = smptPropeties.getAltSmtpHost(); + this.mailDebug = mailDebug; + return this; + } + + public List load(List products, List users, SmptPropeties smptPropeties + , boolean mailDebug) { + List promotionMailClaims = new ArrayList<>(); + for (Product product : products) { + for (User user : users) { + PromotionMailClaim promotionMailClaim = new PromotionMailClaim() + .init(product, user, smptPropeties, true); + promotionMailClaims.add(promotionMailClaim); + } + } + return promotionMailClaims; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + + public Boolean getMailDebug() { + return mailDebug; + } + + public void setMailDebug(Boolean mailDebug) { + this.mailDebug = mailDebug; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getSmtpHost() { + return smtpHost; + } - public void setMessage(Product product, User user) { - this.message = null; + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; } + } diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailableBehavior.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailableBehavior.java index 548f69722a..39321d554a 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailableBehavior.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailableBehavior.java @@ -7,7 +7,34 @@ */ public class PromotionMailableBehavior { - void send(List emailSendClaimList) { + public void send(List emailSendClaimList) { + for (PromotionMailClaim promotionMailClaim : emailSendClaimList) { + sendEmailForOneClaim(promotionMailClaim); + } + } + + private void sendEmailForOneClaim(PromotionMailClaim promotionMailClaim) { + System.out.println("开始发送邮件"); + + try { + doSendMail(promotionMailClaim); + } catch (Exception e) { + promotionMailClaim.setSmtpHost(promotionMailClaim.getAltSmtpHost()); + try { + doSendMail(promotionMailClaim); + } catch (Exception e1) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e.getMessage()); + } + } + } + private void doSendMail(PromotionMailClaim promotionMailClaim) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(promotionMailClaim.getFromAddress()).append("\n"); + buffer.append("To:").append(promotionMailClaim.getToAddress()).append("\n"); + buffer.append("Subject:").append(promotionMailClaim.getSubject()).append("\n"); + buffer.append("Content:").append(promotionMailClaim.getMessage()).append("\n"); + System.out.println(buffer.toString()); } } diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/SmptPropeties.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/SmptPropeties.java index fd6d7be8d7..affdf946fd 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/SmptPropeties.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/SmptPropeties.java @@ -4,7 +4,31 @@ * Created by luoziyihao on 6/12/17. */ public class SmptPropeties { - private String smtpHost; - private String altSmtpHost; - private String fromAddress; + private String smtpHost = "smtp.server";; + private String altSmtpHost = "smtp1.163.com"; + private String fromAddress = " admin@company.com"; + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } } diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/User.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/User.java index 45f7739a7b..b7ec110914 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/User.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/User.java @@ -6,4 +6,20 @@ public class User { private String name; private String email; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } } diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/UserService.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/UserService.java index 5178489e0f..fe5317cc55 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/UserService.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/UserService.java @@ -1,5 +1,6 @@ package com.coderising.ood.srp.optimize; +import java.util.ArrayList; import java.util.List; /** @@ -7,7 +8,14 @@ */ public class UserService { - List loadMailingList(){ - return null; + List loadMailingList() { + List userList = new ArrayList<>(); + for (int i = 1; i <= 3; i++) { + User user = new User(); + user.setName("User" + i); + user.setEmail("aa@bb.com"); + userList.add(user); + } + return userList; } } From 93d655df3e04ebed259db9d8802cc36f26b079dc Mon Sep 17 00:00:00 2001 From: luoziyihao Date: Tue, 13 Jun 2017 21:36:23 +0800 Subject: [PATCH 069/214] com --- .../coderising/ood/srp/optimize/PromotionMailableBehavior.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailableBehavior.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailableBehavior.java index 39321d554a..e350b1f735 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailableBehavior.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailableBehavior.java @@ -19,8 +19,8 @@ private void sendEmailForOneClaim(PromotionMailClaim promotionMailClaim) { try { doSendMail(promotionMailClaim); } catch (Exception e) { - promotionMailClaim.setSmtpHost(promotionMailClaim.getAltSmtpHost()); try { + promotionMailClaim.setSmtpHost(promotionMailClaim.getAltSmtpHost()); doSendMail(promotionMailClaim); } catch (Exception e1) { System.out.println("通过备用 SMTP服务器发送邮件失败: " + e.getMessage()); From 9fae307f75dfbab4f191c4169f58a1f0acc601bf Mon Sep 17 00:00:00 2001 From: luoziyihao Date: Tue, 13 Jun 2017 21:39:00 +0800 Subject: [PATCH 070/214] com --- .../com/coderising/ood/srp/optimize/PromotionMailClaim.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailClaim.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailClaim.java index 7d2011a5df..c1bd087af7 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailClaim.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailClaim.java @@ -32,7 +32,7 @@ public List load(List products, List users, S for (Product product : products) { for (User user : users) { PromotionMailClaim promotionMailClaim = new PromotionMailClaim() - .init(product, user, smptPropeties, true); + .init(product, user, smptPropeties, mailDebug); promotionMailClaims.add(promotionMailClaim); } } From d20173c84894798cd630715afd261f14e1b7f919 Mon Sep 17 00:00:00 2001 From: luoziyihao Date: Tue, 13 Jun 2017 21:41:51 +0800 Subject: [PATCH 071/214] add comment --- .../src/main/java/com/coderising/ood/srp/optimize/Product.java | 3 +-- .../java/com/coderising/ood/srp/optimize/ProductParser.java | 1 + .../java/com/coderising/ood/srp/optimize/PromotionMailApp.java | 1 + .../com/coderising/ood/srp/optimize/PromotionMailClaim.java | 1 + .../coderising/ood/srp/optimize/PromotionMailableBehavior.java | 1 + .../java/com/coderising/ood/srp/optimize/SmptPropeties.java | 1 + .../src/main/java/com/coderising/ood/srp/optimize/User.java | 1 + .../main/java/com/coderising/ood/srp/optimize/UserService.java | 1 + 8 files changed, 8 insertions(+), 2 deletions(-) diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/Product.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/Product.java index ffb440712f..3aef295085 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/Product.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/Product.java @@ -1,8 +1,7 @@ package com.coderising.ood.srp.optimize; -import lombok.Setter; - /** + * 产品对象 * Created by luoziyihao on 6/12/17. */ diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/ProductParser.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/ProductParser.java index 0c1cbba2e4..080c999ffa 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/ProductParser.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/ProductParser.java @@ -7,6 +7,7 @@ import static com.coding.common.util.IOUtils2.readToStringList; /** + * 产品文件解析器 * Created by luoziyihao on 6/12/17. */ public class ProductParser { diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailApp.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailApp.java index ce49ae101b..6ee832307a 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailApp.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailApp.java @@ -3,6 +3,7 @@ import java.util.List; /** + * main 函数启动类 * Created by luoziyihao on 6/12/17. */ public class PromotionMailApp { diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailClaim.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailClaim.java index c1bd087af7..189b3ced17 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailClaim.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailClaim.java @@ -4,6 +4,7 @@ import java.util.List; /** + * 发发送邮件的必要参数 vo * Created by luoziyihao on 6/12/17. */ public class PromotionMailClaim { diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailableBehavior.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailableBehavior.java index e350b1f735..ad37998e45 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailableBehavior.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/PromotionMailableBehavior.java @@ -3,6 +3,7 @@ import java.util.List; /** + * 发邮件的行为类 * Created by luoziyihao on 6/12/17. */ public class PromotionMailableBehavior { diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/SmptPropeties.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/SmptPropeties.java index affdf946fd..c6664fafc3 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/SmptPropeties.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/SmptPropeties.java @@ -1,6 +1,7 @@ package com.coderising.ood.srp.optimize; /** + * 邮件服务器配置类 * Created by luoziyihao on 6/12/17. */ public class SmptPropeties { diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/User.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/User.java index b7ec110914..996efadbb6 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/User.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/User.java @@ -1,6 +1,7 @@ package com.coderising.ood.srp.optimize; /** + * 用户类 * Created by luoziyihao on 6/12/17. */ public class User { diff --git a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/UserService.java b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/UserService.java index fe5317cc55..f5da2d3908 100644 --- a/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/UserService.java +++ b/students/1204187480/code/homework/coderising/src/main/java/com/coderising/ood/srp/optimize/UserService.java @@ -4,6 +4,7 @@ import java.util.List; /** + * 用户service, 管理用户的数据 * Created by luoziyihao on 6/12/17. */ public class UserService { From ca957fc96db0c1d27032225cbfaf4bbba3bf3179 Mon Sep 17 00:00:00 2001 From: liyang34 Date: Tue, 13 Jun 2017 22:04:25 +0800 Subject: [PATCH 072/214] =?UTF-8?q?360682644=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/360682644/ood-assignment/pom.xml | 32 +++++++ .../com/coderising/ood/srp/Configuration.java | 25 ++++++ .../java/com/coderising/ood/srp/DBUtil.java | 24 +++++ .../com/coderising/ood/srp/IMailable.java | 10 +++ .../com/coderising/ood/srp/MailHosts.java | 23 +++++ .../com/coderising/ood/srp/MailReceiver.java | 38 ++++++++ .../com/coderising/ood/srp/MailSender.java | 87 +++++++++++++++++++ .../java/com/coderising/ood/srp/MailUtil.java | 21 +++++ .../java/com/coderising/ood/srp/Product.java | 28 ++++++ .../coderising/ood/srp/ReceiverService.java | 24 +++++ .../coderising/ood/srp/product_promotion.txt | 4 + 11 files changed, 316 insertions(+) create mode 100644 students/360682644/ood-assignment/pom.xml create mode 100644 students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/IMailable.java create mode 100644 students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/MailHosts.java create mode 100644 students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/MailReceiver.java create mode 100644 students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/MailSender.java create mode 100644 students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/ReceiverService.java create mode 100644 students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/360682644/ood-assignment/pom.xml b/students/360682644/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/360682644/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..e17b664d48 --- /dev/null +++ b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + public static final String EMAIL_ADMIN = "email.admin"; + private Map configurations = new HashMap(); + private static Configuration configuration = new Configuration(); + { + configurations.put(EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + return configurations.get(key); + } + + public static Configuration getInstance(){ + return configuration; + } +} diff --git a/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..e6f0ca21cb --- /dev/null +++ b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,24 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql, Product product){ + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + MailReceiver receiver = new MailReceiver(); + receiver.setName("User" + i); + receiver.setEmail("aa@bb.com"); + receiver.setMessage(product); + receiver.setSubject(product); + userList.add(receiver); + } + return userList; + } +} diff --git a/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/IMailable.java b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/IMailable.java new file mode 100644 index 0000000000..019a96394e --- /dev/null +++ b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/IMailable.java @@ -0,0 +1,10 @@ +package com.coderising.ood.srp; + +/** + * Created by 360682644 on 2017/6/13. + */ +public interface IMailable { + + String toEmailText(String... params); + String toEmailSubject(String... params); +} diff --git a/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/MailHosts.java b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/MailHosts.java new file mode 100644 index 0000000000..72a39d0601 --- /dev/null +++ b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/MailHosts.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by 360682644 on 2017/6/13. + */ +public class MailHosts { + + private List hosts = new ArrayList(); + { + hosts.add("smtp.163.com"); + hosts.add("smtp1.163.com"); + } + + public List getHosts() { + return hosts; + } + public void setHosts(List hosts) { + this.hosts = hosts; + } +} diff --git a/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/MailReceiver.java b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/MailReceiver.java new file mode 100644 index 0000000000..250f594a89 --- /dev/null +++ b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/MailReceiver.java @@ -0,0 +1,38 @@ +package com.coderising.ood.srp; + +/** + * Created by 360682644 on 2017/6/13. + */ +public class MailReceiver { + private String name; + private String email; + private String message; + private String subject; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getEmail() { + return email; + } + public void setEmail(String email) { + this.email = email; + } + public String getMessage() { + return message; + } + public void setMessage(IMailable IMailable) { + message = IMailable.toEmailText(name); + } + public void setSubject(IMailable IMailable) { + subject = IMailable.toEmailSubject(null); + } + public void setMessage(String message) { + this.message = message; + } + public String getSubject() { + return subject; + } +} diff --git a/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/MailSender.java b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/MailSender.java new file mode 100644 index 0000000000..4d22aff9c5 --- /dev/null +++ b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/MailSender.java @@ -0,0 +1,87 @@ +package com.coderising.ood.srp; + +import org.junit.Assert; + +import java.io.*; +import java.util.*; + +public class MailSender { + + protected MailHosts hosts = null; + protected String fromAddress = null; + protected InputStream is = null; + protected List receivers = null; + private Configuration config = Configuration.getInstance(); + + public void init() throws Exception { + setSMTPHost(); + setFromAddress(); + } + + protected MailSender setEmailInput(InputStream is) throws IOException { + this.is = is; + return this; + } + + protected MailSender setReceiver() throws IOException { + Assert.assertNotNull("is cannot be null", is); + List products = read(is); + receivers = new ArrayList(); + for (Product product : products) { + receivers.addAll(ReceiverService.getInstance().loadMailingList(product)); + } + return this; + } + + protected void setSMTPHost() { + hosts = new MailHosts(); + } + + protected void setFromAddress() { + fromAddress = config.getProperty(Configuration.EMAIL_ADMIN); + } + + protected List read(InputStream is) throws IOException { + BufferedReader br = null; + List products = new ArrayList(); + try { + br = new BufferedReader(new InputStreamReader(is)); + String temp; + while ((temp = br.readLine()) != null) { + String[] data = temp.split(" "); + Product product = new Product(); + product.setProductID(data[0]); + product.setProductDesc(data[1]); + products.add(product); + System.out.println("产品ID = " + product.getProductID() + "\n"); + System.out.println("产品描述 = " + product.getProductDesc() + "\n"); + } + } catch (IOException e) { + throw e; + } finally { + br.close(); + } + return products; + } + + protected void send() throws IOException { + System.out.println("开始发送邮件"); + if (receivers == null || receivers.isEmpty()) { + System.out.println("没有邮件发送"); + return; + } + for (MailReceiver receiver : receivers) { + MailUtil.sendEmail(receiver.getEmail(), fromAddress, receiver.getSubject(), receiver.getMessage(), hosts); + } + } + + public static void main(String[] args) throws Exception { + + File f = new File("D:\\coding2017\\students\\360682644\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"); + MailSender pe = new MailSender(); + pe.init(); + pe.setEmailInput(new FileInputStream(f)) + .setReceiver() + .send(); + } +} diff --git a/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..2d4545cf06 --- /dev/null +++ b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,21 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, MailHosts smtpHosts) { + for(String hosts : smtpHosts.getHosts()) { + try { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + break; + } catch (Exception e) { + System.err.println(String.format("通过SMTP服务器(%s)发送邮件失败: %s", hosts,e.getMessage())); + } + } + } +} diff --git a/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..51dc9ea460 --- /dev/null +++ b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,28 @@ +package com.coderising.ood.srp; + +/** + * Created by 360682644 on 2017/6/13. + */ +public class Product implements IMailable { + private String productID = null; + private String productDesc = null; + + public String getProductID() { + return productID; + } + public void setProductID(String productID) { + this.productID = productID; + } + public String getProductDesc() { + return productDesc; + } + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + public String toEmailText(String... params) { + return "尊敬的 "+ params[0] +", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + } + public String toEmailSubject(String... params) { + return "您关注的产品降价了"; + } +} diff --git a/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/ReceiverService.java b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/ReceiverService.java new file mode 100644 index 0000000000..f5ea863389 --- /dev/null +++ b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/ReceiverService.java @@ -0,0 +1,24 @@ +package com.coderising.ood.srp; + +import java.util.List; + +/** + * Created by 360682644 on 2017/6/13. + */ +public class ReceiverService { + + private final static ReceiverService service = new ReceiverService(); + public static ReceiverService getInstance(){return service;} + + public List loadMailingList(Product product){ + return DBUtil.query(getLoadQuery(), product); + } + + public String getLoadQuery(){ + String query = "Select name from subscriptions " + + "where product_id= ? " + + "and send_mail=1 "; + System.out.println("loadQuery set"); + return query; + } +} diff --git a/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/360682644/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 2c7ddedf6e6a85f68d6624171b6b3df93e5f2be7 Mon Sep 17 00:00:00 2001 From: EightWolf <675554906@qq.com> Date: Tue, 13 Jun 2017 22:04:55 +0800 Subject: [PATCH 073/214] =?UTF-8?q?=E4=BD=9C=E4=B8=9A=20=EF=BC=9A=E7=AC=AC?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E6=80=9D=E8=80=83=20=20=20=E8=BF=98=E6=9C=89?= =?UTF-8?q?=E4=B8=8D=E8=B6=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/lilei/com/cn/Configuration.java | 27 +++++ .../src/lilei/com/cn/ConfigurationKeys.java | 13 +++ .../675554906/src/lilei/com/cn/DBUtil.java | 29 +++++ .../src/lilei/com/cn/MailAssemble.java | 100 ++++++++++++++++++ .../675554906/src/lilei/com/cn/MailUtil.java | 60 +++++++++++ .../src/lilei/com/cn/PromotionMail.java | 29 +++++ .../675554906/src/lilei/com/cn/ReadFile.java | 33 ++++++ .../src/lilei/com/cn/product_promotion.txt | 4 + 8 files changed, 295 insertions(+) create mode 100644 students/675554906/src/lilei/com/cn/Configuration.java create mode 100644 students/675554906/src/lilei/com/cn/ConfigurationKeys.java create mode 100644 students/675554906/src/lilei/com/cn/DBUtil.java create mode 100644 students/675554906/src/lilei/com/cn/MailAssemble.java create mode 100644 students/675554906/src/lilei/com/cn/MailUtil.java create mode 100644 students/675554906/src/lilei/com/cn/PromotionMail.java create mode 100644 students/675554906/src/lilei/com/cn/ReadFile.java create mode 100644 students/675554906/src/lilei/com/cn/product_promotion.txt diff --git a/students/675554906/src/lilei/com/cn/Configuration.java b/students/675554906/src/lilei/com/cn/Configuration.java new file mode 100644 index 0000000000..c8020595fd --- /dev/null +++ b/students/675554906/src/lilei/com/cn/Configuration.java @@ -0,0 +1,27 @@ +package lilei.com.cn; +import java.util.HashMap; +import java.util.Map; +/** + * Configuration 读取/获取配置信息类 + * 唯一能引起此类变化的是配置文件的改变 + * + * */ +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/675554906/src/lilei/com/cn/ConfigurationKeys.java b/students/675554906/src/lilei/com/cn/ConfigurationKeys.java new file mode 100644 index 0000000000..ddcaeb4def --- /dev/null +++ b/students/675554906/src/lilei/com/cn/ConfigurationKeys.java @@ -0,0 +1,13 @@ +package lilei.com.cn; +/** + * ConfigurationKeys 配置信息定义类 + * 个人理解,唯一能引发变化的是“需求”比如,要增加一个KEY + * + * */ +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/675554906/src/lilei/com/cn/DBUtil.java b/students/675554906/src/lilei/com/cn/DBUtil.java new file mode 100644 index 0000000000..9ae43cc7dc --- /dev/null +++ b/students/675554906/src/lilei/com/cn/DBUtil.java @@ -0,0 +1,29 @@ +package lilei.com.cn; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + /** + * DBUtil 连接数据库,获取数据 + * 个人理解,此类只是负责数据库的连接,和数据的读取,存储,修改 + * + * */ +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/675554906/src/lilei/com/cn/MailAssemble.java b/students/675554906/src/lilei/com/cn/MailAssemble.java new file mode 100644 index 0000000000..8c17e5f3bf --- /dev/null +++ b/students/675554906/src/lilei/com/cn/MailAssemble.java @@ -0,0 +1,100 @@ +package lilei.com.cn; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +/** + * MailAssemble 组装邮件类 + * 个人理解, 此类的职责是组装邮件,而邮件是根据Configuration(配置信息类)、ReadFile(读取信息类)来组成的,这两个类也是引起变化的因素 + * + * */ +public class MailAssemble { + protected String sendMailQuery = null; + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + protected String productID = null; + protected String productDesc = null; + + private static Configuration config = new Configuration(); + private static ReadFile rf = new ReadFile();; + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + //组装信息 + public void assembleMail() throws Exception{ + String data[] = rf.readFile(); + setProductID(data[0]); + setProductDesc(data[1]); + setSMTPHost(); + setAltSMTPHost(); + setFromAddress(); + setLoadQuery(); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + String name = (String) userInfo.get(NAME_KEY); + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + } + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } +} diff --git a/students/675554906/src/lilei/com/cn/MailUtil.java b/students/675554906/src/lilei/com/cn/MailUtil.java new file mode 100644 index 0000000000..2bd129dc75 --- /dev/null +++ b/students/675554906/src/lilei/com/cn/MailUtil.java @@ -0,0 +1,60 @@ +package lilei.com.cn; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +/** + * MailUtil 邮件方法类 + * 个人理解,唯一引起变化的就是邮件发送方法的改变 + * + * */ +public class MailUtil { + + private static MailAssemble assembleMail = new MailAssemble(); + + public static void sendEmail(MailAssemble assembleMail , boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(assembleMail.fromAddress).append("\n"); + buffer.append("To:").append(assembleMail.toAddress).append("\n"); + buffer.append("Subject:").append(assembleMail.subject).append("\n"); + buffer.append("Content:").append(assembleMail.message).append("\n"); + System.out.println(buffer.toString()); + + } + + protected void sendEMails(boolean debug) throws Exception + { + assembleMail.assembleMail(); + List mailingList = assembleMail.loadMailingList(); + System.out.println("开始发送邮件"); + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + assembleMail.configureEMail((HashMap) iter.next()); + try + { + if (assembleMail.toAddress.length() > 0) + sendEmail(assembleMail, debug); + } + catch (Exception e) + { + + try { + sendEmail(assembleMail, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + } + + else { + System.out.println("没有邮件发送"); + } + } +} diff --git a/students/675554906/src/lilei/com/cn/PromotionMail.java b/students/675554906/src/lilei/com/cn/PromotionMail.java new file mode 100644 index 0000000000..7522bab6b1 --- /dev/null +++ b/students/675554906/src/lilei/com/cn/PromotionMail.java @@ -0,0 +1,29 @@ +package lilei.com.cn; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +/** + * PromotionMail 推送邮件类 + * 个人理解, 此类唯一能引起变化的就是发送邮件的方法变化 + * + * */ +public class PromotionMail { + + private static MailUtil mailUtil; + public static void main(String[] args) throws Exception { + + boolean emailDebug = false; + PromotionMail pe = new PromotionMail(emailDebug); + } + + public PromotionMail(boolean mailDebug) throws Exception { + mailUtil = new MailUtil(); + mailUtil.sendEMails(mailDebug); + } +} diff --git a/students/675554906/src/lilei/com/cn/ReadFile.java b/students/675554906/src/lilei/com/cn/ReadFile.java new file mode 100644 index 0000000000..e13a9d644e --- /dev/null +++ b/students/675554906/src/lilei/com/cn/ReadFile.java @@ -0,0 +1,33 @@ +package lilei.com.cn; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +/** + * ReadFile 读取文件类 + * 个人理解, 唯一能引发变化的就是文件路径的变动 + * + * */ +public class ReadFile { + + private File file; + private String filePath = "F:\\product_promotion.txt"; + + protected String[] readFile() throws IOException // @02C + { + BufferedReader br = null; + try { + file = new File(filePath); + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + return data; + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } +} diff --git a/students/675554906/src/lilei/com/cn/product_promotion.txt b/students/675554906/src/lilei/com/cn/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/675554906/src/lilei/com/cn/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From bb922a12e63663fc1e18a53027cf3b3af550d4d4 Mon Sep 17 00:00:00 2001 From: Arthur <465034663@qq.com> Date: Tue, 13 Jun 2017 22:09:35 +0800 Subject: [PATCH 074/214] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/465034663/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/465034663/README.md diff --git a/students/465034663/README.md b/students/465034663/README.md new file mode 100644 index 0000000000..773f79cede --- /dev/null +++ b/students/465034663/README.md @@ -0,0 +1 @@ +OOD面向对象 \ No newline at end of file From a4a1a1671bc210408582bfb5b4175e7350ef589b Mon Sep 17 00:00:00 2001 From: jiangzhimin Date: Tue, 13 Jun 2017 22:59:41 +0800 Subject: [PATCH 075/214] the first time commit --- students/630727874/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/630727874/README.md diff --git a/students/630727874/README.md b/students/630727874/README.md new file mode 100644 index 0000000000..fbfe6df257 --- /dev/null +++ b/students/630727874/README.md @@ -0,0 +1 @@ +This is my first git commit From bf1d250c989711a8ae240a779a4726645dd9a8b5 Mon Sep 17 00:00:00 2001 From: jimmy Date: Tue, 13 Jun 2017 23:07:56 +0800 Subject: [PATCH 076/214] Revert "the first time commit" This reverts commit a4a1a1671bc210408582bfb5b4175e7350ef589b. --- students/630727874/README.md | 1 - 1 file changed, 1 deletion(-) delete mode 100644 students/630727874/README.md diff --git a/students/630727874/README.md b/students/630727874/README.md deleted file mode 100644 index fbfe6df257..0000000000 --- a/students/630727874/README.md +++ /dev/null @@ -1 +0,0 @@ -This is my first git commit From a35a5f9356ec0654ef4d351d4218d19832c1e56a Mon Sep 17 00:00:00 2001 From: jimmy Date: Tue, 13 Jun 2017 23:19:17 +0800 Subject: [PATCH 077/214] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E8=87=AA=E5=B7=B1?= =?UTF-8?q?=E7=9A=84=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/63072784/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/63072784/README.md diff --git a/students/63072784/README.md b/students/63072784/README.md new file mode 100644 index 0000000000..f5e57e7b78 --- /dev/null +++ b/students/63072784/README.md @@ -0,0 +1 @@ +这是我的目录 From 3f0e0a912f0a9237784e17ec566390967d78863e Mon Sep 17 00:00:00 2001 From: jyp Date: Tue, 13 Jun 2017 23:47:05 +0800 Subject: [PATCH 078/214] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/coderising/ood/srp/{ => config}/Configuration.java | 0 .../com/coderising/ood/srp/{ => config}/ConfigurationKeys.java | 0 .../src/main/java/com/coderising/ood/srp/{ => util}/DBUtil.java | 0 .../src/main/java/com/coderising/ood/srp/{ => util}/MailUtil.java | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/{ => config}/Configuration.java (100%) rename students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/{ => config}/ConfigurationKeys.java (100%) rename students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/{ => util}/DBUtil.java (100%) rename students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/{ => util}/MailUtil.java (100%) diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/Configuration.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/Configuration.java similarity index 100% rename from students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/Configuration.java rename to students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/Configuration.java diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java similarity index 100% rename from students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java rename to students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/util/DBUtil.java similarity index 100% rename from students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/DBUtil.java rename to students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/util/DBUtil.java diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/util/MailUtil.java similarity index 100% rename from students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/MailUtil.java rename to students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/util/MailUtil.java From 9ba07844b62008a74ee078631541be73e79cf15c Mon Sep 17 00:00:00 2001 From: zhanghaojie Date: Wed, 14 Jun 2017 00:08:46 +0800 Subject: [PATCH 079/214] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E5=91=A8=20=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E4=BF=83=E9=94=80=E9=82=AE=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/550727632/pom.xml | 37 ++++++++++++ .../com/coderising/ood/srp/Configuration.java | 23 ++++++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 +++ .../java/com/coderising/ood/srp/DBUtil.java | 31 ++++++++++ .../java/com/coderising/ood/srp/FileUtil.java | 28 ++++++++++ .../java/com/coderising/ood/srp/MailUtil.java | 56 +++++++++++++++++++ .../java/com/coderising/ood/srp/Product.java | 29 ++++++++++ .../com/coderising/ood/srp/PromotionMail.java | 37 ++++++++++++ .../java/com/coderising/ood/srp/User.java | 34 +++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 ++ 10 files changed, 288 insertions(+) create mode 100644 students/550727632/pom.xml create mode 100644 students/550727632/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/550727632/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/550727632/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/550727632/src/main/java/com/coderising/ood/srp/FileUtil.java create mode 100644 students/550727632/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/550727632/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/550727632/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/550727632/src/main/java/com/coderising/ood/srp/User.java create mode 100644 students/550727632/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/550727632/pom.xml b/students/550727632/pom.xml new file mode 100644 index 0000000000..3483fcca18 --- /dev/null +++ b/students/550727632/pom.xml @@ -0,0 +1,37 @@ + + 4.0.0 + com.codering.ood + coderising + 0.0.1-SNAPSHOT + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + org.apache.commons + commons-lang3 + 3.4 + + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + \ No newline at end of file diff --git a/students/550727632/src/main/java/com/coderising/ood/srp/Configuration.java b/students/550727632/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/550727632/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/550727632/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/550727632/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/550727632/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/550727632/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/550727632/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..3b73afe420 --- /dev/null +++ b/students/550727632/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,31 @@ +package com.coderising.ood.srp; + +import java.util.ArrayList; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * + * @param sql + * @return + */ + public static List query(String sql) { + + List userList = new ArrayList<>(); + for (int i = 1; i <= 3; i++) { + User user = new User("User" + i, "aa@bb.com"); + userList.add(user); + } + + return userList; + } + + public static List loadProductUsers(Product product){ + String sql = "Select name from subscriptions " + + "where product_id= '" + product.getProductID() +"' " + + "and send_mail=1 "; + return query(sql); + } +} diff --git a/students/550727632/src/main/java/com/coderising/ood/srp/FileUtil.java b/students/550727632/src/main/java/com/coderising/ood/srp/FileUtil.java new file mode 100644 index 0000000000..0054feb8e0 --- /dev/null +++ b/students/550727632/src/main/java/com/coderising/ood/srp/FileUtil.java @@ -0,0 +1,28 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class FileUtil { + + public static Product readProductFile(File file) throws IOException { + BufferedReader br = null; + Product product = new Product(); + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + product.setProductID(data[0]); + product.setProductDesc(data[1]); + System.out.println(product.toString()); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + return product; + } +} diff --git a/students/550727632/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/550727632/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..a61ab7e52c --- /dev/null +++ b/students/550727632/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,56 @@ +package com.coderising.ood.srp; + +import java.util.List; + +import org.apache.commons.lang3.StringUtils; + +public class MailUtil { + + private static String fromAddress; + private static String smtpHost; + private static String altSmtpHost; + + static { + Configuration config = new Configuration(); + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + } + + public static void sendPromotionEmail(PromotionMail mail, Product product, boolean debug) { + System.out.println("开始发送邮件"); + List userList = DBUtil.loadProductUsers(product); + if(userList != null && !userList.isEmpty()){ + for (User user : userList) { + mail.setMessage(user, product); + if (StringUtils.isNotEmpty(user.getEmail())){ + sendEmail(mail.getSubject(), mail.getMessage(), user.getEmail(), debug); + } + } + } else { + System.out.println("没有需要发送的邮件"); + } + } + + public static void sendEmail(String subject, String message,String toAddress,boolean debug) { + // 假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + // 先用smtpHost发送 + // 如果失败用altSmtpHost发送 + // 如果还失败,则记录日志 + try { + buffer.append("smtpHost:").append(smtpHost).append("\n"); + } catch (Exception e) { + try { + buffer.append("smtpHost:").append(altSmtpHost).append("\n"); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + System.out.println(buffer.toString()); + } +} diff --git a/students/550727632/src/main/java/com/coderising/ood/srp/Product.java b/students/550727632/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..fd107fa4f4 --- /dev/null +++ b/students/550727632/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,29 @@ +package com.coderising.ood.srp; + +public class Product { + + private String productID; + private String productDesc; + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + + @Override + public String toString() { + return "Product [productID=" + productID + ", productDesc=" + productDesc + "]"; + } + +} diff --git a/students/550727632/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/550727632/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..23d48b0ae1 --- /dev/null +++ b/students/550727632/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,37 @@ +package com.coderising.ood.srp; + +import java.io.File; + +public class PromotionMail { + private String subject; + private String message; + + public PromotionMail() { + subject = "您关注的产品降价了"; + } + + public static void main(String[] args) throws Exception { + File f = new File("src/main/java/com/coderising/ood/srp/product_promotion.txt"); + boolean emailDebug = false; + Product product = FileUtil.readProductFile(f); + PromotionMail mail = new PromotionMail(); + MailUtil.sendPromotionEmail(mail, product, emailDebug); + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public void setMessage(User user, Product product) { + message = "尊敬的 " + user.getName() + ", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!"; + } + + public String getMessage() { + return message; + } + +} diff --git a/students/550727632/src/main/java/com/coderising/ood/srp/User.java b/students/550727632/src/main/java/com/coderising/ood/srp/User.java new file mode 100644 index 0000000000..b63f655e7c --- /dev/null +++ b/students/550727632/src/main/java/com/coderising/ood/srp/User.java @@ -0,0 +1,34 @@ +package com.coderising.ood.srp; + +public class User { + + private String name; + private String email; + + public User() { + super(); + } + + public User(String name, String email) { + super(); + this.name = name; + this.email = email; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + +} diff --git a/students/550727632/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/550727632/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/550727632/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 51be40786b1f53a2e0cff22a15620d02d29ff19d Mon Sep 17 00:00:00 2001 From: Star <294022181@qq.com> Date: Wed, 14 Jun 2017 00:15:47 +0800 Subject: [PATCH 080/214] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BF=83=E9=94=80?= =?UTF-8?q?=E9=82=AE=E4=BB=B6=E5=8F=91=E9=80=81=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ood-assignment/product_promotion.txt | 4 + .../com/coderising/ood/srp/Configuration.java | 23 +++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 ++ .../src/com/coderising/ood/srp/DBUtil.java | 25 ++++++ .../src/com/coderising/ood/srp/Email.java | 85 +++++++++++++++++++ .../src/com/coderising/ood/srp/MailUtil.java | 18 ++++ .../src/com/coderising/ood/srp/Product.java | 23 +++++ .../com/coderising/ood/srp/ProductDao.java | 36 ++++++++ .../com/coderising/ood/srp/PromotionMail.java | 70 +++++++++++++++ .../coderising/ood/srp/SubscriptionDao.java | 15 ++++ .../coderising/ood/srp/product_promotion.txt | 4 + 11 files changed, 312 insertions(+) create mode 100644 students/294022181/ood-assignment/product_promotion.txt create mode 100644 students/294022181/ood-assignment/src/com/coderising/ood/srp/Configuration.java create mode 100644 students/294022181/ood-assignment/src/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/294022181/ood-assignment/src/com/coderising/ood/srp/DBUtil.java create mode 100644 students/294022181/ood-assignment/src/com/coderising/ood/srp/Email.java create mode 100644 students/294022181/ood-assignment/src/com/coderising/ood/srp/MailUtil.java create mode 100644 students/294022181/ood-assignment/src/com/coderising/ood/srp/Product.java create mode 100644 students/294022181/ood-assignment/src/com/coderising/ood/srp/ProductDao.java create mode 100644 students/294022181/ood-assignment/src/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/294022181/ood-assignment/src/com/coderising/ood/srp/SubscriptionDao.java create mode 100644 students/294022181/ood-assignment/src/com/coderising/ood/srp/product_promotion.txt diff --git a/students/294022181/ood-assignment/product_promotion.txt b/students/294022181/ood-assignment/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/294022181/ood-assignment/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/294022181/ood-assignment/src/com/coderising/ood/srp/Configuration.java b/students/294022181/ood-assignment/src/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/294022181/ood-assignment/src/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/294022181/ood-assignment/src/com/coderising/ood/srp/ConfigurationKeys.java b/students/294022181/ood-assignment/src/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/294022181/ood-assignment/src/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/294022181/ood-assignment/src/com/coderising/ood/srp/DBUtil.java b/students/294022181/ood-assignment/src/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/294022181/ood-assignment/src/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/294022181/ood-assignment/src/com/coderising/ood/srp/Email.java b/students/294022181/ood-assignment/src/com/coderising/ood/srp/Email.java new file mode 100644 index 0000000000..956cb57493 --- /dev/null +++ b/students/294022181/ood-assignment/src/com/coderising/ood/srp/Email.java @@ -0,0 +1,85 @@ +package com.coderising.ood.srp; + +public class Email { + private String smtpHost; + private String altSmtpHost; + private String fromAddress; + private String toAddress; + private String subject; + private String message; + private boolean debug; + + public Email() { + + } + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isDebug() { + return debug; + } + + public void setDebug(boolean debug) { + this.debug = debug; + } + + public void sendToTarget() { + try { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } catch (Exception e) { + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } +} diff --git a/students/294022181/ood-assignment/src/com/coderising/ood/srp/MailUtil.java b/students/294022181/ood-assignment/src/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/294022181/ood-assignment/src/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/294022181/ood-assignment/src/com/coderising/ood/srp/Product.java b/students/294022181/ood-assignment/src/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..fbb4f29db3 --- /dev/null +++ b/students/294022181/ood-assignment/src/com/coderising/ood/srp/Product.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; + +public class Product { + private String productID; + private String productDesc; + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + +} diff --git a/students/294022181/ood-assignment/src/com/coderising/ood/srp/ProductDao.java b/students/294022181/ood-assignment/src/com/coderising/ood/srp/ProductDao.java new file mode 100644 index 0000000000..ad5c53356e --- /dev/null +++ b/students/294022181/ood-assignment/src/com/coderising/ood/srp/ProductDao.java @@ -0,0 +1,36 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class ProductDao { + + public Product getProduct(String productFileName) throws IOException { + BufferedReader br = null; + Product product = null; + + try { + File file = new File(productFileName); + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + String productID = data[0]; + String productDesc = data[1]; + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + product = new Product(); + product.setProductID(productID); + product.setProductDesc(productDesc); + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + + return product; + } +} diff --git a/students/294022181/ood-assignment/src/com/coderising/ood/srp/PromotionMail.java b/students/294022181/ood-assignment/src/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..1cda5d5859 --- /dev/null +++ b/students/294022181/ood-assignment/src/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,70 @@ +package com.coderising.ood.srp; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + private Configuration config; + private ProductDao productDao; + private SubscriptionDao subscriptionDao; + + public static void main(String[] args) throws Exception { + boolean emailDebug = false; + String productFileName = "./product_promotion.txt"; + PromotionMail pe = new PromotionMail(productFileName, emailDebug); + } + + public PromotionMail(String productFileName, boolean mailDebug) throws Exception { + config = new Configuration(); + productDao = new ProductDao(); + subscriptionDao = new SubscriptionDao(); + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + Product product = productDao.getProduct(productFileName); + + if (product == null) { + return; + } + + sendEMails(mailDebug, product, subscriptionDao.loadMailingList(product.getProductID())); + } + + protected void sendEMails(boolean debug, Product product, List mailingList) throws IOException { + + System.out.println("开始发送邮件"); + + if (mailingList != null) { + Email email = new Email(); + String smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + String altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + String fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + Iterator iter = mailingList.iterator(); + + while (iter.hasNext()) { + HashMap userInfo = (HashMap) iter.next(); + String toAddress = (String) userInfo.get(EMAIL_KEY); + + if (toAddress.length() > 0) { + String name = (String) userInfo.get(NAME_KEY); + String subject = "您关注的产品降价了"; + String message = "尊敬的 " + name + ", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!" ; + email.setFromAddress(fromAddress); + email.setToAddress(toAddress); + email.setSmtpHost(smtpHost); + email.setAltSmtpHost(altSmtpHost); + email.setSubject(subject); + email.setMessage(message); + email.setDebug(debug); + email.sendToTarget(); + } + } + } else { + System.out.println("没有邮件发送"); + } + + } +} diff --git a/students/294022181/ood-assignment/src/com/coderising/ood/srp/SubscriptionDao.java b/students/294022181/ood-assignment/src/com/coderising/ood/srp/SubscriptionDao.java new file mode 100644 index 0000000000..2cd57ed644 --- /dev/null +++ b/students/294022181/ood-assignment/src/com/coderising/ood/srp/SubscriptionDao.java @@ -0,0 +1,15 @@ +package com.coderising.ood.srp; + +import java.util.List; + +public class SubscriptionDao { + + public List loadMailingList(String productID) throws Exception { + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + System.out.println("loadQuery set"); + return DBUtil.query(sendMailQuery); + } +} diff --git a/students/294022181/ood-assignment/src/com/coderising/ood/srp/product_promotion.txt b/students/294022181/ood-assignment/src/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/294022181/ood-assignment/src/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From c3bfe5112c9dee7dbb803c272026c333dc8b7e5d Mon Sep 17 00:00:00 2001 From: MAC Date: Wed, 14 Jun 2017 00:15:58 +0800 Subject: [PATCH 081/214] srp first commit --- students/89460886/ood/srp/Configuration.java | 25 ++++++++ .../89460886/ood/srp/ConfigurationKeys.java | 9 +++ students/89460886/ood/srp/DBUtil.java | 43 ++++++++++++++ students/89460886/ood/srp/IRequest.java | 16 ++++++ students/89460886/ood/srp/MailRequest.java | 57 +++++++++++++++++++ students/89460886/ood/srp/Product.java | 26 +++++++++ .../89460886/ood/srp/ProductRepository.java | 38 +++++++++++++ students/89460886/ood/srp/PromotionMail.java | 42 ++++++++++++++ students/89460886/ood/srp/SmtpClient.java | 45 +++++++++++++++ students/89460886/ood/srp/User.java | 26 +++++++++ .../89460886/ood/srp/product_promotion.txt | 4 ++ 11 files changed, 331 insertions(+) create mode 100644 students/89460886/ood/srp/Configuration.java create mode 100644 students/89460886/ood/srp/ConfigurationKeys.java create mode 100644 students/89460886/ood/srp/DBUtil.java create mode 100644 students/89460886/ood/srp/IRequest.java create mode 100644 students/89460886/ood/srp/MailRequest.java create mode 100644 students/89460886/ood/srp/Product.java create mode 100644 students/89460886/ood/srp/ProductRepository.java create mode 100644 students/89460886/ood/srp/PromotionMail.java create mode 100644 students/89460886/ood/srp/SmtpClient.java create mode 100644 students/89460886/ood/srp/User.java create mode 100644 students/89460886/ood/srp/product_promotion.txt diff --git a/students/89460886/ood/srp/Configuration.java b/students/89460886/ood/srp/Configuration.java new file mode 100644 index 0000000000..20cb685583 --- /dev/null +++ b/students/89460886/ood/srp/Configuration.java @@ -0,0 +1,25 @@ +package ood.srp; + +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + return configurations.get(key); + } + + +} diff --git a/students/89460886/ood/srp/ConfigurationKeys.java b/students/89460886/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..47cf4153b3 --- /dev/null +++ b/students/89460886/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/89460886/ood/srp/DBUtil.java b/students/89460886/ood/srp/DBUtil.java new file mode 100644 index 0000000000..89dbacc9bf --- /dev/null +++ b/students/89460886/ood/srp/DBUtil.java @@ -0,0 +1,43 @@ +package ood.srp; + +import java.util.ArrayList; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + List userList = new ArrayList<>(); + for (int i = 1; i <= 3; i++) { + User user = new User(); + user.setName("User" + i); + user.setEmail("aa@bb.com"); + userList.add(user); + } + return userList; + } + + public static List queryValidUserList(String sql) { + List userList = query(sql); + List resultList = new ArrayList<>(); + for (int i = 0, len = userList.size(); i < len; i++) { + String email = userList.get(i).getEmail(); + if (email != null && email.length() > 0) { + resultList.add(userList.get(i)); + } + } + return resultList; + } + + public static List queryUserListByProduct(Product product) { + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + product.getProductID() + "' " + + "and send_mail=1 "; + return queryValidUserList(sendMailQuery); + } + +} diff --git a/students/89460886/ood/srp/IRequest.java b/students/89460886/ood/srp/IRequest.java new file mode 100644 index 0000000000..5d15088f60 --- /dev/null +++ b/students/89460886/ood/srp/IRequest.java @@ -0,0 +1,16 @@ +package ood.srp; + +import java.util.Map; + +/** + * @author jiaxun + */ +public interface IRequest { + + Map getHeaders(); + + Map getParams(); + + String getUrl(); + +} diff --git a/students/89460886/ood/srp/MailRequest.java b/students/89460886/ood/srp/MailRequest.java new file mode 100644 index 0000000000..62b9c6ed52 --- /dev/null +++ b/students/89460886/ood/srp/MailRequest.java @@ -0,0 +1,57 @@ +package ood.srp; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author jiaxun + */ +public class MailRequest implements IRequest { + + private String toAddress; + private String subject; + private String message; + + @Override + public Map getHeaders() { + return null; + } + + @Override + public Map getParams() { + Map map = new HashMap<>(); + map.put("toAddress", toAddress); + map.put("subject", subject); + map.put("message", message); + return map; + } + + @Override + public String getUrl() { + return null; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } +} diff --git a/students/89460886/ood/srp/Product.java b/students/89460886/ood/srp/Product.java new file mode 100644 index 0000000000..f92235f0a7 --- /dev/null +++ b/students/89460886/ood/srp/Product.java @@ -0,0 +1,26 @@ +package ood.srp; + +/** + * @author jiaxun + */ +public class Product { + + private String productID = null; + private String productDesc = null; + + public String getProductID() { + return productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } +} diff --git a/students/89460886/ood/srp/ProductRepository.java b/students/89460886/ood/srp/ProductRepository.java new file mode 100644 index 0000000000..0c987504ac --- /dev/null +++ b/students/89460886/ood/srp/ProductRepository.java @@ -0,0 +1,38 @@ +package ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +/** + * @author jiaxun + */ +public class ProductRepository { + + public static Product getProductFromFile(File file) throws IOException { + Product product = null; + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String line = br.readLine(); + String[] data = line.split(" "); + + product = new Product(); + product.setProductID(data[0]); + product.setProductDesc(data[1]); + + System.out.println("产品ID = " + product.getProductID() + "\n"); + System.out.println("产品描述 = " + product.getProductDesc() + "\n"); + + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (br != null) { + br.close(); + } + } + return product; + } + +} diff --git a/students/89460886/ood/srp/PromotionMail.java b/students/89460886/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..199a80a7cc --- /dev/null +++ b/students/89460886/ood/srp/PromotionMail.java @@ -0,0 +1,42 @@ +package ood.srp; + +import java.io.File; +import java.util.List; + +public class PromotionMail { + + public static void main(String[] args) throws Exception { + + File file = new File("/Users/jiaxun/OpenSource/Algorithm/src/ood/srp/product_promotion.txt"); + boolean emailDebug = false; + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + Product product = ProductRepository.getProductFromFile(file); + + List userList = DBUtil.queryUserListByProduct(product); + + PromotionMail pe = new PromotionMail(); + + pe.sendEMails(emailDebug, userList, product); + } + + protected void sendEMails(boolean debug, List userList, Product product) { + + System.out.println("开始发送邮件"); + + if (userList != null) { + for (int i = 0, len = userList.size(); i < len; i++) { + MailRequest mailRequest = new MailRequest(); + + mailRequest.setSubject("您关注的产品降价了"); + mailRequest.setMessage("尊敬的 " + userList.get(i).getName() + ", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!"); + mailRequest.setToAddress(userList.get(i).getEmail()); + + SmtpClient.sharedInstance().sendEmail(mailRequest, debug); + } + } else { + System.out.println("没有邮件发送"); + } + } + +} diff --git a/students/89460886/ood/srp/SmtpClient.java b/students/89460886/ood/srp/SmtpClient.java new file mode 100644 index 0000000000..a0c52b6fc3 --- /dev/null +++ b/students/89460886/ood/srp/SmtpClient.java @@ -0,0 +1,45 @@ +package ood.srp; + +import java.util.Map; + +/** + * @author jiaxun + */ +public class SmtpClient { + + private static volatile SmtpClient instance = null; + + private String smtpHost = null; + private String altSmtpHost = null; + private String fromAddress = null; + + private SmtpClient() { + Configuration config = new Configuration(); + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + public static SmtpClient sharedInstance() { + if (instance == null) { + synchronized (SmtpClient.class) { + if (instance == null) { + return new SmtpClient(); + } + } + } + return instance; + } + + public void sendEmail(IRequest request, boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + Map params = request.getParams(); + // 这里为了演示方便,直接根据请求的 key 获取内容 + buffer.append("To:").append(params.get("toAddress")).append("\n"); + buffer.append("Subject:").append(params.get("subject")).append("\n"); + buffer.append("Content:").append(params.get("message")).append("\n"); + System.out.println(buffer.toString()); + } +} diff --git a/students/89460886/ood/srp/User.java b/students/89460886/ood/srp/User.java new file mode 100644 index 0000000000..71bc2b7702 --- /dev/null +++ b/students/89460886/ood/srp/User.java @@ -0,0 +1,26 @@ +package ood.srp; + +/** + * @author jiaxun + */ +public class User { + + private String name; + private String email; + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/students/89460886/ood/srp/product_promotion.txt b/students/89460886/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/89460886/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 30695118965726d5000c29c3fbd86281382f1ed6 Mon Sep 17 00:00:00 2001 From: jyp Date: Wed, 14 Jun 2017 00:32:51 +0800 Subject: [PATCH 082/214] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coderising/ood/srp/PromotionMail.java | 189 ++++++------------ .../ood/srp/config/Configuration.java | 3 +- .../ood/srp/config/ConfigurationKeys.java | 2 +- .../ood/srp/config/ConnectionConfig.java | 37 ++++ .../com/coderising/ood/srp/model/Mail.java | 48 +++++ .../com/coderising/ood/srp/model/Product.java | 38 ++++ .../ood/srp/model/Subscriptions.java | 48 +++++ .../ood/srp/service/ProductService.java | 17 ++ .../ood/srp/service/SubscriptionsService.java | 11 + .../srp/service/impl/ProductServiceImpl.java | 40 ++++ .../impl/SubscriptionsServiceImpl.java | 40 ++++ .../com/coderising/ood/srp/util/DBUtil.java | 2 +- .../com/coderising/ood/srp/util/MailUtil.java | 2 +- .../coderising/ood/srp/PromotionMailTest.java | 34 ++++ 14 files changed, 375 insertions(+), 136 deletions(-) create mode 100644 students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConnectionConfig.java create mode 100644 students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Mail.java create mode 100644 students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Product.java create mode 100644 students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Subscriptions.java create mode 100644 students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/ProductService.java create mode 100644 students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/SubscriptionsService.java create mode 100644 students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/impl/ProductServiceImpl.java create mode 100644 students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/impl/SubscriptionsServiceImpl.java create mode 100644 students/992331664/ood/ood/src/test/java/com/coderising/ood/srp/PromotionMailTest.java diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java index d98b32e7fc..cd31beac90 100644 --- a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -1,162 +1,89 @@ package com.coderising.ood.srp; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.Serializable; -import java.util.HashMap; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; -public class PromotionMail { - - protected String sendMailQuery = null; - - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; - protected String toAddress = null; - protected String subject = null; - protected String message = null; - - protected String productID = null; - protected String productDesc = null; - - private static Configuration config; - - private static final String NAME_KEY = "NAME"; - private static final String EMAIL_KEY = "EMAIL"; - - public static void main(String[] args) throws Exception { - - File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); - boolean emailDebug = false; - - PromotionMail pe = new PromotionMail(f, emailDebug); - - } - - public PromotionMail(File file, boolean mailDebug) throws Exception { - - // 读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - readFile(file); - - config = new Configuration(); - - setSMTPHost(); - setAltSMTPHost(); - - setFromAddress(); - - setLoadQuery(); - - sendEMails(mailDebug, loadMailingList()); - - } - - protected void setProductID(String productID) { - this.productID = productID; - - } - - protected String getproductID() { - return productID; - } - - protected void setLoadQuery() throws Exception { - - sendMailQuery = "Select name from subscriptions " + "where product_id= '" + productID + "' " - + "and send_mail=1 "; +import com.coderising.ood.srp.config.Configuration; +import com.coderising.ood.srp.config.ConnectionConfig; +import com.coderising.ood.srp.model.Mail; +import com.coderising.ood.srp.model.Product; +import com.coderising.ood.srp.model.Subscriptions; +import com.coderising.ood.srp.service.ProductService; +import com.coderising.ood.srp.service.SubscriptionsService; +import com.coderising.ood.srp.util.MailUtil; - System.out.println("loadQuery set"); - } +public class PromotionMail { - protected void setSMTPHost() { - smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); - } + protected SubscriptionsService subscriptionsService; - protected void setAltSMTPHost() { - altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + protected ProductService productService; + public PromotionMail(SubscriptionsService subscriptionsService, ProductService productService) { + this.subscriptionsService = subscriptionsService; + this.productService = productService; } - protected void setFromAddress() { - fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); - } + /** + * 发送促销邮件 + * @param file 促销产品文件 + * @param mailDebug + * @throws Exception + */ + public void sendPromotionMail(File file, boolean mailDebug) throws Exception { - protected void setMessage(HashMap userInfo) throws IOException { + // 得到促销的产品 + List products = productService.doFindPromotionalProducts(file); - String name = (String) userInfo.get(NAME_KEY); + // 得到促销产品的订阅信息 + List subscriptions = subscriptionsService.doFindByProducts(products); - subject = "您关注的产品降价了"; - message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + // 得到订阅人的邮箱和名称,邮箱内容 + List mails = getMails(subscriptions); + // 发送邮箱 + sendEMails(new ConnectionConfig(new Configuration()), mails, mailDebug); } - protected void readFile(File file) throws IOException // @02C - { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - setProductID(data[0]); - setProductDesc(data[1]); + protected List getMails(List subscriptions) { - System.out.println("产品ID = " + productID + "\n"); - System.out.println("产品描述 = " + productDesc + "\n"); + List mails = new ArrayList(); + String subject = "您关注的产品降价了"; - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); + for (Subscriptions sub : subscriptions) { + String productDesc = sub.getProduct().getProductDesc(); + String message = "尊敬的 " + sub.getName() + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + mails.add(new Mail(subject, message, sub.getEmail())); } + return mails; } - private void setProductDesc(String desc) { - this.productDesc = desc; - } - - protected void configureEMail(HashMap userInfo) throws IOException { - toAddress = (String) userInfo.get(EMAIL_KEY); - if (toAddress.length() > 0) - setMessage(userInfo); - } - - protected List loadMailingList() throws Exception { - return DBUtil.query(this.sendMailQuery); - } - - protected void sendEMails(boolean debug, List mailingList) throws IOException { - + protected void sendEMails(ConnectionConfig config, List mails, boolean debug) { + if (mails == null) { + System.out.println("没有邮件需要发送"); + return; + } System.out.println("开始发送邮件"); - - if (mailingList != null) { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) { - configureEMail((HashMap) iter.next()); + Iterator iter = mails.iterator(); + while (iter.hasNext()) { + Mail mail = iter.next(); + if (mail.getToAddress().length() <= 0) { + continue; + } + try { + MailUtil.sendEmail(mail.getToAddress(), config.getFromAddress(), mail.getSubject(), mail.getMessage(), + config.getSmtpHost(), debug); + } catch (Exception e) { try { - if (toAddress.length() > 0) - MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } catch (Exception e) { + MailUtil.sendEmail(mail.getToAddress(), config.getFromAddress(), mail.getSubject(), + mail.getMessage(), config.getAltSmtpHost(), debug); - try { - MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); } } - - } - - else { - System.out.println("没有邮件发送"); - } - + System.out.println("发送邮件结束"); } } diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/Configuration.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/Configuration.java index f328c1816a..bbed122807 100644 --- a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/Configuration.java +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/Configuration.java @@ -1,4 +1,4 @@ -package com.coderising.ood.srp; +package com.coderising.ood.srp.config; import java.util.HashMap; import java.util.Map; @@ -16,7 +16,6 @@ public class Configuration { * @return */ public String getProperty(String key) { - return configurations.get(key); } diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java index 8695aed644..7fe226d1bd 100644 --- a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java @@ -1,4 +1,4 @@ -package com.coderising.ood.srp; +package com.coderising.ood.srp.config; public class ConfigurationKeys { diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConnectionConfig.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConnectionConfig.java new file mode 100644 index 0000000000..be33ad6ed2 --- /dev/null +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConnectionConfig.java @@ -0,0 +1,37 @@ +package com.coderising.ood.srp.config; + +public class ConnectionConfig { + private String smtpHost; + private String altSmtpHost; + private String fromAddress; + + public ConnectionConfig(Configuration config) { + this.smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + this.altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + this.fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } +} diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Mail.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Mail.java new file mode 100644 index 0000000000..f0b72d7759 --- /dev/null +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Mail.java @@ -0,0 +1,48 @@ +package com.coderising.ood.srp.model; + +public class Mail { + private String subject; + private String message; + private String toAddress; + + public Mail() { + super(); + } + + public Mail(String subject, String message, String toAddress) { + super(); + this.subject = subject; + this.message = message; + this.toAddress = toAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + @Override + public String toString() { + return "Mail [subject=" + subject + ", message=" + message + ", toAddress=" + toAddress + "]"; + } + +} diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Product.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Product.java new file mode 100644 index 0000000000..fbcbcf9a89 --- /dev/null +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Product.java @@ -0,0 +1,38 @@ +package com.coderising.ood.srp.model; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class Product { + + private String productID; + + private String productDesc; + + public Product() { + } + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + + @Override + public String toString() { + return "Product [productID=" + productID + ", productDesc=" + productDesc + "]"; + } + +} diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Subscriptions.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Subscriptions.java new file mode 100644 index 0000000000..8ac1def689 --- /dev/null +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Subscriptions.java @@ -0,0 +1,48 @@ +package com.coderising.ood.srp.model; + +public class Subscriptions { + + private String name; + private String email; + private String productId; + private Product product; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getProductId() { + return productId; + } + + public void setProductId(String productId) { + this.productId = productId; + } + + public Product getProduct() { + return product; + } + + public void setProduct(Product product) { + this.product = product; + } + + @Override + public String toString() { + return "Subscriptions [name=" + name + ", email=" + email + ", productId=" + productId + ", product=" + product + + "]"; + } + +} diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/ProductService.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/ProductService.java new file mode 100644 index 0000000000..23f5ffb7e3 --- /dev/null +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/ProductService.java @@ -0,0 +1,17 @@ +package com.coderising.ood.srp.service; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +import com.coderising.ood.srp.model.Product; + +public interface ProductService { + + /** + * 查询促销产品 + * @return + * @throws IOException + */ + List doFindPromotionalProducts(File file) throws IOException; +} diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/SubscriptionsService.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/SubscriptionsService.java new file mode 100644 index 0000000000..5b5cc2830b --- /dev/null +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/SubscriptionsService.java @@ -0,0 +1,11 @@ +package com.coderising.ood.srp.service; + +import java.util.List; + +import com.coderising.ood.srp.model.Product; +import com.coderising.ood.srp.model.Subscriptions; + +public interface SubscriptionsService { + + List doFindByProducts(List products); +} diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/impl/ProductServiceImpl.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/impl/ProductServiceImpl.java new file mode 100644 index 0000000000..66fe9b6d90 --- /dev/null +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/impl/ProductServiceImpl.java @@ -0,0 +1,40 @@ +package com.coderising.ood.srp.service.impl; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import com.coderising.ood.srp.model.Product; +import com.coderising.ood.srp.service.ProductService; + +public class ProductServiceImpl implements ProductService { + + @Override + public List doFindPromotionalProducts(File file) throws IOException { + + List products = new ArrayList(); + + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + while (br.read() != -1) { + Product product = new Product(); + String temp = br.readLine(); + String[] data = temp.split(" "); + product.setProductID(data[0]); + product.setProductDesc(data[1]); + products.add(product); + } + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + return products; + } + +} diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/impl/SubscriptionsServiceImpl.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/impl/SubscriptionsServiceImpl.java new file mode 100644 index 0000000000..164621e2ea --- /dev/null +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/impl/SubscriptionsServiceImpl.java @@ -0,0 +1,40 @@ +package com.coderising.ood.srp.service.impl; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.stream.Collectors; + +import com.coderising.ood.srp.model.Product; +import com.coderising.ood.srp.model.Subscriptions; +import com.coderising.ood.srp.service.SubscriptionsService; +import com.coderising.ood.srp.util.DBUtil; + +public class SubscriptionsServiceImpl implements SubscriptionsService { + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + @SuppressWarnings({ "unused", "rawtypes" }) + @Override + public List doFindByProducts(List products) { + + List productIds = products.stream().map(Product::getProductID).collect(Collectors.toList()); + String sendMailQuery = "Select name from subscriptions where product_id in( productIds ) and send_mail = 1 "; + List list = DBUtil.query(sendMailQuery); + + // 这里只是模拟数据 + List subscriptions = new ArrayList(); + for (int i = 0; i < list.size(); i++) { + HashMap userInfo = (HashMap) list.get(i); + Subscriptions ss = new Subscriptions(); + ss.setName((String) userInfo.get(NAME_KEY)); + ss.setEmail((String) userInfo.get(EMAIL_KEY)); + ss.setProduct(products.get(i)); + subscriptions.add(ss); + } + + return subscriptions; + } + +} diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/util/DBUtil.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/util/DBUtil.java index 7597905da1..9af40a67bd 100644 --- a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/util/DBUtil.java +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/util/DBUtil.java @@ -1,4 +1,4 @@ -package com.coderising.ood.srp; +package com.coderising.ood.srp.util; import java.util.ArrayList; import java.util.HashMap; diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/util/MailUtil.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/util/MailUtil.java index 9f9e749af7..bb028c690c 100644 --- a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/util/MailUtil.java +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/util/MailUtil.java @@ -1,4 +1,4 @@ -package com.coderising.ood.srp; +package com.coderising.ood.srp.util; public class MailUtil { diff --git a/students/992331664/ood/ood/src/test/java/com/coderising/ood/srp/PromotionMailTest.java b/students/992331664/ood/ood/src/test/java/com/coderising/ood/srp/PromotionMailTest.java new file mode 100644 index 0000000000..a3c91dc4bb --- /dev/null +++ b/students/992331664/ood/ood/src/test/java/com/coderising/ood/srp/PromotionMailTest.java @@ -0,0 +1,34 @@ +package com.coderising.ood.srp; + +import java.io.File; + +import org.junit.Before; +import org.junit.Test; + +import com.coderising.ood.srp.service.ProductService; +import com.coderising.ood.srp.service.SubscriptionsService; +import com.coderising.ood.srp.service.impl.ProductServiceImpl; +import com.coderising.ood.srp.service.impl.SubscriptionsServiceImpl; + +public class PromotionMailTest { + + PromotionMail promotionMail; + + @Before + public void before() { + SubscriptionsService subscriptionsService = new SubscriptionsServiceImpl(); + ProductService productService = new ProductServiceImpl(); + promotionMail = new PromotionMail(subscriptionsService, productService); + } + + @Test + public void testSendPromotionMail() throws Exception { + String path = System.getProperty("user.dir"); + + path += "\\bin\\com\\coderising\\ood\\srp\\product_promotion.txt"; + + File file = new File(path); + + promotionMail.sendPromotionMail(file, false); + } +} From 3dbbd57c01540014b73d436abb930e570ef755d5 Mon Sep 17 00:00:00 2001 From: johnChnia Date: Wed, 14 Jun 2017 04:54:05 +0800 Subject: [PATCH 083/214] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E7=AC=AC=E4=B8=80=E5=91=A8=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/315863321/ood/ood-assignment/pom.xml | 32 ++++++++ .../java/com/coderising/ood/srp/Build.java | 18 +++++ .../com/coderising/ood/srp/BuildMail.java | 59 ++++++++++++++ .../coderising/ood/srp/BuildMailServer.java | 20 +++++ .../com/coderising/ood/srp/BuildProduct.java | 21 +++++ .../com/coderising/ood/srp/Configuration.java | 34 +++++++++ .../coderising/ood/srp/ConfigurationKeys.java | 11 +++ .../java/com/coderising/ood/srp/DBUtil.java | 25 ++++++ .../java/com/coderising/ood/srp/Mail.java | 44 +++++++++++ .../com/coderising/ood/srp/MailServer.java | 25 ++++++ .../java/com/coderising/ood/srp/MailUtil.java | 18 +++++ .../java/com/coderising/ood/srp/Product.java | 25 ++++++ .../com/coderising/ood/srp/PromotionMail.java | 76 +++++++++++++++++++ .../coderising/ood/srp/ReadFromDatabase.java | 21 +++++ .../com/coderising/ood/srp/ReadFromFile.java | 44 +++++++++++ .../com/coderising/ood/srp/ReadFromMap.java | 18 +++++ .../java/com/coderising/ood/srp/Reader.java | 22 ++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 18 files changed, 517 insertions(+) create mode 100644 students/315863321/ood/ood-assignment/pom.xml create mode 100644 students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Build.java create mode 100644 students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/BuildMail.java create mode 100644 students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/BuildMailServer.java create mode 100644 students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/BuildProduct.java create mode 100644 students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java create mode 100644 students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailServer.java create mode 100644 students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ReadFromDatabase.java create mode 100644 students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ReadFromFile.java create mode 100644 students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ReadFromMap.java create mode 100644 students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Reader.java create mode 100644 students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/315863321/ood/ood-assignment/pom.xml b/students/315863321/ood/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/315863321/ood/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Build.java b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Build.java new file mode 100644 index 0000000000..e8ea1476cd --- /dev/null +++ b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Build.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +/** + * Created by john on 2017/6/14. + */ +public abstract class Build { + Reader reader = null; + + abstract void build(); + + public Reader getReader() { + return reader; + } + + public void setReader(Reader reader) { + this.reader = reader; + } +} diff --git a/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/BuildMail.java b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/BuildMail.java new file mode 100644 index 0000000000..b1176d9eff --- /dev/null +++ b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/BuildMail.java @@ -0,0 +1,59 @@ +package com.coderising.ood.srp; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +/** + * Created by john on 2017/6/14. + */ +public class BuildMail extends Build{ + List mailList; + Product product; + Mail mail; + Configuration config = new Configuration(); + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public BuildMail(List mailList, Product product) { + this.mailList = mailList; + this.product = product; + } + void build() { + List list = reader.read(); + if (list != null) { + Iterator iter = list.iterator(); + while (iter.hasNext()) { + mail = new Mail(); + mail.setSubject(config.getProperty(ConfigurationKeys.SUBJECT)); + mail.setFromAddress(config.getProperty(ConfigurationKeys.EMAIL_ADMIN)); + try { + configureEMail((HashMap) iter.next()); + } catch (IOException e) { + e.printStackTrace(); + } + this.mailList.add(mail); + } + + } + + } + + protected void configureEMail(HashMap userInfo) throws IOException { + mail.setToAddress((String) userInfo.get(EMAIL_KEY)); + if (mail.getToAddress().length() > 0) + setMessage(userInfo); + } + + protected void setMessage(HashMap userInfo) throws IOException { + + String name = (String) userInfo.get(NAME_KEY); + + mail.setMessage("尊敬的 "+name+", 您关注的产品 " + this.product.getProductDesc() + " 降价了,欢迎购买!"); + + + } + +} diff --git a/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/BuildMailServer.java b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/BuildMailServer.java new file mode 100644 index 0000000000..eebc5946c6 --- /dev/null +++ b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/BuildMailServer.java @@ -0,0 +1,20 @@ +package com.coderising.ood.srp; + +import java.util.List; + +/** + * Created by john on 2017/6/14. + */ +public class BuildMailServer extends Build{ + private MailServer mailServer; + + public BuildMailServer(MailServer mailServer) { + this.mailServer = mailServer; + } + void build() { + List data = reader.read(); + mailServer.setSmtpHost((String) data.get(0)); + mailServer.setAltSmtpHost((String) data.get(1)); + } + +} diff --git a/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/BuildProduct.java b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/BuildProduct.java new file mode 100644 index 0000000000..0c8a66942e --- /dev/null +++ b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/BuildProduct.java @@ -0,0 +1,21 @@ +package com.coderising.ood.srp; + +import java.util.List; + +/** + * Created by john on 2017/6/14. + */ +public class BuildProduct extends Build{ + private Product product; + + public BuildProduct(Product product) { + this.product = product; + } + void build() { + List data = reader.read(); + product.setProductID((String) data.get(0)); + product.setProductDesc((String) data.get(1)); + } + + +} diff --git a/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..815afc3101 --- /dev/null +++ b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,34 @@ +package com.coderising.ood.srp; + +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap(); + static final String QUERY = "Select name from subscriptions " + + "where product_id= '" + "%s" +"' " + + "and send_mail=1 "; + + static { + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + configurations.put(ConfigurationKeys.SEND_MAIL_QUERY, QUERY); + configurations.put(ConfigurationKeys.SUBJECT, "您关注的产品降价了"); + + } + + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + + +} diff --git a/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..afda0b749f --- /dev/null +++ b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,11 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + public static final String SEND_MAIL_QUERY = "email.query"; + public static final String SUBJECT = "email.subject"; + +} diff --git a/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java new file mode 100644 index 0000000000..5d8d222aed --- /dev/null +++ b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java @@ -0,0 +1,44 @@ +package com.coderising.ood.srp; + +/** + * Created by john on 2017/6/14. + */ + +public class Mail { + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailServer.java b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailServer.java new file mode 100644 index 0000000000..07a8d588e6 --- /dev/null +++ b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailServer.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; + +/** + * Created by john on 2017/6/14. + */ +public class MailServer { + protected String smtpHost = null; + protected String altSmtpHost = null; + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } +} diff --git a/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..3346dfb597 --- /dev/null +++ b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(Mail mail, MailServer mailServer, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(mail.getFromAddress()).append("\n"); + buffer.append("To:").append(mail.getToAddress()).append("\n"); + buffer.append("Subject:").append(mail.getSubject()).append("\n"); + buffer.append("Content:").append(mail.getMessage()).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..a3027a26cc --- /dev/null +++ b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; + +/** + * Created by john on 2017/6/14. + */ +public class Product { + private String productID = null; + private String productDesc = null; + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } +} diff --git a/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..93fce8bbbd --- /dev/null +++ b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,76 @@ +package com.coderising.ood.srp; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + private Product product = new Product(); + private MailServer mailServer = new MailServer(); + private List mailList = new ArrayList(); + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + public static void main(String[] args) throws Exception { + + File f = new File("/Users/john/Documents/mygit_2/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail promotionMail = new PromotionMail(); + //配置产品 + Build build = new BuildProduct(promotionMail.product); + build.setReader(new ReadFromFile(f)); + build.build(); + + //配置邮件服务器 + build = new BuildMailServer(promotionMail.mailServer); + build.setReader(new ReadFromMap()); + build.build(); + + //配置邮件 + build = new BuildMail(promotionMail.mailList, promotionMail.product); + build.setReader(new ReadFromDatabase(promotionMail.product)); + build.build(); + //发送邮件 + promotionMail.sendEMails(emailDebug, promotionMail.mailList); + + + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + Mail mail = (Mail) iter.next(); + try { + if (mail.getToAddress().length() > 0) { + MailUtil.sendEmail(mail, mailServer, debug); + + } + } catch (Exception e) { + + try { + MailUtil.sendEmail(mail, mailServer, debug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ReadFromDatabase.java b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ReadFromDatabase.java new file mode 100644 index 0000000000..e3f6f2bae8 --- /dev/null +++ b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ReadFromDatabase.java @@ -0,0 +1,21 @@ +package com.coderising.ood.srp; + +import java.util.List; + +/** + * Created by john on 2017/6/14. + */ +public class ReadFromDatabase extends Reader { + Configuration config = new Configuration(); + Product product = null; + + public ReadFromDatabase(Product product) { + this.product = product; + } + + List read() { + System.out.println("loadQuery set"); + return DBUtil.query(config.getProperty(String.format(ConfigurationKeys.SEND_MAIL_QUERY, product.getProductID()))); + + } +} diff --git a/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ReadFromFile.java b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ReadFromFile.java new file mode 100644 index 0000000000..a3a7eb2d5f --- /dev/null +++ b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ReadFromFile.java @@ -0,0 +1,44 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +/** + * Created by john on 2017/6/13. + */ +public class ReadFromFile extends Reader{ + + + public ReadFromFile(File file) { + super(file); + } + + List read() { + BufferedReader br = null; + List data = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + data = Arrays.asList(temp.split(" ")); + } catch (IOException e) { + try { + throw new IOException(e.getMessage()); + } catch (IOException e1) { + e1.printStackTrace(); + } + } finally { + try { + br.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + System.out.println("产品ID = " + data.get(0) + "\n"); + System.out.println("产品描述 = " + data.get(1) + "\n"); + return data; + } +} diff --git a/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ReadFromMap.java b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ReadFromMap.java new file mode 100644 index 0000000000..880f297411 --- /dev/null +++ b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ReadFromMap.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by john on 2017/6/14. + */ +public class ReadFromMap extends Reader{ + Configuration config = new Configuration(); + + List read() { + List list = new ArrayList(); + list.add(config.getProperty(ConfigurationKeys.SMTP_SERVER)); + list.add(config.getProperty((ConfigurationKeys.ALT_SMTP_SERVER))); + return list; + } +} diff --git a/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Reader.java b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Reader.java new file mode 100644 index 0000000000..7e59a0bb68 --- /dev/null +++ b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Reader.java @@ -0,0 +1,22 @@ +package com.coderising.ood.srp; + +import java.io.File; +import java.util.List; + +/** + * Created by john on 2017/6/14. + */ +public abstract class Reader { + File file; + + public Reader(File file) { + this.file = file; + } + + public Reader() { + + } + + abstract List read(); + +} diff --git a/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/315863321/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 47d1952354085013e748437cf7329f74cd9c5178 Mon Sep 17 00:00:00 2001 From: zheng <765324639@qq.com> Date: Wed, 14 Jun 2017 09:25:43 +0800 Subject: [PATCH 084/214] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/765324639/ood/ood-assignment/pom.xml | 45 ++++ .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 7 files changed, 323 insertions(+) create mode 100644 students/765324639/ood/ood-assignment/pom.xml create mode 100644 students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/765324639/ood/ood-assignment/pom.xml b/students/765324639/ood/ood-assignment/pom.xml new file mode 100644 index 0000000000..cb72faa5f8 --- /dev/null +++ b/students/765324639/ood/ood-assignment/pom.xml @@ -0,0 +1,45 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..781587a846 --- /dev/null +++ b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From c00bd7946417cdcb5171f53bed24ad31d858027b Mon Sep 17 00:00:00 2001 From: wuliLawra Date: Wed, 14 Jun 2017 09:36:10 +0800 Subject: [PATCH 085/214] first --- students/986547781/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/986547781/README.md diff --git a/students/986547781/README.md b/students/986547781/README.md new file mode 100644 index 0000000000..d25f86ec2d --- /dev/null +++ b/students/986547781/README.md @@ -0,0 +1 @@ +##һγ From 58ff91c21cacbf433306a0bfa13cc8e340a5cadb Mon Sep 17 00:00:00 2001 From: lizhao Date: Wed, 14 Jun 2017 09:48:13 +0800 Subject: [PATCH 086/214] new line --- students/986547781/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/students/986547781/README.md b/students/986547781/README.md index d25f86ec2d..f568850ceb 100644 --- a/students/986547781/README.md +++ b/students/986547781/README.md @@ -1 +1,2 @@ -##һγ +##第一次尝试 +##解决编码问题 \ No newline at end of file From 2264d865850c93d6950873711fa68796d9e8af7e Mon Sep 17 00:00:00 2001 From: kkc7316 <1170794299@qq.com> Date: Wed, 14 Jun 2017 10:06:19 +0800 Subject: [PATCH 087/214] first commit --- students/1170794299/README.MD | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/1170794299/README.MD diff --git a/students/1170794299/README.MD b/students/1170794299/README.MD new file mode 100644 index 0000000000..f29dee6099 --- /dev/null +++ b/students/1170794299/README.MD @@ -0,0 +1 @@ +测试 From ab89336e4efe42d0ac6eb0ca25dafb37884be64c Mon Sep 17 00:00:00 2001 From: KevinSmile Date: Wed, 14 Jun 2017 11:47:25 +0800 Subject: [PATCH 088/214] update readme --- students/279069328/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/students/279069328/readme.md b/students/279069328/readme.md index ce14d38509..e84336bfe0 100644 --- a/students/279069328/readme.md +++ b/students/279069328/readme.md @@ -1,3 +1,3 @@ # Coding 2017 -@KevinSmile \ No newline at end of file +KevinSmile@coderising \ No newline at end of file From c371b4e7dc13ef4c06e804e1b54162ba09ff3304 Mon Sep 17 00:00:00 2001 From: sheng <1158154002@qq.com> Date: Wed, 14 Jun 2017 12:28:07 +0800 Subject: [PATCH 089/214] test01 --- students/1158154002/pom.xml | 32 ++++++++++ .../coderising/ood/srp/BaseFileLoader.java | 10 +++ .../com/coderising/ood/srp/Configuration.java | 23 +++++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 +++ .../java/com/coderising/ood/srp/DBUtil.java | 25 ++++++++ .../coderising/ood/srp/FileLoaderImpl.java | 39 ++++++++++++ .../com/coderising/ood/srp/HandlerEmail.java | 55 ++++++++++++++++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++++++ .../com/coderising/ood/srp/PromotionMail.java | 63 +++++++++++++++++++ .../coderising/ood/srp/api/FileLoader.java | 8 +++ .../coderising/ood/srp/model/Constant.java | 6 ++ .../com/coderising/ood/srp/model/Mail.java | 63 +++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 ++ 13 files changed, 355 insertions(+) create mode 100644 students/1158154002/pom.xml create mode 100644 students/1158154002/src/main/java/com/coderising/ood/srp/BaseFileLoader.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/srp/FileLoaderImpl.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/srp/HandlerEmail.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/srp/api/FileLoader.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/srp/model/Constant.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/srp/model/Mail.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/1158154002/pom.xml b/students/1158154002/pom.xml new file mode 100644 index 0000000000..1be81576cc --- /dev/null +++ b/students/1158154002/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/1158154002/src/main/java/com/coderising/ood/srp/BaseFileLoader.java b/students/1158154002/src/main/java/com/coderising/ood/srp/BaseFileLoader.java new file mode 100644 index 0000000000..8294670ffd --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/srp/BaseFileLoader.java @@ -0,0 +1,10 @@ +package com.coderising.ood.srp; + +import java.io.File; +import java.util.Map; + +import com.coderising.ood.srp.api.FileLoader; + +public abstract class BaseFileLoader implements FileLoader { + public abstract Map readFile(File file); +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/srp/Configuration.java b/students/1158154002/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..927c7155cc --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/1158154002/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..868a03ff83 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/1158154002/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..65383e4dba --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/srp/FileLoaderImpl.java b/students/1158154002/src/main/java/com/coderising/ood/srp/FileLoaderImpl.java new file mode 100644 index 0000000000..75c0bf4954 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/srp/FileLoaderImpl.java @@ -0,0 +1,39 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class FileLoaderImpl extends BaseFileLoader{ + + @Override + public Map readFile(File file) { + BufferedReader br = null; + Map map=new HashMap(); + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + map.put("productID", data[0]); + map.put("productDesc", data[1]); + + System.out.println("产品ID = " + data[0] + "\n"); + System.out.println("产品描述 = " + data[1] + "\n"); + + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + br.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return map; + } + +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/srp/HandlerEmail.java b/students/1158154002/src/main/java/com/coderising/ood/srp/HandlerEmail.java new file mode 100644 index 0000000000..f9ec8061e6 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/srp/HandlerEmail.java @@ -0,0 +1,55 @@ +package com.coderising.ood.srp; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.coderising.ood.srp.model.Constant; +import com.coderising.ood.srp.model.Mail; + +public class HandlerEmail { + + protected String sendMailQuery; + + protected Configuration config = new Configuration(); + + protected Mail mail=new Mail(); + + protected FileLoaderImpl fileLoader=new FileLoaderImpl(); + + public HandlerEmail(File file) { + Map map=fileLoader.readFile(file); + mail.setProductDesc((String)map.get("productDesc")); + mail.setProductID((String)map.get("productID")); + mail.setSmtpHost(config.getProperty(ConfigurationKeys.SMTP_SERVER)); + mail.setAltSmtpHost(config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + mail.setFromAddress(config.getProperty(ConfigurationKeys.EMAIL_ADMIN)); + + } + + protected void setLoadQuery() throws Exception { + sendMailQuery = "Select name from subscriptions " + "where product_id= '" + mail.getProductID() + "' " + + "and send_mail=1 "; + + System.out.println("loadQuery set"); + } + + protected void setMessage(HashMap userInfo) throws IOException { + String name = (String) userInfo.get(Constant.NAME_KEY); + mail.setSubject("您关注的产品降价了"); + mail.setMessage("尊敬的 " + name + ", 您关注的产品 " + mail.getProductDesc() + " 降价了,欢迎购买!"); + } + + protected void configureEMail(HashMap userInfo) throws IOException { + mail.setToAddress((String) userInfo.get(Constant.EMAIL_KEY)); + if (mail.getToAddress().length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/1158154002/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..373f3ee306 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/1158154002/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..91a5c0328c --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,63 @@ +package com.coderising.ood.srp; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + protected HandlerEmail hanlder; + + public static void main(String[] args) throws Exception { + + File f = new File( + "D:\\mygit\\coding2017\\students\\1158154002\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + hanlder = new HandlerEmail(file); + hanlder.setLoadQuery(); + sendEMails(mailDebug, hanlder.loadMailingList()); + + } + + protected void sendEMails(boolean debug, List mailingList) throws IOException { + + System.out.println("开始发送邮件"); + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + hanlder.configureEMail((HashMap) iter.next()); + try { + if (hanlder.mail.getToAddress().length() > 0) + MailUtil.sendEmail(hanlder.mail.getToAddress(), hanlder.mail.getFromAddress(), hanlder.mail.getSubject(), + hanlder.mail.getMessage(), hanlder.mail.getSmtpHost(), debug); + } catch (Exception e) { + + try { + MailUtil.sendEmail(hanlder.mail.getToAddress(), hanlder.mail.getFromAddress(), hanlder.mail.getSubject(), + hanlder.mail.getMessage(), hanlder.mail.getAltSmtpHost(), debug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/srp/api/FileLoader.java b/students/1158154002/src/main/java/com/coderising/ood/srp/api/FileLoader.java new file mode 100644 index 0000000000..d864d5e74e --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/srp/api/FileLoader.java @@ -0,0 +1,8 @@ +package com.coderising.ood.srp.api; + +import java.io.File; +import java.util.Map; + +public interface FileLoader { + Map readFile(File file); +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/srp/model/Constant.java b/students/1158154002/src/main/java/com/coderising/ood/srp/model/Constant.java new file mode 100644 index 0000000000..ad6258531e --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/srp/model/Constant.java @@ -0,0 +1,6 @@ +package com.coderising.ood.srp.model; + +public class Constant { + public static final String NAME_KEY = "NAME"; + public static final String EMAIL_KEY = "EMAIL"; +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/srp/model/Mail.java b/students/1158154002/src/main/java/com/coderising/ood/srp/model/Mail.java new file mode 100644 index 0000000000..c6715e4385 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/srp/model/Mail.java @@ -0,0 +1,63 @@ +package com.coderising.ood.srp.model; + +public class Mail { + private String smtpHost; + private String altSmtpHost; + private String fromAddress; + private String toAddress; + private String subject; + private String message; + private String productID; + private String productDesc; + + public String getSmtpHost() { + return smtpHost; + } + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + public String getAltSmtpHost() { + return altSmtpHost; + } + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + public String getFromAddress() { + return fromAddress; + } + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + public String getToAddress() { + return toAddress; + } + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + public String getSubject() { + return subject; + } + public void setSubject(String subject) { + this.subject = subject; + } + public String getMessage() { + return message; + } + public void setMessage(String message) { + this.message = message; + } + public String getProductID() { + return productID; + } + public void setProductID(String productID) { + this.productID = productID; + } + public String getProductDesc() { + return productDesc; + } + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + + +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/1158154002/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From d894371dd19dbbf3e29fba98e30e521c7a9b3e72 Mon Sep 17 00:00:00 2001 From: renxin Date: Wed, 14 Jun 2017 14:50:31 +0800 Subject: [PATCH 090/214] =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coderising/ood/srp/Configuration.java | 23 --- .../coderising/ood/srp/ConfigurationKeys.java | 9 - .../java/com/coderising/ood/srp/MailUtil.java | 18 -- .../com/coderising/ood/srp/PromotionMail.java | 192 +----------------- .../ood/srp/dao/PromotionMailDao.java | 12 ++ .../srp/dao/impl/PromotionMailDaoImpl.java | 35 ++++ .../ood/srp/service/PromotionMailService.java | 9 + .../impl/PromotionMailServiceImpl.java | 86 ++++++++ .../ood/srp/{ => utils}/DBUtil.java | 2 +- .../coderising/ood/srp/utils/FileUtil.java | 28 +++ .../coderising/ood/srp/utils/MailUtil.java | 55 +++++ .../ood/srp/utils/PropertiesUtils.java | 40 ++++ .../resources/configurationKeys.properties | 3 + 13 files changed, 276 insertions(+), 236 deletions(-) delete mode 100644 students/335402763/src/main/java/com/coderising/ood/srp/Configuration.java delete mode 100644 students/335402763/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java delete mode 100644 students/335402763/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/335402763/src/main/java/com/coderising/ood/srp/dao/PromotionMailDao.java create mode 100644 students/335402763/src/main/java/com/coderising/ood/srp/dao/impl/PromotionMailDaoImpl.java create mode 100644 students/335402763/src/main/java/com/coderising/ood/srp/service/PromotionMailService.java create mode 100644 students/335402763/src/main/java/com/coderising/ood/srp/service/impl/PromotionMailServiceImpl.java rename students/335402763/src/main/java/com/coderising/ood/srp/{ => utils}/DBUtil.java (88%) create mode 100644 students/335402763/src/main/java/com/coderising/ood/srp/utils/FileUtil.java create mode 100644 students/335402763/src/main/java/com/coderising/ood/srp/utils/MailUtil.java create mode 100644 students/335402763/src/main/java/com/coderising/ood/srp/utils/PropertiesUtils.java create mode 100644 students/335402763/src/main/resources/configurationKeys.properties diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/Configuration.java b/students/335402763/src/main/java/com/coderising/ood/srp/Configuration.java deleted file mode 100644 index 927c7155cc..0000000000 --- a/students/335402763/src/main/java/com/coderising/ood/srp/Configuration.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.coderising.ood.srp; -import java.util.HashMap; -import java.util.Map; - -public class Configuration { - - static Map configurations = new HashMap<>(); - static{ - configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); - configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); - configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); - } - /** - * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 - * @param key - * @return - */ - public String getProperty(String key) { - - return configurations.get(key); - } - -} diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/335402763/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java deleted file mode 100644 index 868a03ff83..0000000000 --- a/students/335402763/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.coderising.ood.srp; - -public class ConfigurationKeys { - - public static final String SMTP_SERVER = "smtp.server"; - public static final String ALT_SMTP_SERVER = "alt.smtp.server"; - public static final String EMAIL_ADMIN = "email.admin"; - -} diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/335402763/src/main/java/com/coderising/ood/srp/MailUtil.java deleted file mode 100644 index 373f3ee306..0000000000 --- a/students/335402763/src/main/java/com/coderising/ood/srp/MailUtil.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.coderising.ood.srp; - -public class MailUtil { - - public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, - boolean debug) { - //假装发了一封邮件 - StringBuilder buffer = new StringBuilder(); - buffer.append("From:").append(fromAddress).append("\n"); - buffer.append("To:").append(toAddress).append("\n"); - buffer.append("Subject:").append(subject).append("\n"); - buffer.append("Content:").append(message).append("\n"); - System.out.println(buffer.toString()); - - } - - -} diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/335402763/src/main/java/com/coderising/ood/srp/PromotionMail.java index 94bfcbaf54..4e777b5cd0 100644 --- a/students/335402763/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/335402763/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -1,199 +1,21 @@ package com.coderising.ood.srp; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.Serializable; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -public class PromotionMail { - - - protected String sendMailQuery = null; +import com.coderising.ood.srp.service.PromotionMailService; +import com.coderising.ood.srp.service.impl.PromotionMailServiceImpl; - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; - protected String toAddress = null; - protected String subject = null; - protected String message = null; - - protected String productID = null; - protected String productDesc = null; +public class PromotionMail { - private static Configuration config; - - - - private static final String NAME_KEY = "NAME"; - private static final String EMAIL_KEY = "EMAIL"; - public static void main(String[] args) throws Exception { - - File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); - boolean emailDebug = false; - - PromotionMail pe = new PromotionMail(f, emailDebug); - - } - - - public PromotionMail(File file, boolean mailDebug) throws Exception { - - //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - readFile(file); - - - config = new Configuration(); - - setSMTPHost(); - setAltSMTPHost(); - - - setFromAddress(); - - - setLoadQuery(); - - sendEMails(mailDebug, loadMailingList()); - - - } - - - - - protected void setProductID(String productID) - { - this.productID = productID; - - } - - protected String getproductID() - { - return productID; - } - - protected void setLoadQuery() throws Exception { - - sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + productID +"' " - + "and send_mail=1 "; + File f = new File("D:\\Program Files\\mygit\\coding2017\\students\\335402763\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"); + boolean emailDebug = false; - System.out.println("loadQuery set"); - } - - - protected void setSMTPHost() - { - smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); - } - - - protected void setAltSMTPHost() - { - altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); - - } - - - protected void setFromAddress() - { - fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); - } - - protected void setMessage(HashMap userInfo) throws IOException - { - - String name = (String) userInfo.get(NAME_KEY); - - subject = "您关注的产品降价了"; - message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; - - - - } - - - protected void readFile(File file) throws IOException // @02C - { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - setProductID(data[0]); - setProductDesc(data[1]); - - System.out.println("产品ID = " + productID + "\n"); - System.out.println("产品描述 = " + productDesc + "\n"); - - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); - } - } - - private void setProductDesc(String desc) { - this.productDesc = desc; - } - - - protected void configureEMail(HashMap userInfo) throws IOException - { - toAddress = (String) userInfo.get(EMAIL_KEY); - if (toAddress.length() > 0) - setMessage(userInfo); - } - - protected List loadMailingList() throws Exception { - return DBUtil.query(this.sendMailQuery); + PromotionMailService promotionMailService = new PromotionMailServiceImpl(); + promotionMailService.sendPromotionMail(f, emailDebug); } - - protected void sendEMails(boolean debug, List mailingList) throws IOException - { - - System.out.println("开始发送邮件"); - - - if (mailingList != null) { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) { - configureEMail((HashMap) iter.next()); - try - { - if (toAddress.length() > 0) - MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } - catch (Exception e) - { - - try { - MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) - { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } - } - - - } - - else { - System.out.println("没有邮件发送"); - - } - - } } diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/dao/PromotionMailDao.java b/students/335402763/src/main/java/com/coderising/ood/srp/dao/PromotionMailDao.java new file mode 100644 index 0000000000..21f143ff11 --- /dev/null +++ b/students/335402763/src/main/java/com/coderising/ood/srp/dao/PromotionMailDao.java @@ -0,0 +1,12 @@ +package com.coderising.ood.srp.dao; + +import java.util.List; + +public interface PromotionMailDao { + + /** + * 读取用户信息 + */ + List loadMailingList(String productID); + +} diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/dao/impl/PromotionMailDaoImpl.java b/students/335402763/src/main/java/com/coderising/ood/srp/dao/impl/PromotionMailDaoImpl.java new file mode 100644 index 0000000000..339f5b648e --- /dev/null +++ b/students/335402763/src/main/java/com/coderising/ood/srp/dao/impl/PromotionMailDaoImpl.java @@ -0,0 +1,35 @@ +package com.coderising.ood.srp.dao.impl; + +import java.util.List; + +import com.coderising.ood.srp.dao.PromotionMailDao; +import com.coderising.ood.srp.utils.DBUtil; + +public class PromotionMailDaoImpl implements PromotionMailDao { + + protected String productID = null; + protected String sendMailQuery = null; + + /** + * 读取用户信息 + */ + @Override + public List loadMailingList(String productID) { + try { + setLoadQuery(productID); + + } catch (Exception e) { + e.printStackTrace(); + } + return DBUtil.query(this.sendMailQuery); + + } + + protected void setLoadQuery(String productID) throws Exception { + + sendMailQuery = "Select name from subscriptions " + "where product_id= '" + productID + "' " + + "and send_mail=1 "; + + System.out.println("loadQuery set"); + } +} diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/service/PromotionMailService.java b/students/335402763/src/main/java/com/coderising/ood/srp/service/PromotionMailService.java new file mode 100644 index 0000000000..9d1db0cd71 --- /dev/null +++ b/students/335402763/src/main/java/com/coderising/ood/srp/service/PromotionMailService.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp.service; + +import java.io.File; + +public interface PromotionMailService { + + void sendPromotionMail(File file, boolean mailDebug) throws Exception; + +} diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/service/impl/PromotionMailServiceImpl.java b/students/335402763/src/main/java/com/coderising/ood/srp/service/impl/PromotionMailServiceImpl.java new file mode 100644 index 0000000000..2d1e8b491a --- /dev/null +++ b/students/335402763/src/main/java/com/coderising/ood/srp/service/impl/PromotionMailServiceImpl.java @@ -0,0 +1,86 @@ +package com.coderising.ood.srp.service.impl; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +import com.coderising.ood.srp.dao.PromotionMailDao; +import com.coderising.ood.srp.dao.impl.PromotionMailDaoImpl; +import com.coderising.ood.srp.service.PromotionMailService; +import com.coderising.ood.srp.utils.FileUtil; +import com.coderising.ood.srp.utils.MailUtil; +import com.coderising.ood.srp.utils.PropertiesUtils; + +public class PromotionMailServiceImpl implements PromotionMailService { + + private PromotionMailDao promotionMailDao = new PromotionMailDaoImpl(); + + protected String smtpHost = (String) PropertiesUtils.get(PropertiesUtils.SMTP_SERVER);; + protected String altSmtpHost = (String) PropertiesUtils.get(PropertiesUtils.ALT_SMTP_SERVER);; + protected String fromAddress = (String) PropertiesUtils.get(PropertiesUtils.EMAIL_ADMIN); + + @Override + public void sendPromotionMail(File file, boolean mailDebug) throws Exception { + + // 读取商品信息 + String[] productData = FileUtil.readFile(file); + String productID = productData[0]; + String productDesc = productData[1]; + + List mailingList = promotionMailDao.loadMailingList(productID); + + sendEMails(mailDebug, mailingList, productDesc); + + } + + + /** + * 发送邮件 + * @param debug + * @param mailingList + * @param productDesc + * @throws IOException + */ + protected void sendEMails(boolean debug, List mailingList, String productDesc) throws IOException { + + System.out.println("开始发送邮件"); + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + + HashMap usrInfo = (HashMap) iter.next(); + + HashMap eMailInfo = MailUtil.configureEMail(usrInfo, productDesc); + String toAddress = (String) eMailInfo.get("toAddress"); + String subject = (String) eMailInfo.get("subject"); + String message = (String) eMailInfo.get("message"); + + try { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } catch (Exception e) { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + } + + else { + System.out.println("没有邮件发送"); + + } + } + + + + +} diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/335402763/src/main/java/com/coderising/ood/srp/utils/DBUtil.java similarity index 88% rename from students/335402763/src/main/java/com/coderising/ood/srp/DBUtil.java rename to students/335402763/src/main/java/com/coderising/ood/srp/utils/DBUtil.java index 65383e4dba..2a7002dede 100644 --- a/students/335402763/src/main/java/com/coderising/ood/srp/DBUtil.java +++ b/students/335402763/src/main/java/com/coderising/ood/srp/utils/DBUtil.java @@ -1,4 +1,4 @@ -package com.coderising.ood.srp; +package com.coderising.ood.srp.utils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/utils/FileUtil.java b/students/335402763/src/main/java/com/coderising/ood/srp/utils/FileUtil.java new file mode 100644 index 0000000000..d0a53ec6ae --- /dev/null +++ b/students/335402763/src/main/java/com/coderising/ood/srp/utils/FileUtil.java @@ -0,0 +1,28 @@ +package com.coderising.ood.srp.utils; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; + +public class FileUtil { + + public static String[] readFile(File file) throws IOException // @02C + { + HashMap map = new HashMap(); + BufferedReader br = null; + String[] data = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + data = temp.split(" "); + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + return data; + } + +} diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/utils/MailUtil.java b/students/335402763/src/main/java/com/coderising/ood/srp/utils/MailUtil.java new file mode 100644 index 0000000000..5e72b2e163 --- /dev/null +++ b/students/335402763/src/main/java/com/coderising/ood/srp/utils/MailUtil.java @@ -0,0 +1,55 @@ +package com.coderising.ood.srp.utils; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; + +public class MailUtil { + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + //配置发送地址及内容 + public static HashMap configureEMail(HashMap userInfo , String productDesc) throws IOException { + HashMap map = new HashMap(); + String toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) { + map = setMessage(userInfo,productDesc); + } + map.put("toAddress", toAddress); + return map; + } + + //设置信息内容 + public static HashMap setMessage(HashMap userInfo , String productDesc) throws IOException + { + HashMap map = new HashMap(); + String name = (String) userInfo.get(NAME_KEY); + + String subject = "您关注的产品降价了"; + String message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + map.put("subject", subject); + map.put("message", message); + + return map; + + } + + + +} diff --git a/students/335402763/src/main/java/com/coderising/ood/srp/utils/PropertiesUtils.java b/students/335402763/src/main/java/com/coderising/ood/srp/utils/PropertiesUtils.java new file mode 100644 index 0000000000..e8b94b20e7 --- /dev/null +++ b/students/335402763/src/main/java/com/coderising/ood/srp/utils/PropertiesUtils.java @@ -0,0 +1,40 @@ +package com.coderising.ood.srp.utils; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +public class PropertiesUtils { + + private static Map props=new HashMap(); + private static Properties properties=new Properties(); + + public static final String SMTP_SERVER = "SMTP_SERVER"; + public static final String ALT_SMTP_SERVER = "ALT_SMTP_SERVER"; + public static final String EMAIL_ADMIN = "EMAIL_ADMIN"; + static{ + try { + InputStream inStream=PropertiesUtils.class.getClassLoader().getResourceAsStream("configurationKeys.properties"); + properties.load(inStream); + + props.put(SMTP_SERVER, properties.get(SMTP_SERVER)); + props.put(ALT_SMTP_SERVER, properties.get(ALT_SMTP_SERVER)); + props.put(EMAIL_ADMIN, properties.get(EMAIL_ADMIN)); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + + public static Object get(String key){ + return props.get(key); + } + + public static void set(String key,Object obj){ + props.put(key, obj); + } + +} diff --git a/students/335402763/src/main/resources/configurationKeys.properties b/students/335402763/src/main/resources/configurationKeys.properties new file mode 100644 index 0000000000..acde908d1c --- /dev/null +++ b/students/335402763/src/main/resources/configurationKeys.properties @@ -0,0 +1,3 @@ +SMTP_SERVER=smtp.server +ALT_SMTP_SERVER=alt.smtp.server +EMAIL_ADMIN=email.admin \ No newline at end of file From 0417aeca8ce1d53321f60b283b5d5d850709497a Mon Sep 17 00:00:00 2001 From: zheng <765324639@qq.com> Date: Wed, 14 Jun 2017 15:13:25 +0800 Subject: [PATCH 091/214] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E9=87=8D?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/coderising/ood/srp/DBUtil.java | 10 +- .../java/com/coderising/ood/srp/MailInfo.java | 40 ++++ .../java/com/coderising/ood/srp/MailUtil.java | 50 +++-- .../java/com/coderising/ood/srp/Product.java | 30 +++ .../coderising/ood/srp/ProductInfoReader.java | 36 ++++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++---------------- .../java/com/coderising/ood/srp/UserInfo.java | 30 +++ .../coderising/ood/srp/UserInfoReader.java | 14 ++ 8 files changed, 213 insertions(+), 196 deletions(-) create mode 100644 students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailInfo.java create mode 100644 students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfoReader.java create mode 100644 students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfo.java create mode 100644 students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfoReader.java diff --git a/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java index 82e9261d18..60c50b34f0 100644 --- a/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java +++ b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -10,13 +10,13 @@ public class DBUtil { * @param sql * @return */ - public static List query(String sql){ + public static List query(String sql){ - List userList = new ArrayList(); + List userList = new ArrayList<>(); for (int i = 1; i <= 3; i++) { - HashMap userInfo = new HashMap(); - userInfo.put("NAME", "User" + i); - userInfo.put("EMAIL", "aa@bb.com"); + UserInfo userInfo = new UserInfo(); + userInfo.setUsername("User" + i); + userInfo.setEmail("aa@bb.com"); userList.add(userInfo); } diff --git a/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailInfo.java b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailInfo.java new file mode 100644 index 0000000000..dcb42dcc95 --- /dev/null +++ b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailInfo.java @@ -0,0 +1,40 @@ +package com.coderising.ood.srp; + +public class MailInfo { + private String fromAddress; + private String toAddress; + private String subject; + private String message; + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java index 9f9e749af7..614de5a438 100644 --- a/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java +++ b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -1,18 +1,44 @@ package com.coderising.ood.srp; +import java.util.List; + public class MailUtil { - public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, - boolean debug) { - //假装发了一封邮件 - StringBuilder buffer = new StringBuilder(); - buffer.append("From:").append(fromAddress).append("\n"); - buffer.append("To:").append(toAddress).append("\n"); - buffer.append("Subject:").append(subject).append("\n"); - buffer.append("Content:").append(message).append("\n"); - System.out.println(buffer.toString()); - - } + public static void sendEmail(List mailInfoList, boolean debug) { + if (mailInfoList == null && mailInfoList.size() == 0) { + System.out.println("无邮件发送"); + return; + } + System.out.println("开始发送邮件"); + for (MailInfo mailInfo : mailInfoList) { + sendEmail(mailInfo, debug); + } + } + + public static void sendEmail(MailInfo mailInfo, boolean debug) { + Configuration configuration = new Configuration(); + String smtpServer = configuration.getProperty(ConfigurationKeys.SMTP_SERVER); + String altSmtpServer = configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + String emailAdmin = configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN); + try { + send(mailInfo.getToAddress(), emailAdmin, mailInfo.getSubject(), mailInfo.getMessage(), smtpServer, debug); + } catch (Exception e) { + try { + send(mailInfo.getToAddress(), emailAdmin, mailInfo.getSubject(), mailInfo.getMessage(), altSmtpServer, debug); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } - + public static void send(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + } } diff --git a/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..8887870547 --- /dev/null +++ b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,30 @@ +package com.coderising.ood.srp; + +public class Product { + private String id; + private String desc; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } + + @Override + public String toString() { + return "Product{" + + "id='" + id + '\'' + + ", desc='" + desc + '\'' + + '}'; + } +} diff --git a/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfoReader.java b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfoReader.java new file mode 100644 index 0000000000..3e1660608d --- /dev/null +++ b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfoReader.java @@ -0,0 +1,36 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ProductInfoReader { + + protected static List readProductInfo() { + File file = new File("E:\\coding2017\\students\\765324639\\ood\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"); + List productList = new ArrayList<>(); + try (BufferedReader br = new BufferedReader(new FileReader(file));) { + String temp = null; + while ((temp = br.readLine()) != null) { + String[] data = temp.split(" "); + Product product = new Product(); + product.setId(data[0]); + product.setDesc(data[1]); + productList.add(product); + System.out.println("产品ID = " + data[0]); + System.out.println("产品描述 = " + data[1] + "\n"); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return productList; + } +} diff --git a/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index 781587a846..b47f451636 100644 --- a/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -5,195 +5,36 @@ import java.io.FileReader; import java.io.IOException; import java.io.Serializable; +import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; public class PromotionMail { - - protected String sendMailQuery = null; - - - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; - protected String toAddress = null; - protected String subject = null; - protected String message = null; - - protected String productID = null; - protected String productDesc = null; - - private static Configuration config; - - - - private static final String NAME_KEY = "NAME"; - private static final String EMAIL_KEY = "EMAIL"; - - public static void main(String[] args) throws Exception { - File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); - boolean emailDebug = false; - - PromotionMail pe = new PromotionMail(f, emailDebug); - - } - - - public PromotionMail(File file, boolean mailDebug) throws Exception { - - //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - readFile(file); - - - config = new Configuration(); - - setSMTPHost(); - setAltSMTPHost(); - - - setFromAddress(); - - - setLoadQuery(); - - sendEMails(mailDebug, loadMailingList()); - - - } - - - - - protected void setProductID(String productID) - { - this.productID = productID; - - } - - protected String getproductID() - { - return productID; - } - - protected void setLoadQuery() throws Exception { - - sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + productID +"' " - + "and send_mail=1 "; - - - System.out.println("loadQuery set"); - } - - - protected void setSMTPHost() - { - smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); - } - - - protected void setAltSMTPHost() - { - altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); - - } - - - protected void setFromAddress() - { - fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); - } - - protected void setMessage(HashMap userInfo) throws IOException - { - - String name = (String) userInfo.get(NAME_KEY); - - subject = "您关注的产品降价了"; - message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; - - - - } - - - protected void readFile(File file) throws IOException // @02C - { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - setProductID(data[0]); - setProductDesc(data[1]); - - System.out.println("产品ID = " + productID + "\n"); - System.out.println("产品描述 = " + productDesc + "\n"); + List productList = ProductInfoReader.readProductInfo(); - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); - } - } - - private void setProductDesc(String desc) { - this.productDesc = desc; - } - - - protected void configureEMail(HashMap userInfo) throws IOException - { - toAddress = (String) userInfo.get(EMAIL_KEY); - if (toAddress.length() > 0) - setMessage(userInfo); - } + for (Product product : productList) { + List userInfoList = UserInfoReader.readUserInfo(product.getId()); + List mailInfoList = generateEmails(product, userInfoList); + boolean emailDebug = false; + MailUtil.sendEmail(mailInfoList, emailDebug); + } - protected List loadMailingList() throws Exception { - return DBUtil.query(this.sendMailQuery); } - - - protected void sendEMails(boolean debug, List mailingList) throws IOException - { - System.out.println("开始发送邮件"); - - - if (mailingList != null) { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) { - configureEMail((HashMap) iter.next()); - try - { - if (toAddress.length() > 0) - MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } - catch (Exception e) - { - - try { - MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) - { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } - } - - - } - - else { - System.out.println("没有邮件发送"); - - } - - } + public static List generateEmails(Product product, List userInfoList) { + List mailInfoList = new ArrayList<>(); + for (UserInfo userInfo : userInfoList) { + MailInfo mailInfo = new MailInfo(); + mailInfo.setToAddress(userInfo.getEmail()); + mailInfo.setSubject("您关注的产品降价了"); + mailInfo.setMessage("尊敬的 "+ userInfo.getUsername() +", 您关注的产品 " + product.getDesc() + " 降价了,欢迎购买!"); + mailInfoList.add(mailInfo); + } + return mailInfoList; + } } diff --git a/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfo.java b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfo.java new file mode 100644 index 0000000000..c4f501a3ff --- /dev/null +++ b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfo.java @@ -0,0 +1,30 @@ +package com.coderising.ood.srp; + +public class UserInfo { + private String username; + private String email; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + @Override + public String toString() { + return "UserInfo{" + + "username='" + username + '\'' + + ", email='" + email + '\'' + + '}'; + } +} diff --git a/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfoReader.java b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfoReader.java new file mode 100644 index 0000000000..edfe66d829 --- /dev/null +++ b/students/765324639/ood/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfoReader.java @@ -0,0 +1,14 @@ +package com.coderising.ood.srp; + +import java.util.List; + +public class UserInfoReader { + + public static List readUserInfo(String productID) { + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + System.out.println("loadQuery set"); + return DBUtil.query(sendMailQuery); + } +} From 7f7a9e6991454a22f95624a147403aed6882d0a8 Mon Sep 17 00:00:00 2001 From: Wen Wei Date: Wed, 14 Jun 2017 16:34:02 +0800 Subject: [PATCH 092/214] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/463256809/ood-assignment/pom.xml | 32 ++++ .../com/coderising/ood/srp/Configuration.java | 23 +++ .../coderising/ood/srp/ConfigurationKeys.java | 9 ++ .../java/com/coderising/ood/srp/DBUtil.java | 25 ++++ .../java/com/coderising/ood/srp/FileUtil.java | 35 +++++ .../java/com/coderising/ood/srp/Mail.java | 73 +++++++++ .../java/com/coderising/ood/srp/MailUtil.java | 138 ++++++++++++++++++ .../java/com/coderising/ood/srp/Product.java | 26 ++++ .../com/coderising/ood/srp/PromotionMail.java | 27 ++++ .../coderising/ood/srp/product_promotion.txt | 4 + 10 files changed, 392 insertions(+) create mode 100644 students/463256809/ood-assignment/pom.xml create mode 100644 students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java create mode 100644 students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java create mode 100644 students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/463256809/ood-assignment/pom.xml b/students/463256809/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/463256809/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java new file mode 100644 index 0000000000..fad6f3219b --- /dev/null +++ b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java @@ -0,0 +1,35 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +/** + * Created by wenwei on 2017/6/14. + */ +public class FileUtil { + + private static Product product = new Product(); + + public static void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + product.setProductID(data[0]); + product.setProductDesc(data[1]); + + System.out.println("产品ID = " + product.getProductID() + "\n"); + System.out.println("产品描述 = " + product.getProductDesc() + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } +} diff --git a/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java new file mode 100644 index 0000000000..9c6bfff697 --- /dev/null +++ b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java @@ -0,0 +1,73 @@ +package com.coderising.ood.srp; + +/** + * Created by wenwei on 2017/6/14. + */ +public class Mail { + + private String sendMailQuery = null; + + + private String smtpHost = null; + private String altSmtpHost = null; + private String fromAddress = null; + private String toAddress = null; + private String subject = null; + private String message = null; + + public String getSendMailQuery() { + return sendMailQuery; + } + + public void setSendMailQuery(String sendMailQuery) { + this.sendMailQuery = sendMailQuery; + } + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..73e822f361 --- /dev/null +++ b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,138 @@ +package com.coderising.ood.srp; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class MailUtil { + + + private static String sendMailQuery = null; + private static String smtpHost = null; + private static String altSmtpHost = null; + private static String fromAddress = null; + private static String toAddress = null; + private static String subject = null; + private static String message = null; + + + private static Configuration config = new Configuration(); + private static Product product = new Product(); + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + protected static void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + product.getProductID() +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected static void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected static void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected static void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected static void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!" ; + + + + } + + + + protected static void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected static List loadMailingList() throws Exception { + return DBUtil.query(sendMailQuery); + } + + + public static void sendEMails(boolean debug, List mailingList) throws Exception + { + + setSMTPHost(); + setAltSMTPHost(); + setFromAddress(); + setLoadQuery(); + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..4420dfc603 --- /dev/null +++ b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,26 @@ +package com.coderising.ood.srp; + +/** + * Created by wenwei on 2017/6/14. + */ +public class Product { + + private String productID = null; + private String productDesc = null; + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } +} diff --git a/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..e4241217a2 --- /dev/null +++ b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,27 @@ +package com.coderising.ood.srp; + +import java.io.File; + +public class PromotionMail { + + public static void main(String[] args) throws Exception { + +// File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + File f = new File("/Users/wenwei/mygit/coding2017/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + FileUtil.readFile(file); + + MailUtil.sendEMails(mailDebug, MailUtil.loadMailingList()); + } + + +} diff --git a/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 63b1474bf5e791cd124ff480eda92b923f696add Mon Sep 17 00:00:00 2001 From: Wen Wei Date: Wed, 14 Jun 2017 16:37:47 +0800 Subject: [PATCH 093/214] merge --- students/463256809/src/Demo.java | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 students/463256809/src/Demo.java diff --git a/students/463256809/src/Demo.java b/students/463256809/src/Demo.java deleted file mode 100644 index a67375a605..0000000000 --- a/students/463256809/src/Demo.java +++ /dev/null @@ -1,6 +0,0 @@ - -public class Demo { - public static void main(String[] args) { - System.out.println("Test"); - } -} From 22105d448b6b507fd8c8464a06a215d030048e9e Mon Sep 17 00:00:00 2001 From: Wen Wei Date: Wed, 14 Jun 2017 16:40:44 +0800 Subject: [PATCH 094/214] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/coderising/ood/srp/Mail.java | 73 ------------------- 1 file changed, 73 deletions(-) delete mode 100644 students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java diff --git a/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java b/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java deleted file mode 100644 index 9c6bfff697..0000000000 --- a/students/463256809/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.coderising.ood.srp; - -/** - * Created by wenwei on 2017/6/14. - */ -public class Mail { - - private String sendMailQuery = null; - - - private String smtpHost = null; - private String altSmtpHost = null; - private String fromAddress = null; - private String toAddress = null; - private String subject = null; - private String message = null; - - public String getSendMailQuery() { - return sendMailQuery; - } - - public void setSendMailQuery(String sendMailQuery) { - this.sendMailQuery = sendMailQuery; - } - - public String getSmtpHost() { - return smtpHost; - } - - public void setSmtpHost(String smtpHost) { - this.smtpHost = smtpHost; - } - - public String getAltSmtpHost() { - return altSmtpHost; - } - - public void setAltSmtpHost(String altSmtpHost) { - this.altSmtpHost = altSmtpHost; - } - - public String getFromAddress() { - return fromAddress; - } - - public void setFromAddress(String fromAddress) { - this.fromAddress = fromAddress; - } - - public String getToAddress() { - return toAddress; - } - - public void setToAddress(String toAddress) { - this.toAddress = toAddress; - } - - public String getSubject() { - return subject; - } - - public void setSubject(String subject) { - this.subject = subject; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } -} From f558eac06059fe7d449ea10f0f5f81ad96a6bcbf Mon Sep 17 00:00:00 2001 From: cheungchan <1377699408@qq.com> Date: Tue, 13 Jun 2017 00:32:05 +0800 Subject: [PATCH 095/214] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1srp=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1377699408/data-structure/answer/pom.xml | 32 +++ .../coderising/download/DownloadThread.java | 20 ++ .../coderising/download/FileDownloader.java | 73 +++++++ .../download/FileDownloaderTest.java | 59 ++++++ .../coderising/download/api/Connection.java | 23 ++ .../download/api/ConnectionException.java | 5 + .../download/api/ConnectionManager.java | 10 + .../download/api/DownloadListener.java | 5 + .../download/impl/ConnectionImpl.java | 27 +++ .../download/impl/ConnectionManagerImpl.java | 15 ++ .../coderising/litestruts/LoginAction.java | 39 ++++ .../com/coderising/litestruts/Struts.java | 34 +++ .../com/coderising/litestruts/StrutsTest.java | 43 ++++ .../java/com/coderising/litestruts/View.java | 23 ++ .../java/com/coderising/litestruts/struts.xml | 11 + .../main/java/com/coding/basic/Iterator.java | 7 + .../src/main/java/com/coding/basic/List.java | 9 + .../com/coding/basic/array/ArrayList.java | 35 +++ .../com/coding/basic/array/ArrayUtil.java | 96 +++++++++ .../coding/basic/linklist/LRUPageFrame.java | 164 +++++++++++++++ .../basic/linklist/LRUPageFrameTest.java | 34 +++ .../com/coding/basic/linklist/LinkedList.java | 125 +++++++++++ .../com/coding/basic/queue/CircleQueue.java | 47 +++++ .../coding/basic/queue/CircleQueueTest.java | 44 ++++ .../java/com/coding/basic/queue/Josephus.java | 39 ++++ .../com/coding/basic/queue/JosephusTest.java | 27 +++ .../java/com/coding/basic/queue/Queue.java | 61 ++++++ .../basic/queue/QueueWithTwoStacks.java | 55 +++++ .../com/coding/basic/stack/QuickMinStack.java | 44 ++++ .../coding/basic/stack/QuickMinStackTest.java | 39 ++++ .../java/com/coding/basic/stack/Stack.java | 24 +++ .../com/coding/basic/stack/StackUtil.java | 168 +++++++++++++++ .../com/coding/basic/stack/StackUtilTest.java | 86 ++++++++ .../basic/stack/StackWithTwoQueues.java | 53 +++++ .../basic/stack/StackWithTwoQueuesTest.java | 36 ++++ .../java/com/coding/basic/stack/Tail.java | 5 + .../basic/stack/TwoStackInOneArray.java | 117 ++++++++++ .../basic/stack/TwoStackInOneArrayTest.java | 65 ++++++ .../coding/basic/stack/expr/InfixExpr.java | 72 +++++++ .../basic/stack/expr/InfixExprTest.java | 52 +++++ .../basic/stack/expr/InfixToPostfix.java | 43 ++++ .../basic/stack/expr/InfixToPostfixTest.java | 41 ++++ .../coding/basic/stack/expr/PostfixExpr.java | 46 ++++ .../basic/stack/expr/PostfixExprTest.java | 41 ++++ .../coding/basic/stack/expr/PrefixExpr.java | 52 +++++ .../basic/stack/expr/PrefixExprTest.java | 45 ++++ .../com/coding/basic/stack/expr/Token.java | 50 +++++ .../coding/basic/stack/expr/TokenParser.java | 57 +++++ .../basic/stack/expr/TokenParserTest.java | 41 ++++ .../coding/basic/tree/BinarySearchTree.java | 189 +++++++++++++++++ .../basic/tree/BinarySearchTreeTest.java | 108 ++++++++++ .../com/coding/basic/tree/BinaryTreeNode.java | 36 ++++ .../com/coding/basic/tree/BinaryTreeUtil.java | 116 ++++++++++ .../coding/basic/tree/BinaryTreeUtilTest.java | 75 +++++++ .../java/com/coding/basic/tree/FileList.java | 34 +++ .../data-structure/assignment/pom.xml | 32 +++ .../coderising/download/DownloadThread.java | 20 ++ .../coderising/download/FileDownloader.java | 73 +++++++ .../download/FileDownloaderTest.java | 59 ++++++ .../coderising/download/api/Connection.java | 23 ++ .../download/api/ConnectionException.java | 5 + .../download/api/ConnectionManager.java | 10 + .../download/api/DownloadListener.java | 5 + .../download/impl/ConnectionImpl.java | 27 +++ .../download/impl/ConnectionManagerImpl.java | 15 ++ .../coderising/litestruts/LoginAction.java | 39 ++++ .../com/coderising/litestruts/Struts.java | 34 +++ .../com/coderising/litestruts/StrutsTest.java | 43 ++++ .../java/com/coderising/litestruts/View.java | 23 ++ .../java/com/coderising/litestruts/struts.xml | 11 + .../com/coderising/ood/course/bad/Course.java | 24 +++ .../ood/course/bad/CourseOffering.java | 26 +++ .../ood/course/bad/CourseService.java | 16 ++ .../coderising/ood/course/bad/Student.java | 14 ++ .../coderising/ood/course/good/Course.java | 18 ++ .../ood/course/good/CourseOffering.java | 34 +++ .../ood/course/good/CourseService.java | 14 ++ .../coderising/ood/course/good/Student.java | 21 ++ .../java/com/coderising/ood/ocp/DateUtil.java | 10 + .../java/com/coderising/ood/ocp/Logger.java | 38 ++++ .../java/com/coderising/ood/ocp/MailUtil.java | 10 + .../java/com/coderising/ood/ocp/SMSUtil.java | 10 + .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + .../main/java/com/coding/basic/Iterator.java | 7 + .../src/main/java/com/coding/basic/List.java | 9 + .../com/coding/basic/array/ArrayList.java | 35 +++ .../com/coding/basic/array/ArrayUtil.java | 96 +++++++++ .../coding/basic/linklist/LRUPageFrame.java | 57 +++++ .../basic/linklist/LRUPageFrameTest.java | 34 +++ .../com/coding/basic/linklist/LinkedList.java | 125 +++++++++++ .../com/coding/basic/queue/CircleQueue.java | 39 ++++ .../java/com/coding/basic/queue/Josephus.java | 18 ++ .../com/coding/basic/queue/JosephusTest.java | 27 +++ .../java/com/coding/basic/queue/Queue.java | 61 ++++++ .../basic/queue/QueueWithTwoStacks.java | 47 +++++ .../com/coding/basic/stack/QuickMinStack.java | 19 ++ .../java/com/coding/basic/stack/Stack.java | 24 +++ .../com/coding/basic/stack/StackUtil.java | 48 +++++ .../com/coding/basic/stack/StackUtilTest.java | 65 ++++++ .../basic/stack/StackWithTwoQueues.java | 16 ++ .../basic/stack/TwoStackInOneArray.java | 57 +++++ .../coding/basic/stack/expr/InfixExpr.java | 15 ++ .../basic/stack/expr/InfixExprTest.java | 52 +++++ .../basic/stack/expr/InfixToPostfix.java | 14 ++ .../coding/basic/stack/expr/PostfixExpr.java | 18 ++ .../basic/stack/expr/PostfixExprTest.java | 41 ++++ .../coding/basic/stack/expr/PrefixExpr.java | 18 ++ .../basic/stack/expr/PrefixExprTest.java | 45 ++++ .../com/coding/basic/stack/expr/Token.java | 50 +++++ .../coding/basic/stack/expr/TokenParser.java | 57 +++++ .../basic/stack/expr/TokenParserTest.java | 41 ++++ .../coding/basic/tree/BinarySearchTree.java | 55 +++++ .../basic/tree/BinarySearchTreeTest.java | 109 ++++++++++ .../com/coding/basic/tree/BinaryTreeNode.java | 35 +++ .../com/coding/basic/tree/BinaryTreeUtil.java | 66 ++++++ .../coding/basic/tree/BinaryTreeUtilTest.java | 75 +++++++ .../java/com/coding/basic/tree/FileList.java | 10 + .../1377699408/ood/ood-assignment/pom.xml | 32 +++ .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../coderising/ood/srp/EmailException.java | 7 + .../com/coderising/ood/srp/PromotionMail.java | 57 +++++ .../com/coderising/ood/srp/bean/Email.java | 102 +++++++++ .../com/coderising/ood/srp/bean/Product.java | 66 ++++++ .../com/coderising/ood/srp/dao/EmailDAO.java | 22 ++ .../coderising/ood/srp/product_promotion.txt | 4 + .../ood/srp/utils/CollectionUtils.java | 9 + .../coderising/ood/srp/utils/StringUtils.java | 7 + 133 files changed, 5652 insertions(+) create mode 100644 students/1377699408/data-structure/answer/pom.xml create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/View.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/Iterator.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/List.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtilTest.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java create mode 100644 students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java create mode 100644 students/1377699408/data-structure/assignment/pom.xml create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/DownloadThread.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/FileDownloader.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/FileDownloaderTest.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/api/Connection.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionException.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionManager.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/api/DownloadListener.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionImpl.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/LoginAction.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/Struts.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/StrutsTest.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/View.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/struts.xml create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Course.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseOffering.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseService.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Student.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Course.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseOffering.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseService.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Student.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/ocp/Logger.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/Iterator.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/List.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayList.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayUtil.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrame.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/linklist/LinkedList.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/CircleQueue.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/Josephus.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/JosephusTest.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/Queue.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/QuickMinStack.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/Stack.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtil.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtilTest.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExpr.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/Token.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParser.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTree.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeNode.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java create mode 100644 students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/FileList.java create mode 100644 students/1377699408/ood/ood-assignment/pom.xml create mode 100644 students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/EmailException.java create mode 100644 students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Email.java create mode 100644 students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Product.java create mode 100644 students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/dao/EmailDAO.java create mode 100644 students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt create mode 100644 students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/utils/CollectionUtils.java create mode 100644 students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/utils/StringUtils.java diff --git a/students/1377699408/data-structure/answer/pom.xml b/students/1377699408/data-structure/answer/pom.xml new file mode 100644 index 0000000000..ac6ba882df --- /dev/null +++ b/students/1377699408/data-structure/answer/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ds-answer + 0.0.1-SNAPSHOT + jar + + ds-answer + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..900a3ad358 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java @@ -0,0 +1,20 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + + } +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..c3c8a3f27d --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java @@ -0,0 +1,73 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn,0,length-1).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..4ff7f46ae0 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..36a9d2ce15 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,27 @@ +package com.coderising.download.impl; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + @Override + public int getContentLength() { + + return 0; + } + + @Override + public void close() { + + + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..172371dd55 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return null; + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java b/students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java b/students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java b/students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/View.java b/students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml b/students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/Iterator.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/List.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java new file mode 100644 index 0000000000..4576c016af --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java @@ -0,0 +1,35 @@ +package com.coding.basic.array; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java new file mode 100644 index 0000000000..45740e6d57 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coding.basic.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..24b9d8b155 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,164 @@ +package com.coding.basic.linklist; + + +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + private int currentSize; + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + this.currentSize = 0; + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + + Node node = find(pageNum); + //在该队列中存在, 则提到队列头 + if (node != null) { + + moveExistingNodeToHead(node); + + } else{ + + node = new Node(); + node.pageNum = pageNum; + + // 缓存容器是否已经超过大小. + if (currentSize >= capacity) { + removeLast(); + + } + + addNewNodetoHead(node); + + + + + } + } + + private void addNewNodetoHead(Node node) { + + if(isEmpty()){ + + node.prev = null; + node.next = null; + first = node; + last = node; + + } else{ + node.prev = null; + node.next = first; + first.prev = node; + first = node; + } + this.currentSize ++; + } + + private Node find(int data){ + + Node node = first; + while(node != null){ + if(node.pageNum == data){ + return node; + } + node = node.next; + } + return null; + + } + + + + + + + /** + * 删除链表尾部节点 表示 删除最少使用的缓存对象 + */ + private void removeLast() { + Node prev = last.prev; + prev.next = null; + last.prev = null; + last = prev; + this.currentSize --; + } + + /** + * 移动到链表头,表示这个节点是最新使用过的 + * + * @param node + */ + private void moveExistingNodeToHead(Node node) { + + if (node == first) { + + return; + } + else if(node == last){ + //当前节点是链表尾, 需要放到链表头 + Node prevNode = node.prev; + prevNode.next = null; + last.prev = null; + last = prevNode; + + } else{ + //node 在链表的中间, 把node 的前后节点连接起来 + Node prevNode = node.prev; + prevNode.next = node.next; + + Node nextNode = node.next; + nextNode.prev = prevNode; + + + } + + node.prev = null; + node.next = first; + first.prev = node; + first = node; + + } + private boolean isEmpty(){ + return (first == null) && (last == null); + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + + + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..7fd72fc2b4 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,34 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + frame.access(5); + Assert.assertEquals("5,4,0", frame.toString()); + + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java new file mode 100644 index 0000000000..f4c7556a2e --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java @@ -0,0 +1,125 @@ +package com.coding.basic.linklist; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java new file mode 100644 index 0000000000..f169d5f8e4 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java @@ -0,0 +1,47 @@ +package com.coding.basic.queue; + +public class CircleQueue { + + //用数组来保存循环队列的元素 + private Object[] elementData ; + int size = 0; + //队头 + private int front = 0; + //队尾 + private int rear = 0; + + public CircleQueue(int capacity){ + elementData = new Object[capacity]; + } + public boolean isEmpty() { + return (front == rear) && !isFull(); + + } + + public boolean isFull(){ + return size == elementData.length; + } + public int size() { + return size; + } + + public void enQueue(E data) { + if(isFull()){ + throw new RuntimeException("The queue is full"); + } + rear = (rear+1) % elementData.length; + elementData[rear++] = data; + size++; + } + + public E deQueue() { + if(isEmpty()){ + throw new RuntimeException("The queue is empty"); + } + E data = (E)elementData[front]; + elementData[front] = null; + front = (front+1) % elementData.length; + size --; + return data; + } +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java new file mode 100644 index 0000000000..7307eb77d4 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java @@ -0,0 +1,44 @@ +package com.coding.basic.queue; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class CircleQueueTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + CircleQueue queue = new CircleQueue(5); + Assert.assertTrue(queue.isEmpty()); + Assert.assertFalse(queue.isFull()); + + queue.enQueue("a"); + queue.enQueue("b"); + queue.enQueue("c"); + queue.enQueue("d"); + queue.enQueue("e"); + + Assert.assertTrue(queue.isFull()); + Assert.assertFalse(queue.isEmpty()); + Assert.assertEquals(5, queue.size()); + + Assert.assertEquals("a", queue.deQueue()); + Assert.assertEquals("b", queue.deQueue()); + Assert.assertEquals("c", queue.deQueue()); + Assert.assertEquals("d", queue.deQueue()); + Assert.assertEquals("e", queue.deQueue()); + + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java new file mode 100644 index 0000000000..36ec615d36 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java @@ -0,0 +1,39 @@ +package com.coding.basic.queue; + +import java.util.ArrayList; +import java.util.List; + +/** + * 用Queue来实现Josephus问题 + * 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来 + * @author liuxin + * + */ +public class Josephus { + + public static List execute(int n, int m){ + + Queue queue = new Queue(); + for (int i = 0; i < n; i++){ + queue.enQueue(i); + } + + List result = new ArrayList(); + int i = 0; + + while (!queue.isEmpty()) { + + int x = queue.deQueue(); + + if (++i % m == 0){ + result.add(x); + } else{ + queue.enQueue(x); + } + } + + + return result; + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java new file mode 100644 index 0000000000..7d90318b51 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java @@ -0,0 +1,27 @@ +package com.coding.basic.queue; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class JosephusTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testExecute() { + + Assert.assertEquals("[1, 3, 5, 0, 4, 2, 6]", Josephus.execute(7, 2).toString()); + + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java new file mode 100644 index 0000000000..c4c4b7325e --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java @@ -0,0 +1,61 @@ +package com.coding.basic.queue; + +import java.util.NoSuchElementException; + +public class Queue { + private Node first; + private Node last; + private int size; + + + private static class Node { + private E item; + private Node next; + } + + + public Queue() { + first = null; + last = null; + size = 0; + } + + + public boolean isEmpty() { + return first == null; + } + + public int size() { + return size; + } + + + + public void enQueue(E data) { + Node oldlast = last; + last = new Node(); + last.item = data; + last.next = null; + if (isEmpty()) { + first = last; + } + else{ + oldlast.next = last; + } + size++; + } + + public E deQueue() { + if (isEmpty()) { + throw new NoSuchElementException("Queue underflow"); + } + E item = first.item; + first = first.next; + size--; + if (isEmpty()) { + last = null; + } + return item; + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java new file mode 100644 index 0000000000..bc97df0800 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java @@ -0,0 +1,55 @@ +package com.coding.basic.queue; + +import java.util.NoSuchElementException; +import java.util.Stack; + +public class QueueWithTwoStacks { + private Stack stack1; + private Stack stack2; + + + public QueueWithTwoStacks() { + stack1 = new Stack(); + stack2 = new Stack(); + } + + + private void moveStack1ToStack2() { + while (!stack1.isEmpty()){ + stack2.push(stack1.pop()); + } + + } + + + public boolean isEmpty() { + return stack1.isEmpty() && stack2.isEmpty(); + } + + + + public int size() { + return stack1.size() + stack2.size(); + } + + + + public void enQueue(E item) { + stack1.push(item); + } + + public E deQueue() { + if (isEmpty()) { + throw new NoSuchElementException("Queue is empty"); + } + if (stack2.isEmpty()) { + moveStack1ToStack2(); + } + + return stack2.pop(); + } + + + + } + diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java new file mode 100644 index 0000000000..faf2644ab1 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java @@ -0,0 +1,44 @@ +package com.coding.basic.stack; + +import java.util.Stack; +/** + * 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素 + * finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值 + * @author liuxin + * + */ +public class QuickMinStack { + + private Stack normalStack = new Stack(); + private Stack minNumStack = new Stack(); + + public void push(int data){ + + normalStack.push(data); + + if(minNumStack.isEmpty()){ + minNumStack.push(data); + } else{ + if(minNumStack.peek() >= data) { + minNumStack.push(data); + } + } + + } + public int pop(){ + if(normalStack.isEmpty()){ + throw new RuntimeException("the stack is empty"); + } + int value = normalStack.pop(); + if(value == minNumStack.peek()){ + minNumStack.pop(); + } + return value; + } + public int findMin(){ + if(minNumStack.isEmpty()){ + throw new RuntimeException("the stack is empty"); + } + return minNumStack.peek(); + } +} \ No newline at end of file diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java new file mode 100644 index 0000000000..efe41a9f8f --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java @@ -0,0 +1,39 @@ +package com.coding.basic.stack; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class QuickMinStackTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + QuickMinStack stack = new QuickMinStack(); + stack.push(5); + Assert.assertEquals(5, stack.findMin()); + stack.push(6); + Assert.assertEquals(5, stack.findMin()); + stack.push(4); + Assert.assertEquals(4, stack.findMin()); + stack.push(4); + Assert.assertEquals(4, stack.findMin()); + + stack.pop(); + Assert.assertEquals(4, stack.findMin()); + stack.pop(); + Assert.assertEquals(5, stack.findMin()); + stack.pop(); + Assert.assertEquals(5, stack.findMin()); + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java new file mode 100644 index 0000000000..fedb243604 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java @@ -0,0 +1,24 @@ +package com.coding.basic.stack; + +import com.coding.basic.array.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java new file mode 100644 index 0000000000..7c86d22fe7 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java @@ -0,0 +1,168 @@ +package com.coding.basic.stack; +import java.util.Stack; +public class StackUtil { + + public static void bad_reverse(Stack s) { + if(s == null || s.isEmpty()){ + return; + } + Stack tmpStack = new Stack(); + while(!s.isEmpty()){ + tmpStack.push(s.pop()); + } + + s = tmpStack; + + } + + + + public static void reverse_247565311(Stack s){ + if(s == null || s.isEmpty()) { + return; + } + + int size = s.size(); + Stack tmpStack = new Stack(); + + for(int i=0;ii){ + tmpStack.push(s.pop()); + } + s.push(top); + while(tmpStack.size()>0){ + s.push(tmpStack.pop()); + } + } + } + + + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + if(s == null || s.isEmpty()){ + return; + } + + Stack tmp = new Stack(); + while(!s.isEmpty()){ + tmp.push(s.pop()); + } + while(!tmp.isEmpty()){ + Integer top = tmp.pop(); + addToBottom(s,top); + } + + + } + public static void addToBottom(Stack s, Integer value){ + if(s.isEmpty()){ + s.push(value); + } else{ + Integer top = s.pop(); + addToBottom(s,value); + s.push(top); + } + + } + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s,Object o) { + if(s == null || s.isEmpty()){ + return; + } + Stack tmpStack = new Stack(); + + while(!s.isEmpty()){ + Object value = s.pop(); + if(!value.equals(o)){ + tmpStack.push(value); + } + } + + while(!tmpStack.isEmpty()){ + s.push(tmpStack.pop()); + } + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) { + + if(s == null || s.isEmpty() || s.size() stack = new Stack(); + for(int i=0;i s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + + StackUtil.addToBottom(s, 0); + + Assert.assertEquals("[0, 1, 2, 3]", s.toString()); + + } + @Test + public void testReverse() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); + StackUtil.reverse(s); + Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString()); + } + @Test + public void testReverse_247565311() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + + Assert.assertEquals("[1, 2, 3]", s.toString()); + StackUtil.reverse_247565311(s); + Assert.assertEquals("[3, 2, 1]", s.toString()); + } + @Test + public void testRemove() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + StackUtil.remove(s, 2); + Assert.assertEquals("[1, 3]", s.toString()); + } + + @Test + public void testGetTop() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + { + Object[] values = StackUtil.getTop(s, 3); + Assert.assertEquals(5, values[0]); + Assert.assertEquals(4, values[1]); + Assert.assertEquals(3, values[2]); + } + } + + @Test + public void testIsValidPairs() { + Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])")); + Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})")); + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java new file mode 100644 index 0000000000..7a58fbff56 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java @@ -0,0 +1,53 @@ +package com.coding.basic.stack; + +import java.util.ArrayDeque; +import java.util.Queue; + +public class StackWithTwoQueues { + Queue queue1 = new ArrayDeque<>(); + Queue queue2 = new ArrayDeque<>(); + + public void push(int data) { + //两个栈都为空时,优先考虑queue1 + if (queue1.isEmpty()&&queue2.isEmpty()) { + queue1.add(data); + return; + } + + if (queue1.isEmpty()) { + queue2.add(data); + return; + } + + if (queue2.isEmpty()) { + queue1.add(data); + return; + } + + } + + public int pop() { + + if (queue1.isEmpty()&&queue2.isEmpty()) { + throw new RuntimeException("stack is empty"); + } + + if (queue1.isEmpty()) { + while (queue2.size()>1) { + queue1.add(queue2.poll()); + } + return queue2.poll(); + } + + if (queue2.isEmpty()) { + while (queue1.size()>1) { + queue2.add(queue1.poll()); + } + return queue1.poll(); + } + + throw new RuntimeException("no queue is empty, this is not allowed"); + + + } +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java new file mode 100644 index 0000000000..4541b1f040 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java @@ -0,0 +1,36 @@ +package com.coding.basic.stack; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class StackWithTwoQueuesTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + StackWithTwoQueues stack = new StackWithTwoQueues(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + Assert.assertEquals(4, stack.pop()); + Assert.assertEquals(3, stack.pop()); + + stack.push(5); + Assert.assertEquals(5, stack.pop()); + Assert.assertEquals(2, stack.pop()); + Assert.assertEquals(1, stack.pop()); + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java new file mode 100644 index 0000000000..7f30ce55c8 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java @@ -0,0 +1,5 @@ +package com.coding.basic.stack; + +public class Tail { + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java new file mode 100644 index 0000000000..a532fd6e6c --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java @@ -0,0 +1,117 @@ +package com.coding.basic.stack; + +import java.util.Arrays; + +/** + * 用一个数组实现两个栈 + * 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。 + * @author liuxin + * + */ +public class TwoStackInOneArray { + private Object[] data = new Object[10]; + private int size; + private int top1, top2; + + public TwoStackInOneArray(int n){ + data = new Object[n]; + size = n; + top1 = -1; + top2 = data.length; + } + /** + * 向第一个栈中压入元素 + * @param o + */ + public void push1(Object o){ + ensureCapacity(); + data[++top1] = o; + } + /* + * 向第二个栈压入元素 + */ + public void push2(Object o){ + ensureCapacity(); + data[--top2] = o; + } + public void ensureCapacity(){ + if(top2-top1>1){ + return; + } else{ + + Object[] newArray = new Object[data.length*2]; + System.arraycopy(data, 0, newArray, 0, top1+1); + + int stack2Size = data.length-top2; + int newTop2 = newArray.length-stack2Size; + System.arraycopy(data, top2, newArray, newTop2, stack2Size); + + top2 = newTop2; + data = newArray; + } + } + /** + * 从第一个栈中弹出元素 + * @return + */ + public Object pop1(){ + if(top1 == -1){ + throw new RuntimeException("Stack1 is empty"); + } + Object o = data[top1]; + data[top1] = null; + top1--; + return o; + + } + /** + * 从第二个栈弹出元素 + * @return + */ + public Object pop2(){ + if(top2 == data.length){ + throw new RuntimeException("Stack2 is empty"); + } + Object o = data[top2]; + data[top2] = null; + top2++; + return o; + } + /** + * 获取第一个栈的栈顶元素 + * @return + */ + + public Object peek1(){ + if(top1 == -1){ + throw new RuntimeException("Stack1 is empty"); + } + return data[top1]; + } + + + /** + * 获取第二个栈的栈顶元素 + * @return + */ + + public Object peek2(){ + if(top2 == data.length){ + throw new RuntimeException("Stack2 is empty"); + } + return data[top2]; + } + + public Object[] stack1ToArray(){ + return Arrays.copyOf(data, top1+1); + } + public Object[] stack2ToArray(){ + int size = data.length-top2; + Object [] stack2Data = new Object[size]; + int j=0; + for(int i=data.length-1; i>=top2 ;i--){ + stack2Data[j++] = data[i]; + } + return stack2Data; + } +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java new file mode 100644 index 0000000000..b743d422c6 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java @@ -0,0 +1,65 @@ +package com.coding.basic.stack; + +import java.util.Arrays; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class TwoStackInOneArrayTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test1() { + TwoStackInOneArray stack = new TwoStackInOneArray(10); + stack.push1(1); + stack.push1(2); + stack.push1(3); + stack.push1(4); + stack.push1(5); + + stack.push2(1); + stack.push2(2); + stack.push2(3); + stack.push2(4); + stack.push2(5); + + for(int i=1;i<=5;i++){ + Assert.assertEquals(stack.peek1(), stack.peek2()); + Assert.assertEquals(stack.pop1(), stack.pop2()); + } + + + } + @Test + public void test2() { + TwoStackInOneArray stack = new TwoStackInOneArray(5); + stack.push1(1); + stack.push1(2); + stack.push1(3); + stack.push1(4); + stack.push1(5); + stack.push1(6); + stack.push1(7); + + stack.push2(1); + stack.push2(2); + stack.push2(3); + stack.push2(4); + + + Assert.assertEquals("[1, 2, 3, 4, 5, 6, 7]",Arrays.toString(stack.stack1ToArray())); + Assert.assertEquals("[1, 2, 3, 4]",Arrays.toString(stack.stack2ToArray())); + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java new file mode 100644 index 0000000000..cebef21fa3 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java @@ -0,0 +1,72 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + + +public class InfixExpr { + String expr = null; + + public InfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + + + TokenParser parser = new TokenParser(); + List tokens = parser.parse(this.expr); + + + Stack opStack = new Stack<>(); + Stack numStack = new Stack<>(); + + for(Token token : tokens){ + + if (token.isOperator()){ + + while(!opStack.isEmpty() + && !token.hasHigherPriority(opStack.peek())){ + Token prevOperator = opStack.pop(); + Float f2 = numStack.pop(); + Float f1 = numStack.pop(); + Float result = calculate(prevOperator.toString(), f1,f2); + numStack.push(result); + + } + opStack.push(token); + } + if(token.isNumber()){ + numStack.push(new Float(token.getIntValue())); + } + } + + while(!opStack.isEmpty()){ + Token token = opStack.pop(); + Float f2 = numStack.pop(); + Float f1 = numStack.pop(); + numStack.push(calculate(token.toString(), f1,f2)); + } + + + return numStack.pop().floatValue(); + } + private Float calculate(String op, Float f1, Float f2){ + if(op.equals("+")){ + return f1+f2; + } + if(op.equals("-")){ + return f1-f2; + } + if(op.equals("*")){ + return f1*f2; + } + if(op.equals("/")){ + return f1/f2; + } + throw new RuntimeException(op + " is not supported"); + } + + + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java new file mode 100644 index 0000000000..20e34e8852 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java @@ -0,0 +1,52 @@ +package com.coding.basic.stack.expr; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class InfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); + { + InfixExpr expr = new InfixExpr("2+3*4+5"); + Assert.assertEquals(19.0, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); + Assert.assertEquals(100.0, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("3*20/2"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("20/2*3"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("10-30+50"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("10-2*3+50"); + Assert.assertEquals(54, expr.evaluate(), 0.001f); + } + + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java new file mode 100644 index 0000000000..9e501eda20 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java @@ -0,0 +1,43 @@ +package com.coding.basic.stack.expr; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class InfixToPostfix { + + public static List convert(String expr) { + List inFixTokens = new TokenParser().parse(expr); + + List postFixTokens = new ArrayList<>(); + + Stack opStack = new Stack(); + for(Token token : inFixTokens){ + + if(token.isOperator()){ + + while(!opStack.isEmpty() + && !token.hasHigherPriority(opStack.peek())){ + postFixTokens.add(opStack.pop()); + + } + opStack.push(token); + + } + if(token.isNumber()){ + + postFixTokens.add(token); + + } + } + + while(!opStack.isEmpty()){ + postFixTokens.add(opStack.pop()); + } + + return postFixTokens; + } + + + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java new file mode 100644 index 0000000000..f879f55f14 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java @@ -0,0 +1,41 @@ +package com.coding.basic.stack.expr; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class InfixToPostfixTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testConvert() { + { + List tokens = InfixToPostfix.convert("2+3"); + Assert.assertEquals("[2, 3, +]", tokens.toString()); + } + { + + List tokens = InfixToPostfix.convert("2+3*4"); + Assert.assertEquals("[2, 3, 4, *, +]", tokens.toString()); + } + + { + + List tokens = InfixToPostfix.convert("2-3*4+5"); + Assert.assertEquals("[2, 3, 4, *, -, 5, +]", tokens.toString()); + } + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java new file mode 100644 index 0000000000..c54eb69e2a --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java @@ -0,0 +1,46 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + +public class PostfixExpr { +String expr = null; + + public PostfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + TokenParser parser = new TokenParser(); + List tokens = parser.parse(this.expr); + + + Stack numStack = new Stack<>(); + for(Token token : tokens){ + if(token.isNumber()){ + numStack.push(new Float(token.getIntValue())); + } else{ + Float f2 = numStack.pop(); + Float f1 = numStack.pop(); + numStack.push(calculate(token.toString(),f1,f2)); + } + } + return numStack.pop().floatValue(); + } + + private Float calculate(String op, Float f1, Float f2){ + if(op.equals("+")){ + return f1+f2; + } + if(op.equals("-")){ + return f1-f2; + } + if(op.equals("*")){ + return f1*f2; + } + if(op.equals("/")){ + return f1/f2; + } + throw new RuntimeException(op + " is not supported"); + } +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java new file mode 100644 index 0000000000..c0435a2db5 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java @@ -0,0 +1,41 @@ +package com.coding.basic.stack.expr; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class PostfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + { + PostfixExpr expr = new PostfixExpr("6 5 2 3 + 8 * + 3 + *"); + Assert.assertEquals(288, expr.evaluate(),0.0f); + } + { + //9+(3-1)*3+10/2 + PostfixExpr expr = new PostfixExpr("9 3 1-3*+ 10 2/+"); + Assert.assertEquals(20, expr.evaluate(),0.0f); + } + + { + //10-2*3+50 + PostfixExpr expr = new PostfixExpr("10 2 3 * - 50 +"); + Assert.assertEquals(54, expr.evaluate(),0.0f); + } + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java new file mode 100644 index 0000000000..f811fd6d9a --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java @@ -0,0 +1,52 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + +public class PrefixExpr { + String expr = null; + + public PrefixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + TokenParser parser = new TokenParser(); + List tokens = parser.parse(this.expr); + + Stack exprStack = new Stack<>(); + Stack numStack = new Stack<>(); + for(Token token : tokens){ + exprStack.push(token); + } + + while(!exprStack.isEmpty()){ + Token t = exprStack.pop(); + if(t.isNumber()){ + numStack.push(new Float(t.getIntValue())); + }else{ + Float f1 = numStack.pop(); + Float f2 = numStack.pop(); + numStack.push(calculate(t.toString(),f1,f2)); + + } + } + return numStack.pop().floatValue(); + } + + private Float calculate(String op, Float f1, Float f2){ + if(op.equals("+")){ + return f1+f2; + } + if(op.equals("-")){ + return f1-f2; + } + if(op.equals("*")){ + return f1*f2; + } + if(op.equals("/")){ + return f1/f2; + } + throw new RuntimeException(op + " is not supported"); + } +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java new file mode 100644 index 0000000000..5cec210e75 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java @@ -0,0 +1,45 @@ +package com.coding.basic.stack.expr; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class PrefixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + { + // 2*3+4*5 + PrefixExpr expr = new PrefixExpr("+ * 2 3* 4 5"); + Assert.assertEquals(26, expr.evaluate(),0.001f); + } + { + // 4*2 + 6+9*2/3 -8 + PrefixExpr expr = new PrefixExpr("-++6/*2 9 3 * 4 2 8"); + Assert.assertEquals(12, expr.evaluate(),0.001f); + } + { + //(3+4)*5-6 + PrefixExpr expr = new PrefixExpr("- * + 3 4 5 6"); + Assert.assertEquals(29, expr.evaluate(),0.001f); + } + { + //1+((2+3)*4)-5 + PrefixExpr expr = new PrefixExpr("- + 1 * + 2 3 4 5"); + Assert.assertEquals(16, expr.evaluate(),0.001f); + } + + + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java new file mode 100644 index 0000000000..8579743fe9 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java @@ -0,0 +1,50 @@ +package com.coding.basic.stack.expr; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +class Token { + public static final List OPERATORS = Arrays.asList("+", "-", "*", "/"); + private static final Map priorities = new HashMap<>(); + static { + priorities.put("+", 1); + priorities.put("-", 1); + priorities.put("*", 2); + priorities.put("/", 2); + } + static final int OPERATOR = 1; + static final int NUMBER = 2; + String value; + int type; + public Token(int type, String value){ + this.type = type; + this.value = value; + } + + public boolean isNumber() { + return type == NUMBER; + } + + public boolean isOperator() { + return type == OPERATOR; + } + + public int getIntValue() { + return Integer.valueOf(value).intValue(); + } + public String toString(){ + return value; + } + + public boolean hasHigherPriority(Token t){ + if(!this.isOperator() && !t.isOperator()){ + throw new RuntimeException("numbers can't compare priority"); + } + return priorities.get(this.value) - priorities.get(t.value) > 0; + } + + + +} \ No newline at end of file diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java new file mode 100644 index 0000000000..d3b0f167e1 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java @@ -0,0 +1,57 @@ +package com.coding.basic.stack.expr; + +import java.util.ArrayList; +import java.util.List; + +public class TokenParser { + + + public List parse(String expr) { + List tokens = new ArrayList<>(); + + int i = 0; + + while (i < expr.length()) { + + char c = expr.charAt(i); + + if (isOperator(c)) { + + Token t = new Token(Token.OPERATOR, String.valueOf(c)); + tokens.add(t); + i++; + + } else if (Character.isDigit(c)) { + + int nextOperatorIndex = indexOfNextOperator(i, expr); + String value = expr.substring(i, nextOperatorIndex); + Token t = new Token(Token.NUMBER, value); + tokens.add(t); + i = nextOperatorIndex; + + } else{ + System.out.println("char :["+c+"] is not number or operator,ignore"); + i++; + } + + } + return tokens; + } + + private int indexOfNextOperator(int i, String expr) { + + while (Character.isDigit(expr.charAt(i))) { + i++; + if (i == expr.length()) { + break; + } + } + return i; + + } + + private boolean isOperator(char c) { + String sc = String.valueOf(c); + return Token.OPERATORS.contains(sc); + } +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java new file mode 100644 index 0000000000..399d3e857e --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java @@ -0,0 +1,41 @@ +package com.coding.basic.stack.expr; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TokenParserTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + + TokenParser parser = new TokenParser(); + List tokens = parser.parse("300*20+12*5-20/4"); + + Assert.assertEquals(300, tokens.get(0).getIntValue()); + Assert.assertEquals("*", tokens.get(1).toString()); + Assert.assertEquals(20, tokens.get(2).getIntValue()); + Assert.assertEquals("+", tokens.get(3).toString()); + Assert.assertEquals(12, tokens.get(4).getIntValue()); + Assert.assertEquals("*", tokens.get(5).toString()); + Assert.assertEquals(5, tokens.get(6).getIntValue()); + Assert.assertEquals("-", tokens.get(7).toString()); + Assert.assertEquals(20, tokens.get(8).getIntValue()); + Assert.assertEquals("/", tokens.get(9).toString()); + Assert.assertEquals(4, tokens.get(10).getIntValue()); + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java new file mode 100644 index 0000000000..284e5b0011 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java @@ -0,0 +1,189 @@ +package com.coding.basic.tree; + +import java.util.ArrayList; +import java.util.List; + +import com.coding.basic.queue.Queue; + + +public class BinarySearchTree { + + BinaryTreeNode root; + public BinarySearchTree(BinaryTreeNode root){ + this.root = root; + } + public BinaryTreeNode getRoot(){ + return root; + } + public T findMin(){ + if(root == null){ + return null; + } + return findMin(root).data; + } + public T findMax(){ + if(root == null){ + return null; + } + return findMax(root).data; + } + public int height() { + return height(root); + } + public int size() { + return size(root); + } + public void remove(T e){ + remove(e, root); + } + + private BinaryTreeNode remove(T x, BinaryTreeNode t){ + if(t == null){ + return t; + } + int compareResult = x.compareTo(t.data); + + if(compareResult< 0 ){ + t.left = remove(x,t.left); + + } else if(compareResult > 0){ + t.right = remove(x, t.right); + + } else { + if(t.left != null && t.right != null){ + + t.data = findMin(t.right).data; + t.right = remove(t.data,t.right); + + } else{ + t = (t.left != null) ? t.left : t.right; + } + } + return t; + } + + private BinaryTreeNode findMin(BinaryTreeNode p){ + if (p==null){ + return null; + } else if (p.left == null){ + return p; + } else{ + return findMin(p.left); + } + } + private BinaryTreeNode findMax(BinaryTreeNode p){ + if (p==null){ + return null; + }else if (p.right==null){ + return p; + } else{ + return findMax(p.right); + } + } + private int height(BinaryTreeNode t){ + if (t==null){ + return 0; + }else { + int leftChildHeight=height(t.left); + int rightChildHeight=height(t.right); + if(leftChildHeight > rightChildHeight){ + return leftChildHeight+1; + } else{ + return rightChildHeight+1; + } + } + } + private int size(BinaryTreeNode t){ + if (t == null){ + return 0; + } + return size(t.left) + 1 + size(t.right); + + } + + public List levelVisit(){ + List result = new ArrayList(); + if(root == null){ + return result; + } + Queue> queue = new Queue>(); + BinaryTreeNode node = root; + queue.enQueue(node); + while (!queue.isEmpty()) { + node = queue.deQueue(); + result.add(node.data); + if (node.left != null){ + queue.enQueue(node.left); + } + if (node.right != null){ + queue.enQueue(node.right); + } + } + return result; + } + public boolean isValid(){ + return isValid(root); + } + public T getLowestCommonAncestor(T n1, T n2){ + if (root == null){ + return null; + } + return lowestCommonAncestor(root,n1,n2); + + } + public List getNodesBetween(T n1, T n2){ + List elements = new ArrayList<>(); + getNodesBetween(elements,root,n1,n2); + return elements; + } + + public void getNodesBetween(List elements ,BinaryTreeNode node, T n1, T n2){ + + if (node == null) { + return; + } + + if (n1.compareTo(node.data) < 0) { + getNodesBetween(elements,node.left, n1, n2); + } + + if ((n1.compareTo(node.data) <= 0 ) + && (n2.compareTo(node.data) >= 0 )) { + elements.add(node.data); + } + if (n2.compareTo(node.data)>0) { + getNodesBetween(elements,node.right, n1, n2); + } + } + private T lowestCommonAncestor(BinaryTreeNode node,T n1, T n2){ + if(node == null){ + return null; + } + // 如果n1和n2都比 node的值小, LCA在左孩子 + if (node.data.compareTo(n1) > 0 && node.data.compareTo(n2) >0){ + return lowestCommonAncestor(node.left, n1, n2); + } + + // 如果n1和n2都比 node的值小, LCA在右孩子 + if (node.data.compareTo(n1) < 0 && node.data.compareTo(n2) <0) + return lowestCommonAncestor(node.right, n1, n2); + + return node.data; + } + private boolean isValid(BinaryTreeNode t){ + if(t == null){ + return true; + } + if(t.left != null && findMax(t.left).data.compareTo(t.data) >0){ + return false; + } + if(t.right !=null && findMin(t.right).data.compareTo(t.data) <0){ + return false; + } + if(!isValid(t.left) || !isValid(t.right)){ + return false; + } + return true; + } +} + diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java new file mode 100644 index 0000000000..590e60306c --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java @@ -0,0 +1,108 @@ +package com.coding.basic.tree; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class BinarySearchTreeTest { + + BinarySearchTree tree = null; + + @Before + public void setUp() throws Exception { + BinaryTreeNode root = new BinaryTreeNode(6); + root.left = new BinaryTreeNode(2); + root.right = new BinaryTreeNode(8); + root.left.left = new BinaryTreeNode(1); + root.left.right = new BinaryTreeNode(4); + root.left.right.left = new BinaryTreeNode(3); + root.left.right.right = new BinaryTreeNode(5); + tree = new BinarySearchTree(root); + } + + @After + public void tearDown() throws Exception { + tree = null; + } + + @Test + public void testFindMin() { + Assert.assertEquals(1, tree.findMin().intValue()); + + } + + @Test + public void testFindMax() { + Assert.assertEquals(8, tree.findMax().intValue()); + } + + @Test + public void testHeight() { + Assert.assertEquals(4, tree.height()); + } + + @Test + public void testSize() { + Assert.assertEquals(7, tree.size()); + } + + @Test + public void testRemoveLeaf() { + tree.remove(3); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(4, root.left.right.data.intValue()); + + } + @Test + public void testRemoveMiddleNode1() { + tree.remove(4); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(5, root.left.right.data.intValue()); + Assert.assertEquals(3, root.left.right.left.data.intValue()); + } + @Test + public void testRemoveMiddleNode2() { + tree.remove(2); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(3, root.left.data.intValue()); + Assert.assertEquals(4, root.left.right.data.intValue()); + } + + @Test + public void testLevelVisit() { + List values = tree.levelVisit(); + Assert.assertEquals("[6, 2, 8, 1, 4, 3, 5]", values.toString()); + + } + @Test + public void testLCA(){ + Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 5).intValue()); + Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 4).intValue()); + Assert.assertEquals(6,tree.getLowestCommonAncestor(3, 8).intValue()); + } + @Test + public void testIsValid() { + + Assert.assertTrue(tree.isValid()); + + BinaryTreeNode root = new BinaryTreeNode(6); + root.left = new BinaryTreeNode(2); + root.right = new BinaryTreeNode(8); + root.left.left = new BinaryTreeNode(4); + root.left.right = new BinaryTreeNode(1); + root.left.right.left = new BinaryTreeNode(3); + tree = new BinarySearchTree(root); + + Assert.assertFalse(tree.isValid()); + } + @Test + public void testGetNodesBetween(){ + List numbers = this.tree.getNodesBetween(3, 8); + Assert.assertEquals("[3, 4, 5, 6, 8]",numbers.toString()); + } +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java new file mode 100644 index 0000000000..3f6f4d2b44 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java @@ -0,0 +1,36 @@ +package com.coding.basic.tree; + +public class BinaryTreeNode { + + public T data; + public BinaryTreeNode left; + public BinaryTreeNode right; + + public BinaryTreeNode(T data){ + this.data=data; + } + public T getData() { + return data; + } + public void setData(T data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java new file mode 100644 index 0000000000..f2a6515fa6 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java @@ -0,0 +1,116 @@ +package com.coding.basic.tree; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class BinaryTreeUtil { + /** + * 用递归的方式实现对二叉树的前序遍历, 需要通过BinaryTreeUtilTest测试 + * + * @param root + * @return + */ + public static List preOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + preOrderVisit(root, result); + return result; + } + + /** + * 用递归的方式实现对二叉树的中遍历 + * + * @param root + * @return + */ + public static List inOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + inOrderVisit(root, result); + return result; + } + + /** + * 用递归的方式实现对二叉树的后遍历 + * + * @param root + * @return + */ + public static List postOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + postOrderVisit(root, result); + return result; + } + + public static List preOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + Stack> stack = new Stack>(); + + BinaryTreeNode node = root; + + if(node != null){ + stack.push(node); + } + + while(!stack.isEmpty()){ + node = stack.pop(); + result.add(node.data); + + if(node.right != null){ + stack.push(node.right); + } + + if(node.left != null){ + stack.push(node.right); + } + } + return result; + } + + public static List inOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + BinaryTreeNode node = root; + Stack> stack = new Stack>(); + + while (node != null || !stack.isEmpty()) { + + while (node != null) { + stack.push(node); + node = node.left; + } + BinaryTreeNode currentNode = stack.pop(); + result.add(currentNode.data); + node = currentNode.right; + } + return result; + } + + private static void preOrderVisit(BinaryTreeNode node, List result) { + if (node == null) { + return; + } + result.add(node.getData()); + preOrderVisit(node.getLeft(), result); + preOrderVisit(node.getRight(), result); + } + + private static void inOrderVisit(BinaryTreeNode node, List result) { + if (node == null) { + return; + } + inOrderVisit(node.getLeft(), result); + result.add(node.getData()); + inOrderVisit(node.getRight(), result); + } + + private static void postOrderVisit(BinaryTreeNode node, List result) { + if (node == null) { + return; + } + postOrderVisit(node.getLeft(), result); + postOrderVisit(node.getRight(), result); + result.add(node.getData()); + } + +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java new file mode 100644 index 0000000000..41857e137d --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java @@ -0,0 +1,75 @@ +package com.coding.basic.tree; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class BinaryTreeUtilTest { + + BinaryTreeNode root = null; + @Before + public void setUp() throws Exception { + root = new BinaryTreeNode(1); + root.setLeft(new BinaryTreeNode(2)); + root.setRight(new BinaryTreeNode(5)); + root.getLeft().setLeft(new BinaryTreeNode(3)); + root.getLeft().setRight(new BinaryTreeNode(4)); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testPreOrderVisit() { + + List result = BinaryTreeUtil.preOrderVisit(root); + Assert.assertEquals("[1, 2, 3, 4, 5]", result.toString()); + + + } + @Test + public void testInOrderVisit() { + + + List result = BinaryTreeUtil.inOrderVisit(root); + Assert.assertEquals("[3, 2, 4, 1, 5]", result.toString()); + + } + + @Test + public void testPostOrderVisit() { + + + List result = BinaryTreeUtil.postOrderVisit(root); + Assert.assertEquals("[3, 4, 2, 5, 1]", result.toString()); + + } + + + @Test + public void testInOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.inOrderWithoutRecursion(root); + Assert.assertEquals("[3, 2, 6, 4, 7, 1, 5]", result.toString()); + + } + @Test + public void testPreOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.preOrderWithoutRecursion(root); + Assert.assertEquals("[1, 2, 3, 4, 6, 7, 5]", result.toString()); + + } +} diff --git a/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java new file mode 100644 index 0000000000..85fb8ab2a4 --- /dev/null +++ b/students/1377699408/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java @@ -0,0 +1,34 @@ +package com.coding.basic.tree; + +import java.io.File; + +public class FileList { + public void list(File f) { + list(f, 0); + } + + public void list(File f, int depth) { + printName(f, depth); + if (f.isDirectory()) { + File[] files = f.listFiles(); + for (File i : files) + list(i, depth + 1); + } + } + + void printName(File f, int depth) { + String name = f.getName(); + for (int i = 0; i < depth; i++) + System.out.print("+"); + if (f.isDirectory()) + System.out.println("Dir: " + name); + else + System.out.println(f.getName() + " " + f.length()); + } + + public static void main(String args[]) { + FileList L = new FileList(); + File f = new File("C:\\coderising\\tmp"); + L.list(f); + } +} diff --git a/students/1377699408/data-structure/assignment/pom.xml b/students/1377699408/data-structure/assignment/pom.xml new file mode 100644 index 0000000000..5024466d17 --- /dev/null +++ b/students/1377699408/data-structure/assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ds-assignment + 0.0.1-SNAPSHOT + jar + + ds-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/DownloadThread.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..900a3ad358 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/DownloadThread.java @@ -0,0 +1,20 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/FileDownloader.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..c3c8a3f27d --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/FileDownloader.java @@ -0,0 +1,73 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn,0,length-1).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/FileDownloaderTest.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..4ff7f46ae0 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/api/Connection.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionException.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionManager.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/api/DownloadListener.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionImpl.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..36a9d2ce15 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,27 @@ +package com.coderising.download.impl; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + @Override + public int getContentLength() { + + return 0; + } + + @Override + public void close() { + + + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..172371dd55 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return null; + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/LoginAction.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/Struts.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/StrutsTest.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/View.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/struts.xml b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Course.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Course.java new file mode 100644 index 0000000000..436d092f58 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Course.java @@ -0,0 +1,24 @@ +package com.coderising.ood.course.bad; + +import java.util.List; + +public class Course { + private String id; + private String desc; + private int duration ; + + List prerequisites; + + public List getPrerequisites() { + return prerequisites; + } + + + public boolean equals(Object o){ + if(o == null || !(o instanceof Course)){ + return false; + } + Course c = (Course)o; + return (c != null) && c.id.equals(id); + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseOffering.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseOffering.java new file mode 100644 index 0000000000..ab8c764584 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseOffering.java @@ -0,0 +1,26 @@ +package com.coderising.ood.course.bad; + +import java.util.ArrayList; +import java.util.List; + + +public class CourseOffering { + private Course course; + private String location; + private String teacher; + private int maxStudents; + + List students = new ArrayList(); + + public int getMaxStudents() { + return maxStudents; + } + + public List getStudents() { + return students; + } + + public Course getCourse() { + return course; + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseService.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseService.java new file mode 100644 index 0000000000..8c34bad0c3 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseService.java @@ -0,0 +1,16 @@ +package com.coderising.ood.course.bad; + + + +public class CourseService { + + public void chooseCourse(Student student, CourseOffering sc){ + //如果学生上过该科目的先修科目,并且该课程还未满, 则学生可以加入该课程 + if(student.getCoursesAlreadyTaken().containsAll( + sc.getCourse().getPrerequisites()) + && sc.getMaxStudents() > sc.getStudents().size()){ + sc.getStudents().add(student); + } + + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Student.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Student.java new file mode 100644 index 0000000000..a651923ef5 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Student.java @@ -0,0 +1,14 @@ +package com.coderising.ood.course.bad; + +import java.util.ArrayList; +import java.util.List; + +public class Student { + private String id; + private String name; + private List coursesAlreadyTaken = new ArrayList(); + + public List getCoursesAlreadyTaken() { + return coursesAlreadyTaken; + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Course.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Course.java new file mode 100644 index 0000000000..aefc9692bb --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Course.java @@ -0,0 +1,18 @@ +package com.coderising.ood.course.good; + +import java.util.List; + +public class Course { + private String id; + private String desc; + private int duration ; + + List prerequisites; + + public List getPrerequisites() { + return prerequisites; + } + +} + + diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseOffering.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseOffering.java new file mode 100644 index 0000000000..8660ec8109 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseOffering.java @@ -0,0 +1,34 @@ +package com.coderising.ood.course.good; + +import java.util.ArrayList; +import java.util.List; + +public class CourseOffering { + private Course course; + private String location; + private String teacher; + private int maxStudents; + + List students = new ArrayList(); + + public List getStudents() { + return students; + } + public int getMaxStudents() { + return maxStudents; + } + public Course getCourse() { + return course; + } + + + // 第二步: 把主要逻辑移动到CourseOffering 中 + public void addStudent(Student student){ + + if(student.canAttend(course) + && this.maxStudents > students.size()){ + students.add(student); + } + } + // 第三步: 重构CourseService +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseService.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseService.java new file mode 100644 index 0000000000..22ba4a5450 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseService.java @@ -0,0 +1,14 @@ +package com.coderising.ood.course.good; + + + +public class CourseService { + + public void chooseCourse(Student student, CourseOffering sc){ + //第一步:重构: canAttend , 但是还有问题 + if(student.canAttend(sc.getCourse()) + && sc.getMaxStudents() > sc.getStudents().size()){ + sc.getStudents().add(student); + } + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Student.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Student.java new file mode 100644 index 0000000000..2c7e128b2a --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Student.java @@ -0,0 +1,21 @@ +package com.coderising.ood.course.good; + +import java.util.ArrayList; +import java.util.List; + +public class Student { + private String id; + private String name; + private List coursesAlreadyTaken = new ArrayList(); + + public List getCoursesAlreadyTaken() { + return coursesAlreadyTaken; + } + + public boolean canAttend(Course course){ + return this.coursesAlreadyTaken.containsAll( + course.getPrerequisites()); + } +} + + diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java new file mode 100644 index 0000000000..b6cf28c096 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class DateUtil { + + public static String getCurrentDateAsString() { + + return null; + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/ocp/Logger.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/ocp/Logger.java new file mode 100644 index 0000000000..0357c4d912 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/ocp/Logger.java @@ -0,0 +1,38 @@ +package com.coderising.ood.ocp; + +public class Logger { + + public final int RAW_LOG = 1; + public final int RAW_LOG_WITH_DATE = 2; + public final int EMAIL_LOG = 1; + public final int SMS_LOG = 2; + public final int PRINT_LOG = 3; + + int type = 0; + int method = 0; + + public Logger(int logType, int logMethod){ + this.type = logType; + this.method = logMethod; + } + public void log(String msg){ + + String logMsg = msg; + + if(this.type == RAW_LOG){ + logMsg = msg; + } else if(this.type == RAW_LOG_WITH_DATE){ + String txtDate = DateUtil.getCurrentDateAsString(); + logMsg = txtDate + ": " + msg; + } + + if(this.method == EMAIL_LOG){ + MailUtil.send(logMsg); + } else if(this.method == SMS_LOG){ + SMSUtil.send(logMsg); + } else if(this.method == PRINT_LOG){ + System.out.println(logMsg); + } + } +} + diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java new file mode 100644 index 0000000000..ec54b839c5 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class MailUtil { + + public static void send(String logMsg) { + // TODO Auto-generated method stub + + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java new file mode 100644 index 0000000000..13cf802418 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class SMSUtil { + + public static void send(String logMsg) { + // TODO Auto-generated method stub + + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..781587a846 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..a98917f829 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo R15 +P4955 Vivo X20 \ No newline at end of file diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/Iterator.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/List.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayList.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayList.java new file mode 100644 index 0000000000..4576c016af --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayList.java @@ -0,0 +1,35 @@ +package com.coding.basic.array; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayUtil.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayUtil.java new file mode 100644 index 0000000000..45740e6d57 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coding.basic.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrame.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..994a241a3d --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,57 @@ +package com.coding.basic.linklist; + + +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + private int currentSize; + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + this.currentSize = 0; + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + + + } + + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + + + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..7fd72fc2b4 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,34 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + frame.access(5); + Assert.assertEquals("5,4,0", frame.toString()); + + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/linklist/LinkedList.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/linklist/LinkedList.java new file mode 100644 index 0000000000..f4c7556a2e --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/linklist/LinkedList.java @@ -0,0 +1,125 @@ +package com.coding.basic.linklist; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/CircleQueue.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/CircleQueue.java new file mode 100644 index 0000000000..2e0550c67e --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/CircleQueue.java @@ -0,0 +1,39 @@ +package com.coding.basic.queue; + +/** + * 用数组实现循环队列 + * @author liuxin + * + * @param + */ +public class CircleQueue { + + private final static int DEFAULT_SIZE = 10; + + //用数组来保存循环队列的元素 + private Object[] elementData = new Object[DEFAULT_SIZE] ; + + //队头 + private int front = 0; + //队尾 + private int rear = 0; + + public boolean isEmpty() { + return false; + + } + + public int size() { + return -1; + } + + + + public void enQueue(E data) { + + } + + public E deQueue() { + return null; + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/Josephus.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/Josephus.java new file mode 100644 index 0000000000..6a3ea639b9 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/Josephus.java @@ -0,0 +1,18 @@ +package com.coding.basic.queue; + +import java.util.List; + +/** + * 用Queue来实现Josephus问题 + * 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来 + * 该方法返回一个List, 包含了被杀死人的次序 + * @author liuxin + * + */ +public class Josephus { + + public static List execute(int n, int m){ + return null; + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/JosephusTest.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/JosephusTest.java new file mode 100644 index 0000000000..7d90318b51 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/JosephusTest.java @@ -0,0 +1,27 @@ +package com.coding.basic.queue; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class JosephusTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testExecute() { + + Assert.assertEquals("[1, 3, 5, 0, 4, 2, 6]", Josephus.execute(7, 2).toString()); + + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/Queue.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/Queue.java new file mode 100644 index 0000000000..c4c4b7325e --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/Queue.java @@ -0,0 +1,61 @@ +package com.coding.basic.queue; + +import java.util.NoSuchElementException; + +public class Queue { + private Node first; + private Node last; + private int size; + + + private static class Node { + private E item; + private Node next; + } + + + public Queue() { + first = null; + last = null; + size = 0; + } + + + public boolean isEmpty() { + return first == null; + } + + public int size() { + return size; + } + + + + public void enQueue(E data) { + Node oldlast = last; + last = new Node(); + last.item = data; + last.next = null; + if (isEmpty()) { + first = last; + } + else{ + oldlast.next = last; + } + size++; + } + + public E deQueue() { + if (isEmpty()) { + throw new NoSuchElementException("Queue underflow"); + } + E item = first.item; + first = first.next; + size--; + if (isEmpty()) { + last = null; + } + return item; + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java new file mode 100644 index 0000000000..cef19a8b59 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java @@ -0,0 +1,47 @@ +package com.coding.basic.queue; + +import java.util.Stack; + +/** + * 用两个栈来实现一个队列 + * @author liuxin + * + * @param + */ +public class QueueWithTwoStacks { + private Stack stack1; + private Stack stack2; + + + public QueueWithTwoStacks() { + stack1 = new Stack(); + stack2 = new Stack(); + } + + + + + public boolean isEmpty() { + return false; + } + + + + public int size() { + return -1; + } + + + + public void enQueue(E item) { + + } + + public E deQueue() { + return null; + } + + + + } + diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/QuickMinStack.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/QuickMinStack.java new file mode 100644 index 0000000000..f391d92b8f --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/QuickMinStack.java @@ -0,0 +1,19 @@ +package com.coding.basic.stack; + +/** + * 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素 + * finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值 + * @author liuxin + * + */ +public class QuickMinStack { + public void push(int data){ + + } + public int pop(){ + return -1; + } + public int findMin(){ + return -1; + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/Stack.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/Stack.java new file mode 100644 index 0000000000..fedb243604 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/Stack.java @@ -0,0 +1,24 @@ +package com.coding.basic.stack; + +import com.coding.basic.array.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtil.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtil.java new file mode 100644 index 0000000000..b0ec38161d --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtil.java @@ -0,0 +1,48 @@ +package com.coding.basic.stack; +import java.util.Stack; +public class StackUtil { + + + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + + + + } + + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s,Object o) { + + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) { + return null; + } + /** + * 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz + * 使用堆栈检查字符串s中的括号是不是成对出现的。 + * 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true + * 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; + * @param s + * @return + */ + public static boolean isValidPairs(String s){ + return false; + } + + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtilTest.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtilTest.java new file mode 100644 index 0000000000..76f2cb7668 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtilTest.java @@ -0,0 +1,65 @@ +package com.coding.basic.stack; + +import java.util.Stack; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +public class StackUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + + @Test + public void testReverse() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); + StackUtil.reverse(s); + Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString()); + } + + @Test + public void testRemove() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + StackUtil.remove(s, 2); + Assert.assertEquals("[1, 3]", s.toString()); + } + + @Test + public void testGetTop() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + { + Object[] values = StackUtil.getTop(s, 3); + Assert.assertEquals(5, values[0]); + Assert.assertEquals(4, values[1]); + Assert.assertEquals(3, values[2]); + } + } + + @Test + public void testIsValidPairs() { + Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])")); + Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})")); + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java new file mode 100644 index 0000000000..d0ab4387d2 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java @@ -0,0 +1,16 @@ +package com.coding.basic.stack; + + +public class StackWithTwoQueues { + + + public void push(int data) { + + } + + public int pop() { + return -1; + } + + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java new file mode 100644 index 0000000000..e86d056a24 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java @@ -0,0 +1,57 @@ +package com.coding.basic.stack; + +/** + * 用一个数组实现两个栈 + * 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。 + * @author liuxin + * + */ +public class TwoStackInOneArray { + Object[] data = new Object[10]; + + /** + * 向第一个栈中压入元素 + * @param o + */ + public void push1(Object o){ + + } + /** + * 从第一个栈中弹出元素 + * @return + */ + public Object pop1(){ + return null; + } + + /** + * 获取第一个栈的栈顶元素 + * @return + */ + + public Object peek1(){ + return null; + } + /* + * 向第二个栈压入元素 + */ + public void push2(Object o){ + + } + /** + * 从第二个栈弹出元素 + * @return + */ + public Object pop2(){ + return null; + } + /** + * 获取第二个栈的栈顶元素 + * @return + */ + + public Object peek2(){ + return null; + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExpr.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExpr.java new file mode 100644 index 0000000000..ef85ff007f --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExpr.java @@ -0,0 +1,15 @@ +package com.coding.basic.stack.expr; + +public class InfixExpr { + String expr = null; + + public InfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + return 0.0f; + } + + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java new file mode 100644 index 0000000000..20e34e8852 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java @@ -0,0 +1,52 @@ +package com.coding.basic.stack.expr; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class InfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); + { + InfixExpr expr = new InfixExpr("2+3*4+5"); + Assert.assertEquals(19.0, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); + Assert.assertEquals(100.0, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("3*20/2"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("20/2*3"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("10-30+50"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("10-2*3+50"); + Assert.assertEquals(54, expr.evaluate(), 0.001f); + } + + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java new file mode 100644 index 0000000000..96a2194a67 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java @@ -0,0 +1,14 @@ +package com.coding.basic.stack.expr; + +import java.util.List; + +public class InfixToPostfix { + + public static List convert(String expr) { + + return null; + } + + + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java new file mode 100644 index 0000000000..dcbb18be4b --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java @@ -0,0 +1,18 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + +public class PostfixExpr { +String expr = null; + + public PostfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + return 0.0f; + } + + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java new file mode 100644 index 0000000000..c0435a2db5 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java @@ -0,0 +1,41 @@ +package com.coding.basic.stack.expr; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class PostfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + { + PostfixExpr expr = new PostfixExpr("6 5 2 3 + 8 * + 3 + *"); + Assert.assertEquals(288, expr.evaluate(),0.0f); + } + { + //9+(3-1)*3+10/2 + PostfixExpr expr = new PostfixExpr("9 3 1-3*+ 10 2/+"); + Assert.assertEquals(20, expr.evaluate(),0.0f); + } + + { + //10-2*3+50 + PostfixExpr expr = new PostfixExpr("10 2 3 * - 50 +"); + Assert.assertEquals(54, expr.evaluate(),0.0f); + } + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java new file mode 100644 index 0000000000..956927e2df --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java @@ -0,0 +1,18 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + +public class PrefixExpr { + String expr = null; + + public PrefixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + return 0.0f; + } + + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java new file mode 100644 index 0000000000..5cec210e75 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java @@ -0,0 +1,45 @@ +package com.coding.basic.stack.expr; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class PrefixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + { + // 2*3+4*5 + PrefixExpr expr = new PrefixExpr("+ * 2 3* 4 5"); + Assert.assertEquals(26, expr.evaluate(),0.001f); + } + { + // 4*2 + 6+9*2/3 -8 + PrefixExpr expr = new PrefixExpr("-++6/*2 9 3 * 4 2 8"); + Assert.assertEquals(12, expr.evaluate(),0.001f); + } + { + //(3+4)*5-6 + PrefixExpr expr = new PrefixExpr("- * + 3 4 5 6"); + Assert.assertEquals(29, expr.evaluate(),0.001f); + } + { + //1+((2+3)*4)-5 + PrefixExpr expr = new PrefixExpr("- + 1 * + 2 3 4 5"); + Assert.assertEquals(16, expr.evaluate(),0.001f); + } + + + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/Token.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/Token.java new file mode 100644 index 0000000000..8579743fe9 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/Token.java @@ -0,0 +1,50 @@ +package com.coding.basic.stack.expr; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +class Token { + public static final List OPERATORS = Arrays.asList("+", "-", "*", "/"); + private static final Map priorities = new HashMap<>(); + static { + priorities.put("+", 1); + priorities.put("-", 1); + priorities.put("*", 2); + priorities.put("/", 2); + } + static final int OPERATOR = 1; + static final int NUMBER = 2; + String value; + int type; + public Token(int type, String value){ + this.type = type; + this.value = value; + } + + public boolean isNumber() { + return type == NUMBER; + } + + public boolean isOperator() { + return type == OPERATOR; + } + + public int getIntValue() { + return Integer.valueOf(value).intValue(); + } + public String toString(){ + return value; + } + + public boolean hasHigherPriority(Token t){ + if(!this.isOperator() && !t.isOperator()){ + throw new RuntimeException("numbers can't compare priority"); + } + return priorities.get(this.value) - priorities.get(t.value) > 0; + } + + + +} \ No newline at end of file diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParser.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParser.java new file mode 100644 index 0000000000..d3b0f167e1 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParser.java @@ -0,0 +1,57 @@ +package com.coding.basic.stack.expr; + +import java.util.ArrayList; +import java.util.List; + +public class TokenParser { + + + public List parse(String expr) { + List tokens = new ArrayList<>(); + + int i = 0; + + while (i < expr.length()) { + + char c = expr.charAt(i); + + if (isOperator(c)) { + + Token t = new Token(Token.OPERATOR, String.valueOf(c)); + tokens.add(t); + i++; + + } else if (Character.isDigit(c)) { + + int nextOperatorIndex = indexOfNextOperator(i, expr); + String value = expr.substring(i, nextOperatorIndex); + Token t = new Token(Token.NUMBER, value); + tokens.add(t); + i = nextOperatorIndex; + + } else{ + System.out.println("char :["+c+"] is not number or operator,ignore"); + i++; + } + + } + return tokens; + } + + private int indexOfNextOperator(int i, String expr) { + + while (Character.isDigit(expr.charAt(i))) { + i++; + if (i == expr.length()) { + break; + } + } + return i; + + } + + private boolean isOperator(char c) { + String sc = String.valueOf(c); + return Token.OPERATORS.contains(sc); + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java new file mode 100644 index 0000000000..399d3e857e --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java @@ -0,0 +1,41 @@ +package com.coding.basic.stack.expr; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TokenParserTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + + TokenParser parser = new TokenParser(); + List tokens = parser.parse("300*20+12*5-20/4"); + + Assert.assertEquals(300, tokens.get(0).getIntValue()); + Assert.assertEquals("*", tokens.get(1).toString()); + Assert.assertEquals(20, tokens.get(2).getIntValue()); + Assert.assertEquals("+", tokens.get(3).toString()); + Assert.assertEquals(12, tokens.get(4).getIntValue()); + Assert.assertEquals("*", tokens.get(5).toString()); + Assert.assertEquals(5, tokens.get(6).getIntValue()); + Assert.assertEquals("-", tokens.get(7).toString()); + Assert.assertEquals(20, tokens.get(8).getIntValue()); + Assert.assertEquals("/", tokens.get(9).toString()); + Assert.assertEquals(4, tokens.get(10).getIntValue()); + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTree.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTree.java new file mode 100644 index 0000000000..4536ee7a2b --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTree.java @@ -0,0 +1,55 @@ +package com.coding.basic.tree; + +import java.util.ArrayList; +import java.util.List; + +import com.coding.basic.queue.Queue; + +public class BinarySearchTree { + + BinaryTreeNode root; + public BinarySearchTree(BinaryTreeNode root){ + this.root = root; + } + public BinaryTreeNode getRoot(){ + return root; + } + public T findMin(){ + return null; + } + public T findMax(){ + return null; + } + public int height() { + return -1; + } + public int size() { + return -1; + } + public void remove(T e){ + + } + public List levelVisit(){ + + return null; + } + public boolean isValid(){ + return false; + } + public T getLowestCommonAncestor(T n1, T n2){ + return null; + + } + /** + * 返回所有满足下列条件的节点的值: n1 <= n <= n2 , n 为 + * 该二叉查找树中的某一节点 + * @param n1 + * @param n2 + * @return + */ + public List getNodesBetween(T n1, T n2){ + return null; + } + +} + diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java new file mode 100644 index 0000000000..4a53dbe2f1 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java @@ -0,0 +1,109 @@ +package com.coding.basic.tree; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class BinarySearchTreeTest { + + BinarySearchTree tree = null; + + @Before + public void setUp() throws Exception { + BinaryTreeNode root = new BinaryTreeNode(6); + root.left = new BinaryTreeNode(2); + root.right = new BinaryTreeNode(8); + root.left.left = new BinaryTreeNode(1); + root.left.right = new BinaryTreeNode(4); + root.left.right.left = new BinaryTreeNode(3); + root.left.right.right = new BinaryTreeNode(5); + tree = new BinarySearchTree(root); + } + + @After + public void tearDown() throws Exception { + tree = null; + } + + @Test + public void testFindMin() { + Assert.assertEquals(1, tree.findMin().intValue()); + + } + + @Test + public void testFindMax() { + Assert.assertEquals(8, tree.findMax().intValue()); + } + + @Test + public void testHeight() { + Assert.assertEquals(4, tree.height()); + } + + @Test + public void testSize() { + Assert.assertEquals(7, tree.size()); + } + + @Test + public void testRemoveLeaf() { + tree.remove(3); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(4, root.left.right.data.intValue()); + + } + @Test + public void testRemoveMiddleNode1() { + tree.remove(4); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(5, root.left.right.data.intValue()); + Assert.assertEquals(3, root.left.right.left.data.intValue()); + } + @Test + public void testRemoveMiddleNode2() { + tree.remove(2); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(3, root.left.data.intValue()); + Assert.assertEquals(4, root.left.right.data.intValue()); + } + + @Test + public void testLevelVisit() { + List values = tree.levelVisit(); + Assert.assertEquals("[6, 2, 8, 1, 4, 3, 5]", values.toString()); + + } + @Test + public void testLCA(){ + Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 5).intValue()); + Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 4).intValue()); + Assert.assertEquals(6,tree.getLowestCommonAncestor(3, 8).intValue()); + } + @Test + public void testIsValid() { + + Assert.assertTrue(tree.isValid()); + + BinaryTreeNode root = new BinaryTreeNode(6); + root.left = new BinaryTreeNode(2); + root.right = new BinaryTreeNode(8); + root.left.left = new BinaryTreeNode(4); + root.left.right = new BinaryTreeNode(1); + root.left.right.left = new BinaryTreeNode(3); + tree = new BinarySearchTree(root); + + Assert.assertFalse(tree.isValid()); + } + @Test + public void testGetNodesBetween(){ + List numbers = this.tree.getNodesBetween(3, 8); + System.out.println(numbers.toString()); + + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeNode.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeNode.java new file mode 100644 index 0000000000..c1421cd398 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeNode.java @@ -0,0 +1,35 @@ +package com.coding.basic.tree; + +public class BinaryTreeNode { + + public T data; + public BinaryTreeNode left; + public BinaryTreeNode right; + + public BinaryTreeNode(T data){ + this.data=data; + } + public T getData() { + return data; + } + public void setData(T data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java new file mode 100644 index 0000000000..b033cbe1d5 --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java @@ -0,0 +1,66 @@ +package com.coding.basic.tree; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class BinaryTreeUtil { + /** + * 用递归的方式实现对二叉树的前序遍历, 需要通过BinaryTreeUtilTest测试 + * + * @param root + * @return + */ + public static List preOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + + return result; + } + + /** + * 用递归的方式实现对二叉树的中遍历 + * + * @param root + * @return + */ + public static List inOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + + return result; + } + + /** + * 用递归的方式实现对二叉树的后遍历 + * + * @param root + * @return + */ + public static List postOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + + return result; + } + /** + * 用非递归的方式实现对二叉树的前序遍历 + * @param root + * @return + */ + public static List preOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + + return result; + } + /** + * 用非递归的方式实现对二叉树的中序遍历 + * @param root + * @return + */ + public static List inOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + + return result; + } + +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java new file mode 100644 index 0000000000..41857e137d --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java @@ -0,0 +1,75 @@ +package com.coding.basic.tree; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class BinaryTreeUtilTest { + + BinaryTreeNode root = null; + @Before + public void setUp() throws Exception { + root = new BinaryTreeNode(1); + root.setLeft(new BinaryTreeNode(2)); + root.setRight(new BinaryTreeNode(5)); + root.getLeft().setLeft(new BinaryTreeNode(3)); + root.getLeft().setRight(new BinaryTreeNode(4)); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testPreOrderVisit() { + + List result = BinaryTreeUtil.preOrderVisit(root); + Assert.assertEquals("[1, 2, 3, 4, 5]", result.toString()); + + + } + @Test + public void testInOrderVisit() { + + + List result = BinaryTreeUtil.inOrderVisit(root); + Assert.assertEquals("[3, 2, 4, 1, 5]", result.toString()); + + } + + @Test + public void testPostOrderVisit() { + + + List result = BinaryTreeUtil.postOrderVisit(root); + Assert.assertEquals("[3, 4, 2, 5, 1]", result.toString()); + + } + + + @Test + public void testInOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.inOrderWithoutRecursion(root); + Assert.assertEquals("[3, 2, 6, 4, 7, 1, 5]", result.toString()); + + } + @Test + public void testPreOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.preOrderWithoutRecursion(root); + Assert.assertEquals("[1, 2, 3, 4, 6, 7, 5]", result.toString()); + + } +} diff --git a/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/FileList.java b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/FileList.java new file mode 100644 index 0000000000..6e65192e4a --- /dev/null +++ b/students/1377699408/data-structure/assignment/src/main/java/com/coding/basic/tree/FileList.java @@ -0,0 +1,10 @@ +package com.coding.basic.tree; + +import java.io.File; + +public class FileList { + public void list(File f) { + } + + +} diff --git a/students/1377699408/ood/ood-assignment/pom.xml b/students/1377699408/ood/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/1377699408/ood/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..ccffce577d --- /dev/null +++ b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public static String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/EmailException.java b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/EmailException.java new file mode 100644 index 0000000000..1be735e5f5 --- /dev/null +++ b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/EmailException.java @@ -0,0 +1,7 @@ +package com.coderising.ood.srp; + +public class EmailException extends Exception { + public EmailException(String message) { + super(message); + } +} diff --git a/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..4397366a16 --- /dev/null +++ b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,57 @@ +package com.coderising.ood.srp; + +import com.coderising.ood.srp.bean.Email; +import com.coderising.ood.srp.bean.Product; +import com.coderising.ood.srp.dao.EmailDAO; + +import java.io.IOException; +import java.util.*; + +public class PromotionMail { + private static EmailDAO emailDAO = new EmailDAO(); + + public static void main(String[] args) throws Exception { + List emailList = getEmails("product_promotion.txt"); + for (Email email : emailList) { + sendEmail(email); + } + + } + + public static List getEmails(String file) throws IOException { + List emailList = new ArrayList(); + List list = Product.getProductByFile(file); + + for (Product p : list) { + String productId = p.getProductID(); + List> l = emailDAO.listSubscriptionsByProdoctId(productId); + for (Map userInfo : l) { + String username = userInfo.get("NAME"); + String productDesc = p.getProductDesc(); + String fromAddr = Configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN); + List toAddr = new ArrayList(); + toAddr.add(userInfo.get("EMAIL")); + String smtpServer = Configuration.getProperty(ConfigurationKeys.SMTP_SERVER); + Email email = new Email(fromAddr, toAddr, "\"您关注的产品降价了\"", "尊敬的 " + username + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!", smtpServer, fromAddr); + emailList.add(email); + } + } + return emailList; + } + + public static void sendEmail(Email email) { + if (email == null) { + System.out.println("没有邮件发送"); + } + try { + email.send(); + } catch (EmailException e) { + email.setSmtpServer(Configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + try { + email.send(); + } catch (EmailException e1) { + System.out.println("使用备用服务器发送失败"); + } + } + } +} diff --git a/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Email.java b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Email.java new file mode 100644 index 0000000000..de6cfe1636 --- /dev/null +++ b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Email.java @@ -0,0 +1,102 @@ +package com.coderising.ood.srp.bean; + + +import com.coderising.ood.srp.utils.CollectionUtils; +import com.coderising.ood.srp.EmailException; +import com.coderising.ood.srp.utils.StringUtils; + +import java.util.Arrays; +import java.util.List; + +public class Email { + private String fromAddress; + private List toAddresses; + private String subject; + private String content; + private String smtpServer; + private String email_admin; + + public Email(String fromAddress, List toAddresses, String subject, String content, String smtpServer, String email_admin) { + this.fromAddress = fromAddress; + this.toAddresses = toAddresses; + this.subject = subject; + this.content = content; + this.smtpServer = smtpServer; + this.email_admin = email_admin; + } + + public void send() throws EmailException { + //假装发了一封邮件 + check_send(); + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(this.getFromAddress()).append("\n"); + buffer.append("To:").append(Arrays.toString(this.getToAddresses().toArray())).append("\n"); + buffer.append("Subject:").append(this.getSubject()).append("\n"); + buffer.append("Content:").append(this.getContent()).append("\n"); + System.out.println(buffer.toString()); + } + + private void check_send() throws EmailException { + if (StringUtils.isBlank(this.fromAddress)) { + throw new EmailException("fromAddress is empty"); + } + if (CollectionUtils.isEmpty(this.toAddresses)) { + throw new EmailException("toAddresses is empty"); + } + if (StringUtils.isBlank(this.subject)) { + throw new EmailException("subject is empty"); + } + } + + public Email() { + } + + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public List getToAddresses() { + return toAddresses; + } + + public void setToAddresses(List toAddresses) { + this.toAddresses = toAddresses; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getSmtpServer() { + return smtpServer; + } + + public void setSmtpServer(String smtpServer) { + this.smtpServer = smtpServer; + } + + public String getEmail_admin() { + return email_admin; + } + + public void setEmail_admin(String email_admin) { + this.email_admin = email_admin; + } +} diff --git a/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Product.java b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Product.java new file mode 100644 index 0000000000..c3872c33bb --- /dev/null +++ b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Product.java @@ -0,0 +1,66 @@ +package com.coderising.ood.srp.bean; + +import com.coderising.ood.srp.utils.StringUtils; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class Product { + private String productID; + private String productDesc; + + public static List getProductByFile(String f) throws IOException { + List list = new ArrayList(); + File file = new File(f); + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String s = ""; + while (!StringUtils.isBlank((s=br.readLine()))) { + String[] data = s.split(" "); + Product p = new Product(data[0], data[1]); + System.out.println("产品ID = " + data[0] + "\n"); + System.out.println("产品描述 = " + data[1] + "\n"); + list.add(p); + } + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + if (br != null) { + br.close(); + } + } + return list; + } + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + + public Product() { + + } + + public Product(String productID, String productDesc) { + + this.productID = productID; + this.productDesc = productDesc; + } +} diff --git a/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/dao/EmailDAO.java b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/dao/EmailDAO.java new file mode 100644 index 0000000000..39975b9276 --- /dev/null +++ b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/dao/EmailDAO.java @@ -0,0 +1,22 @@ +package com.coderising.ood.srp.dao; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class EmailDAO { + public List> listSubscriptionsByProdoctId(String productId) { + String sql = "Select name from subscriptions " + + "where product_id= '" + productId + "' " + + "and send_mail=1 "; + List> userList = new ArrayList>(); + for (int i = 1; i <= 3; i++) { + Map userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + return userList; + } +} diff --git a/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/utils/CollectionUtils.java b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/utils/CollectionUtils.java new file mode 100644 index 0000000000..ff60683472 --- /dev/null +++ b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/utils/CollectionUtils.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp.utils; + +import java.util.List; + +public class CollectionUtils { + public static boolean isEmpty(List list) { + return list == null || list.size() == 0; + } +} diff --git a/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/utils/StringUtils.java b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/utils/StringUtils.java new file mode 100644 index 0000000000..4d86f4ba08 --- /dev/null +++ b/students/1377699408/ood/ood-assignment/src/main/java/com/coderising/ood/srp/utils/StringUtils.java @@ -0,0 +1,7 @@ +package com.coderising.ood.srp.utils; + +public class StringUtils { + public static boolean isBlank(String s) { + return s == null || "".equals(s.trim()); + } +} From 86d52a9b0a2f8eea1d99a777db26fb42d117bc6d Mon Sep 17 00:00:00 2001 From: duoduo1222 <277093528@qq.com> Date: Wed, 14 Jun 2017 17:44:44 +0800 Subject: [PATCH 096/214] =?UTF-8?q?=E9=87=8D=E6=9E=84=E9=82=AE=E4=BB=B6?= =?UTF-8?q?=E5=8F=91=E9=80=81=E5=B0=8F=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/277093528/ood-assignment/pom.xml | 32 ++++++++ .../com/coderising/ood/srp/Configuration.java | 23 ++++++ .../coderising/ood/srp/ConfigurationKeys.java | 12 +++ .../java/com/coderising/ood/srp/DBUtil.java | 25 ++++++ .../com/coderising/ood/srp/FileUtils.java | 39 +++++++++ .../java/com/coderising/ood/srp/MailUtil.java | 82 +++++++++++++++++++ .../java/com/coderising/ood/srp/Product.java | 26 ++++++ .../com/coderising/ood/srp/PromotionMail.java | 40 +++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 9 files changed, 283 insertions(+) create mode 100644 students/277093528/ood-assignment/pom.xml create mode 100644 students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtils.java create mode 100644 students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/277093528/ood-assignment/pom.xml b/students/277093528/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/277093528/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..1cd9f713c8 --- /dev/null +++ b/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,12 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + public static final String MESSAGE_FILE_PATH = "D:\\BaiduYunDownload\\Second_Season\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"; + + public static final String NAME_KEY = "NAME"; + public static final String EMAIL_KEY = "EMAIL"; +} diff --git a/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtils.java b/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtils.java new file mode 100644 index 0000000000..e206b0aa24 --- /dev/null +++ b/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtils.java @@ -0,0 +1,39 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class FileUtils { + /** + * 解析文件内容 + * @return + * @throws IOException + */ + public static Product readFile() throws IOException { + + BufferedReader br = null; + try { + Product product = new Product(); + File file = new File( ConfigurationKeys.MESSAGE_FILE_PATH ); + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + product.setProductID(data[0]); + product.setProductDesc(data[1]); + + System.out.println("产品ID = " + data[0] + "\n"); + System.out.println("产品描述 = " + data[1] + "\n"); + return product; + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + + } + +} diff --git a/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..5765cb0070 --- /dev/null +++ b/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,82 @@ +package com.coderising.ood.srp; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class MailUtil { + + private static String smtpHost = null; + private static String altSmtpHost = null; + private static String fromAddress = null; + private static String toAddress = null; + private static String subject = null; + private static String message = null; + + static { + Configuration config = new Configuration(); + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected static void sendEmail(String toAddress, String subject, String message, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + protected static void sendEMails(boolean debug,Product product, List mailingList) throws IOException { + + System.out.println("开始发送邮件"); + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next(),product); + try + { + if ( toAddress.length() > 0 ) + sendEmail(toAddress, subject, message, debug); + } + catch (Exception e) + { + + try { + sendEmail(toAddress, subject, message, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + } else { + System.out.println("没有邮件发送"); + + } + + } + + private static void setMessage( HashMap userInfo,Product product) throws IOException { + String name = (String) userInfo.get( ConfigurationKeys.NAME_KEY ); + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!" ; + } + + private static void configureEMail(HashMap userInfo,Product product) throws IOException + { + toAddress = (String) userInfo.get( ConfigurationKeys.EMAIL_KEY ); + if (toAddress.length() > 0) + setMessage( userInfo, product ); + } + + +} diff --git a/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..ba93dd2f1b --- /dev/null +++ b/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,26 @@ +package com.coderising.ood.srp; + + +public class Product { + + private String productID = null; + + private String productDesc = null; + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + +} diff --git a/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..5c10796dbf --- /dev/null +++ b/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,40 @@ +package com.coderising.ood.srp; + +import java.util.List; + +public class PromotionMail { + + protected String sendMailQuery = null; + private Product product = null; + + public static void main(String[] args) throws Exception { + + boolean emailDebug = false; + PromotionMail pe = new PromotionMail( emailDebug ); + + } + + public PromotionMail( boolean mailDebug ) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + product = FileUtils.readFile(); + + setLoadQuery(); + + MailUtil.sendEMails(mailDebug, product ,loadMailingList()); + + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + product.getProductID() +"' " + + "and send_mail=1 "; + System.out.println("loadQuery set"); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + +} diff --git a/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/277093528/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 8d3bd06979ed82ca4e419d21cacac8c86ec34fe0 Mon Sep 17 00:00:00 2001 From: jyp Date: Wed, 14 Jun 2017 18:20:24 +0800 Subject: [PATCH 097/214] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coderising/ood/srp/PromotionMail.java | 28 ++++++++++--------- .../ood/srp/config/ConfigurationKeys.java | 3 ++ .../ood/srp/config/ConnectionConfig.java | 4 +++ .../srp/model/{Mail.java => MailInfo.java} | 10 +++++-- .../com/coderising/ood/srp/model/Product.java | 8 ++---- .../ood/srp/model/Subscriptions.java | 5 ++++ .../ood/srp/service/ProductService.java | 2 +- .../ood/srp/service/SubscriptionsService.java | 7 +++++ .../com/coderising/ood/srp/util/DBUtil.java | 1 + 9 files changed, 46 insertions(+), 22 deletions(-) rename students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/{Mail.java => MailInfo.java} (84%) diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java index cd31beac90..9df4e1c77a 100644 --- a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -7,7 +7,7 @@ import com.coderising.ood.srp.config.Configuration; import com.coderising.ood.srp.config.ConnectionConfig; -import com.coderising.ood.srp.model.Mail; +import com.coderising.ood.srp.model.MailInfo; import com.coderising.ood.srp.model.Product; import com.coderising.ood.srp.model.Subscriptions; import com.coderising.ood.srp.service.ProductService; @@ -27,7 +27,9 @@ public PromotionMail(SubscriptionsService subscriptionsService, ProductService p /** * 发送促销邮件 - * @param file 促销产品文件 + * + * @param file + * 促销产品文件 * @param mailDebug * @throws Exception */ @@ -40,44 +42,44 @@ public void sendPromotionMail(File file, boolean mailDebug) throws Exception { List subscriptions = subscriptionsService.doFindByProducts(products); // 得到订阅人的邮箱和名称,邮箱内容 - List mails = getMails(subscriptions); + List mails = getMails(subscriptions); // 发送邮箱 sendEMails(new ConnectionConfig(new Configuration()), mails, mailDebug); } - protected List getMails(List subscriptions) { + // 得到发送的邮箱对象 + protected List getMails(List subscriptions) { - List mails = new ArrayList(); + List mails = new ArrayList(); String subject = "您关注的产品降价了"; for (Subscriptions sub : subscriptions) { String productDesc = sub.getProduct().getProductDesc(); String message = "尊敬的 " + sub.getName() + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; - mails.add(new Mail(subject, message, sub.getEmail())); + mails.add(new MailInfo(subject, message, sub.getEmail())); } return mails; } - protected void sendEMails(ConnectionConfig config, List mails, boolean debug) { + // 发送邮件 + protected void sendEMails(ConnectionConfig config, List mails, boolean debug) { if (mails == null) { System.out.println("没有邮件需要发送"); return; } System.out.println("开始发送邮件"); - Iterator iter = mails.iterator(); + Iterator iter = mails.iterator(); while (iter.hasNext()) { - Mail mail = iter.next(); + MailInfo mail = iter.next(); if (mail.getToAddress().length() <= 0) { continue; } try { - MailUtil.sendEmail(mail.getToAddress(), config.getFromAddress(), mail.getSubject(), mail.getMessage(), - config.getSmtpHost(), debug); + MailUtil.sendEmail(mail.getToAddress(), config.getFromAddress(), mail.getSubject(), mail.getMessage(),config.getSmtpHost(), debug); } catch (Exception e) { try { - MailUtil.sendEmail(mail.getToAddress(), config.getFromAddress(), mail.getSubject(), - mail.getMessage(), config.getAltSmtpHost(), debug); + MailUtil.sendEmail(mail.getToAddress(), config.getFromAddress(), mail.getSubject(),mail.getMessage(), config.getAltSmtpHost(), debug); } catch (Exception e2) { System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java index 7fe226d1bd..1c99770ed8 100644 --- a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java @@ -1,5 +1,8 @@ package com.coderising.ood.srp.config; +/** + * 实际该类应该为配置文件 + */ public class ConfigurationKeys { public static final String SMTP_SERVER = "smtp.server"; diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConnectionConfig.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConnectionConfig.java index be33ad6ed2..531efe251d 100644 --- a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConnectionConfig.java +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConnectionConfig.java @@ -1,5 +1,9 @@ package com.coderising.ood.srp.config; +/** + * 邮箱连接配置类 + * + */ public class ConnectionConfig { private String smtpHost; private String altSmtpHost; diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Mail.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/MailInfo.java similarity index 84% rename from students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Mail.java rename to students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/MailInfo.java index f0b72d7759..2744e2fd1f 100644 --- a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Mail.java +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/MailInfo.java @@ -1,15 +1,19 @@ package com.coderising.ood.srp.model; -public class Mail { +/** + * 邮箱信息 + * + */ +public class MailInfo { private String subject; private String message; private String toAddress; - public Mail() { + public MailInfo() { super(); } - public Mail(String subject, String message, String toAddress) { + public MailInfo(String subject, String message, String toAddress) { super(); this.subject = subject; this.message = message; diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Product.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Product.java index fbcbcf9a89..fcfc5f7cf4 100644 --- a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Product.java +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Product.java @@ -1,10 +1,8 @@ package com.coderising.ood.srp.model; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; - +/** + * 产品信息 + */ public class Product { private String productID; diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Subscriptions.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Subscriptions.java index 8ac1def689..8a25fe19ed 100644 --- a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Subscriptions.java +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/model/Subscriptions.java @@ -1,7 +1,12 @@ package com.coderising.ood.srp.model; +/** + * 订阅信息,主要有订阅产品,订阅用户 + * + */ public class Subscriptions { + // name 和 email 应该存放在用户信息中,如叫订阅用户, private String name; private String email; private String productId; diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/ProductService.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/ProductService.java index 23f5ffb7e3..aae01818f9 100644 --- a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/ProductService.java +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/ProductService.java @@ -10,7 +10,7 @@ public interface ProductService { /** * 查询促销产品 - * @return + * @return 促销产品 * @throws IOException */ List doFindPromotionalProducts(File file) throws IOException; diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/SubscriptionsService.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/SubscriptionsService.java index 5b5cc2830b..c31c25c084 100644 --- a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/SubscriptionsService.java +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/service/SubscriptionsService.java @@ -7,5 +7,12 @@ public interface SubscriptionsService { + /** + * 查询产品的订阅人 + * + * @param products + * 产品 + * @return 订阅信息 + */ List doFindByProducts(List products); } diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/util/DBUtil.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/util/DBUtil.java index 9af40a67bd..33bb5cfd43 100644 --- a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/util/DBUtil.java +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/util/DBUtil.java @@ -12,6 +12,7 @@ public class DBUtil { * @param sql * @return */ + @SuppressWarnings({ "rawtypes", "unchecked" }) public static List query(String sql) { List userList = new ArrayList(); From 20457bc7f4bf672e2e988c9633cb6930e42f5a23 Mon Sep 17 00:00:00 2001 From: ShiningChenCode Date: Wed, 14 Jun 2017 18:47:52 +0800 Subject: [PATCH 098/214] =?UTF-8?q?=E4=BF=83=E9=94=80Mail=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E5=90=8E=EF=BC=8C=E5=8F=AF=E4=BB=A5=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E4=B8=8D=E5=90=8C=E8=BF=90=E8=90=A5=E6=96=B9=E6=A1=88=EF=BC=8C?= =?UTF-8?q?=E5=90=91=E8=AE=A2=E9=98=85=E8=80=85=E5=8F=91=E9=80=81=E9=80=9A?= =?UTF-8?q?=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coderising/ood/srp/product_promotion.txt | 4 + students/2831099157/ood-assignment/pom.xml | 32 +++++ .../com/coderising/ood/srp/PromotionMail.java | 21 ++++ .../ood/srp/configure/Configuration.java | 23 ++++ .../ood/srp/configure/ConfigurationKeys.java | 9 ++ .../com/coderising/ood/srp/dao/DBUtil.java | 27 +++++ .../srp/interfaces/GetProductsFunction.java | 13 ++ .../ood/srp/interfaces/SendMailFunction.java | 13 ++ .../com/coderising/ood/srp/model/Mail.java | 112 ++++++++++++++++++ .../com/coderising/ood/srp/model/Product.java | 46 +++++++ .../com/coderising/ood/srp/model/User.java | 25 ++++ .../coderising/ood/srp/product_promotion.txt | 4 + .../ood/srp/service/GetProductsFromFile.java | 47 ++++++++ .../ood/srp/service/GoodsArrivalNotice.java | 13 ++ .../coderising/ood/srp/service/Notice.java | 25 ++++ .../ood/srp/service/PricePromotion.java | 12 ++ .../ood/srp/service/SendGoodsArrivalMail.java | 42 +++++++ .../ood/srp/service/SendPriceMail.java | 42 +++++++ ...10\351\207\215\346\236\204\357\274\211.md" | 23 ++++ 19 files changed, 533 insertions(+) create mode 100644 students/2831099157/ood-assignment/out/production/main/com/coderising/ood/srp/product_promotion.txt create mode 100644 students/2831099157/ood-assignment/pom.xml create mode 100644 students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/configure/Configuration.java create mode 100644 students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/configure/ConfigurationKeys.java create mode 100644 students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/dao/DBUtil.java create mode 100644 students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/interfaces/GetProductsFunction.java create mode 100644 students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/interfaces/SendMailFunction.java create mode 100644 students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/model/Mail.java create mode 100644 students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/model/Product.java create mode 100644 students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/model/User.java create mode 100644 students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt create mode 100644 students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/GetProductsFromFile.java create mode 100644 students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/GoodsArrivalNotice.java create mode 100644 students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/Notice.java create mode 100644 students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/PricePromotion.java create mode 100644 students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/SendGoodsArrivalMail.java create mode 100644 students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/SendPriceMail.java create mode 100644 "students/2831099157/ood-assignment/\344\277\203\351\224\200Mail\345\217\221\351\200\201\347\273\203\344\271\240\357\274\210\351\207\215\346\236\204\357\274\211.md" diff --git a/students/2831099157/ood-assignment/out/production/main/com/coderising/ood/srp/product_promotion.txt b/students/2831099157/ood-assignment/out/production/main/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/2831099157/ood-assignment/out/production/main/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/2831099157/ood-assignment/pom.xml b/students/2831099157/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/2831099157/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..9eb1b21f29 --- /dev/null +++ b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,21 @@ +package com.coderising.ood.srp; + +import com.coderising.ood.srp.service.GoodsArrivalNotice; +import com.coderising.ood.srp.service.Notice; + +/** + * 可以根据不同运营方案,添加GetProductsFunction,SendMailFunction接口实现类及Notice子类,向订阅者发送通告 + */ +public class PromotionMail { + + public static void main(String[] args) throws Exception { + //降价促销 +// Notice notice = new PricePromotion(); + //到货通知 + Notice notice = new GoodsArrivalNotice(); + notice.sendMail(notice.getProducts()); + + } + + +} diff --git a/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/configure/Configuration.java b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/configure/Configuration.java new file mode 100644 index 0000000000..5c0697782a --- /dev/null +++ b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/configure/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp.configure; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/configure/ConfigurationKeys.java b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/configure/ConfigurationKeys.java new file mode 100644 index 0000000000..c9cfba4974 --- /dev/null +++ b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/configure/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp.configure; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/dao/DBUtil.java b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/dao/DBUtil.java new file mode 100644 index 0000000000..c0e7f6508d --- /dev/null +++ b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/dao/DBUtil.java @@ -0,0 +1,27 @@ +package com.coderising.ood.srp.dao; +import com.coderising.ood.srp.model.User; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + User user = new User(); + user.setName("User" + i); + user.seteMail("aa@bb.com"); + userList.add(user); + } + + return userList; + } +} diff --git a/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/interfaces/GetProductsFunction.java b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/interfaces/GetProductsFunction.java new file mode 100644 index 0000000000..f0894ea3c7 --- /dev/null +++ b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/interfaces/GetProductsFunction.java @@ -0,0 +1,13 @@ +package com.coderising.ood.srp.interfaces; + +import com.coderising.ood.srp.model.Product; + +import java.util.List; + +/** + * Created by Iden on 2017/6/14. + */ +public interface GetProductsFunction { + + List getProducts(); +} diff --git a/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/interfaces/SendMailFunction.java b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/interfaces/SendMailFunction.java new file mode 100644 index 0000000000..cd27a45767 --- /dev/null +++ b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/interfaces/SendMailFunction.java @@ -0,0 +1,13 @@ +package com.coderising.ood.srp.interfaces; + +import com.coderising.ood.srp.model.Product; + +import java.util.List; + +/** + * Created by Iden on 2017/6/14. + */ +public interface SendMailFunction { + + void sendMail(List products); +} diff --git a/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/model/Mail.java b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/model/Mail.java new file mode 100644 index 0000000000..b94f27b29d --- /dev/null +++ b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/model/Mail.java @@ -0,0 +1,112 @@ +package com.coderising.ood.srp.model; + +import com.coderising.ood.srp.configure.Configuration; +import com.coderising.ood.srp.configure.ConfigurationKeys; + +/** + * Created by Iden on 2017/6/14. + */ +public class Mail { + private String fromAddress; + private String toAddress; + private String subject; + private String content; + private String smtpHost = null; + private String altSmtpHost = null; + + public Mail() { + Configuration config = new Configuration(); + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + + + public void send() { + if(null==toAddress){ + System.out.println("发送地址不能为空"); + return; + } + try { + sendMailBySmtpHost(); + } catch (Exception e) { + try { + sendMailBySmtpHost(); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + + } + } + + private void sendMailBySmtpHost() { + System.out.println("通过SMTP服务器开始发送邮件"); + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(content).append("\n"); + System.out.println(buffer.toString()); + } + + private void sendMailByAlSmtpHost() { + System.out.println("通过备用SMTP服务器开始发送邮件"); + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(content).append("\n"); + System.out.println(buffer.toString()); + } +} diff --git a/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/model/Product.java b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/model/Product.java new file mode 100644 index 0000000000..f9f5b5a145 --- /dev/null +++ b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/model/Product.java @@ -0,0 +1,46 @@ +package com.coderising.ood.srp.model; + +import com.coderising.ood.srp.dao.DBUtil; + +import java.util.List; + +/** + * Created by Iden on 2017/6/14. + */ +public class Product { + String id; + String description; + + public Product(String id, String descript) { + this.id = id; + this.description = descript; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public List getSubscribers() { + List userList = null; + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + id + "' " + + "and send_mail=1 "; + userList = DBUtil.query(sendMailQuery); + return userList; + + } + + +} diff --git a/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/model/User.java b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/model/User.java new file mode 100644 index 0000000000..38bec29f59 --- /dev/null +++ b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/model/User.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp.model; + +/** + * Created by Iden on 2017/6/14. + */ +public class User { + String name; + String eMail; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String geteMail() { + return eMail; + } + + public void seteMail(String eMail) { + this.eMail = eMail; + } +} diff --git a/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/GetProductsFromFile.java b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/GetProductsFromFile.java new file mode 100644 index 0000000000..92a1090c5e --- /dev/null +++ b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/GetProductsFromFile.java @@ -0,0 +1,47 @@ +package com.coderising.ood.srp.service; + +import com.coderising.ood.srp.interfaces.GetProductsFunction; +import com.coderising.ood.srp.model.Product; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Iden on 2017/6/14. + */ +public class GetProductsFromFile implements GetProductsFunction { + public String filePath = "E:\\StudyProjects\\Java\\Workspace\\HomeWork\\coding2017\\liuxin\\ood\\ood-assignment\\" + + "src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"; + + @Override + public List getProducts() { + BufferedReader br = null; + List products = new ArrayList<>(); + try { + File file = new File(filePath); + br = new BufferedReader(new FileReader(file)); + String temp = null; + while ((temp = br.readLine()) != null) { + String[] data = temp.split(" "); + Product product = new Product(data[0], data[1]); + System.out.println("促销产品ID = " + product.getId()); + System.out.println("促销产品描述 = " + product.getDescription()); + products.add(product); + } + + } catch (IOException e) { + System.out.println("读取文件失败"); + } finally { + try { + br.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return products; + } +} diff --git a/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/GoodsArrivalNotice.java b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/GoodsArrivalNotice.java new file mode 100644 index 0000000000..ee4723ec06 --- /dev/null +++ b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/GoodsArrivalNotice.java @@ -0,0 +1,13 @@ +package com.coderising.ood.srp.service; + +/** + * Created by Iden on 2017/6/14. + * 到货通知 + */ +public class GoodsArrivalNotice extends Notice { + public GoodsArrivalNotice() { + getProductsFunction = new GetProductsFromFile(); + sendMailFunction = new SendGoodsArrivalMail(); + } + +} diff --git a/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/Notice.java b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/Notice.java new file mode 100644 index 0000000000..6ac8e62402 --- /dev/null +++ b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/Notice.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp.service; + +import com.coderising.ood.srp.interfaces.GetProductsFunction; +import com.coderising.ood.srp.interfaces.SendMailFunction; +import com.coderising.ood.srp.model.Product; + +import java.util.List; + +/** + * Created by Iden on 2017/6/14. + * 各类通知(降价促销,抢购活动,到货通知等等) + */ +public abstract class Notice { + GetProductsFunction getProductsFunction; + SendMailFunction sendMailFunction; + + public List getProducts() { + return getProductsFunction.getProducts(); + } + + public void sendMail(List products) { + sendMailFunction.sendMail(products); + } + +} diff --git a/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/PricePromotion.java b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/PricePromotion.java new file mode 100644 index 0000000000..ebb59571c0 --- /dev/null +++ b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/PricePromotion.java @@ -0,0 +1,12 @@ +package com.coderising.ood.srp.service; + +/** + * Created by Iden on 2017/6/14. + */ +public class PricePromotion extends Notice { + public PricePromotion() { + getProductsFunction = new GetProductsFromFile(); + sendMailFunction = new SendPriceMail(); + } + +} diff --git a/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/SendGoodsArrivalMail.java b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/SendGoodsArrivalMail.java new file mode 100644 index 0000000000..79f3a6985f --- /dev/null +++ b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/SendGoodsArrivalMail.java @@ -0,0 +1,42 @@ +package com.coderising.ood.srp.service; + +import com.coderising.ood.srp.interfaces.SendMailFunction; +import com.coderising.ood.srp.model.Mail; +import com.coderising.ood.srp.model.Product; +import com.coderising.ood.srp.model.User; + +import java.util.Iterator; +import java.util.List; + +/** + * Created by Iden on 2017/6/14. + */ +public class SendGoodsArrivalMail implements SendMailFunction { + + + @Override + public void sendMail(List products) { + if (null == products || products.size() == 0) { + System.out.println("没有发现到货的产品"); + return; + } + Iterator iterator = products.iterator(); + while (iterator.hasNext()) { + Product product = iterator.next(); + List userList = product.getSubscribers(); + if (null == userList || userList.size() == 0) { + System.out.println("没有人订阅" + product.getDescription() + " 信息"); + continue; + } + Iterator iter = userList.iterator(); + while (iter.hasNext()) { + User user = (User) iter.next(); + Mail mail = new Mail(); + mail.setSubject("您关注的产品到货了"); + mail.setContent("尊敬的 " + user.getName() + ", 您关注的产品 " + product.getDescription() + " 到货了,欢迎购买"); + mail.setToAddress(user.geteMail()); + mail.send(); + } + } + } +} diff --git a/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/SendPriceMail.java b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/SendPriceMail.java new file mode 100644 index 0000000000..bae4803e3f --- /dev/null +++ b/students/2831099157/ood-assignment/src/main/java/com/coderising/ood/srp/service/SendPriceMail.java @@ -0,0 +1,42 @@ +package com.coderising.ood.srp.service; + +import com.coderising.ood.srp.interfaces.SendMailFunction; +import com.coderising.ood.srp.model.Mail; +import com.coderising.ood.srp.model.Product; +import com.coderising.ood.srp.model.User; + +import java.util.Iterator; +import java.util.List; + +/** + * Created by Iden on 2017/6/14. + */ +public class SendPriceMail implements SendMailFunction { + + + @Override + public void sendMail(List products) { + if (null == products || products.size() == 0) { + System.out.println("没有发现需要促销的产品"); + return; + } + Iterator iterator = products.iterator(); + while (iterator.hasNext()) { + Product product = iterator.next(); + List userList = product.getSubscribers(); + if (null == userList || userList.size() == 0) { + System.out.println("没有人订阅 " + product.getDescription() + " 信息"); + continue; + } + Iterator iter = userList.iterator(); + while (iter.hasNext()) { + User user = (User) iter.next(); + Mail mail = new Mail(); + mail.setSubject("您关注的产品降价了"); + mail.setContent("尊敬的 " + user.getName() + ", 您关注的产品 " + product.getDescription() + " 降价了,欢迎购买"); + mail.setToAddress(user.geteMail()); + mail.send(); + } + } + } +} diff --git "a/students/2831099157/ood-assignment/\344\277\203\351\224\200Mail\345\217\221\351\200\201\347\273\203\344\271\240\357\274\210\351\207\215\346\236\204\357\274\211.md" "b/students/2831099157/ood-assignment/\344\277\203\351\224\200Mail\345\217\221\351\200\201\347\273\203\344\271\240\357\274\210\351\207\215\346\236\204\357\274\211.md" new file mode 100644 index 0000000000..33634cb9a9 --- /dev/null +++ "b/students/2831099157/ood-assignment/\344\277\203\351\224\200Mail\345\217\221\351\200\201\347\273\203\344\271\240\357\274\210\351\207\215\346\236\204\357\274\211.md" @@ -0,0 +1,23 @@ +# 第一次OOD练习 # + +## 需求 ## +### 原项目已经实现根据产品列表文件发送促销Mail,需求一直在变化,比如通过数据库获取促销产品或者促销活动改为到货通知,抢购等;为了应变各种变化,需重构代码。 ### +## 需求分析 ## +需求可变因素:
+ +1. 促销产品列表文件可能会变更,或者变更获取方式(如通过数据库获取) +2. 促销活动会根据运营情况变更 +## 重构方案 ## +1. 提取GetProductsFunction,SendMailFunction接口 +2. 添加Notice抽象类,针对接口添加getProducts,sendMai方法 -------各类通知(降价促销,抢购活动,到货通知等等) +3. 添加Mail,Product,User实体类 +4. Mail初始化设置smtpHost,alSmtpHost,fromAddress参数,添加sendMail功能 +5. Product添加getSubscribers功能 +6. 添加GetProductsFunction,SendMailFunction接口实现类 +7. 添加PricePromotion继承Promotion,实现降价促销功能,实例化GetProductsFunction,SendMailFunction接口 +8. 主函数调用sendMail方法 + +## 重构后 ## +重构后项目,可以根据不同运营方案,添加GetProductsFunction,SendMailFunction接口实现类及Notice子类,向订阅者发送通告 + + From 312602f928acca1625ea44d742f5adea308f7101 Mon Sep 17 00:00:00 2001 From: gongxun Date: Wed, 14 Jun 2017 18:51:06 +0800 Subject: [PATCH 099/214] =?UTF-8?q?=E4=BA=8C=E6=AC=A1=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../785396327/first/ood/srp/ConfigParser.java | 9 +++ .../785396327/first/ood/srp/DBParser.java | 22 +++++++ .../785396327/first/ood/srp/EmailParser.java | 63 ++++--------------- .../785396327/first/ood/srp/FileParser.java | 16 ++--- .../first/ood/srp/PromotionFileParser.java | 27 ++++++++ .../ood/srp/PromotionMailConfigParser.java | 15 +++++ .../first/ood/srp/PromotionMailDBParser.java | 39 ++++++++++++ .../785396327/first/ood/srp/SendMailTest.java | 10 ++- 8 files changed, 137 insertions(+), 64 deletions(-) create mode 100644 students/785396327/first/ood/srp/ConfigParser.java create mode 100644 students/785396327/first/ood/srp/DBParser.java create mode 100644 students/785396327/first/ood/srp/PromotionFileParser.java create mode 100644 students/785396327/first/ood/srp/PromotionMailConfigParser.java create mode 100644 students/785396327/first/ood/srp/PromotionMailDBParser.java diff --git a/students/785396327/first/ood/srp/ConfigParser.java b/students/785396327/first/ood/srp/ConfigParser.java new file mode 100644 index 0000000000..21ab317bc9 --- /dev/null +++ b/students/785396327/first/ood/srp/ConfigParser.java @@ -0,0 +1,9 @@ +package first.ood.srp; + +/** + * Created by gongxun on 2017/6/14. + */ +public abstract class ConfigParser { + + abstract void parseInfoFromConfig(Email email); +} diff --git a/students/785396327/first/ood/srp/DBParser.java b/students/785396327/first/ood/srp/DBParser.java new file mode 100644 index 0000000000..d85f602528 --- /dev/null +++ b/students/785396327/first/ood/srp/DBParser.java @@ -0,0 +1,22 @@ +package first.ood.srp; + +import java.util.HashMap; +import java.util.List; + +/** + * Created by gongxun on 2017/6/14. + */ +public abstract class DBParser { + private String sql; + + protected DBParser(String sql) { + this.sql = sql; + } + + protected List parseInfoFromDB(Email email) { + List> data = DBUtil.query(sql, null); + return convertData(email, data); + } + + abstract List convertData(Email email, List> data); +} diff --git a/students/785396327/first/ood/srp/EmailParser.java b/students/785396327/first/ood/srp/EmailParser.java index 06782d2c09..74341ad610 100644 --- a/students/785396327/first/ood/srp/EmailParser.java +++ b/students/785396327/first/ood/srp/EmailParser.java @@ -1,63 +1,26 @@ package first.ood.srp; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; /** * Created by gongxun on 2017/6/12. */ public class EmailParser { - - public List parseEmailList(String filepath, String loadQuery) { - PromotionMail email = packageInfoFromConfig(); - packageInfoFromFile(email, filepath); - return packageInfoFromDB(loadQuery, email); - } - - private String parseMessage(HashMap map, PromotionMail promotionMail) { - String name = map.get(ConfigurationKeys.NAME_KEY); - String message = "尊敬的 " + name + ", 您关注的产品 " + promotionMail.getProductDesc() + " 降价了,欢迎购买!"; - return message; - } - - private String parseToAddress(HashMap map) { - return map.get(ConfigurationKeys.EMAIL_KEY); - } - - - private List packageInfoFromDB(String loadQuery, PromotionMail email) { - List> individualInfo = getIndividualInfo(loadQuery, new Object[]{email.getproductID()}); - List mailList = new ArrayList(); - for (HashMap map : individualInfo) { - PromotionMail completeMail = new PromotionMail(); - BeanUtils.copyProperties(completeMail, email); - completeMail.setToAddress(parseToAddress(map)); - completeMail.setMessage(parseMessage(map, completeMail)); - completeMail.setSubject("您关注的产品降价了"); - mailList.add(completeMail); - } - return mailList; - } - - private PromotionMail packageInfoFromFile(PromotionMail email, String filepath) { - FileParser fileParser = new FileParser(filepath); - email.setProductDesc(fileParser.parseProductDesc()); - email.setProductID(fileParser.parseProductID()); - return email; + private ConfigParser configParser; + private FileParser fileParser; + private DBParser dbParser; + + public EmailParser(ConfigParser configParser,FileParser fileParser,DBParser dbParser) { + this.configParser = configParser; + this.fileParser = fileParser; + this.dbParser = dbParser; } - private PromotionMail packageInfoFromConfig() { - PromotionMail email = new PromotionMail(); - - Configuration configuration = new Configuration(); - email.setSMTPHost(configuration.getProperty(ConfigurationKeys.SMTP_SERVER)); - email.setFromAddress(configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN)); - email.setAltSMTPHost(configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); - return email; + public List parseEmailList() { + PromotionMail promotionMail = new PromotionMail(); + configParser.parseInfoFromConfig(promotionMail); + fileParser.parseInfoFromFile(promotionMail); + return dbParser.parseInfoFromDB(promotionMail); } - private List> getIndividualInfo(String loadQuery, Object[] params) { - return DBUtil.query(loadQuery, params); - } } diff --git a/students/785396327/first/ood/srp/FileParser.java b/students/785396327/first/ood/srp/FileParser.java index 52827cdd32..1f14c11389 100644 --- a/students/785396327/first/ood/srp/FileParser.java +++ b/students/785396327/first/ood/srp/FileParser.java @@ -7,10 +7,10 @@ /** * Created by gongxun on 2017/6/12. */ -public class FileParser { - private String[] data; +public abstract class FileParser { + protected String[] data; - public FileParser(String filePath) { + protected FileParser(String filePath) { try { if (StringUtils.isEmpty(filePath)) throw new RuntimeException("init file parser must contains a legal file"); @@ -20,8 +20,7 @@ public FileParser(String filePath) { } } - private void readFile(String filePath) throws IOException - { + private void readFile(String filePath) throws IOException { BufferedReader br = null; try { br = new BufferedReader(new FileReader(filePath)); @@ -34,11 +33,6 @@ private void readFile(String filePath) throws IOException } } - public String parseProductID() { - return data[0]; - } + protected abstract void parseInfoFromFile(Email email); - public String parseProductDesc() { - return data[1]; - } } diff --git a/students/785396327/first/ood/srp/PromotionFileParser.java b/students/785396327/first/ood/srp/PromotionFileParser.java new file mode 100644 index 0000000000..78a8dfaa66 --- /dev/null +++ b/students/785396327/first/ood/srp/PromotionFileParser.java @@ -0,0 +1,27 @@ +package first.ood.srp; + +/** + * Created by gongxun on 2017/6/14. + */ +public class PromotionFileParser extends FileParser { + + + public PromotionFileParser(String filePath) { + super(filePath); + } + + @Override + protected void parseInfoFromFile(Email email) { + PromotionMail promotionMail = (PromotionMail) email; + promotionMail.setProductID(parseProductID()); + promotionMail.setProductDesc(parseProductDesc()); + } + + private String parseProductID() { + return super.data[0]; + } + + private String parseProductDesc() { + return super.data[1]; + } +} diff --git a/students/785396327/first/ood/srp/PromotionMailConfigParser.java b/students/785396327/first/ood/srp/PromotionMailConfigParser.java new file mode 100644 index 0000000000..dd6ce2e89f --- /dev/null +++ b/students/785396327/first/ood/srp/PromotionMailConfigParser.java @@ -0,0 +1,15 @@ +package first.ood.srp; + +/** + * Created by gongxun on 2017/6/14. + */ +public class PromotionMailConfigParser extends ConfigParser { + + @Override + void parseInfoFromConfig(Email email) { + Configuration configuration = new Configuration(); + email.setSMTPHost(configuration.getProperty(ConfigurationKeys.SMTP_SERVER)); + email.setFromAddress(configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN)); + email.setAltSMTPHost(configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + } +} diff --git a/students/785396327/first/ood/srp/PromotionMailDBParser.java b/students/785396327/first/ood/srp/PromotionMailDBParser.java new file mode 100644 index 0000000000..68878c02d4 --- /dev/null +++ b/students/785396327/first/ood/srp/PromotionMailDBParser.java @@ -0,0 +1,39 @@ +package first.ood.srp; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * Created by gongxun on 2017/6/14. + */ +public class PromotionMailDBParser extends DBParser { + + protected PromotionMailDBParser(String sql) { + super(sql); + } + + @Override + List convertData(Email email, List> data) { + List mailList = new ArrayList(); + for (HashMap map : data) { + PromotionMail completeMail = new PromotionMail(); + BeanUtils.copyProperties(completeMail, email); + completeMail.setToAddress(parseToAddress(map)); + completeMail.setMessage(parseMessage(map, completeMail)); + completeMail.setSubject("您关注的产品降价了"); + mailList.add(completeMail); + } + return mailList; + } + + private String parseMessage(HashMap map, PromotionMail promotionMail) { + String name = map.get(ConfigurationKeys.NAME_KEY); + String message = "尊敬的 " + name + ", 您关注的产品 " + promotionMail.getProductDesc() + " 降价了,欢迎购买!"; + return message; + } + + private String parseToAddress(HashMap map) { + return map.get(ConfigurationKeys.EMAIL_KEY); + } +} diff --git a/students/785396327/first/ood/srp/SendMailTest.java b/students/785396327/first/ood/srp/SendMailTest.java index d82df14609..ef917aa593 100644 --- a/students/785396327/first/ood/srp/SendMailTest.java +++ b/students/785396327/first/ood/srp/SendMailTest.java @@ -8,12 +8,16 @@ public class SendMailTest { public static void main(String[] args) { - String loadQuery = "Select name from subscriptions where product_id= ? and send_mail=1"; + String sql = "Select name from subscriptions where product_id= ? and send_mail=1"; String filepath = "D:\\workspace\\IDEA\\homework\\coding2017_section2\\coding2017\\students\\785396327\\first\\ood\\srp\\product_promotion.txt"; boolean isDebug = false; - EmailParser emailParser = new EmailParser(); - List promotionMails = emailParser.parseEmailList(filepath, loadQuery); + ConfigParser configParser = new PromotionMailConfigParser(); + FileParser fileParser = new PromotionFileParser(filepath); + DBParser DBParser = new PromotionMailDBParser(sql); + + EmailParser emailParser = new EmailParser(configParser, fileParser, DBParser); + List promotionMails = emailParser.parseEmailList(); MailSender mailSender = new MailSender(); mailSender.sendMailList(promotionMails, isDebug); } From aacfc516d9b527422345438ab52e9b730c030ae1 Mon Sep 17 00:00:00 2001 From: Xujie Date: Wed, 14 Jun 2017 19:09:10 +0800 Subject: [PATCH 100/214] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9Asrp=E5=B7=A5=E7=A8=8B=E7=9A=84=E9=87=8D=E6=9E=8401?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1、增加FileUtil类,负责读取txt中降价产品。 2、增加Product类,包含id和desc属性。 3、重构MailUtil类,负责配置邮件服务器和发送邮件。 4、增加UserService类,负责获得关注降价产品的用户列表。 5、重构PromotionMail类,只负责设置邮件主题、正文、收件人列表,以及发送邮件。 --- .../edu/coerscnu/ood/srp/Configuration.java | 25 +++++ .../coerscnu/ood/srp/ConfigurationKeys.java | 9 ++ .../src/edu/coerscnu/ood/srp/DBUtil.java | 26 ++++++ .../src/edu/coerscnu/ood/srp/FileUtil.java | 37 ++++++++ .../src/edu/coerscnu/ood/srp/MailUtil.java | 73 +++++++++++++++ .../src/edu/coerscnu/ood/srp/Product.java | 33 +++++++ .../edu/coerscnu/ood/srp/PromotionMail.java | 93 +++++++++++++++++++ .../src/edu/coerscnu/ood/srp/UserService.java | 28 ++++++ .../coerscnu/ood/srp/product_promotion.txt | 4 + 9 files changed, 328 insertions(+) create mode 100644 students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/Configuration.java create mode 100644 students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/ConfigurationKeys.java create mode 100644 students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/DBUtil.java create mode 100644 students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/FileUtil.java create mode 100644 students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/MailUtil.java create mode 100644 students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/Product.java create mode 100644 students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/PromotionMail.java create mode 100644 students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/UserService.java create mode 100644 students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/product_promotion.txt diff --git a/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/Configuration.java b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/Configuration.java new file mode 100644 index 0000000000..b6a82c425f --- /dev/null +++ b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/Configuration.java @@ -0,0 +1,25 @@ +package edu.coerscnu.ood.srp; + +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static { + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + configurations.put(ConfigurationKeys.IS_DEBUG, true); + } + + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * + * @param key + * @return + */ + public Object getProperty(String key) { + return configurations.get(key); + } +} diff --git a/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/ConfigurationKeys.java b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..b7cecc897b --- /dev/null +++ b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package edu.coerscnu.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + public static final String IS_DEBUG = "debug"; +} diff --git a/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/DBUtil.java b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/DBUtil.java new file mode 100644 index 0000000000..6632d110fa --- /dev/null +++ b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/DBUtil.java @@ -0,0 +1,26 @@ +package edu.coerscnu.ood.srp; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 获取用户列表,应该从数据库读, 但是简化为直接生成。 + * + * @param sql + * @return + */ + public static List> query(String sql) { + + List> userList = new ArrayList>(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put(UserService.NAME_KEY, "User" + i); + userInfo.put(UserService.MAIL_KEY, i + "aa@bb.com"); + userList.add(userInfo); + } + return userList; + } +} diff --git a/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/FileUtil.java b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/FileUtil.java new file mode 100644 index 0000000000..fd474c2da2 --- /dev/null +++ b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/FileUtil.java @@ -0,0 +1,37 @@ +package edu.coerscnu.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * FileUtil类负责读取.txt文件内容,并以List(产品id,产品描述)形式返回。 + * + * @author xujie + * + */ +public class FileUtil { + + public static List readFile(String path) throws IOException { + BufferedReader br = null; + List list = new ArrayList<>(); + try { + File file = new File(path); + FileReader fr = new FileReader(file); + br = new BufferedReader(fr); + String temp; + while ((temp = br.readLine()) != null) { + String[] data = temp.split(" "); + list.add(data); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + br.close(); + } + return list; + } +} diff --git a/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/MailUtil.java b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/MailUtil.java new file mode 100644 index 0000000000..806c9fff62 --- /dev/null +++ b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/MailUtil.java @@ -0,0 +1,73 @@ +package edu.coerscnu.ood.srp; + +/** + * 邮件类公共类 1、配置服务器 2、发送邮件 + * + * @author xujie + * + */ +public class MailUtil { + + private static String smtpHost; // 主服务器 + private static String altSmtpHost; // 备用服务器 + private static String fromAddress; // 发件人 + private static boolean isDebug; // 是否为调试环境 + + /** + * 配置服务器 + */ + public static void configureHost() { + Configuration config = new Configuration(); + smtpHost = (String) config.getProperty(ConfigurationKeys.SMTP_SERVER); + altSmtpHost = (String) config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + fromAddress = (String) config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + isDebug = (boolean) config.getProperty(ConfigurationKeys.IS_DEBUG); + } + + /** + * 发送邮件,对外不可见 + * + * @param toAddress + * @param fromAddress + * @param subject + * @param message + * @param smtpHost + * @param debug + */ + private static void sendMail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + // 假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + } + + /** + * 发送邮件,对外可见 + * + * @param toAddress + * @param subject + * @param message + */ + public static void sendMail(String toAddress, String subject, String message) { + configureHost(); + if (isDebug) { + System.out.println("调试环境"); + } else { + System.out.println("正式环境"); + } + if (smtpHost != null) { + System.out.println("使用主服务器发送邮件"); + sendMail(toAddress, fromAddress, subject, message, smtpHost, isDebug); + } else if (altSmtpHost != null) { + System.out.println("使用备用服务器发送邮件"); + sendMail(toAddress, fromAddress, subject, message, altSmtpHost, isDebug); + } else { + System.out.println("服务器异常,无法发送邮件"); + } + + } +} diff --git a/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/Product.java b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/Product.java new file mode 100644 index 0000000000..64cbcc6d0c --- /dev/null +++ b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/Product.java @@ -0,0 +1,33 @@ +package edu.coerscnu.ood.srp; + +/** + * 产品类,包含id和描述两个属性 + * + * @author xujie + * + */ +public class Product { + protected String productID = null; + protected String productDesc = null; + + public Product(String id, String desc) { + productID = id; + productDesc = desc; + } + + public void setProductID(String id) { + productID = id; + } + + public void setProductDesc(String desc) { + productDesc = desc; + } + + public String getProductID() { + return productID; + } + + public String getProductDesc() { + return productDesc; + } +} diff --git a/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/PromotionMail.java b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..14d80dcaf9 --- /dev/null +++ b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/PromotionMail.java @@ -0,0 +1,93 @@ +package edu.coerscnu.ood.srp; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +/** + * 促销邮件类 + * + * 1、设置邮件主题 + * + * 2、设置邮件正文 + * + * 3、设置邮件收件人列表 + * + * 4、发送邮件 + * + * @author xujie + * + */ +public class PromotionMail { + + protected String subject = null; + protected String message = null; + protected List> mailList; + + public static void main(String[] args) throws Exception { + + String path = "src/edu/coerscnu/ood/srp/product_promotion.txt"; + List productList = FileUtil.readFile(path); + for (String[] prod : productList) { + Product product = new Product(prod[0], prod[1]); + PromotionMail pm = new PromotionMail(); + pm.setMailList(product); + pm.sendMails(product); + } + } + + /** + * 设置邮件主题 + * + * @param subject + */ + public void setSubject(String subject) { + this.subject = subject; + } + + /** + * 设置邮件正文 + * + * @param userInfo + * @param product + * @throws IOException + */ + protected void setMessage(HashMap userInfo, Product product) throws IOException { + String name = (String) userInfo.get(UserService.NAME_KEY); + String desc = product.getProductDesc(); + message = "尊敬的 " + name + ", 您关注的产品 " + desc + " 降价了,欢迎购买!"; + } + + /** + * 设置收件人列表 + * + * @param product + * @throws Exception + */ + protected void setMailList(Product product) throws Exception { + UserService userService = new UserService(); + userService.setLoadQuery(product); + mailList = userService.loadMailingList(); + } + + /** + * 发送邮件 + * + * @throws IOException + */ + protected void sendMails(Product product) throws IOException { + if (mailList != null) { + Iterator> iter = mailList.iterator(); + while (iter.hasNext()) { + HashMap user = iter.next(); + String toAddress = (String) user.get(UserService.MAIL_KEY); + if (toAddress.length() > 0) { + setSubject("您关注的产品降价了"); + setMessage(user, product); + MailUtil.sendMail(toAddress, subject, message); + } + } + } + } +} diff --git a/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/UserService.java b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/UserService.java new file mode 100644 index 0000000000..af04bf5709 --- /dev/null +++ b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/UserService.java @@ -0,0 +1,28 @@ +package edu.coerscnu.ood.srp; + +import java.util.HashMap; +import java.util.List; + +/** + * 用户类,获取关注降价产品的客户的名字和邮箱地址 + * + * @author xujie + * + */ +public class UserService { + + protected static final String NAME_KEY = "NAME"; + protected static final String MAIL_KEY = "EMAIL"; + + protected String query = ""; + + protected void setLoadQuery(Product product) { + query = "Select name from subscriptions " + "where product_id= '" + product.getProductID() + "' " + + "and send_mail=1 "; + System.out.println("loadQuery set\n"); + } + + protected List> loadMailingList() throws Exception { + return DBUtil.query(query); + } +} diff --git a/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/product_promotion.txt b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 9c8a7b57e001e08f30fe7916d63c62fee70d2323 Mon Sep 17 00:00:00 2001 From: Xujie Date: Wed, 14 Jun 2017 19:11:32 +0800 Subject: [PATCH 101/214] =?UTF-8?q?=E5=B9=BF=E5=B7=9E-=E8=AE=B8=E6=B4=81?= =?UTF-8?q?=EF=BC=9A=E7=AC=AC=E4=B8=80=E6=AC=A1ood=E4=BD=9C=E4=B8=9Asrp?= =?UTF-8?q?=E5=B7=A5=E7=A8=8B=E9=87=8D=E6=9E=8402?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1、增加FileUtil类,负责读取txt中降价产品。 2、增加Product类,包含id和desc属性。 3、重构MailUtil类,负责配置邮件服务器和发送邮件。 4、增加UserService类,负责获得关注降价产品的用户列表。 5、重构PromotionMail类,只负责设置邮件主题、正文、收件人列表,以及发送邮件。 --- .../assignment01/src/edu/coerscnu/ood/srp/MailUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/MailUtil.java b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/MailUtil.java index 806c9fff62..1dd007b3d5 100644 --- a/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/MailUtil.java +++ b/students/617314917/ood/ood-assignment/assignment01/src/edu/coerscnu/ood/srp/MailUtil.java @@ -1,7 +1,7 @@ package edu.coerscnu.ood.srp; /** - * 邮件类公共类 1、配置服务器 2、发送邮件 + * 邮件公共类 1、配置服务器 2、发送邮件 * * @author xujie * From 9b9a3a8068a9a2eff80835807c5664265eec0b4d Mon Sep 17 00:00:00 2001 From: Lyccccc Date: Wed, 14 Jun 2017 21:34:26 +0800 Subject: [PATCH 102/214] ood-assignment task --- students/472779948/ood-assignment/pom.xml | 34 ++++++++ .../com/coderising/ood/srp/Configuration.java | 23 +++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 ++ .../java/com/coderising/ood/srp/DBUtil.java | 37 ++++++++ .../com/coderising/ood/srp/FileManager.java | 25 ++++++ .../java/com/coderising/ood/srp/Mail.java | 83 ++++++++++++++++++ .../java/com/coderising/ood/srp/MailUtil.java | 23 +++++ .../java/com/coderising/ood/srp/Product.java | 30 +++++++ .../com/coderising/ood/srp/PromotionMail.java | 85 +++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 10 files changed, 353 insertions(+) create mode 100644 students/472779948/ood-assignment/pom.xml create mode 100644 students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/FileManager.java create mode 100644 students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java create mode 100644 students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/472779948/ood-assignment/pom.xml b/students/472779948/ood-assignment/pom.xml new file mode 100644 index 0000000000..06d60721b0 --- /dev/null +++ b/students/472779948/ood-assignment/pom.xml @@ -0,0 +1,34 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + 1.8 + 1.8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..930ab2805c --- /dev/null +++ b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,37 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + protected String sendMailQuery = null; + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param productID + * @return + */ + public static List query(String productID){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } + + protected void setLoadQuery(String productID) throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID + "' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } +} diff --git a/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/FileManager.java b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/FileManager.java new file mode 100644 index 0000000000..a280c09d67 --- /dev/null +++ b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/FileManager.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class FileManager { + //读取配置文件方法,单独提出来,其他的类需要时可复用 + public static String[] readFile(File file) throws IOException // @02C + { + String data[] = null; + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + data = temp.split(" "); + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + return data; + } +} diff --git a/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java new file mode 100644 index 0000000000..42da2ec0b1 --- /dev/null +++ b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java @@ -0,0 +1,83 @@ +package com.coderising.ood.srp; + +public class Mail { + private String smtpHost = null; + private String altSmtpHost = null; + private String fromAddress = null; + private String toAddress = null; + private String subject = null; + private String message = null; + + + public Mail() { + } + + public Mail(String smtpHost, String altSmtpHost, String fromAddress, String toAddress, String subject, String message) { + this.smtpHost = smtpHost; + this.altSmtpHost = altSmtpHost; + this.fromAddress = fromAddress; + this.toAddress = toAddress; + this.subject = subject; + this.message = message; + } + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + @Override + public String toString() { + return "Mail{" + + "smtpHost='" + smtpHost + '\'' + + ", altSmtpHost='" + altSmtpHost + '\'' + + ", fromAddress='" + fromAddress + '\'' + + ", toAddress='" + toAddress + '\'' + + ", subject='" + subject + '\'' + + ", message='" + message + '\'' + + '}'; + } +} diff --git a/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..ff00545c91 --- /dev/null +++ b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; + +import java.util.List; + +public class MailUtil { + + public static void sendEmail(Mail mail, boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(mail.getFromAddress()).append("\n"); + buffer.append("To:").append(mail.getToAddress()).append("\n"); + buffer.append("Subject:").append(mail.getSubject()).append("\n"); + buffer.append("Content:").append(mail.getMessage()).append("\n"); + System.out.println(buffer.toString()); + + } + + public static List loadMailingList(String productID) throws Exception { + return DBUtil.query(productID); + } + + +} diff --git a/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..e4074ba44a --- /dev/null +++ b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,30 @@ +package com.coderising.ood.srp; + +/** + * Created by lenovo on 2017/6/13. + */ +public class Product { + private String productID; + private String productDesc; + + public Product(String productID, String productDesc) { + this.productID = productID; + this.productDesc = productDesc; + } + + public String getproductID() { + return productID; + } + + public void setproductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } +} diff --git a/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..f7e083ec4b --- /dev/null +++ b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,85 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + protected Mail mail = null; + protected Product product = null; + + private static Configuration config; + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + public static void main(String[] args) throws Exception { + + File f = new File("E:\\workspace\\private\\projects\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + config = new Configuration(); + + //构造Mail对象 + mail = new Mail(); + mail.setSmtpHost(config.getProperty(ConfigurationKeys.SMTP_SERVER)); + mail.setAltSmtpHost(config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + mail.setFromAddress(config.getProperty(ConfigurationKeys.EMAIL_ADMIN)); + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + String[] data = FileManager.readFile(file); + product = new Product(data[0],data[1]); + + sendEMails(mailDebug, MailUtil.loadMailingList(product.getproductID()));//MailUtil + } + + protected void setMessage(HashMap userInfo) throws IOException { + String name = (String) userInfo.get(NAME_KEY); + mail.setSubject("您关注的产品降价了"); + mail.setMessage("尊敬的 " + name + ", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!"); + + } + + protected void configureEMail(HashMap userInfo) throws IOException { + mail.setToAddress((String) userInfo.get(EMAIL_KEY)); + if (mail.getToAddress().length() > 0) + setMessage(userInfo); + } + + protected void sendEMails(boolean debug, List mailingList) throws IOException { + System.out.println("开始发送邮件"); + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try { + if (mail.getToAddress().length() > 0) + MailUtil.sendEmail(mail, debug); + } catch (Exception e) { + + try { + MailUtil.sendEmail(mail, debug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + } else { + System.out.println("没有邮件发送"); + + } + } +} diff --git a/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/472779948/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 2507cdbbbc0fc6af99146608515bbd671ec1b2d1 Mon Sep 17 00:00:00 2001 From: cmhello88 Date: Wed, 14 Jun 2017 23:15:41 +0800 Subject: [PATCH 103/214] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../34594980/data-structure/answer/pom.xml | 32 +++ .../coderising/download/DownloadThread.java | 20 ++ .../coderising/download/FileDownloader.java | 73 +++++++ .../download/FileDownloaderTest.java | 59 ++++++ .../coderising/download/api/Connection.java | 23 ++ .../download/api/ConnectionException.java | 5 + .../download/api/ConnectionManager.java | 10 + .../download/api/DownloadListener.java | 5 + .../download/impl/ConnectionImpl.java | 27 +++ .../download/impl/ConnectionManagerImpl.java | 15 ++ .../coderising/litestruts/LoginAction.java | 39 ++++ .../com/coderising/litestruts/Struts.java | 34 +++ .../com/coderising/litestruts/StrutsTest.java | 43 ++++ .../java/com/coderising/litestruts/View.java | 23 ++ .../java/com/coderising/litestruts/struts.xml | 11 + .../main/java/com/coding/basic/Iterator.java | 7 + .../src/main/java/com/coding/basic/List.java | 9 + .../com/coding/basic/array/ArrayList.java | 35 +++ .../com/coding/basic/array/ArrayUtil.java | 96 +++++++++ .../coding/basic/linklist/LRUPageFrame.java | 164 +++++++++++++++ .../basic/linklist/LRUPageFrameTest.java | 34 +++ .../com/coding/basic/linklist/LinkedList.java | 125 +++++++++++ .../com/coding/basic/queue/CircleQueue.java | 47 +++++ .../coding/basic/queue/CircleQueueTest.java | 44 ++++ .../java/com/coding/basic/queue/Josephus.java | 39 ++++ .../com/coding/basic/queue/JosephusTest.java | 27 +++ .../java/com/coding/basic/queue/Queue.java | 61 ++++++ .../basic/queue/QueueWithTwoStacks.java | 55 +++++ .../com/coding/basic/stack/QuickMinStack.java | 44 ++++ .../coding/basic/stack/QuickMinStackTest.java | 39 ++++ .../java/com/coding/basic/stack/Stack.java | 24 +++ .../com/coding/basic/stack/StackUtil.java | 168 +++++++++++++++ .../com/coding/basic/stack/StackUtilTest.java | 86 ++++++++ .../basic/stack/StackWithTwoQueues.java | 53 +++++ .../basic/stack/StackWithTwoQueuesTest.java | 36 ++++ .../java/com/coding/basic/stack/Tail.java | 5 + .../basic/stack/TwoStackInOneArray.java | 117 ++++++++++ .../basic/stack/TwoStackInOneArrayTest.java | 65 ++++++ .../coding/basic/stack/expr/InfixExpr.java | 72 +++++++ .../basic/stack/expr/InfixExprTest.java | 52 +++++ .../basic/stack/expr/InfixToPostfix.java | 43 ++++ .../basic/stack/expr/InfixToPostfixTest.java | 41 ++++ .../coding/basic/stack/expr/PostfixExpr.java | 46 ++++ .../basic/stack/expr/PostfixExprTest.java | 41 ++++ .../coding/basic/stack/expr/PrefixExpr.java | 52 +++++ .../basic/stack/expr/PrefixExprTest.java | 45 ++++ .../com/coding/basic/stack/expr/Token.java | 50 +++++ .../coding/basic/stack/expr/TokenParser.java | 57 +++++ .../basic/stack/expr/TokenParserTest.java | 41 ++++ .../coding/basic/tree/BinarySearchTree.java | 189 +++++++++++++++++ .../basic/tree/BinarySearchTreeTest.java | 108 ++++++++++ .../com/coding/basic/tree/BinaryTreeNode.java | 36 ++++ .../com/coding/basic/tree/BinaryTreeUtil.java | 116 ++++++++++ .../coding/basic/tree/BinaryTreeUtilTest.java | 75 +++++++ .../java/com/coding/basic/tree/FileList.java | 34 +++ .../data-structure/assignment/pom.xml | 32 +++ .../coderising/download/DownloadThread.java | 20 ++ .../coderising/download/FileDownloader.java | 73 +++++++ .../download/FileDownloaderTest.java | 59 ++++++ .../coderising/download/api/Connection.java | 23 ++ .../download/api/ConnectionException.java | 5 + .../download/api/ConnectionManager.java | 10 + .../download/api/DownloadListener.java | 5 + .../download/impl/ConnectionImpl.java | 27 +++ .../download/impl/ConnectionManagerImpl.java | 15 ++ .../coderising/litestruts/LoginAction.java | 39 ++++ .../com/coderising/litestruts/Struts.java | 34 +++ .../com/coderising/litestruts/StrutsTest.java | 43 ++++ .../java/com/coderising/litestruts/View.java | 23 ++ .../java/com/coderising/litestruts/struts.xml | 11 + .../com/coderising/ood/course/bad/Course.java | 24 +++ .../ood/course/bad/CourseOffering.java | 26 +++ .../ood/course/bad/CourseService.java | 16 ++ .../coderising/ood/course/bad/Student.java | 14 ++ .../coderising/ood/course/good/Course.java | 18 ++ .../ood/course/good/CourseOffering.java | 34 +++ .../ood/course/good/CourseService.java | 14 ++ .../coderising/ood/course/good/Student.java | 21 ++ .../java/com/coderising/ood/ocp/DateUtil.java | 10 + .../java/com/coderising/ood/ocp/Logger.java | 38 ++++ .../java/com/coderising/ood/ocp/MailUtil.java | 10 + .../java/com/coderising/ood/ocp/SMSUtil.java | 10 + .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + .../main/java/com/coding/basic/Iterator.java | 7 + .../src/main/java/com/coding/basic/List.java | 9 + .../com/coding/basic/array/ArrayList.java | 35 +++ .../com/coding/basic/array/ArrayUtil.java | 96 +++++++++ .../coding/basic/linklist/LRUPageFrame.java | 57 +++++ .../basic/linklist/LRUPageFrameTest.java | 34 +++ .../com/coding/basic/linklist/LinkedList.java | 125 +++++++++++ .../com/coding/basic/queue/CircleQueue.java | 39 ++++ .../java/com/coding/basic/queue/Josephus.java | 18 ++ .../com/coding/basic/queue/JosephusTest.java | 27 +++ .../java/com/coding/basic/queue/Queue.java | 61 ++++++ .../basic/queue/QueueWithTwoStacks.java | 47 +++++ .../com/coding/basic/stack/QuickMinStack.java | 19 ++ .../java/com/coding/basic/stack/Stack.java | 24 +++ .../com/coding/basic/stack/StackUtil.java | 48 +++++ .../com/coding/basic/stack/StackUtilTest.java | 65 ++++++ .../basic/stack/StackWithTwoQueues.java | 16 ++ .../basic/stack/TwoStackInOneArray.java | 57 +++++ .../coding/basic/stack/expr/InfixExpr.java | 15 ++ .../basic/stack/expr/InfixExprTest.java | 52 +++++ .../basic/stack/expr/InfixToPostfix.java | 14 ++ .../coding/basic/stack/expr/PostfixExpr.java | 18 ++ .../basic/stack/expr/PostfixExprTest.java | 41 ++++ .../coding/basic/stack/expr/PrefixExpr.java | 18 ++ .../basic/stack/expr/PrefixExprTest.java | 45 ++++ .../com/coding/basic/stack/expr/Token.java | 50 +++++ .../coding/basic/stack/expr/TokenParser.java | 57 +++++ .../basic/stack/expr/TokenParserTest.java | 41 ++++ .../coding/basic/tree/BinarySearchTree.java | 55 +++++ .../basic/tree/BinarySearchTreeTest.java | 109 ++++++++++ .../com/coding/basic/tree/BinaryTreeNode.java | 35 +++ .../com/coding/basic/tree/BinaryTreeUtil.java | 66 ++++++ .../coding/basic/tree/BinaryTreeUtilTest.java | 75 +++++++ .../java/com/coding/basic/tree/FileList.java | 10 + students/34594980/ood/ood-assignment/pom.xml | 44 ++++ .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 129 files changed, 5636 insertions(+) create mode 100644 students/34594980/data-structure/answer/pom.xml create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/View.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/Iterator.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/List.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtilTest.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java create mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java create mode 100644 students/34594980/data-structure/assignment/pom.xml create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/DownloadThread.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/FileDownloader.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/FileDownloaderTest.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/Connection.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionException.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionManager.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/DownloadListener.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionImpl.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/LoginAction.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/Struts.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/StrutsTest.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/View.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/struts.xml create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Course.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseOffering.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseService.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Student.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Course.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseOffering.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseService.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Student.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/Logger.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/Iterator.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/List.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayList.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayUtil.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrame.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LinkedList.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/CircleQueue.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/Josephus.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/JosephusTest.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/Queue.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/QuickMinStack.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/Stack.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtil.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtilTest.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExpr.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/Token.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParser.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTree.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeNode.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java create mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/FileList.java create mode 100644 students/34594980/ood/ood-assignment/pom.xml create mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/34594980/data-structure/answer/pom.xml b/students/34594980/data-structure/answer/pom.xml new file mode 100644 index 0000000000..ac6ba882df --- /dev/null +++ b/students/34594980/data-structure/answer/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ds-answer + 0.0.1-SNAPSHOT + jar + + ds-answer + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..900a3ad358 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java @@ -0,0 +1,20 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + + } +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..c3c8a3f27d --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java @@ -0,0 +1,73 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn,0,length-1).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..4ff7f46ae0 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..36a9d2ce15 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,27 @@ +package com.coderising.download.impl; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + @Override + public int getContentLength() { + + return 0; + } + + @Override + public void close() { + + + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..172371dd55 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return null; + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/View.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml b/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/Iterator.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/List.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java new file mode 100644 index 0000000000..4576c016af --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java @@ -0,0 +1,35 @@ +package com.coding.basic.array; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java new file mode 100644 index 0000000000..45740e6d57 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coding.basic.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..24b9d8b155 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,164 @@ +package com.coding.basic.linklist; + + +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + private int currentSize; + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + this.currentSize = 0; + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + + Node node = find(pageNum); + //在该队列中存在, 则提到队列头 + if (node != null) { + + moveExistingNodeToHead(node); + + } else{ + + node = new Node(); + node.pageNum = pageNum; + + // 缓存容器是否已经超过大小. + if (currentSize >= capacity) { + removeLast(); + + } + + addNewNodetoHead(node); + + + + + } + } + + private void addNewNodetoHead(Node node) { + + if(isEmpty()){ + + node.prev = null; + node.next = null; + first = node; + last = node; + + } else{ + node.prev = null; + node.next = first; + first.prev = node; + first = node; + } + this.currentSize ++; + } + + private Node find(int data){ + + Node node = first; + while(node != null){ + if(node.pageNum == data){ + return node; + } + node = node.next; + } + return null; + + } + + + + + + + /** + * 删除链表尾部节点 表示 删除最少使用的缓存对象 + */ + private void removeLast() { + Node prev = last.prev; + prev.next = null; + last.prev = null; + last = prev; + this.currentSize --; + } + + /** + * 移动到链表头,表示这个节点是最新使用过的 + * + * @param node + */ + private void moveExistingNodeToHead(Node node) { + + if (node == first) { + + return; + } + else if(node == last){ + //当前节点是链表尾, 需要放到链表头 + Node prevNode = node.prev; + prevNode.next = null; + last.prev = null; + last = prevNode; + + } else{ + //node 在链表的中间, 把node 的前后节点连接起来 + Node prevNode = node.prev; + prevNode.next = node.next; + + Node nextNode = node.next; + nextNode.prev = prevNode; + + + } + + node.prev = null; + node.next = first; + first.prev = node; + first = node; + + } + private boolean isEmpty(){ + return (first == null) && (last == null); + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + + + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..7fd72fc2b4 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,34 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + frame.access(5); + Assert.assertEquals("5,4,0", frame.toString()); + + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java new file mode 100644 index 0000000000..f4c7556a2e --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java @@ -0,0 +1,125 @@ +package com.coding.basic.linklist; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java new file mode 100644 index 0000000000..f169d5f8e4 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java @@ -0,0 +1,47 @@ +package com.coding.basic.queue; + +public class CircleQueue { + + //用数组来保存循环队列的元素 + private Object[] elementData ; + int size = 0; + //队头 + private int front = 0; + //队尾 + private int rear = 0; + + public CircleQueue(int capacity){ + elementData = new Object[capacity]; + } + public boolean isEmpty() { + return (front == rear) && !isFull(); + + } + + public boolean isFull(){ + return size == elementData.length; + } + public int size() { + return size; + } + + public void enQueue(E data) { + if(isFull()){ + throw new RuntimeException("The queue is full"); + } + rear = (rear+1) % elementData.length; + elementData[rear++] = data; + size++; + } + + public E deQueue() { + if(isEmpty()){ + throw new RuntimeException("The queue is empty"); + } + E data = (E)elementData[front]; + elementData[front] = null; + front = (front+1) % elementData.length; + size --; + return data; + } +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java new file mode 100644 index 0000000000..7307eb77d4 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java @@ -0,0 +1,44 @@ +package com.coding.basic.queue; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class CircleQueueTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + CircleQueue queue = new CircleQueue(5); + Assert.assertTrue(queue.isEmpty()); + Assert.assertFalse(queue.isFull()); + + queue.enQueue("a"); + queue.enQueue("b"); + queue.enQueue("c"); + queue.enQueue("d"); + queue.enQueue("e"); + + Assert.assertTrue(queue.isFull()); + Assert.assertFalse(queue.isEmpty()); + Assert.assertEquals(5, queue.size()); + + Assert.assertEquals("a", queue.deQueue()); + Assert.assertEquals("b", queue.deQueue()); + Assert.assertEquals("c", queue.deQueue()); + Assert.assertEquals("d", queue.deQueue()); + Assert.assertEquals("e", queue.deQueue()); + + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java new file mode 100644 index 0000000000..36ec615d36 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java @@ -0,0 +1,39 @@ +package com.coding.basic.queue; + +import java.util.ArrayList; +import java.util.List; + +/** + * 用Queue来实现Josephus问题 + * 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来 + * @author liuxin + * + */ +public class Josephus { + + public static List execute(int n, int m){ + + Queue queue = new Queue(); + for (int i = 0; i < n; i++){ + queue.enQueue(i); + } + + List result = new ArrayList(); + int i = 0; + + while (!queue.isEmpty()) { + + int x = queue.deQueue(); + + if (++i % m == 0){ + result.add(x); + } else{ + queue.enQueue(x); + } + } + + + return result; + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java new file mode 100644 index 0000000000..7d90318b51 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java @@ -0,0 +1,27 @@ +package com.coding.basic.queue; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class JosephusTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testExecute() { + + Assert.assertEquals("[1, 3, 5, 0, 4, 2, 6]", Josephus.execute(7, 2).toString()); + + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java new file mode 100644 index 0000000000..c4c4b7325e --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java @@ -0,0 +1,61 @@ +package com.coding.basic.queue; + +import java.util.NoSuchElementException; + +public class Queue { + private Node first; + private Node last; + private int size; + + + private static class Node { + private E item; + private Node next; + } + + + public Queue() { + first = null; + last = null; + size = 0; + } + + + public boolean isEmpty() { + return first == null; + } + + public int size() { + return size; + } + + + + public void enQueue(E data) { + Node oldlast = last; + last = new Node(); + last.item = data; + last.next = null; + if (isEmpty()) { + first = last; + } + else{ + oldlast.next = last; + } + size++; + } + + public E deQueue() { + if (isEmpty()) { + throw new NoSuchElementException("Queue underflow"); + } + E item = first.item; + first = first.next; + size--; + if (isEmpty()) { + last = null; + } + return item; + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java new file mode 100644 index 0000000000..bc97df0800 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java @@ -0,0 +1,55 @@ +package com.coding.basic.queue; + +import java.util.NoSuchElementException; +import java.util.Stack; + +public class QueueWithTwoStacks { + private Stack stack1; + private Stack stack2; + + + public QueueWithTwoStacks() { + stack1 = new Stack(); + stack2 = new Stack(); + } + + + private void moveStack1ToStack2() { + while (!stack1.isEmpty()){ + stack2.push(stack1.pop()); + } + + } + + + public boolean isEmpty() { + return stack1.isEmpty() && stack2.isEmpty(); + } + + + + public int size() { + return stack1.size() + stack2.size(); + } + + + + public void enQueue(E item) { + stack1.push(item); + } + + public E deQueue() { + if (isEmpty()) { + throw new NoSuchElementException("Queue is empty"); + } + if (stack2.isEmpty()) { + moveStack1ToStack2(); + } + + return stack2.pop(); + } + + + + } + diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java new file mode 100644 index 0000000000..faf2644ab1 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java @@ -0,0 +1,44 @@ +package com.coding.basic.stack; + +import java.util.Stack; +/** + * 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素 + * finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值 + * @author liuxin + * + */ +public class QuickMinStack { + + private Stack normalStack = new Stack(); + private Stack minNumStack = new Stack(); + + public void push(int data){ + + normalStack.push(data); + + if(minNumStack.isEmpty()){ + minNumStack.push(data); + } else{ + if(minNumStack.peek() >= data) { + minNumStack.push(data); + } + } + + } + public int pop(){ + if(normalStack.isEmpty()){ + throw new RuntimeException("the stack is empty"); + } + int value = normalStack.pop(); + if(value == minNumStack.peek()){ + minNumStack.pop(); + } + return value; + } + public int findMin(){ + if(minNumStack.isEmpty()){ + throw new RuntimeException("the stack is empty"); + } + return minNumStack.peek(); + } +} \ No newline at end of file diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java new file mode 100644 index 0000000000..efe41a9f8f --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java @@ -0,0 +1,39 @@ +package com.coding.basic.stack; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class QuickMinStackTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + QuickMinStack stack = new QuickMinStack(); + stack.push(5); + Assert.assertEquals(5, stack.findMin()); + stack.push(6); + Assert.assertEquals(5, stack.findMin()); + stack.push(4); + Assert.assertEquals(4, stack.findMin()); + stack.push(4); + Assert.assertEquals(4, stack.findMin()); + + stack.pop(); + Assert.assertEquals(4, stack.findMin()); + stack.pop(); + Assert.assertEquals(5, stack.findMin()); + stack.pop(); + Assert.assertEquals(5, stack.findMin()); + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java new file mode 100644 index 0000000000..fedb243604 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java @@ -0,0 +1,24 @@ +package com.coding.basic.stack; + +import com.coding.basic.array.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java new file mode 100644 index 0000000000..7c86d22fe7 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java @@ -0,0 +1,168 @@ +package com.coding.basic.stack; +import java.util.Stack; +public class StackUtil { + + public static void bad_reverse(Stack s) { + if(s == null || s.isEmpty()){ + return; + } + Stack tmpStack = new Stack(); + while(!s.isEmpty()){ + tmpStack.push(s.pop()); + } + + s = tmpStack; + + } + + + + public static void reverse_247565311(Stack s){ + if(s == null || s.isEmpty()) { + return; + } + + int size = s.size(); + Stack tmpStack = new Stack(); + + for(int i=0;ii){ + tmpStack.push(s.pop()); + } + s.push(top); + while(tmpStack.size()>0){ + s.push(tmpStack.pop()); + } + } + } + + + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + if(s == null || s.isEmpty()){ + return; + } + + Stack tmp = new Stack(); + while(!s.isEmpty()){ + tmp.push(s.pop()); + } + while(!tmp.isEmpty()){ + Integer top = tmp.pop(); + addToBottom(s,top); + } + + + } + public static void addToBottom(Stack s, Integer value){ + if(s.isEmpty()){ + s.push(value); + } else{ + Integer top = s.pop(); + addToBottom(s,value); + s.push(top); + } + + } + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s,Object o) { + if(s == null || s.isEmpty()){ + return; + } + Stack tmpStack = new Stack(); + + while(!s.isEmpty()){ + Object value = s.pop(); + if(!value.equals(o)){ + tmpStack.push(value); + } + } + + while(!tmpStack.isEmpty()){ + s.push(tmpStack.pop()); + } + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) { + + if(s == null || s.isEmpty() || s.size() stack = new Stack(); + for(int i=0;i s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + + StackUtil.addToBottom(s, 0); + + Assert.assertEquals("[0, 1, 2, 3]", s.toString()); + + } + @Test + public void testReverse() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); + StackUtil.reverse(s); + Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString()); + } + @Test + public void testReverse_247565311() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + + Assert.assertEquals("[1, 2, 3]", s.toString()); + StackUtil.reverse_247565311(s); + Assert.assertEquals("[3, 2, 1]", s.toString()); + } + @Test + public void testRemove() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + StackUtil.remove(s, 2); + Assert.assertEquals("[1, 3]", s.toString()); + } + + @Test + public void testGetTop() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + { + Object[] values = StackUtil.getTop(s, 3); + Assert.assertEquals(5, values[0]); + Assert.assertEquals(4, values[1]); + Assert.assertEquals(3, values[2]); + } + } + + @Test + public void testIsValidPairs() { + Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])")); + Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})")); + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java new file mode 100644 index 0000000000..7a58fbff56 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java @@ -0,0 +1,53 @@ +package com.coding.basic.stack; + +import java.util.ArrayDeque; +import java.util.Queue; + +public class StackWithTwoQueues { + Queue queue1 = new ArrayDeque<>(); + Queue queue2 = new ArrayDeque<>(); + + public void push(int data) { + //两个栈都为空时,优先考虑queue1 + if (queue1.isEmpty()&&queue2.isEmpty()) { + queue1.add(data); + return; + } + + if (queue1.isEmpty()) { + queue2.add(data); + return; + } + + if (queue2.isEmpty()) { + queue1.add(data); + return; + } + + } + + public int pop() { + + if (queue1.isEmpty()&&queue2.isEmpty()) { + throw new RuntimeException("stack is empty"); + } + + if (queue1.isEmpty()) { + while (queue2.size()>1) { + queue1.add(queue2.poll()); + } + return queue2.poll(); + } + + if (queue2.isEmpty()) { + while (queue1.size()>1) { + queue2.add(queue1.poll()); + } + return queue1.poll(); + } + + throw new RuntimeException("no queue is empty, this is not allowed"); + + + } +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java new file mode 100644 index 0000000000..4541b1f040 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java @@ -0,0 +1,36 @@ +package com.coding.basic.stack; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class StackWithTwoQueuesTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + StackWithTwoQueues stack = new StackWithTwoQueues(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + Assert.assertEquals(4, stack.pop()); + Assert.assertEquals(3, stack.pop()); + + stack.push(5); + Assert.assertEquals(5, stack.pop()); + Assert.assertEquals(2, stack.pop()); + Assert.assertEquals(1, stack.pop()); + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java new file mode 100644 index 0000000000..7f30ce55c8 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java @@ -0,0 +1,5 @@ +package com.coding.basic.stack; + +public class Tail { + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java new file mode 100644 index 0000000000..a532fd6e6c --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java @@ -0,0 +1,117 @@ +package com.coding.basic.stack; + +import java.util.Arrays; + +/** + * 用一个数组实现两个栈 + * 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。 + * @author liuxin + * + */ +public class TwoStackInOneArray { + private Object[] data = new Object[10]; + private int size; + private int top1, top2; + + public TwoStackInOneArray(int n){ + data = new Object[n]; + size = n; + top1 = -1; + top2 = data.length; + } + /** + * 向第一个栈中压入元素 + * @param o + */ + public void push1(Object o){ + ensureCapacity(); + data[++top1] = o; + } + /* + * 向第二个栈压入元素 + */ + public void push2(Object o){ + ensureCapacity(); + data[--top2] = o; + } + public void ensureCapacity(){ + if(top2-top1>1){ + return; + } else{ + + Object[] newArray = new Object[data.length*2]; + System.arraycopy(data, 0, newArray, 0, top1+1); + + int stack2Size = data.length-top2; + int newTop2 = newArray.length-stack2Size; + System.arraycopy(data, top2, newArray, newTop2, stack2Size); + + top2 = newTop2; + data = newArray; + } + } + /** + * 从第一个栈中弹出元素 + * @return + */ + public Object pop1(){ + if(top1 == -1){ + throw new RuntimeException("Stack1 is empty"); + } + Object o = data[top1]; + data[top1] = null; + top1--; + return o; + + } + /** + * 从第二个栈弹出元素 + * @return + */ + public Object pop2(){ + if(top2 == data.length){ + throw new RuntimeException("Stack2 is empty"); + } + Object o = data[top2]; + data[top2] = null; + top2++; + return o; + } + /** + * 获取第一个栈的栈顶元素 + * @return + */ + + public Object peek1(){ + if(top1 == -1){ + throw new RuntimeException("Stack1 is empty"); + } + return data[top1]; + } + + + /** + * 获取第二个栈的栈顶元素 + * @return + */ + + public Object peek2(){ + if(top2 == data.length){ + throw new RuntimeException("Stack2 is empty"); + } + return data[top2]; + } + + public Object[] stack1ToArray(){ + return Arrays.copyOf(data, top1+1); + } + public Object[] stack2ToArray(){ + int size = data.length-top2; + Object [] stack2Data = new Object[size]; + int j=0; + for(int i=data.length-1; i>=top2 ;i--){ + stack2Data[j++] = data[i]; + } + return stack2Data; + } +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java new file mode 100644 index 0000000000..b743d422c6 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java @@ -0,0 +1,65 @@ +package com.coding.basic.stack; + +import java.util.Arrays; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class TwoStackInOneArrayTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test1() { + TwoStackInOneArray stack = new TwoStackInOneArray(10); + stack.push1(1); + stack.push1(2); + stack.push1(3); + stack.push1(4); + stack.push1(5); + + stack.push2(1); + stack.push2(2); + stack.push2(3); + stack.push2(4); + stack.push2(5); + + for(int i=1;i<=5;i++){ + Assert.assertEquals(stack.peek1(), stack.peek2()); + Assert.assertEquals(stack.pop1(), stack.pop2()); + } + + + } + @Test + public void test2() { + TwoStackInOneArray stack = new TwoStackInOneArray(5); + stack.push1(1); + stack.push1(2); + stack.push1(3); + stack.push1(4); + stack.push1(5); + stack.push1(6); + stack.push1(7); + + stack.push2(1); + stack.push2(2); + stack.push2(3); + stack.push2(4); + + + Assert.assertEquals("[1, 2, 3, 4, 5, 6, 7]",Arrays.toString(stack.stack1ToArray())); + Assert.assertEquals("[1, 2, 3, 4]",Arrays.toString(stack.stack2ToArray())); + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java new file mode 100644 index 0000000000..cebef21fa3 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java @@ -0,0 +1,72 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + + +public class InfixExpr { + String expr = null; + + public InfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + + + TokenParser parser = new TokenParser(); + List tokens = parser.parse(this.expr); + + + Stack opStack = new Stack<>(); + Stack numStack = new Stack<>(); + + for(Token token : tokens){ + + if (token.isOperator()){ + + while(!opStack.isEmpty() + && !token.hasHigherPriority(opStack.peek())){ + Token prevOperator = opStack.pop(); + Float f2 = numStack.pop(); + Float f1 = numStack.pop(); + Float result = calculate(prevOperator.toString(), f1,f2); + numStack.push(result); + + } + opStack.push(token); + } + if(token.isNumber()){ + numStack.push(new Float(token.getIntValue())); + } + } + + while(!opStack.isEmpty()){ + Token token = opStack.pop(); + Float f2 = numStack.pop(); + Float f1 = numStack.pop(); + numStack.push(calculate(token.toString(), f1,f2)); + } + + + return numStack.pop().floatValue(); + } + private Float calculate(String op, Float f1, Float f2){ + if(op.equals("+")){ + return f1+f2; + } + if(op.equals("-")){ + return f1-f2; + } + if(op.equals("*")){ + return f1*f2; + } + if(op.equals("/")){ + return f1/f2; + } + throw new RuntimeException(op + " is not supported"); + } + + + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java new file mode 100644 index 0000000000..20e34e8852 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java @@ -0,0 +1,52 @@ +package com.coding.basic.stack.expr; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class InfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); + { + InfixExpr expr = new InfixExpr("2+3*4+5"); + Assert.assertEquals(19.0, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); + Assert.assertEquals(100.0, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("3*20/2"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("20/2*3"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("10-30+50"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("10-2*3+50"); + Assert.assertEquals(54, expr.evaluate(), 0.001f); + } + + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java new file mode 100644 index 0000000000..9e501eda20 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java @@ -0,0 +1,43 @@ +package com.coding.basic.stack.expr; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class InfixToPostfix { + + public static List convert(String expr) { + List inFixTokens = new TokenParser().parse(expr); + + List postFixTokens = new ArrayList<>(); + + Stack opStack = new Stack(); + for(Token token : inFixTokens){ + + if(token.isOperator()){ + + while(!opStack.isEmpty() + && !token.hasHigherPriority(opStack.peek())){ + postFixTokens.add(opStack.pop()); + + } + opStack.push(token); + + } + if(token.isNumber()){ + + postFixTokens.add(token); + + } + } + + while(!opStack.isEmpty()){ + postFixTokens.add(opStack.pop()); + } + + return postFixTokens; + } + + + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java new file mode 100644 index 0000000000..f879f55f14 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java @@ -0,0 +1,41 @@ +package com.coding.basic.stack.expr; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class InfixToPostfixTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testConvert() { + { + List tokens = InfixToPostfix.convert("2+3"); + Assert.assertEquals("[2, 3, +]", tokens.toString()); + } + { + + List tokens = InfixToPostfix.convert("2+3*4"); + Assert.assertEquals("[2, 3, 4, *, +]", tokens.toString()); + } + + { + + List tokens = InfixToPostfix.convert("2-3*4+5"); + Assert.assertEquals("[2, 3, 4, *, -, 5, +]", tokens.toString()); + } + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java new file mode 100644 index 0000000000..c54eb69e2a --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java @@ -0,0 +1,46 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + +public class PostfixExpr { +String expr = null; + + public PostfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + TokenParser parser = new TokenParser(); + List tokens = parser.parse(this.expr); + + + Stack numStack = new Stack<>(); + for(Token token : tokens){ + if(token.isNumber()){ + numStack.push(new Float(token.getIntValue())); + } else{ + Float f2 = numStack.pop(); + Float f1 = numStack.pop(); + numStack.push(calculate(token.toString(),f1,f2)); + } + } + return numStack.pop().floatValue(); + } + + private Float calculate(String op, Float f1, Float f2){ + if(op.equals("+")){ + return f1+f2; + } + if(op.equals("-")){ + return f1-f2; + } + if(op.equals("*")){ + return f1*f2; + } + if(op.equals("/")){ + return f1/f2; + } + throw new RuntimeException(op + " is not supported"); + } +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java new file mode 100644 index 0000000000..c0435a2db5 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java @@ -0,0 +1,41 @@ +package com.coding.basic.stack.expr; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class PostfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + { + PostfixExpr expr = new PostfixExpr("6 5 2 3 + 8 * + 3 + *"); + Assert.assertEquals(288, expr.evaluate(),0.0f); + } + { + //9+(3-1)*3+10/2 + PostfixExpr expr = new PostfixExpr("9 3 1-3*+ 10 2/+"); + Assert.assertEquals(20, expr.evaluate(),0.0f); + } + + { + //10-2*3+50 + PostfixExpr expr = new PostfixExpr("10 2 3 * - 50 +"); + Assert.assertEquals(54, expr.evaluate(),0.0f); + } + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java new file mode 100644 index 0000000000..f811fd6d9a --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java @@ -0,0 +1,52 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + +public class PrefixExpr { + String expr = null; + + public PrefixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + TokenParser parser = new TokenParser(); + List tokens = parser.parse(this.expr); + + Stack exprStack = new Stack<>(); + Stack numStack = new Stack<>(); + for(Token token : tokens){ + exprStack.push(token); + } + + while(!exprStack.isEmpty()){ + Token t = exprStack.pop(); + if(t.isNumber()){ + numStack.push(new Float(t.getIntValue())); + }else{ + Float f1 = numStack.pop(); + Float f2 = numStack.pop(); + numStack.push(calculate(t.toString(),f1,f2)); + + } + } + return numStack.pop().floatValue(); + } + + private Float calculate(String op, Float f1, Float f2){ + if(op.equals("+")){ + return f1+f2; + } + if(op.equals("-")){ + return f1-f2; + } + if(op.equals("*")){ + return f1*f2; + } + if(op.equals("/")){ + return f1/f2; + } + throw new RuntimeException(op + " is not supported"); + } +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java new file mode 100644 index 0000000000..5cec210e75 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java @@ -0,0 +1,45 @@ +package com.coding.basic.stack.expr; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class PrefixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + { + // 2*3+4*5 + PrefixExpr expr = new PrefixExpr("+ * 2 3* 4 5"); + Assert.assertEquals(26, expr.evaluate(),0.001f); + } + { + // 4*2 + 6+9*2/3 -8 + PrefixExpr expr = new PrefixExpr("-++6/*2 9 3 * 4 2 8"); + Assert.assertEquals(12, expr.evaluate(),0.001f); + } + { + //(3+4)*5-6 + PrefixExpr expr = new PrefixExpr("- * + 3 4 5 6"); + Assert.assertEquals(29, expr.evaluate(),0.001f); + } + { + //1+((2+3)*4)-5 + PrefixExpr expr = new PrefixExpr("- + 1 * + 2 3 4 5"); + Assert.assertEquals(16, expr.evaluate(),0.001f); + } + + + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java new file mode 100644 index 0000000000..8579743fe9 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java @@ -0,0 +1,50 @@ +package com.coding.basic.stack.expr; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +class Token { + public static final List OPERATORS = Arrays.asList("+", "-", "*", "/"); + private static final Map priorities = new HashMap<>(); + static { + priorities.put("+", 1); + priorities.put("-", 1); + priorities.put("*", 2); + priorities.put("/", 2); + } + static final int OPERATOR = 1; + static final int NUMBER = 2; + String value; + int type; + public Token(int type, String value){ + this.type = type; + this.value = value; + } + + public boolean isNumber() { + return type == NUMBER; + } + + public boolean isOperator() { + return type == OPERATOR; + } + + public int getIntValue() { + return Integer.valueOf(value).intValue(); + } + public String toString(){ + return value; + } + + public boolean hasHigherPriority(Token t){ + if(!this.isOperator() && !t.isOperator()){ + throw new RuntimeException("numbers can't compare priority"); + } + return priorities.get(this.value) - priorities.get(t.value) > 0; + } + + + +} \ No newline at end of file diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java new file mode 100644 index 0000000000..d3b0f167e1 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java @@ -0,0 +1,57 @@ +package com.coding.basic.stack.expr; + +import java.util.ArrayList; +import java.util.List; + +public class TokenParser { + + + public List parse(String expr) { + List tokens = new ArrayList<>(); + + int i = 0; + + while (i < expr.length()) { + + char c = expr.charAt(i); + + if (isOperator(c)) { + + Token t = new Token(Token.OPERATOR, String.valueOf(c)); + tokens.add(t); + i++; + + } else if (Character.isDigit(c)) { + + int nextOperatorIndex = indexOfNextOperator(i, expr); + String value = expr.substring(i, nextOperatorIndex); + Token t = new Token(Token.NUMBER, value); + tokens.add(t); + i = nextOperatorIndex; + + } else{ + System.out.println("char :["+c+"] is not number or operator,ignore"); + i++; + } + + } + return tokens; + } + + private int indexOfNextOperator(int i, String expr) { + + while (Character.isDigit(expr.charAt(i))) { + i++; + if (i == expr.length()) { + break; + } + } + return i; + + } + + private boolean isOperator(char c) { + String sc = String.valueOf(c); + return Token.OPERATORS.contains(sc); + } +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java new file mode 100644 index 0000000000..399d3e857e --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java @@ -0,0 +1,41 @@ +package com.coding.basic.stack.expr; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TokenParserTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + + TokenParser parser = new TokenParser(); + List tokens = parser.parse("300*20+12*5-20/4"); + + Assert.assertEquals(300, tokens.get(0).getIntValue()); + Assert.assertEquals("*", tokens.get(1).toString()); + Assert.assertEquals(20, tokens.get(2).getIntValue()); + Assert.assertEquals("+", tokens.get(3).toString()); + Assert.assertEquals(12, tokens.get(4).getIntValue()); + Assert.assertEquals("*", tokens.get(5).toString()); + Assert.assertEquals(5, tokens.get(6).getIntValue()); + Assert.assertEquals("-", tokens.get(7).toString()); + Assert.assertEquals(20, tokens.get(8).getIntValue()); + Assert.assertEquals("/", tokens.get(9).toString()); + Assert.assertEquals(4, tokens.get(10).getIntValue()); + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java new file mode 100644 index 0000000000..284e5b0011 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java @@ -0,0 +1,189 @@ +package com.coding.basic.tree; + +import java.util.ArrayList; +import java.util.List; + +import com.coding.basic.queue.Queue; + + +public class BinarySearchTree { + + BinaryTreeNode root; + public BinarySearchTree(BinaryTreeNode root){ + this.root = root; + } + public BinaryTreeNode getRoot(){ + return root; + } + public T findMin(){ + if(root == null){ + return null; + } + return findMin(root).data; + } + public T findMax(){ + if(root == null){ + return null; + } + return findMax(root).data; + } + public int height() { + return height(root); + } + public int size() { + return size(root); + } + public void remove(T e){ + remove(e, root); + } + + private BinaryTreeNode remove(T x, BinaryTreeNode t){ + if(t == null){ + return t; + } + int compareResult = x.compareTo(t.data); + + if(compareResult< 0 ){ + t.left = remove(x,t.left); + + } else if(compareResult > 0){ + t.right = remove(x, t.right); + + } else { + if(t.left != null && t.right != null){ + + t.data = findMin(t.right).data; + t.right = remove(t.data,t.right); + + } else{ + t = (t.left != null) ? t.left : t.right; + } + } + return t; + } + + private BinaryTreeNode findMin(BinaryTreeNode p){ + if (p==null){ + return null; + } else if (p.left == null){ + return p; + } else{ + return findMin(p.left); + } + } + private BinaryTreeNode findMax(BinaryTreeNode p){ + if (p==null){ + return null; + }else if (p.right==null){ + return p; + } else{ + return findMax(p.right); + } + } + private int height(BinaryTreeNode t){ + if (t==null){ + return 0; + }else { + int leftChildHeight=height(t.left); + int rightChildHeight=height(t.right); + if(leftChildHeight > rightChildHeight){ + return leftChildHeight+1; + } else{ + return rightChildHeight+1; + } + } + } + private int size(BinaryTreeNode t){ + if (t == null){ + return 0; + } + return size(t.left) + 1 + size(t.right); + + } + + public List levelVisit(){ + List result = new ArrayList(); + if(root == null){ + return result; + } + Queue> queue = new Queue>(); + BinaryTreeNode node = root; + queue.enQueue(node); + while (!queue.isEmpty()) { + node = queue.deQueue(); + result.add(node.data); + if (node.left != null){ + queue.enQueue(node.left); + } + if (node.right != null){ + queue.enQueue(node.right); + } + } + return result; + } + public boolean isValid(){ + return isValid(root); + } + public T getLowestCommonAncestor(T n1, T n2){ + if (root == null){ + return null; + } + return lowestCommonAncestor(root,n1,n2); + + } + public List getNodesBetween(T n1, T n2){ + List elements = new ArrayList<>(); + getNodesBetween(elements,root,n1,n2); + return elements; + } + + public void getNodesBetween(List elements ,BinaryTreeNode node, T n1, T n2){ + + if (node == null) { + return; + } + + if (n1.compareTo(node.data) < 0) { + getNodesBetween(elements,node.left, n1, n2); + } + + if ((n1.compareTo(node.data) <= 0 ) + && (n2.compareTo(node.data) >= 0 )) { + elements.add(node.data); + } + if (n2.compareTo(node.data)>0) { + getNodesBetween(elements,node.right, n1, n2); + } + } + private T lowestCommonAncestor(BinaryTreeNode node,T n1, T n2){ + if(node == null){ + return null; + } + // 如果n1和n2都比 node的值小, LCA在左孩子 + if (node.data.compareTo(n1) > 0 && node.data.compareTo(n2) >0){ + return lowestCommonAncestor(node.left, n1, n2); + } + + // 如果n1和n2都比 node的值小, LCA在右孩子 + if (node.data.compareTo(n1) < 0 && node.data.compareTo(n2) <0) + return lowestCommonAncestor(node.right, n1, n2); + + return node.data; + } + private boolean isValid(BinaryTreeNode t){ + if(t == null){ + return true; + } + if(t.left != null && findMax(t.left).data.compareTo(t.data) >0){ + return false; + } + if(t.right !=null && findMin(t.right).data.compareTo(t.data) <0){ + return false; + } + if(!isValid(t.left) || !isValid(t.right)){ + return false; + } + return true; + } +} + diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java new file mode 100644 index 0000000000..590e60306c --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java @@ -0,0 +1,108 @@ +package com.coding.basic.tree; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class BinarySearchTreeTest { + + BinarySearchTree tree = null; + + @Before + public void setUp() throws Exception { + BinaryTreeNode root = new BinaryTreeNode(6); + root.left = new BinaryTreeNode(2); + root.right = new BinaryTreeNode(8); + root.left.left = new BinaryTreeNode(1); + root.left.right = new BinaryTreeNode(4); + root.left.right.left = new BinaryTreeNode(3); + root.left.right.right = new BinaryTreeNode(5); + tree = new BinarySearchTree(root); + } + + @After + public void tearDown() throws Exception { + tree = null; + } + + @Test + public void testFindMin() { + Assert.assertEquals(1, tree.findMin().intValue()); + + } + + @Test + public void testFindMax() { + Assert.assertEquals(8, tree.findMax().intValue()); + } + + @Test + public void testHeight() { + Assert.assertEquals(4, tree.height()); + } + + @Test + public void testSize() { + Assert.assertEquals(7, tree.size()); + } + + @Test + public void testRemoveLeaf() { + tree.remove(3); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(4, root.left.right.data.intValue()); + + } + @Test + public void testRemoveMiddleNode1() { + tree.remove(4); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(5, root.left.right.data.intValue()); + Assert.assertEquals(3, root.left.right.left.data.intValue()); + } + @Test + public void testRemoveMiddleNode2() { + tree.remove(2); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(3, root.left.data.intValue()); + Assert.assertEquals(4, root.left.right.data.intValue()); + } + + @Test + public void testLevelVisit() { + List values = tree.levelVisit(); + Assert.assertEquals("[6, 2, 8, 1, 4, 3, 5]", values.toString()); + + } + @Test + public void testLCA(){ + Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 5).intValue()); + Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 4).intValue()); + Assert.assertEquals(6,tree.getLowestCommonAncestor(3, 8).intValue()); + } + @Test + public void testIsValid() { + + Assert.assertTrue(tree.isValid()); + + BinaryTreeNode root = new BinaryTreeNode(6); + root.left = new BinaryTreeNode(2); + root.right = new BinaryTreeNode(8); + root.left.left = new BinaryTreeNode(4); + root.left.right = new BinaryTreeNode(1); + root.left.right.left = new BinaryTreeNode(3); + tree = new BinarySearchTree(root); + + Assert.assertFalse(tree.isValid()); + } + @Test + public void testGetNodesBetween(){ + List numbers = this.tree.getNodesBetween(3, 8); + Assert.assertEquals("[3, 4, 5, 6, 8]",numbers.toString()); + } +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java new file mode 100644 index 0000000000..3f6f4d2b44 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java @@ -0,0 +1,36 @@ +package com.coding.basic.tree; + +public class BinaryTreeNode { + + public T data; + public BinaryTreeNode left; + public BinaryTreeNode right; + + public BinaryTreeNode(T data){ + this.data=data; + } + public T getData() { + return data; + } + public void setData(T data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java new file mode 100644 index 0000000000..f2a6515fa6 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java @@ -0,0 +1,116 @@ +package com.coding.basic.tree; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class BinaryTreeUtil { + /** + * 用递归的方式实现对二叉树的前序遍历, 需要通过BinaryTreeUtilTest测试 + * + * @param root + * @return + */ + public static List preOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + preOrderVisit(root, result); + return result; + } + + /** + * 用递归的方式实现对二叉树的中遍历 + * + * @param root + * @return + */ + public static List inOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + inOrderVisit(root, result); + return result; + } + + /** + * 用递归的方式实现对二叉树的后遍历 + * + * @param root + * @return + */ + public static List postOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + postOrderVisit(root, result); + return result; + } + + public static List preOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + Stack> stack = new Stack>(); + + BinaryTreeNode node = root; + + if(node != null){ + stack.push(node); + } + + while(!stack.isEmpty()){ + node = stack.pop(); + result.add(node.data); + + if(node.right != null){ + stack.push(node.right); + } + + if(node.left != null){ + stack.push(node.right); + } + } + return result; + } + + public static List inOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + BinaryTreeNode node = root; + Stack> stack = new Stack>(); + + while (node != null || !stack.isEmpty()) { + + while (node != null) { + stack.push(node); + node = node.left; + } + BinaryTreeNode currentNode = stack.pop(); + result.add(currentNode.data); + node = currentNode.right; + } + return result; + } + + private static void preOrderVisit(BinaryTreeNode node, List result) { + if (node == null) { + return; + } + result.add(node.getData()); + preOrderVisit(node.getLeft(), result); + preOrderVisit(node.getRight(), result); + } + + private static void inOrderVisit(BinaryTreeNode node, List result) { + if (node == null) { + return; + } + inOrderVisit(node.getLeft(), result); + result.add(node.getData()); + inOrderVisit(node.getRight(), result); + } + + private static void postOrderVisit(BinaryTreeNode node, List result) { + if (node == null) { + return; + } + postOrderVisit(node.getLeft(), result); + postOrderVisit(node.getRight(), result); + result.add(node.getData()); + } + +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java new file mode 100644 index 0000000000..41857e137d --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java @@ -0,0 +1,75 @@ +package com.coding.basic.tree; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class BinaryTreeUtilTest { + + BinaryTreeNode root = null; + @Before + public void setUp() throws Exception { + root = new BinaryTreeNode(1); + root.setLeft(new BinaryTreeNode(2)); + root.setRight(new BinaryTreeNode(5)); + root.getLeft().setLeft(new BinaryTreeNode(3)); + root.getLeft().setRight(new BinaryTreeNode(4)); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testPreOrderVisit() { + + List result = BinaryTreeUtil.preOrderVisit(root); + Assert.assertEquals("[1, 2, 3, 4, 5]", result.toString()); + + + } + @Test + public void testInOrderVisit() { + + + List result = BinaryTreeUtil.inOrderVisit(root); + Assert.assertEquals("[3, 2, 4, 1, 5]", result.toString()); + + } + + @Test + public void testPostOrderVisit() { + + + List result = BinaryTreeUtil.postOrderVisit(root); + Assert.assertEquals("[3, 4, 2, 5, 1]", result.toString()); + + } + + + @Test + public void testInOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.inOrderWithoutRecursion(root); + Assert.assertEquals("[3, 2, 6, 4, 7, 1, 5]", result.toString()); + + } + @Test + public void testPreOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.preOrderWithoutRecursion(root); + Assert.assertEquals("[1, 2, 3, 4, 6, 7, 5]", result.toString()); + + } +} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java new file mode 100644 index 0000000000..85fb8ab2a4 --- /dev/null +++ b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java @@ -0,0 +1,34 @@ +package com.coding.basic.tree; + +import java.io.File; + +public class FileList { + public void list(File f) { + list(f, 0); + } + + public void list(File f, int depth) { + printName(f, depth); + if (f.isDirectory()) { + File[] files = f.listFiles(); + for (File i : files) + list(i, depth + 1); + } + } + + void printName(File f, int depth) { + String name = f.getName(); + for (int i = 0; i < depth; i++) + System.out.print("+"); + if (f.isDirectory()) + System.out.println("Dir: " + name); + else + System.out.println(f.getName() + " " + f.length()); + } + + public static void main(String args[]) { + FileList L = new FileList(); + File f = new File("C:\\coderising\\tmp"); + L.list(f); + } +} diff --git a/students/34594980/data-structure/assignment/pom.xml b/students/34594980/data-structure/assignment/pom.xml new file mode 100644 index 0000000000..5024466d17 --- /dev/null +++ b/students/34594980/data-structure/assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ds-assignment + 0.0.1-SNAPSHOT + jar + + ds-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/DownloadThread.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..900a3ad358 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/DownloadThread.java @@ -0,0 +1,20 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/FileDownloader.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..c3c8a3f27d --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/FileDownloader.java @@ -0,0 +1,73 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn,0,length-1).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/FileDownloaderTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..4ff7f46ae0 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/Connection.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionException.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionManager.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/DownloadListener.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionImpl.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..36a9d2ce15 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,27 @@ +package com.coderising.download.impl; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + @Override + public int getContentLength() { + + return 0; + } + + @Override + public void close() { + + + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..172371dd55 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return null; + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/LoginAction.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/Struts.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/StrutsTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/View.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/struts.xml b/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Course.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Course.java new file mode 100644 index 0000000000..436d092f58 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Course.java @@ -0,0 +1,24 @@ +package com.coderising.ood.course.bad; + +import java.util.List; + +public class Course { + private String id; + private String desc; + private int duration ; + + List prerequisites; + + public List getPrerequisites() { + return prerequisites; + } + + + public boolean equals(Object o){ + if(o == null || !(o instanceof Course)){ + return false; + } + Course c = (Course)o; + return (c != null) && c.id.equals(id); + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseOffering.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseOffering.java new file mode 100644 index 0000000000..ab8c764584 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseOffering.java @@ -0,0 +1,26 @@ +package com.coderising.ood.course.bad; + +import java.util.ArrayList; +import java.util.List; + + +public class CourseOffering { + private Course course; + private String location; + private String teacher; + private int maxStudents; + + List students = new ArrayList(); + + public int getMaxStudents() { + return maxStudents; + } + + public List getStudents() { + return students; + } + + public Course getCourse() { + return course; + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseService.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseService.java new file mode 100644 index 0000000000..8c34bad0c3 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseService.java @@ -0,0 +1,16 @@ +package com.coderising.ood.course.bad; + + + +public class CourseService { + + public void chooseCourse(Student student, CourseOffering sc){ + //如果学生上过该科目的先修科目,并且该课程还未满, 则学生可以加入该课程 + if(student.getCoursesAlreadyTaken().containsAll( + sc.getCourse().getPrerequisites()) + && sc.getMaxStudents() > sc.getStudents().size()){ + sc.getStudents().add(student); + } + + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Student.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Student.java new file mode 100644 index 0000000000..a651923ef5 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Student.java @@ -0,0 +1,14 @@ +package com.coderising.ood.course.bad; + +import java.util.ArrayList; +import java.util.List; + +public class Student { + private String id; + private String name; + private List coursesAlreadyTaken = new ArrayList(); + + public List getCoursesAlreadyTaken() { + return coursesAlreadyTaken; + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Course.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Course.java new file mode 100644 index 0000000000..aefc9692bb --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Course.java @@ -0,0 +1,18 @@ +package com.coderising.ood.course.good; + +import java.util.List; + +public class Course { + private String id; + private String desc; + private int duration ; + + List prerequisites; + + public List getPrerequisites() { + return prerequisites; + } + +} + + diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseOffering.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseOffering.java new file mode 100644 index 0000000000..8660ec8109 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseOffering.java @@ -0,0 +1,34 @@ +package com.coderising.ood.course.good; + +import java.util.ArrayList; +import java.util.List; + +public class CourseOffering { + private Course course; + private String location; + private String teacher; + private int maxStudents; + + List students = new ArrayList(); + + public List getStudents() { + return students; + } + public int getMaxStudents() { + return maxStudents; + } + public Course getCourse() { + return course; + } + + + // 第二步: 把主要逻辑移动到CourseOffering 中 + public void addStudent(Student student){ + + if(student.canAttend(course) + && this.maxStudents > students.size()){ + students.add(student); + } + } + // 第三步: 重构CourseService +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseService.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseService.java new file mode 100644 index 0000000000..22ba4a5450 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseService.java @@ -0,0 +1,14 @@ +package com.coderising.ood.course.good; + + + +public class CourseService { + + public void chooseCourse(Student student, CourseOffering sc){ + //第一步:重构: canAttend , 但是还有问题 + if(student.canAttend(sc.getCourse()) + && sc.getMaxStudents() > sc.getStudents().size()){ + sc.getStudents().add(student); + } + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Student.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Student.java new file mode 100644 index 0000000000..2c7e128b2a --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Student.java @@ -0,0 +1,21 @@ +package com.coderising.ood.course.good; + +import java.util.ArrayList; +import java.util.List; + +public class Student { + private String id; + private String name; + private List coursesAlreadyTaken = new ArrayList(); + + public List getCoursesAlreadyTaken() { + return coursesAlreadyTaken; + } + + public boolean canAttend(Course course){ + return this.coursesAlreadyTaken.containsAll( + course.getPrerequisites()); + } +} + + diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java new file mode 100644 index 0000000000..b6cf28c096 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class DateUtil { + + public static String getCurrentDateAsString() { + + return null; + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/Logger.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/Logger.java new file mode 100644 index 0000000000..0357c4d912 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/Logger.java @@ -0,0 +1,38 @@ +package com.coderising.ood.ocp; + +public class Logger { + + public final int RAW_LOG = 1; + public final int RAW_LOG_WITH_DATE = 2; + public final int EMAIL_LOG = 1; + public final int SMS_LOG = 2; + public final int PRINT_LOG = 3; + + int type = 0; + int method = 0; + + public Logger(int logType, int logMethod){ + this.type = logType; + this.method = logMethod; + } + public void log(String msg){ + + String logMsg = msg; + + if(this.type == RAW_LOG){ + logMsg = msg; + } else if(this.type == RAW_LOG_WITH_DATE){ + String txtDate = DateUtil.getCurrentDateAsString(); + logMsg = txtDate + ": " + msg; + } + + if(this.method == EMAIL_LOG){ + MailUtil.send(logMsg); + } else if(this.method == SMS_LOG){ + SMSUtil.send(logMsg); + } else if(this.method == PRINT_LOG){ + System.out.println(logMsg); + } + } +} + diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java new file mode 100644 index 0000000000..ec54b839c5 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class MailUtil { + + public static void send(String logMsg) { + // TODO Auto-generated method stub + + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java new file mode 100644 index 0000000000..13cf802418 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class SMSUtil { + + public static void send(String logMsg) { + // TODO Auto-generated method stub + + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..781587a846 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..a98917f829 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo R15 +P4955 Vivo X20 \ No newline at end of file diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/Iterator.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/List.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayList.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayList.java new file mode 100644 index 0000000000..4576c016af --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayList.java @@ -0,0 +1,35 @@ +package com.coding.basic.array; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayUtil.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayUtil.java new file mode 100644 index 0000000000..45740e6d57 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coding.basic.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrame.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..994a241a3d --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,57 @@ +package com.coding.basic.linklist; + + +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + private int currentSize; + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + this.currentSize = 0; + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + + + } + + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + + + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..7fd72fc2b4 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,34 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + frame.access(5); + Assert.assertEquals("5,4,0", frame.toString()); + + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LinkedList.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LinkedList.java new file mode 100644 index 0000000000..f4c7556a2e --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LinkedList.java @@ -0,0 +1,125 @@ +package com.coding.basic.linklist; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/CircleQueue.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/CircleQueue.java new file mode 100644 index 0000000000..2e0550c67e --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/CircleQueue.java @@ -0,0 +1,39 @@ +package com.coding.basic.queue; + +/** + * 用数组实现循环队列 + * @author liuxin + * + * @param + */ +public class CircleQueue { + + private final static int DEFAULT_SIZE = 10; + + //用数组来保存循环队列的元素 + private Object[] elementData = new Object[DEFAULT_SIZE] ; + + //队头 + private int front = 0; + //队尾 + private int rear = 0; + + public boolean isEmpty() { + return false; + + } + + public int size() { + return -1; + } + + + + public void enQueue(E data) { + + } + + public E deQueue() { + return null; + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/Josephus.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/Josephus.java new file mode 100644 index 0000000000..6a3ea639b9 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/Josephus.java @@ -0,0 +1,18 @@ +package com.coding.basic.queue; + +import java.util.List; + +/** + * 用Queue来实现Josephus问题 + * 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来 + * 该方法返回一个List, 包含了被杀死人的次序 + * @author liuxin + * + */ +public class Josephus { + + public static List execute(int n, int m){ + return null; + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/JosephusTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/JosephusTest.java new file mode 100644 index 0000000000..7d90318b51 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/JosephusTest.java @@ -0,0 +1,27 @@ +package com.coding.basic.queue; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class JosephusTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testExecute() { + + Assert.assertEquals("[1, 3, 5, 0, 4, 2, 6]", Josephus.execute(7, 2).toString()); + + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/Queue.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/Queue.java new file mode 100644 index 0000000000..c4c4b7325e --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/Queue.java @@ -0,0 +1,61 @@ +package com.coding.basic.queue; + +import java.util.NoSuchElementException; + +public class Queue { + private Node first; + private Node last; + private int size; + + + private static class Node { + private E item; + private Node next; + } + + + public Queue() { + first = null; + last = null; + size = 0; + } + + + public boolean isEmpty() { + return first == null; + } + + public int size() { + return size; + } + + + + public void enQueue(E data) { + Node oldlast = last; + last = new Node(); + last.item = data; + last.next = null; + if (isEmpty()) { + first = last; + } + else{ + oldlast.next = last; + } + size++; + } + + public E deQueue() { + if (isEmpty()) { + throw new NoSuchElementException("Queue underflow"); + } + E item = first.item; + first = first.next; + size--; + if (isEmpty()) { + last = null; + } + return item; + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java new file mode 100644 index 0000000000..cef19a8b59 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java @@ -0,0 +1,47 @@ +package com.coding.basic.queue; + +import java.util.Stack; + +/** + * 用两个栈来实现一个队列 + * @author liuxin + * + * @param + */ +public class QueueWithTwoStacks { + private Stack stack1; + private Stack stack2; + + + public QueueWithTwoStacks() { + stack1 = new Stack(); + stack2 = new Stack(); + } + + + + + public boolean isEmpty() { + return false; + } + + + + public int size() { + return -1; + } + + + + public void enQueue(E item) { + + } + + public E deQueue() { + return null; + } + + + + } + diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/QuickMinStack.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/QuickMinStack.java new file mode 100644 index 0000000000..f391d92b8f --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/QuickMinStack.java @@ -0,0 +1,19 @@ +package com.coding.basic.stack; + +/** + * 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素 + * finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值 + * @author liuxin + * + */ +public class QuickMinStack { + public void push(int data){ + + } + public int pop(){ + return -1; + } + public int findMin(){ + return -1; + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/Stack.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/Stack.java new file mode 100644 index 0000000000..fedb243604 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/Stack.java @@ -0,0 +1,24 @@ +package com.coding.basic.stack; + +import com.coding.basic.array.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtil.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtil.java new file mode 100644 index 0000000000..b0ec38161d --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtil.java @@ -0,0 +1,48 @@ +package com.coding.basic.stack; +import java.util.Stack; +public class StackUtil { + + + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + + + + } + + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s,Object o) { + + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) { + return null; + } + /** + * 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz + * 使用堆栈检查字符串s中的括号是不是成对出现的。 + * 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true + * 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; + * @param s + * @return + */ + public static boolean isValidPairs(String s){ + return false; + } + + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtilTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtilTest.java new file mode 100644 index 0000000000..76f2cb7668 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtilTest.java @@ -0,0 +1,65 @@ +package com.coding.basic.stack; + +import java.util.Stack; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +public class StackUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + + @Test + public void testReverse() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); + StackUtil.reverse(s); + Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString()); + } + + @Test + public void testRemove() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + StackUtil.remove(s, 2); + Assert.assertEquals("[1, 3]", s.toString()); + } + + @Test + public void testGetTop() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + { + Object[] values = StackUtil.getTop(s, 3); + Assert.assertEquals(5, values[0]); + Assert.assertEquals(4, values[1]); + Assert.assertEquals(3, values[2]); + } + } + + @Test + public void testIsValidPairs() { + Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])")); + Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})")); + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java new file mode 100644 index 0000000000..d0ab4387d2 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java @@ -0,0 +1,16 @@ +package com.coding.basic.stack; + + +public class StackWithTwoQueues { + + + public void push(int data) { + + } + + public int pop() { + return -1; + } + + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java new file mode 100644 index 0000000000..e86d056a24 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java @@ -0,0 +1,57 @@ +package com.coding.basic.stack; + +/** + * 用一个数组实现两个栈 + * 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。 + * @author liuxin + * + */ +public class TwoStackInOneArray { + Object[] data = new Object[10]; + + /** + * 向第一个栈中压入元素 + * @param o + */ + public void push1(Object o){ + + } + /** + * 从第一个栈中弹出元素 + * @return + */ + public Object pop1(){ + return null; + } + + /** + * 获取第一个栈的栈顶元素 + * @return + */ + + public Object peek1(){ + return null; + } + /* + * 向第二个栈压入元素 + */ + public void push2(Object o){ + + } + /** + * 从第二个栈弹出元素 + * @return + */ + public Object pop2(){ + return null; + } + /** + * 获取第二个栈的栈顶元素 + * @return + */ + + public Object peek2(){ + return null; + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExpr.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExpr.java new file mode 100644 index 0000000000..ef85ff007f --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExpr.java @@ -0,0 +1,15 @@ +package com.coding.basic.stack.expr; + +public class InfixExpr { + String expr = null; + + public InfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + return 0.0f; + } + + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java new file mode 100644 index 0000000000..20e34e8852 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java @@ -0,0 +1,52 @@ +package com.coding.basic.stack.expr; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class InfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); + { + InfixExpr expr = new InfixExpr("2+3*4+5"); + Assert.assertEquals(19.0, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); + Assert.assertEquals(100.0, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("3*20/2"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("20/2*3"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("10-30+50"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("10-2*3+50"); + Assert.assertEquals(54, expr.evaluate(), 0.001f); + } + + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java new file mode 100644 index 0000000000..96a2194a67 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java @@ -0,0 +1,14 @@ +package com.coding.basic.stack.expr; + +import java.util.List; + +public class InfixToPostfix { + + public static List convert(String expr) { + + return null; + } + + + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java new file mode 100644 index 0000000000..dcbb18be4b --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java @@ -0,0 +1,18 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + +public class PostfixExpr { +String expr = null; + + public PostfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + return 0.0f; + } + + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java new file mode 100644 index 0000000000..c0435a2db5 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java @@ -0,0 +1,41 @@ +package com.coding.basic.stack.expr; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class PostfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + { + PostfixExpr expr = new PostfixExpr("6 5 2 3 + 8 * + 3 + *"); + Assert.assertEquals(288, expr.evaluate(),0.0f); + } + { + //9+(3-1)*3+10/2 + PostfixExpr expr = new PostfixExpr("9 3 1-3*+ 10 2/+"); + Assert.assertEquals(20, expr.evaluate(),0.0f); + } + + { + //10-2*3+50 + PostfixExpr expr = new PostfixExpr("10 2 3 * - 50 +"); + Assert.assertEquals(54, expr.evaluate(),0.0f); + } + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java new file mode 100644 index 0000000000..956927e2df --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java @@ -0,0 +1,18 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + +public class PrefixExpr { + String expr = null; + + public PrefixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + return 0.0f; + } + + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java new file mode 100644 index 0000000000..5cec210e75 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java @@ -0,0 +1,45 @@ +package com.coding.basic.stack.expr; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class PrefixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + { + // 2*3+4*5 + PrefixExpr expr = new PrefixExpr("+ * 2 3* 4 5"); + Assert.assertEquals(26, expr.evaluate(),0.001f); + } + { + // 4*2 + 6+9*2/3 -8 + PrefixExpr expr = new PrefixExpr("-++6/*2 9 3 * 4 2 8"); + Assert.assertEquals(12, expr.evaluate(),0.001f); + } + { + //(3+4)*5-6 + PrefixExpr expr = new PrefixExpr("- * + 3 4 5 6"); + Assert.assertEquals(29, expr.evaluate(),0.001f); + } + { + //1+((2+3)*4)-5 + PrefixExpr expr = new PrefixExpr("- + 1 * + 2 3 4 5"); + Assert.assertEquals(16, expr.evaluate(),0.001f); + } + + + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/Token.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/Token.java new file mode 100644 index 0000000000..8579743fe9 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/Token.java @@ -0,0 +1,50 @@ +package com.coding.basic.stack.expr; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +class Token { + public static final List OPERATORS = Arrays.asList("+", "-", "*", "/"); + private static final Map priorities = new HashMap<>(); + static { + priorities.put("+", 1); + priorities.put("-", 1); + priorities.put("*", 2); + priorities.put("/", 2); + } + static final int OPERATOR = 1; + static final int NUMBER = 2; + String value; + int type; + public Token(int type, String value){ + this.type = type; + this.value = value; + } + + public boolean isNumber() { + return type == NUMBER; + } + + public boolean isOperator() { + return type == OPERATOR; + } + + public int getIntValue() { + return Integer.valueOf(value).intValue(); + } + public String toString(){ + return value; + } + + public boolean hasHigherPriority(Token t){ + if(!this.isOperator() && !t.isOperator()){ + throw new RuntimeException("numbers can't compare priority"); + } + return priorities.get(this.value) - priorities.get(t.value) > 0; + } + + + +} \ No newline at end of file diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParser.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParser.java new file mode 100644 index 0000000000..d3b0f167e1 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParser.java @@ -0,0 +1,57 @@ +package com.coding.basic.stack.expr; + +import java.util.ArrayList; +import java.util.List; + +public class TokenParser { + + + public List parse(String expr) { + List tokens = new ArrayList<>(); + + int i = 0; + + while (i < expr.length()) { + + char c = expr.charAt(i); + + if (isOperator(c)) { + + Token t = new Token(Token.OPERATOR, String.valueOf(c)); + tokens.add(t); + i++; + + } else if (Character.isDigit(c)) { + + int nextOperatorIndex = indexOfNextOperator(i, expr); + String value = expr.substring(i, nextOperatorIndex); + Token t = new Token(Token.NUMBER, value); + tokens.add(t); + i = nextOperatorIndex; + + } else{ + System.out.println("char :["+c+"] is not number or operator,ignore"); + i++; + } + + } + return tokens; + } + + private int indexOfNextOperator(int i, String expr) { + + while (Character.isDigit(expr.charAt(i))) { + i++; + if (i == expr.length()) { + break; + } + } + return i; + + } + + private boolean isOperator(char c) { + String sc = String.valueOf(c); + return Token.OPERATORS.contains(sc); + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java new file mode 100644 index 0000000000..399d3e857e --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java @@ -0,0 +1,41 @@ +package com.coding.basic.stack.expr; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TokenParserTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + + TokenParser parser = new TokenParser(); + List tokens = parser.parse("300*20+12*5-20/4"); + + Assert.assertEquals(300, tokens.get(0).getIntValue()); + Assert.assertEquals("*", tokens.get(1).toString()); + Assert.assertEquals(20, tokens.get(2).getIntValue()); + Assert.assertEquals("+", tokens.get(3).toString()); + Assert.assertEquals(12, tokens.get(4).getIntValue()); + Assert.assertEquals("*", tokens.get(5).toString()); + Assert.assertEquals(5, tokens.get(6).getIntValue()); + Assert.assertEquals("-", tokens.get(7).toString()); + Assert.assertEquals(20, tokens.get(8).getIntValue()); + Assert.assertEquals("/", tokens.get(9).toString()); + Assert.assertEquals(4, tokens.get(10).getIntValue()); + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTree.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTree.java new file mode 100644 index 0000000000..4536ee7a2b --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTree.java @@ -0,0 +1,55 @@ +package com.coding.basic.tree; + +import java.util.ArrayList; +import java.util.List; + +import com.coding.basic.queue.Queue; + +public class BinarySearchTree { + + BinaryTreeNode root; + public BinarySearchTree(BinaryTreeNode root){ + this.root = root; + } + public BinaryTreeNode getRoot(){ + return root; + } + public T findMin(){ + return null; + } + public T findMax(){ + return null; + } + public int height() { + return -1; + } + public int size() { + return -1; + } + public void remove(T e){ + + } + public List levelVisit(){ + + return null; + } + public boolean isValid(){ + return false; + } + public T getLowestCommonAncestor(T n1, T n2){ + return null; + + } + /** + * 返回所有满足下列条件的节点的值: n1 <= n <= n2 , n 为 + * 该二叉查找树中的某一节点 + * @param n1 + * @param n2 + * @return + */ + public List getNodesBetween(T n1, T n2){ + return null; + } + +} + diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java new file mode 100644 index 0000000000..4a53dbe2f1 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java @@ -0,0 +1,109 @@ +package com.coding.basic.tree; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class BinarySearchTreeTest { + + BinarySearchTree tree = null; + + @Before + public void setUp() throws Exception { + BinaryTreeNode root = new BinaryTreeNode(6); + root.left = new BinaryTreeNode(2); + root.right = new BinaryTreeNode(8); + root.left.left = new BinaryTreeNode(1); + root.left.right = new BinaryTreeNode(4); + root.left.right.left = new BinaryTreeNode(3); + root.left.right.right = new BinaryTreeNode(5); + tree = new BinarySearchTree(root); + } + + @After + public void tearDown() throws Exception { + tree = null; + } + + @Test + public void testFindMin() { + Assert.assertEquals(1, tree.findMin().intValue()); + + } + + @Test + public void testFindMax() { + Assert.assertEquals(8, tree.findMax().intValue()); + } + + @Test + public void testHeight() { + Assert.assertEquals(4, tree.height()); + } + + @Test + public void testSize() { + Assert.assertEquals(7, tree.size()); + } + + @Test + public void testRemoveLeaf() { + tree.remove(3); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(4, root.left.right.data.intValue()); + + } + @Test + public void testRemoveMiddleNode1() { + tree.remove(4); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(5, root.left.right.data.intValue()); + Assert.assertEquals(3, root.left.right.left.data.intValue()); + } + @Test + public void testRemoveMiddleNode2() { + tree.remove(2); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(3, root.left.data.intValue()); + Assert.assertEquals(4, root.left.right.data.intValue()); + } + + @Test + public void testLevelVisit() { + List values = tree.levelVisit(); + Assert.assertEquals("[6, 2, 8, 1, 4, 3, 5]", values.toString()); + + } + @Test + public void testLCA(){ + Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 5).intValue()); + Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 4).intValue()); + Assert.assertEquals(6,tree.getLowestCommonAncestor(3, 8).intValue()); + } + @Test + public void testIsValid() { + + Assert.assertTrue(tree.isValid()); + + BinaryTreeNode root = new BinaryTreeNode(6); + root.left = new BinaryTreeNode(2); + root.right = new BinaryTreeNode(8); + root.left.left = new BinaryTreeNode(4); + root.left.right = new BinaryTreeNode(1); + root.left.right.left = new BinaryTreeNode(3); + tree = new BinarySearchTree(root); + + Assert.assertFalse(tree.isValid()); + } + @Test + public void testGetNodesBetween(){ + List numbers = this.tree.getNodesBetween(3, 8); + System.out.println(numbers.toString()); + + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeNode.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeNode.java new file mode 100644 index 0000000000..c1421cd398 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeNode.java @@ -0,0 +1,35 @@ +package com.coding.basic.tree; + +public class BinaryTreeNode { + + public T data; + public BinaryTreeNode left; + public BinaryTreeNode right; + + public BinaryTreeNode(T data){ + this.data=data; + } + public T getData() { + return data; + } + public void setData(T data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java new file mode 100644 index 0000000000..b033cbe1d5 --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java @@ -0,0 +1,66 @@ +package com.coding.basic.tree; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class BinaryTreeUtil { + /** + * 用递归的方式实现对二叉树的前序遍历, 需要通过BinaryTreeUtilTest测试 + * + * @param root + * @return + */ + public static List preOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + + return result; + } + + /** + * 用递归的方式实现对二叉树的中遍历 + * + * @param root + * @return + */ + public static List inOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + + return result; + } + + /** + * 用递归的方式实现对二叉树的后遍历 + * + * @param root + * @return + */ + public static List postOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + + return result; + } + /** + * 用非递归的方式实现对二叉树的前序遍历 + * @param root + * @return + */ + public static List preOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + + return result; + } + /** + * 用非递归的方式实现对二叉树的中序遍历 + * @param root + * @return + */ + public static List inOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + + return result; + } + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java new file mode 100644 index 0000000000..41857e137d --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java @@ -0,0 +1,75 @@ +package com.coding.basic.tree; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class BinaryTreeUtilTest { + + BinaryTreeNode root = null; + @Before + public void setUp() throws Exception { + root = new BinaryTreeNode(1); + root.setLeft(new BinaryTreeNode(2)); + root.setRight(new BinaryTreeNode(5)); + root.getLeft().setLeft(new BinaryTreeNode(3)); + root.getLeft().setRight(new BinaryTreeNode(4)); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testPreOrderVisit() { + + List result = BinaryTreeUtil.preOrderVisit(root); + Assert.assertEquals("[1, 2, 3, 4, 5]", result.toString()); + + + } + @Test + public void testInOrderVisit() { + + + List result = BinaryTreeUtil.inOrderVisit(root); + Assert.assertEquals("[3, 2, 4, 1, 5]", result.toString()); + + } + + @Test + public void testPostOrderVisit() { + + + List result = BinaryTreeUtil.postOrderVisit(root); + Assert.assertEquals("[3, 4, 2, 5, 1]", result.toString()); + + } + + + @Test + public void testInOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.inOrderWithoutRecursion(root); + Assert.assertEquals("[3, 2, 6, 4, 7, 1, 5]", result.toString()); + + } + @Test + public void testPreOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.preOrderWithoutRecursion(root); + Assert.assertEquals("[1, 2, 3, 4, 6, 7, 5]", result.toString()); + + } +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/FileList.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/FileList.java new file mode 100644 index 0000000000..6e65192e4a --- /dev/null +++ b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/FileList.java @@ -0,0 +1,10 @@ +package com.coding.basic.tree; + +import java.io.File; + +public class FileList { + public void list(File f) { + } + + +} diff --git a/students/34594980/ood/ood-assignment/pom.xml b/students/34594980/ood/ood-assignment/pom.xml new file mode 100644 index 0000000000..a7e5041647 --- /dev/null +++ b/students/34594980/ood/ood-assignment/pom.xml @@ -0,0 +1,44 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..781587a846 --- /dev/null +++ b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 86c067292874746d004674e933ed53c2b177e1c8 Mon Sep 17 00:00:00 2001 From: Ginkee Date: Thu, 15 Jun 2017 00:18:14 +0800 Subject: [PATCH 104/214] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/466199956/readme.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 students/466199956/readme.md diff --git a/students/466199956/readme.md b/students/466199956/readme.md new file mode 100644 index 0000000000..e69de29bb2 From 2d6e92a007062557382ee59294234d4227e82921 Mon Sep 17 00:00:00 2001 From: jyp Date: Thu, 15 Jun 2017 09:37:34 +0800 Subject: [PATCH 105/214] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E6=BA=90=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coderising/download/DownloadThread.java | 20 ++ .../coderising/download/FileDownloader.java | 73 +++++++ .../download/FileDownloaderTest.java | 59 ++++++ .../coderising/download/api/Connection.java | 23 ++ .../download/api/ConnectionException.java | 5 + .../download/api/ConnectionManager.java | 10 + .../download/api/DownloadListener.java | 5 + .../download/impl/ConnectionImpl.java | 27 +++ .../download/impl/ConnectionManagerImpl.java | 15 ++ .../coderising/litestruts/LoginAction.java | 39 ++++ .../com/coderising/litestruts/Struts.java | 34 +++ .../com/coderising/litestruts/StrutsTest.java | 43 ++++ .../java/com/coderising/litestruts/View.java | 23 ++ .../java/com/coderising/litestruts/struts.xml | 11 + .../com/coderising/ood/course/bad/Course.java | 24 +++ .../ood/course/bad/CourseOffering.java | 26 +++ .../ood/course/bad/CourseService.java | 16 ++ .../coderising/ood/course/bad/Student.java | 14 ++ .../coderising/ood/course/good/Course.java | 18 ++ .../ood/course/good/CourseOffering.java | 34 +++ .../ood/course/good/CourseService.java | 14 ++ .../coderising/ood/course/good/Student.java | 21 ++ .../java/com/coderising/ood/ocp/DateUtil.java | 10 + .../java/com/coderising/ood/ocp/Logger.java | 38 ++++ .../java/com/coderising/ood/ocp/MailUtil.java | 10 + .../java/com/coderising/ood/ocp/SMSUtil.java | 10 + .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + .../main/java/com/coding/basic/Iterator.java | 7 + .../src/main/java/com/coding/basic/List.java | 9 + .../com/coding/basic/array/ArrayList.java | 35 +++ .../com/coding/basic/array/ArrayUtil.java | 96 +++++++++ .../coding/basic/linklist/LRUPageFrame.java | 57 +++++ .../basic/linklist/LRUPageFrameTest.java | 34 +++ .../com/coding/basic/linklist/LinkedList.java | 125 +++++++++++ .../com/coding/basic/queue/CircleQueue.java | 39 ++++ .../java/com/coding/basic/queue/Josephus.java | 18 ++ .../com/coding/basic/queue/JosephusTest.java | 27 +++ .../java/com/coding/basic/queue/Queue.java | 61 ++++++ .../basic/queue/QueueWithTwoStacks.java | 47 +++++ .../com/coding/basic/stack/QuickMinStack.java | 19 ++ .../java/com/coding/basic/stack/Stack.java | 24 +++ .../com/coding/basic/stack/StackUtil.java | 48 +++++ .../com/coding/basic/stack/StackUtilTest.java | 65 ++++++ .../basic/stack/StackWithTwoQueues.java | 16 ++ .../basic/stack/TwoStackInOneArray.java | 57 +++++ .../coding/basic/stack/expr/InfixExpr.java | 15 ++ .../basic/stack/expr/InfixExprTest.java | 52 +++++ .../basic/stack/expr/InfixToPostfix.java | 14 ++ .../coding/basic/stack/expr/PostfixExpr.java | 18 ++ .../basic/stack/expr/PostfixExprTest.java | 41 ++++ .../coding/basic/stack/expr/PrefixExpr.java | 18 ++ .../basic/stack/expr/PrefixExprTest.java | 45 ++++ .../com/coding/basic/stack/expr/Token.java | 50 +++++ .../coding/basic/stack/expr/TokenParser.java | 57 +++++ .../basic/stack/expr/TokenParserTest.java | 41 ++++ .../coding/basic/tree/BinarySearchTree.java | 55 +++++ .../basic/tree/BinarySearchTreeTest.java | 109 ++++++++++ .../com/coding/basic/tree/BinaryTreeNode.java | 35 +++ .../com/coding/basic/tree/BinaryTreeUtil.java | 66 ++++++ .../coding/basic/tree/BinaryTreeUtilTest.java | 75 +++++++ .../java/com/coding/basic/tree/FileList.java | 10 + .../ood/srp/config/ConfigurationKeys.java | 4 +- 67 files changed, 2386 insertions(+), 3 deletions(-) create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/DownloadThread.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/FileDownloader.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/FileDownloaderTest.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/api/Connection.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/api/ConnectionException.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/api/ConnectionManager.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/api/DownloadListener.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/impl/ConnectionImpl.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/LoginAction.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/Struts.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/StrutsTest.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/View.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/struts.xml create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/bad/Course.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/bad/CourseOffering.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/bad/CourseService.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/bad/Student.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/good/Course.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/good/CourseOffering.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/good/CourseService.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/good/Student.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/ocp/DateUtil.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/ocp/Logger.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/ocp/MailUtil.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/ocp/SMSUtil.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/product_promotion.txt create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/Iterator.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/List.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/array/ArrayList.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/array/ArrayUtil.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/linklist/LRUPageFrame.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/linklist/LinkedList.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/CircleQueue.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/Josephus.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/JosephusTest.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/Queue.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/QuickMinStack.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/Stack.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/StackUtil.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/StackUtilTest.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/InfixExpr.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/Token.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/TokenParser.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinarySearchTree.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinaryTreeNode.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java create mode 100644 students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/FileList.java diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/DownloadThread.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..900a3ad358 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/DownloadThread.java @@ -0,0 +1,20 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + public DownloadThread( Connection conn, int startPos, int endPos){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + } + public void run(){ + + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/FileDownloader.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..c3c8a3f27d --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/FileDownloader.java @@ -0,0 +1,73 @@ +package com.coderising.download; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + + public FileDownloader(String _url) { + this.url = _url; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + new DownloadThread(conn,0,length-1).start(); + + } catch (ConnectionException e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/FileDownloaderTest.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..4ff7f46ae0 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,59 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://localhost:8080/test.jpg"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/api/Connection.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/api/ConnectionException.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/api/ConnectionManager.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/api/DownloadListener.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/impl/ConnectionImpl.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..36a9d2ce15 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,27 @@ +package com.coderising.download.impl; + +import java.io.IOException; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + @Override + public byte[] read(int startPos, int endPos) throws IOException { + + return null; + } + + @Override + public int getContentLength() { + + return 0; + } + + @Override + public void close() { + + + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..172371dd55 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,15 @@ +package com.coderising.download.impl; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return null; + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/LoginAction.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/Struts.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + return null; + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/StrutsTest.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/View.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/struts.xml b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/bad/Course.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/bad/Course.java new file mode 100644 index 0000000000..436d092f58 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/bad/Course.java @@ -0,0 +1,24 @@ +package com.coderising.ood.course.bad; + +import java.util.List; + +public class Course { + private String id; + private String desc; + private int duration ; + + List prerequisites; + + public List getPrerequisites() { + return prerequisites; + } + + + public boolean equals(Object o){ + if(o == null || !(o instanceof Course)){ + return false; + } + Course c = (Course)o; + return (c != null) && c.id.equals(id); + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/bad/CourseOffering.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/bad/CourseOffering.java new file mode 100644 index 0000000000..ab8c764584 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/bad/CourseOffering.java @@ -0,0 +1,26 @@ +package com.coderising.ood.course.bad; + +import java.util.ArrayList; +import java.util.List; + + +public class CourseOffering { + private Course course; + private String location; + private String teacher; + private int maxStudents; + + List students = new ArrayList(); + + public int getMaxStudents() { + return maxStudents; + } + + public List getStudents() { + return students; + } + + public Course getCourse() { + return course; + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/bad/CourseService.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/bad/CourseService.java new file mode 100644 index 0000000000..8c34bad0c3 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/bad/CourseService.java @@ -0,0 +1,16 @@ +package com.coderising.ood.course.bad; + + + +public class CourseService { + + public void chooseCourse(Student student, CourseOffering sc){ + //如果学生上过该科目的先修科目,并且该课程还未满, 则学生可以加入该课程 + if(student.getCoursesAlreadyTaken().containsAll( + sc.getCourse().getPrerequisites()) + && sc.getMaxStudents() > sc.getStudents().size()){ + sc.getStudents().add(student); + } + + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/bad/Student.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/bad/Student.java new file mode 100644 index 0000000000..a651923ef5 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/bad/Student.java @@ -0,0 +1,14 @@ +package com.coderising.ood.course.bad; + +import java.util.ArrayList; +import java.util.List; + +public class Student { + private String id; + private String name; + private List coursesAlreadyTaken = new ArrayList(); + + public List getCoursesAlreadyTaken() { + return coursesAlreadyTaken; + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/good/Course.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/good/Course.java new file mode 100644 index 0000000000..aefc9692bb --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/good/Course.java @@ -0,0 +1,18 @@ +package com.coderising.ood.course.good; + +import java.util.List; + +public class Course { + private String id; + private String desc; + private int duration ; + + List prerequisites; + + public List getPrerequisites() { + return prerequisites; + } + +} + + diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/good/CourseOffering.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/good/CourseOffering.java new file mode 100644 index 0000000000..8660ec8109 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/good/CourseOffering.java @@ -0,0 +1,34 @@ +package com.coderising.ood.course.good; + +import java.util.ArrayList; +import java.util.List; + +public class CourseOffering { + private Course course; + private String location; + private String teacher; + private int maxStudents; + + List students = new ArrayList(); + + public List getStudents() { + return students; + } + public int getMaxStudents() { + return maxStudents; + } + public Course getCourse() { + return course; + } + + + // 第二步: 把主要逻辑移动到CourseOffering 中 + public void addStudent(Student student){ + + if(student.canAttend(course) + && this.maxStudents > students.size()){ + students.add(student); + } + } + // 第三步: 重构CourseService +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/good/CourseService.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/good/CourseService.java new file mode 100644 index 0000000000..22ba4a5450 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/good/CourseService.java @@ -0,0 +1,14 @@ +package com.coderising.ood.course.good; + + + +public class CourseService { + + public void chooseCourse(Student student, CourseOffering sc){ + //第一步:重构: canAttend , 但是还有问题 + if(student.canAttend(sc.getCourse()) + && sc.getMaxStudents() > sc.getStudents().size()){ + sc.getStudents().add(student); + } + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/good/Student.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/good/Student.java new file mode 100644 index 0000000000..2c7e128b2a --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/course/good/Student.java @@ -0,0 +1,21 @@ +package com.coderising.ood.course.good; + +import java.util.ArrayList; +import java.util.List; + +public class Student { + private String id; + private String name; + private List coursesAlreadyTaken = new ArrayList(); + + public List getCoursesAlreadyTaken() { + return coursesAlreadyTaken; + } + + public boolean canAttend(Course course){ + return this.coursesAlreadyTaken.containsAll( + course.getPrerequisites()); + } +} + + diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/ocp/DateUtil.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/ocp/DateUtil.java new file mode 100644 index 0000000000..b6cf28c096 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/ocp/DateUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class DateUtil { + + public static String getCurrentDateAsString() { + + return null; + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/ocp/Logger.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/ocp/Logger.java new file mode 100644 index 0000000000..0357c4d912 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/ocp/Logger.java @@ -0,0 +1,38 @@ +package com.coderising.ood.ocp; + +public class Logger { + + public final int RAW_LOG = 1; + public final int RAW_LOG_WITH_DATE = 2; + public final int EMAIL_LOG = 1; + public final int SMS_LOG = 2; + public final int PRINT_LOG = 3; + + int type = 0; + int method = 0; + + public Logger(int logType, int logMethod){ + this.type = logType; + this.method = logMethod; + } + public void log(String msg){ + + String logMsg = msg; + + if(this.type == RAW_LOG){ + logMsg = msg; + } else if(this.type == RAW_LOG_WITH_DATE){ + String txtDate = DateUtil.getCurrentDateAsString(); + logMsg = txtDate + ": " + msg; + } + + if(this.method == EMAIL_LOG){ + MailUtil.send(logMsg); + } else if(this.method == SMS_LOG){ + SMSUtil.send(logMsg); + } else if(this.method == PRINT_LOG){ + System.out.println(logMsg); + } + } +} + diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/ocp/MailUtil.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/ocp/MailUtil.java new file mode 100644 index 0000000000..ec54b839c5 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/ocp/MailUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class MailUtil { + + public static void send(String logMsg) { + // TODO Auto-generated method stub + + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/ocp/SMSUtil.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/ocp/SMSUtil.java new file mode 100644 index 0000000000..13cf802418 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/ocp/SMSUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class SMSUtil { + + public static void send(String logMsg) { + // TODO Auto-generated method stub + + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/Configuration.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..781587a846 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..a98917f829 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo R15 +P4955 Vivo X20 \ No newline at end of file diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/Iterator.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/List.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/array/ArrayList.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/array/ArrayList.java new file mode 100644 index 0000000000..4576c016af --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/array/ArrayList.java @@ -0,0 +1,35 @@ +package com.coding.basic.array; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + + } + public void add(int index, Object o){ + + } + + public Object get(int index){ + return null; + } + + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/array/ArrayUtil.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/array/ArrayUtil.java new file mode 100644 index 0000000000..45740e6d57 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coding.basic.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + return null; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/linklist/LRUPageFrame.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/linklist/LRUPageFrame.java new file mode 100644 index 0000000000..994a241a3d --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/linklist/LRUPageFrame.java @@ -0,0 +1,57 @@ +package com.coding.basic.linklist; + + +public class LRUPageFrame { + + private static class Node { + + Node prev; + Node next; + int pageNum; + + Node() { + } + } + + private int capacity; + + private int currentSize; + private Node first;// 链表头 + private Node last;// 链表尾 + + + public LRUPageFrame(int capacity) { + this.currentSize = 0; + this.capacity = capacity; + + } + + /** + * 获取缓存中对象 + * + * @param key + * @return + */ + public void access(int pageNum) { + + + } + + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + + + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java new file mode 100644 index 0000000000..7fd72fc2b4 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java @@ -0,0 +1,34 @@ +package com.coding.basic.linklist; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + Assert.assertEquals("4,0,3", frame.toString()); + frame.access(5); + Assert.assertEquals("5,4,0", frame.toString()); + + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/linklist/LinkedList.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/linklist/LinkedList.java new file mode 100644 index 0000000000..f4c7556a2e --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/linklist/LinkedList.java @@ -0,0 +1,125 @@ +package com.coding.basic.linklist; + +import com.coding.basic.Iterator; +import com.coding.basic.List; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/CircleQueue.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/CircleQueue.java new file mode 100644 index 0000000000..2e0550c67e --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/CircleQueue.java @@ -0,0 +1,39 @@ +package com.coding.basic.queue; + +/** + * 用数组实现循环队列 + * @author liuxin + * + * @param + */ +public class CircleQueue { + + private final static int DEFAULT_SIZE = 10; + + //用数组来保存循环队列的元素 + private Object[] elementData = new Object[DEFAULT_SIZE] ; + + //队头 + private int front = 0; + //队尾 + private int rear = 0; + + public boolean isEmpty() { + return false; + + } + + public int size() { + return -1; + } + + + + public void enQueue(E data) { + + } + + public E deQueue() { + return null; + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/Josephus.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/Josephus.java new file mode 100644 index 0000000000..6a3ea639b9 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/Josephus.java @@ -0,0 +1,18 @@ +package com.coding.basic.queue; + +import java.util.List; + +/** + * 用Queue来实现Josephus问题 + * 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来 + * 该方法返回一个List, 包含了被杀死人的次序 + * @author liuxin + * + */ +public class Josephus { + + public static List execute(int n, int m){ + return null; + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/JosephusTest.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/JosephusTest.java new file mode 100644 index 0000000000..7d90318b51 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/JosephusTest.java @@ -0,0 +1,27 @@ +package com.coding.basic.queue; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class JosephusTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testExecute() { + + Assert.assertEquals("[1, 3, 5, 0, 4, 2, 6]", Josephus.execute(7, 2).toString()); + + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/Queue.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/Queue.java new file mode 100644 index 0000000000..c4c4b7325e --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/Queue.java @@ -0,0 +1,61 @@ +package com.coding.basic.queue; + +import java.util.NoSuchElementException; + +public class Queue { + private Node first; + private Node last; + private int size; + + + private static class Node { + private E item; + private Node next; + } + + + public Queue() { + first = null; + last = null; + size = 0; + } + + + public boolean isEmpty() { + return first == null; + } + + public int size() { + return size; + } + + + + public void enQueue(E data) { + Node oldlast = last; + last = new Node(); + last.item = data; + last.next = null; + if (isEmpty()) { + first = last; + } + else{ + oldlast.next = last; + } + size++; + } + + public E deQueue() { + if (isEmpty()) { + throw new NoSuchElementException("Queue underflow"); + } + E item = first.item; + first = first.next; + size--; + if (isEmpty()) { + last = null; + } + return item; + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java new file mode 100644 index 0000000000..cef19a8b59 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java @@ -0,0 +1,47 @@ +package com.coding.basic.queue; + +import java.util.Stack; + +/** + * 用两个栈来实现一个队列 + * @author liuxin + * + * @param + */ +public class QueueWithTwoStacks { + private Stack stack1; + private Stack stack2; + + + public QueueWithTwoStacks() { + stack1 = new Stack(); + stack2 = new Stack(); + } + + + + + public boolean isEmpty() { + return false; + } + + + + public int size() { + return -1; + } + + + + public void enQueue(E item) { + + } + + public E deQueue() { + return null; + } + + + + } + diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/QuickMinStack.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/QuickMinStack.java new file mode 100644 index 0000000000..f391d92b8f --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/QuickMinStack.java @@ -0,0 +1,19 @@ +package com.coding.basic.stack; + +/** + * 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素 + * finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值 + * @author liuxin + * + */ +public class QuickMinStack { + public void push(int data){ + + } + public int pop(){ + return -1; + } + public int findMin(){ + return -1; + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/Stack.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/Stack.java new file mode 100644 index 0000000000..fedb243604 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/Stack.java @@ -0,0 +1,24 @@ +package com.coding.basic.stack; + +import com.coding.basic.array.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/StackUtil.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/StackUtil.java new file mode 100644 index 0000000000..b0ec38161d --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/StackUtil.java @@ -0,0 +1,48 @@ +package com.coding.basic.stack; +import java.util.Stack; +public class StackUtil { + + + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + public static void reverse(Stack s) { + + + + } + + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * + * @param o + */ + public static void remove(Stack s,Object o) { + + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) { + return null; + } + /** + * 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz + * 使用堆栈检查字符串s中的括号是不是成对出现的。 + * 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true + * 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; + * @param s + * @return + */ + public static boolean isValidPairs(String s){ + return false; + } + + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/StackUtilTest.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/StackUtilTest.java new file mode 100644 index 0000000000..76f2cb7668 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/StackUtilTest.java @@ -0,0 +1,65 @@ +package com.coding.basic.stack; + +import java.util.Stack; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +public class StackUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + + @Test + public void testReverse() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); + StackUtil.reverse(s); + Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString()); + } + + @Test + public void testRemove() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + StackUtil.remove(s, 2); + Assert.assertEquals("[1, 3]", s.toString()); + } + + @Test + public void testGetTop() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + { + Object[] values = StackUtil.getTop(s, 3); + Assert.assertEquals(5, values[0]); + Assert.assertEquals(4, values[1]); + Assert.assertEquals(3, values[2]); + } + } + + @Test + public void testIsValidPairs() { + Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])")); + Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})")); + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java new file mode 100644 index 0000000000..d0ab4387d2 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java @@ -0,0 +1,16 @@ +package com.coding.basic.stack; + + +public class StackWithTwoQueues { + + + public void push(int data) { + + } + + public int pop() { + return -1; + } + + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java new file mode 100644 index 0000000000..e86d056a24 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java @@ -0,0 +1,57 @@ +package com.coding.basic.stack; + +/** + * 用一个数组实现两个栈 + * 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。 + * @author liuxin + * + */ +public class TwoStackInOneArray { + Object[] data = new Object[10]; + + /** + * 向第一个栈中压入元素 + * @param o + */ + public void push1(Object o){ + + } + /** + * 从第一个栈中弹出元素 + * @return + */ + public Object pop1(){ + return null; + } + + /** + * 获取第一个栈的栈顶元素 + * @return + */ + + public Object peek1(){ + return null; + } + /* + * 向第二个栈压入元素 + */ + public void push2(Object o){ + + } + /** + * 从第二个栈弹出元素 + * @return + */ + public Object pop2(){ + return null; + } + /** + * 获取第二个栈的栈顶元素 + * @return + */ + + public Object peek2(){ + return null; + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/InfixExpr.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/InfixExpr.java new file mode 100644 index 0000000000..ef85ff007f --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/InfixExpr.java @@ -0,0 +1,15 @@ +package com.coding.basic.stack.expr; + +public class InfixExpr { + String expr = null; + + public InfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + return 0.0f; + } + + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java new file mode 100644 index 0000000000..20e34e8852 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java @@ -0,0 +1,52 @@ +package com.coding.basic.stack.expr; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class InfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); + { + InfixExpr expr = new InfixExpr("2+3*4+5"); + Assert.assertEquals(19.0, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); + Assert.assertEquals(100.0, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("3*20/2"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("20/2*3"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("10-30+50"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("10-2*3+50"); + Assert.assertEquals(54, expr.evaluate(), 0.001f); + } + + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java new file mode 100644 index 0000000000..96a2194a67 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java @@ -0,0 +1,14 @@ +package com.coding.basic.stack.expr; + +import java.util.List; + +public class InfixToPostfix { + + public static List convert(String expr) { + + return null; + } + + + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java new file mode 100644 index 0000000000..dcbb18be4b --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java @@ -0,0 +1,18 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + +public class PostfixExpr { +String expr = null; + + public PostfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + return 0.0f; + } + + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java new file mode 100644 index 0000000000..c0435a2db5 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java @@ -0,0 +1,41 @@ +package com.coding.basic.stack.expr; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class PostfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + { + PostfixExpr expr = new PostfixExpr("6 5 2 3 + 8 * + 3 + *"); + Assert.assertEquals(288, expr.evaluate(),0.0f); + } + { + //9+(3-1)*3+10/2 + PostfixExpr expr = new PostfixExpr("9 3 1-3*+ 10 2/+"); + Assert.assertEquals(20, expr.evaluate(),0.0f); + } + + { + //10-2*3+50 + PostfixExpr expr = new PostfixExpr("10 2 3 * - 50 +"); + Assert.assertEquals(54, expr.evaluate(),0.0f); + } + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java new file mode 100644 index 0000000000..956927e2df --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java @@ -0,0 +1,18 @@ +package com.coding.basic.stack.expr; + +import java.util.List; +import java.util.Stack; + +public class PrefixExpr { + String expr = null; + + public PrefixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + return 0.0f; + } + + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java new file mode 100644 index 0000000000..5cec210e75 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java @@ -0,0 +1,45 @@ +package com.coding.basic.stack.expr; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class PrefixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + { + // 2*3+4*5 + PrefixExpr expr = new PrefixExpr("+ * 2 3* 4 5"); + Assert.assertEquals(26, expr.evaluate(),0.001f); + } + { + // 4*2 + 6+9*2/3 -8 + PrefixExpr expr = new PrefixExpr("-++6/*2 9 3 * 4 2 8"); + Assert.assertEquals(12, expr.evaluate(),0.001f); + } + { + //(3+4)*5-6 + PrefixExpr expr = new PrefixExpr("- * + 3 4 5 6"); + Assert.assertEquals(29, expr.evaluate(),0.001f); + } + { + //1+((2+3)*4)-5 + PrefixExpr expr = new PrefixExpr("- + 1 * + 2 3 4 5"); + Assert.assertEquals(16, expr.evaluate(),0.001f); + } + + + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/Token.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/Token.java new file mode 100644 index 0000000000..8579743fe9 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/Token.java @@ -0,0 +1,50 @@ +package com.coding.basic.stack.expr; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +class Token { + public static final List OPERATORS = Arrays.asList("+", "-", "*", "/"); + private static final Map priorities = new HashMap<>(); + static { + priorities.put("+", 1); + priorities.put("-", 1); + priorities.put("*", 2); + priorities.put("/", 2); + } + static final int OPERATOR = 1; + static final int NUMBER = 2; + String value; + int type; + public Token(int type, String value){ + this.type = type; + this.value = value; + } + + public boolean isNumber() { + return type == NUMBER; + } + + public boolean isOperator() { + return type == OPERATOR; + } + + public int getIntValue() { + return Integer.valueOf(value).intValue(); + } + public String toString(){ + return value; + } + + public boolean hasHigherPriority(Token t){ + if(!this.isOperator() && !t.isOperator()){ + throw new RuntimeException("numbers can't compare priority"); + } + return priorities.get(this.value) - priorities.get(t.value) > 0; + } + + + +} \ No newline at end of file diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/TokenParser.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/TokenParser.java new file mode 100644 index 0000000000..d3b0f167e1 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/TokenParser.java @@ -0,0 +1,57 @@ +package com.coding.basic.stack.expr; + +import java.util.ArrayList; +import java.util.List; + +public class TokenParser { + + + public List parse(String expr) { + List tokens = new ArrayList<>(); + + int i = 0; + + while (i < expr.length()) { + + char c = expr.charAt(i); + + if (isOperator(c)) { + + Token t = new Token(Token.OPERATOR, String.valueOf(c)); + tokens.add(t); + i++; + + } else if (Character.isDigit(c)) { + + int nextOperatorIndex = indexOfNextOperator(i, expr); + String value = expr.substring(i, nextOperatorIndex); + Token t = new Token(Token.NUMBER, value); + tokens.add(t); + i = nextOperatorIndex; + + } else{ + System.out.println("char :["+c+"] is not number or operator,ignore"); + i++; + } + + } + return tokens; + } + + private int indexOfNextOperator(int i, String expr) { + + while (Character.isDigit(expr.charAt(i))) { + i++; + if (i == expr.length()) { + break; + } + } + return i; + + } + + private boolean isOperator(char c) { + String sc = String.valueOf(c); + return Token.OPERATORS.contains(sc); + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java new file mode 100644 index 0000000000..399d3e857e --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java @@ -0,0 +1,41 @@ +package com.coding.basic.stack.expr; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TokenParserTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + + TokenParser parser = new TokenParser(); + List tokens = parser.parse("300*20+12*5-20/4"); + + Assert.assertEquals(300, tokens.get(0).getIntValue()); + Assert.assertEquals("*", tokens.get(1).toString()); + Assert.assertEquals(20, tokens.get(2).getIntValue()); + Assert.assertEquals("+", tokens.get(3).toString()); + Assert.assertEquals(12, tokens.get(4).getIntValue()); + Assert.assertEquals("*", tokens.get(5).toString()); + Assert.assertEquals(5, tokens.get(6).getIntValue()); + Assert.assertEquals("-", tokens.get(7).toString()); + Assert.assertEquals(20, tokens.get(8).getIntValue()); + Assert.assertEquals("/", tokens.get(9).toString()); + Assert.assertEquals(4, tokens.get(10).getIntValue()); + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinarySearchTree.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinarySearchTree.java new file mode 100644 index 0000000000..4536ee7a2b --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinarySearchTree.java @@ -0,0 +1,55 @@ +package com.coding.basic.tree; + +import java.util.ArrayList; +import java.util.List; + +import com.coding.basic.queue.Queue; + +public class BinarySearchTree { + + BinaryTreeNode root; + public BinarySearchTree(BinaryTreeNode root){ + this.root = root; + } + public BinaryTreeNode getRoot(){ + return root; + } + public T findMin(){ + return null; + } + public T findMax(){ + return null; + } + public int height() { + return -1; + } + public int size() { + return -1; + } + public void remove(T e){ + + } + public List levelVisit(){ + + return null; + } + public boolean isValid(){ + return false; + } + public T getLowestCommonAncestor(T n1, T n2){ + return null; + + } + /** + * 返回所有满足下列条件的节点的值: n1 <= n <= n2 , n 为 + * 该二叉查找树中的某一节点 + * @param n1 + * @param n2 + * @return + */ + public List getNodesBetween(T n1, T n2){ + return null; + } + +} + diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java new file mode 100644 index 0000000000..4a53dbe2f1 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java @@ -0,0 +1,109 @@ +package com.coding.basic.tree; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class BinarySearchTreeTest { + + BinarySearchTree tree = null; + + @Before + public void setUp() throws Exception { + BinaryTreeNode root = new BinaryTreeNode(6); + root.left = new BinaryTreeNode(2); + root.right = new BinaryTreeNode(8); + root.left.left = new BinaryTreeNode(1); + root.left.right = new BinaryTreeNode(4); + root.left.right.left = new BinaryTreeNode(3); + root.left.right.right = new BinaryTreeNode(5); + tree = new BinarySearchTree(root); + } + + @After + public void tearDown() throws Exception { + tree = null; + } + + @Test + public void testFindMin() { + Assert.assertEquals(1, tree.findMin().intValue()); + + } + + @Test + public void testFindMax() { + Assert.assertEquals(8, tree.findMax().intValue()); + } + + @Test + public void testHeight() { + Assert.assertEquals(4, tree.height()); + } + + @Test + public void testSize() { + Assert.assertEquals(7, tree.size()); + } + + @Test + public void testRemoveLeaf() { + tree.remove(3); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(4, root.left.right.data.intValue()); + + } + @Test + public void testRemoveMiddleNode1() { + tree.remove(4); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(5, root.left.right.data.intValue()); + Assert.assertEquals(3, root.left.right.left.data.intValue()); + } + @Test + public void testRemoveMiddleNode2() { + tree.remove(2); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(3, root.left.data.intValue()); + Assert.assertEquals(4, root.left.right.data.intValue()); + } + + @Test + public void testLevelVisit() { + List values = tree.levelVisit(); + Assert.assertEquals("[6, 2, 8, 1, 4, 3, 5]", values.toString()); + + } + @Test + public void testLCA(){ + Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 5).intValue()); + Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 4).intValue()); + Assert.assertEquals(6,tree.getLowestCommonAncestor(3, 8).intValue()); + } + @Test + public void testIsValid() { + + Assert.assertTrue(tree.isValid()); + + BinaryTreeNode root = new BinaryTreeNode(6); + root.left = new BinaryTreeNode(2); + root.right = new BinaryTreeNode(8); + root.left.left = new BinaryTreeNode(4); + root.left.right = new BinaryTreeNode(1); + root.left.right.left = new BinaryTreeNode(3); + tree = new BinarySearchTree(root); + + Assert.assertFalse(tree.isValid()); + } + @Test + public void testGetNodesBetween(){ + List numbers = this.tree.getNodesBetween(3, 8); + System.out.println(numbers.toString()); + + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinaryTreeNode.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinaryTreeNode.java new file mode 100644 index 0000000000..c1421cd398 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinaryTreeNode.java @@ -0,0 +1,35 @@ +package com.coding.basic.tree; + +public class BinaryTreeNode { + + public T data; + public BinaryTreeNode left; + public BinaryTreeNode right; + + public BinaryTreeNode(T data){ + this.data=data; + } + public T getData() { + return data; + } + public void setData(T data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java new file mode 100644 index 0000000000..b033cbe1d5 --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java @@ -0,0 +1,66 @@ +package com.coding.basic.tree; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class BinaryTreeUtil { + /** + * 用递归的方式实现对二叉树的前序遍历, 需要通过BinaryTreeUtilTest测试 + * + * @param root + * @return + */ + public static List preOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + + return result; + } + + /** + * 用递归的方式实现对二叉树的中遍历 + * + * @param root + * @return + */ + public static List inOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + + return result; + } + + /** + * 用递归的方式实现对二叉树的后遍历 + * + * @param root + * @return + */ + public static List postOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + + return result; + } + /** + * 用非递归的方式实现对二叉树的前序遍历 + * @param root + * @return + */ + public static List preOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + + return result; + } + /** + * 用非递归的方式实现对二叉树的中序遍历 + * @param root + * @return + */ + public static List inOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + + return result; + } + +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java new file mode 100644 index 0000000000..41857e137d --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java @@ -0,0 +1,75 @@ +package com.coding.basic.tree; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class BinaryTreeUtilTest { + + BinaryTreeNode root = null; + @Before + public void setUp() throws Exception { + root = new BinaryTreeNode(1); + root.setLeft(new BinaryTreeNode(2)); + root.setRight(new BinaryTreeNode(5)); + root.getLeft().setLeft(new BinaryTreeNode(3)); + root.getLeft().setRight(new BinaryTreeNode(4)); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testPreOrderVisit() { + + List result = BinaryTreeUtil.preOrderVisit(root); + Assert.assertEquals("[1, 2, 3, 4, 5]", result.toString()); + + + } + @Test + public void testInOrderVisit() { + + + List result = BinaryTreeUtil.inOrderVisit(root); + Assert.assertEquals("[3, 2, 4, 1, 5]", result.toString()); + + } + + @Test + public void testPostOrderVisit() { + + + List result = BinaryTreeUtil.postOrderVisit(root); + Assert.assertEquals("[3, 4, 2, 5, 1]", result.toString()); + + } + + + @Test + public void testInOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.inOrderWithoutRecursion(root); + Assert.assertEquals("[3, 2, 6, 4, 7, 1, 5]", result.toString()); + + } + @Test + public void testPreOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.preOrderWithoutRecursion(root); + Assert.assertEquals("[1, 2, 3, 4, 6, 7, 5]", result.toString()); + + } +} diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/FileList.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/FileList.java new file mode 100644 index 0000000000..6e65192e4a --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/tree/FileList.java @@ -0,0 +1,10 @@ +package com.coding.basic.tree; + +import java.io.File; + +public class FileList { + public void list(File f) { + } + + +} diff --git a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java index 1c99770ed8..cadb23ed24 100644 --- a/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java +++ b/students/992331664/ood/ood/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java @@ -1,8 +1,6 @@ package com.coderising.ood.srp.config; -/** - * 实际该类应该为配置文件 - */ + public class ConfigurationKeys { public static final String SMTP_SERVER = "smtp.server"; From d46668401fe69a4c920e452aaa130f6a576088b4 Mon Sep 17 00:00:00 2001 From: macvis Date: Thu, 15 Jun 2017 10:10:46 +0800 Subject: [PATCH 106/214] file stage by macvis --- students/75939388/ood/pom.xml | 15 ++ .../ood/src/main/java/srp/Configuration.java | 23 ++ .../src/main/java/srp/ConfigurationKeys.java | 9 + .../ood/src/main/java/srp/DBUtil.java | 25 +++ .../ood/src/main/java/srp/MailUtil.java | 18 ++ .../ood/src/main/java/srp/PromotionMail.java | 198 ++++++++++++++++++ .../src/main/java/srp/product_promotion.txt | 4 + 7 files changed, 292 insertions(+) create mode 100644 students/75939388/ood/pom.xml create mode 100644 students/75939388/ood/src/main/java/srp/Configuration.java create mode 100644 students/75939388/ood/src/main/java/srp/ConfigurationKeys.java create mode 100644 students/75939388/ood/src/main/java/srp/DBUtil.java create mode 100644 students/75939388/ood/src/main/java/srp/MailUtil.java create mode 100644 students/75939388/ood/src/main/java/srp/PromotionMail.java create mode 100644 students/75939388/ood/src/main/java/srp/product_promotion.txt diff --git a/students/75939388/ood/pom.xml b/students/75939388/ood/pom.xml new file mode 100644 index 0000000000..e6a1b1de5a --- /dev/null +++ b/students/75939388/ood/pom.xml @@ -0,0 +1,15 @@ + + + + season2 + learning2017 + 2.0-SEASON2 + + 4.0.0 + + ood + + + \ No newline at end of file diff --git a/students/75939388/ood/src/main/java/srp/Configuration.java b/students/75939388/ood/src/main/java/srp/Configuration.java new file mode 100644 index 0000000000..3a17cdd457 --- /dev/null +++ b/students/75939388/ood/src/main/java/srp/Configuration.java @@ -0,0 +1,23 @@ +package srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/75939388/ood/src/main/java/srp/ConfigurationKeys.java b/students/75939388/ood/src/main/java/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..ef3a07a354 --- /dev/null +++ b/students/75939388/ood/src/main/java/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/75939388/ood/src/main/java/srp/DBUtil.java b/students/75939388/ood/src/main/java/srp/DBUtil.java new file mode 100644 index 0000000000..912bebf080 --- /dev/null +++ b/students/75939388/ood/src/main/java/srp/DBUtil.java @@ -0,0 +1,25 @@ +package srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/75939388/ood/src/main/java/srp/MailUtil.java b/students/75939388/ood/src/main/java/srp/MailUtil.java new file mode 100644 index 0000000000..556976f5c9 --- /dev/null +++ b/students/75939388/ood/src/main/java/srp/MailUtil.java @@ -0,0 +1,18 @@ +package srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/75939388/ood/src/main/java/srp/PromotionMail.java b/students/75939388/ood/src/main/java/srp/PromotionMail.java new file mode 100644 index 0000000000..446f82e9ad --- /dev/null +++ b/students/75939388/ood/src/main/java/srp/PromotionMail.java @@ -0,0 +1,198 @@ +package srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/75939388/ood/src/main/java/srp/product_promotion.txt b/students/75939388/ood/src/main/java/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/75939388/ood/src/main/java/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From c478fdcd2695ce3651e10296007c97ac81ff7d59 Mon Sep 17 00:00:00 2001 From: macvis Date: Thu, 15 Jun 2017 10:18:25 +0800 Subject: [PATCH 107/214] datastructure demo code stage --- students/75939388/datastrure/pom.xml | 15 +++++++++++++ .../src/main/java/tree/BinaryTreeNode.java | 10 +++++++++ .../test/java/tree/BinaryTreeNodeTest.java | 22 +++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 students/75939388/datastrure/pom.xml create mode 100644 students/75939388/datastrure/src/main/java/tree/BinaryTreeNode.java create mode 100644 students/75939388/datastrure/src/test/java/tree/BinaryTreeNodeTest.java diff --git a/students/75939388/datastrure/pom.xml b/students/75939388/datastrure/pom.xml new file mode 100644 index 0000000000..b17b9fe45f --- /dev/null +++ b/students/75939388/datastrure/pom.xml @@ -0,0 +1,15 @@ + + + + season2 + learning2017 + 2.0-SEASON2 + + 4.0.0 + + datastrure + + + \ No newline at end of file diff --git a/students/75939388/datastrure/src/main/java/tree/BinaryTreeNode.java b/students/75939388/datastrure/src/main/java/tree/BinaryTreeNode.java new file mode 100644 index 0000000000..f0802fdceb --- /dev/null +++ b/students/75939388/datastrure/src/main/java/tree/BinaryTreeNode.java @@ -0,0 +1,10 @@ +package tree; + +/** + * Created by Tee on 2017/6/15. + */ +public class BinaryTreeNode { + /** + * 二叉树 + */ +} diff --git a/students/75939388/datastrure/src/test/java/tree/BinaryTreeNodeTest.java b/students/75939388/datastrure/src/test/java/tree/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..8de899c994 --- /dev/null +++ b/students/75939388/datastrure/src/test/java/tree/BinaryTreeNodeTest.java @@ -0,0 +1,22 @@ +package tree; + +import org.junit.Before; +import org.junit.Test; + +/** + * Created by Tee on 2017/6/15. + */ +public class BinaryTreeNodeTest { + + BinaryTreeNode tree; + + @Before + public void init(){ + tree = new BinaryTreeNode(); + } + + @Test + public void test1(){ + + } +} From 8f8bfce1102861533ea3094174a094b4fbf6a5b9 Mon Sep 17 00:00:00 2001 From: macvis Date: Thu, 15 Jun 2017 10:19:27 +0800 Subject: [PATCH 108/214] maven structure --- .../ood/src/test/java/srp/SrpTest.java | 23 ++++ students/75939388/pom.xml | 104 ++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 students/75939388/ood/src/test/java/srp/SrpTest.java create mode 100644 students/75939388/pom.xml diff --git a/students/75939388/ood/src/test/java/srp/SrpTest.java b/students/75939388/ood/src/test/java/srp/SrpTest.java new file mode 100644 index 0000000000..7882177794 --- /dev/null +++ b/students/75939388/ood/src/test/java/srp/SrpTest.java @@ -0,0 +1,23 @@ +package srp; + +import org.junit.Before; +import org.junit.Test; + +/** + * Created by Tee on 2017/6/15. + */ +public class SrpTest { + + @Before + public void init(){ + + } + + /** + * 重构后的代码尝试运行 + */ + @Test + public void runTrial(){ + + } +} diff --git a/students/75939388/pom.xml b/students/75939388/pom.xml new file mode 100644 index 0000000000..e5dd20227d --- /dev/null +++ b/students/75939388/pom.xml @@ -0,0 +1,104 @@ + + + 4.0.0 + + learning2017 + season2 + 2.0-SEASON2 + + + + datastrure + + ood + + + https://github.com/macvis/coding2017 + + 2017编程能力提高,第二季。 + teacher:刘欣 + gitHub: https://github.com/onlyliuxin/coding2017.git + student: TerrenceWen + + + + + TerrenceWen + https://github.com/macvis/ + macvis@126.com + + + + + 1.8 + 1.8 + UTF-8 + UTF-8 + 1.8 + 1.8 + UTF-8 + + + + + + aliyun + aliyun + http://maven.aliyun.com/nexus/content/groups/public + + true + never + + + false + + + + + + + + junit + junit + 4.12 + + + + + dom4j + dom4j + 1.6.1 + + + + + jaxen + jaxen + 1.1.6 + + + + + commons-io + commons-io + 2.5 + + + org.apache.commons + commons-lang3 + 3.5 + + + commons-codec + commons-codec + 1.10 + + + org.apache.commons + commons-collections4 + 4.1 + + + \ No newline at end of file From 9de8835189acb7317fe399f74a65ca9f7cc840ab Mon Sep 17 00:00:00 2001 From: macvis Date: Thu, 15 Jun 2017 10:26:13 +0800 Subject: [PATCH 109/214] add ignore file --- students/75939388/.gitignore | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 students/75939388/.gitignore diff --git a/students/75939388/.gitignore b/students/75939388/.gitignore new file mode 100644 index 0000000000..d3a81f6bdb --- /dev/null +++ b/students/75939388/.gitignore @@ -0,0 +1,10 @@ +target/ +.idea/ +ood/target/ +datastrure/target/ + +*.class +*.jar +*.war +*.zip +*.iml From dc47b0ccf65ecd6ba0600a9129cccc30e29c33da Mon Sep 17 00:00:00 2001 From: yyglider Date: Thu, 15 Jun 2017 11:18:59 +0800 Subject: [PATCH 110/214] update update --- .../main/java/season_1/code01/ArrayList.java | 138 ++++++++ .../main/java/season_1/code01/BinaryTree.java | 97 +++++ .../main/java/season_1/code01/Iterator.java | 7 + .../main/java/season_1/code01/LinkedList.java | 327 +++++++++++++++++ .../main/java/season_1/code01/List.java | 9 + .../main/java/season_1/code01/Queue.java | 24 ++ .../main/java/season_1/code01/Stack.java | 31 ++ .../main/java/season_1/code02/ArrayUtil.java | 257 ++++++++++++++ .../code02/litestruts/ActionConfig.java | 28 ++ .../code02/litestruts/Configuration.java | 64 ++++ .../code02/litestruts/LoginAction.java | 39 ++ .../code02/litestruts/ReflectionUtil.java | 120 +++++++ .../season_1/code02/litestruts/Struts.java | 76 ++++ .../java/season_1/code02/litestruts/View.java | 23 ++ .../season_1/code03/v1/DownloadThread.java | 51 +++ .../season_1/code03/v1/FileDownloader.java | 115 ++++++ .../season_1/code03/v1/api/Connection.java | 23 ++ .../code03/v1/api/ConnectionException.java | 9 + .../code03/v1/api/ConnectionManager.java | 10 + .../code03/v1/api/DownloadListener.java | 5 + .../code03/v1/impl/ConnectionImpl.java | 95 +++++ .../code03/v1/impl/ConnectionManagerImpl.java | 34 ++ .../season_1/code03/v2/DownloadThread.java | 52 +++ .../season_1/code03/v2/FileDownloader.java | 133 +++++++ .../season_1/code03/v2/api/Connection.java | 23 ++ .../code03/v2/api/ConnectionException.java | 8 + .../code03/v2/api/ConnectionManager.java | 10 + .../code03/v2/api/DownloadListener.java | 5 + .../code03/v2/impl/ConnectionImpl.java | 92 +++++ .../code03/v2/impl/ConnectionManagerImpl.java | 16 + .../java/season_1/code04/LRUPageFrame.java | 162 +++++++++ .../main/java/season_1/code05/Stack.java | 54 +++ .../main/java/season_1/code05/StackUtil.java | 148 ++++++++ .../main/java/season_1/code06/InfixExpr.java | 126 +++++++ .../java/season_1/code07/InfixToPostfix.java | 44 +++ .../java/season_1/code07/PostfixExpr.java | 47 +++ .../main/java/season_1/code07/PrefixExpr.java | 55 +++ .../main/java/season_1/code07/Token.java | 63 ++++ .../java/season_1/code07/TokenParser.java | 89 +++++ .../java/season_1/code08/CircleQueue.java | 93 +++++ .../main/java/season_1/code08/Josephus.java | 55 +++ .../main/java/season_1/code08/Queue.java | 61 ++++ .../season_1/code08/QueueWithTwoStacks.java | 66 ++++ .../java/season_1/code09/QuickMinStack.java | 40 +++ .../season_1/code09/StackWithTwoQueues.java | 42 +++ .../season_1/code09/TwoStackInOneArray.java | 152 ++++++++ .../java/season_1/code10/BinaryTreeNode.java | 39 ++ .../java/season_1/code10/BinaryTreeUtil.java | 125 +++++++ .../main/java/season_1/code10/FileList.java | 38 ++ .../season_1/code11/BinarySearchTree.java | 305 ++++++++++++++++ .../season_1/mini_jvm/attr/AttributeInfo.java | 19 + .../java/season_1/mini_jvm/attr/CodeAttr.java | 119 +++++++ .../season_1/mini_jvm/attr/ConstantValue.java | 21 ++ .../mini_jvm/attr/LineNumberTable.java | 69 ++++ .../mini_jvm/attr/LocalVariableTable.java | 98 +++++ .../season_1/mini_jvm/attr/StackMapTable.java | 30 ++ .../season_1/mini_jvm/clz/AccessFlag.java | 25 ++ .../java/season_1/mini_jvm/clz/ClassFile.java | 121 +++++++ .../season_1/mini_jvm/clz/ClassIndex.java | 19 + .../java/season_1/mini_jvm/cmd/BiPushCmd.java | 29 ++ .../mini_jvm/cmd/ByteCodeCommand.java | 158 +++++++++ .../season_1/mini_jvm/cmd/CommandParser.java | 149 ++++++++ .../season_1/mini_jvm/cmd/ComparisonCmd.java | 79 +++++ .../season_1/mini_jvm/cmd/GetFieldCmd.java | 37 ++ .../mini_jvm/cmd/GetStaticFieldCmd.java | 39 ++ .../season_1/mini_jvm/cmd/IncrementCmd.java | 39 ++ .../mini_jvm/cmd/InvokeSpecialCmd.java | 46 +++ .../mini_jvm/cmd/InvokeVirtualCmd.java | 85 +++++ .../java/season_1/mini_jvm/cmd/LdcCmd.java | 51 +++ .../season_1/mini_jvm/cmd/NewObjectCmd.java | 39 ++ .../season_1/mini_jvm/cmd/NoOperandCmd.java | 146 ++++++++ .../season_1/mini_jvm/cmd/OneOperandCmd.java | 29 ++ .../season_1/mini_jvm/cmd/PutFieldCmd.java | 44 +++ .../season_1/mini_jvm/cmd/TwoOperandCmd.java | 67 ++++ .../season_1/mini_jvm/constant/ClassInfo.java | 26 ++ .../mini_jvm/constant/ConstantInfo.java | 28 ++ .../mini_jvm/constant/ConstantPool.java | 25 ++ .../mini_jvm/constant/FieldRefInfo.java | 49 +++ .../mini_jvm/constant/MethodRefInfo.java | 52 +++ .../mini_jvm/constant/NameAndTypeInfo.java | 45 +++ .../mini_jvm/constant/NullConstantInfo.java | 12 + .../mini_jvm/constant/StringInfo.java | 25 ++ .../season_1/mini_jvm/constant/UTF8Info.java | 33 ++ .../mini_jvm/engine/ExecutionResult.java | 57 +++ .../mini_jvm/engine/ExecutorEngine.java | 77 ++++ .../java/season_1/mini_jvm/engine/Heap.java | 39 ++ .../season_1/mini_jvm/engine/JavaObject.java | 71 ++++ .../season_1/mini_jvm/engine/MethodArea.java | 90 +++++ .../season_1/mini_jvm/engine/MiniJVM.java | 30 ++ .../mini_jvm/engine/OperandStack.java | 26 ++ .../season_1/mini_jvm/engine/StackFrame.java | 126 +++++++ .../java/season_1/mini_jvm/field/Field.java | 64 ++++ .../mini_jvm/loader/ByteCodeIterator.java | 63 ++++ .../mini_jvm/loader/ClassFileLoader.java | 144 ++++++++ .../mini_jvm/loader/ClassFileParser.java | 159 +++++++++ .../java/season_1/mini_jvm/method/Method.java | 151 ++++++++ .../java/season_1/mini_jvm/util/Util.java | 22 ++ .../java/season_1/code01/ArrayListTest.java | 67 ++++ .../java/season_1/code01/BinaryTreeTest.java | 27 ++ .../java/season_1/code01/LinkedListTest.java | 174 +++++++++ .../test/java/season_1/code01/QueueTest.java | 24 ++ .../test/java/season_1/code01/StackTest.java | 27 ++ .../java/season_1/code02/ArrayUtilTest.java | 73 ++++ .../code02/litestruts/StrutsTest.java | 43 +++ .../season_1/code03/FileDownloaderTest.java | 60 ++++ .../season_1/code04/LRUPageFrameTest.java | 38 ++ .../java/season_1/code05/StackUtilTest.java | 79 +++++ .../java/season_1/code06/InfixExprTest.java | 49 +++ .../java/season_1/code07/PostfixExprTest.java | 42 +++ .../java/season_1/code07/PrefixExprTest.java | 45 +++ .../java/season_1/code08/JosephusTest.java | 32 ++ .../season_1/code09/QuickMinStackTest.java | 34 ++ .../code09/StackWithTwoQueuesTest.java | 27 ++ .../season_1/code10/BinaryTreeUtilTest.java | 79 +++++ .../season_1/code11/BinarySearchTreeTest.java | 93 +++++ .../mini_jvm/ClassFileloaderTest.java | 335 ++++++++++++++++++ .../java/season_1/mini_jvm/EmployeeV1.java | 28 ++ .../java/season_1/mini_jvm/EmployeeV2.java | 54 +++ .../test/java/season_1/mini_jvm/Example.java | 16 + .../season_1/mini_jvm/HourlyEmployee.java | 27 ++ .../java/season_1/mini_jvm/MiniJVMTest.java | 28 ++ students/769232552/season_two/pom.xml | 31 ++ .../src/main/java/work01/srp/DBUtil.java | 25 ++ .../src/main/java/work01/srp/Mail.java | 73 ++++ .../src/main/java/work01/srp/MailBox.java | 43 +++ .../java/work01/srp/MailBoxConfiguration.java | 62 ++++ .../src/main/java/work01/srp/Product.java | 28 ++ .../main/java/work01/srp/PromotionMail.java | 92 +++++ .../work01/srp/product_promotion.txt | 4 + 129 files changed, 8556 insertions(+) create mode 100644 students/769232552/season_one/main/java/season_1/code01/ArrayList.java create mode 100644 students/769232552/season_one/main/java/season_1/code01/BinaryTree.java create mode 100644 students/769232552/season_one/main/java/season_1/code01/Iterator.java create mode 100644 students/769232552/season_one/main/java/season_1/code01/LinkedList.java create mode 100644 students/769232552/season_one/main/java/season_1/code01/List.java create mode 100644 students/769232552/season_one/main/java/season_1/code01/Queue.java create mode 100644 students/769232552/season_one/main/java/season_1/code01/Stack.java create mode 100644 students/769232552/season_one/main/java/season_1/code02/ArrayUtil.java create mode 100644 students/769232552/season_one/main/java/season_1/code02/litestruts/ActionConfig.java create mode 100644 students/769232552/season_one/main/java/season_1/code02/litestruts/Configuration.java create mode 100644 students/769232552/season_one/main/java/season_1/code02/litestruts/LoginAction.java create mode 100644 students/769232552/season_one/main/java/season_1/code02/litestruts/ReflectionUtil.java create mode 100644 students/769232552/season_one/main/java/season_1/code02/litestruts/Struts.java create mode 100644 students/769232552/season_one/main/java/season_1/code02/litestruts/View.java create mode 100644 students/769232552/season_one/main/java/season_1/code03/v1/DownloadThread.java create mode 100644 students/769232552/season_one/main/java/season_1/code03/v1/FileDownloader.java create mode 100644 students/769232552/season_one/main/java/season_1/code03/v1/api/Connection.java create mode 100644 students/769232552/season_one/main/java/season_1/code03/v1/api/ConnectionException.java create mode 100644 students/769232552/season_one/main/java/season_1/code03/v1/api/ConnectionManager.java create mode 100644 students/769232552/season_one/main/java/season_1/code03/v1/api/DownloadListener.java create mode 100644 students/769232552/season_one/main/java/season_1/code03/v1/impl/ConnectionImpl.java create mode 100644 students/769232552/season_one/main/java/season_1/code03/v1/impl/ConnectionManagerImpl.java create mode 100644 students/769232552/season_one/main/java/season_1/code03/v2/DownloadThread.java create mode 100644 students/769232552/season_one/main/java/season_1/code03/v2/FileDownloader.java create mode 100644 students/769232552/season_one/main/java/season_1/code03/v2/api/Connection.java create mode 100644 students/769232552/season_one/main/java/season_1/code03/v2/api/ConnectionException.java create mode 100644 students/769232552/season_one/main/java/season_1/code03/v2/api/ConnectionManager.java create mode 100644 students/769232552/season_one/main/java/season_1/code03/v2/api/DownloadListener.java create mode 100644 students/769232552/season_one/main/java/season_1/code03/v2/impl/ConnectionImpl.java create mode 100644 students/769232552/season_one/main/java/season_1/code03/v2/impl/ConnectionManagerImpl.java create mode 100644 students/769232552/season_one/main/java/season_1/code04/LRUPageFrame.java create mode 100644 students/769232552/season_one/main/java/season_1/code05/Stack.java create mode 100644 students/769232552/season_one/main/java/season_1/code05/StackUtil.java create mode 100644 students/769232552/season_one/main/java/season_1/code06/InfixExpr.java create mode 100644 students/769232552/season_one/main/java/season_1/code07/InfixToPostfix.java create mode 100644 students/769232552/season_one/main/java/season_1/code07/PostfixExpr.java create mode 100644 students/769232552/season_one/main/java/season_1/code07/PrefixExpr.java create mode 100644 students/769232552/season_one/main/java/season_1/code07/Token.java create mode 100644 students/769232552/season_one/main/java/season_1/code07/TokenParser.java create mode 100644 students/769232552/season_one/main/java/season_1/code08/CircleQueue.java create mode 100644 students/769232552/season_one/main/java/season_1/code08/Josephus.java create mode 100644 students/769232552/season_one/main/java/season_1/code08/Queue.java create mode 100644 students/769232552/season_one/main/java/season_1/code08/QueueWithTwoStacks.java create mode 100644 students/769232552/season_one/main/java/season_1/code09/QuickMinStack.java create mode 100644 students/769232552/season_one/main/java/season_1/code09/StackWithTwoQueues.java create mode 100644 students/769232552/season_one/main/java/season_1/code09/TwoStackInOneArray.java create mode 100644 students/769232552/season_one/main/java/season_1/code10/BinaryTreeNode.java create mode 100644 students/769232552/season_one/main/java/season_1/code10/BinaryTreeUtil.java create mode 100644 students/769232552/season_one/main/java/season_1/code10/FileList.java create mode 100644 students/769232552/season_one/main/java/season_1/code11/BinarySearchTree.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/attr/AttributeInfo.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/attr/CodeAttr.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/attr/ConstantValue.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/attr/LineNumberTable.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/attr/LocalVariableTable.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/attr/StackMapTable.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/clz/AccessFlag.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/clz/ClassFile.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/clz/ClassIndex.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/BiPushCmd.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/ByteCodeCommand.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/CommandParser.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/ComparisonCmd.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/GetFieldCmd.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/GetStaticFieldCmd.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/IncrementCmd.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/InvokeSpecialCmd.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/InvokeVirtualCmd.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/LdcCmd.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/NewObjectCmd.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/NoOperandCmd.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/OneOperandCmd.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/PutFieldCmd.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/TwoOperandCmd.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/ClassInfo.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/ConstantInfo.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/ConstantPool.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/FieldRefInfo.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/MethodRefInfo.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/NameAndTypeInfo.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/NullConstantInfo.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/StringInfo.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/UTF8Info.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/engine/ExecutionResult.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/engine/ExecutorEngine.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/engine/Heap.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/engine/JavaObject.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/engine/MethodArea.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/engine/MiniJVM.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/engine/OperandStack.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/engine/StackFrame.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/field/Field.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/loader/ByteCodeIterator.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/loader/ClassFileLoader.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/loader/ClassFileParser.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/method/Method.java create mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/util/Util.java create mode 100644 students/769232552/season_one/test/java/season_1/code01/ArrayListTest.java create mode 100644 students/769232552/season_one/test/java/season_1/code01/BinaryTreeTest.java create mode 100644 students/769232552/season_one/test/java/season_1/code01/LinkedListTest.java create mode 100644 students/769232552/season_one/test/java/season_1/code01/QueueTest.java create mode 100644 students/769232552/season_one/test/java/season_1/code01/StackTest.java create mode 100644 students/769232552/season_one/test/java/season_1/code02/ArrayUtilTest.java create mode 100644 students/769232552/season_one/test/java/season_1/code02/litestruts/StrutsTest.java create mode 100644 students/769232552/season_one/test/java/season_1/code03/FileDownloaderTest.java create mode 100644 students/769232552/season_one/test/java/season_1/code04/LRUPageFrameTest.java create mode 100644 students/769232552/season_one/test/java/season_1/code05/StackUtilTest.java create mode 100644 students/769232552/season_one/test/java/season_1/code06/InfixExprTest.java create mode 100644 students/769232552/season_one/test/java/season_1/code07/PostfixExprTest.java create mode 100644 students/769232552/season_one/test/java/season_1/code07/PrefixExprTest.java create mode 100644 students/769232552/season_one/test/java/season_1/code08/JosephusTest.java create mode 100644 students/769232552/season_one/test/java/season_1/code09/QuickMinStackTest.java create mode 100644 students/769232552/season_one/test/java/season_1/code09/StackWithTwoQueuesTest.java create mode 100644 students/769232552/season_one/test/java/season_1/code10/BinaryTreeUtilTest.java create mode 100644 students/769232552/season_one/test/java/season_1/code11/BinarySearchTreeTest.java create mode 100644 students/769232552/season_one/test/java/season_1/mini_jvm/ClassFileloaderTest.java create mode 100644 students/769232552/season_one/test/java/season_1/mini_jvm/EmployeeV1.java create mode 100644 students/769232552/season_one/test/java/season_1/mini_jvm/EmployeeV2.java create mode 100644 students/769232552/season_one/test/java/season_1/mini_jvm/Example.java create mode 100644 students/769232552/season_one/test/java/season_1/mini_jvm/HourlyEmployee.java create mode 100644 students/769232552/season_one/test/java/season_1/mini_jvm/MiniJVMTest.java create mode 100644 students/769232552/season_two/pom.xml create mode 100644 students/769232552/season_two/src/main/java/work01/srp/DBUtil.java create mode 100644 students/769232552/season_two/src/main/java/work01/srp/Mail.java create mode 100644 students/769232552/season_two/src/main/java/work01/srp/MailBox.java create mode 100644 students/769232552/season_two/src/main/java/work01/srp/MailBoxConfiguration.java create mode 100644 students/769232552/season_two/src/main/java/work01/srp/Product.java create mode 100644 students/769232552/season_two/src/main/java/work01/srp/PromotionMail.java create mode 100644 students/769232552/season_two/src/main/resources/work01/srp/product_promotion.txt diff --git a/students/769232552/season_one/main/java/season_1/code01/ArrayList.java b/students/769232552/season_one/main/java/season_1/code01/ArrayList.java new file mode 100644 index 0000000000..6746de2a50 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code01/ArrayList.java @@ -0,0 +1,138 @@ +package code01; + +/** + * Created by yaoyuan on 2017/3/6. + */ +public class ArrayList implements List { + + private int max_size = 0;//总长度 + private int current_size = 0; //当前长度 + private float extendPercent = 2; //扩展系数 + + private Object[] elementData; + + /** + * 默认构造函数,初始化数组长度为100 + */ + public ArrayList(){ + this.elementData = new Object[100]; + this.max_size = 100; + } + /** + * 构造函数 + * @param size,初始化数组长度 + */ + public ArrayList(int size){ + this.elementData = new Object[size]; + this.max_size = size; + } + + /** + * 顺序添加元素,超出原始界限时,数组自动扩展 + */ + public void add(Object o) { + //如果越界了,需要复制原有的数组到扩充后的数组中 + if(this.current_size + 1 > this.max_size) { + this.max_size = (int) Math.floor(this.max_size * this.extendPercent); + this.elementData = copyToNew(this.elementData,this.max_size); + } + this.elementData[this.current_size] = o; + this.current_size ++; + + } + + /** + * 指定位置添加元素 + * 一种是在中间,一种是当前插入的位置尾部(如果超过尾部则默认添加到尾部) + */ + public void add(int index, Object o){ + assert(index>=0); + //如果越界了,需要复制原有的数组到扩充后的数组中 + if(this.current_size + 1 > this.max_size) { + //如果越界了,需要复制原有的数组到扩充后的数组中 + this.max_size = (int) Math.floor(this.max_size * this.extendPercent); + this.elementData = copyToNew(this.elementData,this.max_size); + } + //数组中间插入 + if(index < this.current_size){ + //需要把当前位置的元素往后移动 + for (int i = this.current_size - 1; i >= index; i--) { + this.elementData[i+1] = this.elementData[i]; + } + this.elementData[index] = o; + }else { + //后面加入 + this.elementData[this.current_size] = o; + } + this.current_size ++; + } + + public Object get(int index){ + if(index >= 0 && index <= this.current_size-1){ + return this.elementData[index]; + }else { + throw new ArrayIndexOutOfBoundsException(index); + } + } + + /** + * 删除指定位置的元素 + * @param index + * @return + */ + public Object remove(int index){ + Object result = null; + if(index >= 0 && index <= current_size-1){ + result = elementData[index]; + //删除操作 + if(index == current_size - 1){ + elementData[index] = null; + }else { + //需要把当前位置后面的元素往前移动 + for (int i = index; i < this.current_size-1 ; i++) { + this.elementData[i] = this.elementData[i+1]; + } + this.elementData[this.current_size-1] = null; + } + this.current_size --; + }else { + throw new ArrayIndexOutOfBoundsException(index); + } + return result; + } + + public int size(){ + return this.current_size; + } + + public Iterator iterator(){ + return new Iterator() { + int next_pos = 0; + int pos = -1; + public boolean hasNext() { + if(max_size <= 0){ + return false; + } + return next_pos < ArrayList.this.size(); + } + + public Object next() { + Object next = ArrayList.this.get(next_pos); + pos = next_pos ++; + return next; + } + public void remove(){ + ArrayList.this.remove(pos); + } + }; + } + + private Object[] copyToNew(Object[] oldArray, int extendSize){ + Object[] newArray = new Object[extendSize]; + for (int i = 0; i < size(); i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + +} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code01/BinaryTree.java b/students/769232552/season_one/main/java/season_1/code01/BinaryTree.java new file mode 100644 index 0000000000..b29fb960cb --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code01/BinaryTree.java @@ -0,0 +1,97 @@ +package code01; + +/** + * Created by yaoyuan on 2017/3/10. + */ +public class BinaryTree>{ + + private BinaryTreeNode root = null; + private int size = 0; + + public BinaryTreeNode createBinaryTree(T[] array){ + for(T data : array){ + this.insert(data); + } + return this.root; + } + + // recursive way, + // t is the node that roots the subtree. + public BinaryTreeNode insert(T data, BinaryTreeNode t){ + if(t == null){ + return new BinaryTreeNode(data); + } + int comparator = ((T) t.data).compareTo(data); + if(comparator > 0){ + t.left = insert(data,t.right); + }else if(comparator < 0){ + t.right = insert(data,t.left); + }else { + // do nothing + } + return t; + + } + + + //loop way + public void insert(T data){ + if(this.root == null){ + BinaryTreeNode node = new BinaryTreeNode(data); + this.root = node; + this.size ++; + return; + } + BinaryTreeNode cursor = this.root; + while (cursor != null){ + if(data.compareTo((T) cursor.data) <= 0){ + if(cursor.left == null) { + cursor.left = new BinaryTreeNode(data); + return; + } + cursor = cursor.left; + } + if(data.compareTo((T) cursor.data) > 0){ + if(cursor.right == null) { + cursor.right = new BinaryTreeNode(data); + return; + } + cursor = cursor.right; + } + } + this.size ++; + } + + public void leftOrderScan(BinaryTreeNode cursor){ + if(cursor == null){ + return; + } + leftOrderScan(cursor.left); + System.out.println(cursor.data.toString() + " "); + leftOrderScan(cursor.right); + } + + public BinaryTreeNode getRoot(){ + return this.root; + } + + class BinaryTreeNode { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(T data, BinaryTreeNode left, BinaryTreeNode right) { + this.left = right; + this.right = left; + this.data = data; + } + + public BinaryTreeNode(T data) { + this.left = null; + this.right = null; + this.data = data; + } + + } +} diff --git a/students/769232552/season_one/main/java/season_1/code01/Iterator.java b/students/769232552/season_one/main/java/season_1/code01/Iterator.java new file mode 100644 index 0000000000..b4074067bb --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code01/Iterator.java @@ -0,0 +1,7 @@ +package code01; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + public void remove(); +} diff --git a/students/769232552/season_one/main/java/season_1/code01/LinkedList.java b/students/769232552/season_one/main/java/season_1/code01/LinkedList.java new file mode 100644 index 0000000000..f7bbc970a9 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code01/LinkedList.java @@ -0,0 +1,327 @@ +package code01; + + +public class LinkedList implements List { + + private Node head; + private Node tail; //指向链表最后一个元素的引用 + + private int size; //总长度 + + public LinkedList() { + this.head = null; + this.tail = null; + this.size = 0; + } + + /** + * 新增顺序添加一个元素 + * @param o + */ + public void add(Object o){ + Node node = new Node(); + node.data = o; + node.next = null; + //空链表 + if(head == null){ + this.head = node; + this.tail = node; + }else { //非空链表,要先找到链表尾部,再加入新解答 + this.tail.next = node; + this.tail = node; + } + this.size ++; + } + + /** + * 指定索引处添加node + */ + public void add(int index, Object o) { + assert(index >= 0); + Node node = new Node(); + node.data = o; + node.next = null; + + if(index == 0){ + //添加在头部 + node.next = head; + head = node; + }else if(index >= this.size){ + //添加在尾部 + this.tail.next = node; + }else { + //添加在中间 + Node cursor = this.head; + for (int i = 0; i < index - 1; i++) { + cursor = cursor.next; + } + node.next = cursor.next; + cursor.next = node; + } + this.size ++; + } + + public Object get(int index){ + assert(index < this.size); + Node cursor = this.head; + for (int i = 0; i < index; i++) { + cursor = cursor.next; + } + return cursor.data; + } + + public Object remove(int index){ + assert(index >= 0 && index < this.size); + Object result = null; + //删除的是链表尾部的元素 + if(index == this.size - 1){ + Node cursor = this.head; + for (int i = 0; i < index - 1; i++) { + cursor = cursor.next; + } + result = cursor.next.data; + tail = cursor; + cursor.next = null; + }else if(index == 0){ + //删除的是头部元素 + result = head.data; + head = head.next; + }else { + //删除的是链表中间的元素 + Node cursor = this.head; + for (int i = 0; i < index - 1; i++) { + cursor = cursor.next; + } + result = cursor.next.data; + cursor.next = cursor.next.next; + } + this.size --; + return result; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + Node node = new Node(); + node.data = o; + node.next = null; + if(this.head == null){ + this.head = node; + this.tail = node; + }else { + node.next = head; + this.head = node; + } + this.size ++; + } + public void addLast(Object o){ + Node node = new Node(); + node.data = o; + node.next = null; + if(this.head == null){ + this.head = node; + this.tail = node; + }else { + this.tail.next = node; + this.tail = node; + } + this.size ++; + } + + public Object removeFirst(){ + Object first = null; + if(this.head != null){ + first = this.head.data; + head = head.next; + this.size --; + } + return first; + } + + public Object removeLast(){ + Object last = null; + if(this.tail != null){ + if(this.head != this.tail){ + Node cursor; + for (cursor = head;cursor.next!=tail;cursor=cursor.next); + last = this.tail.data; + this.tail = cursor; + this.tail.next = null; + }else { + last = this.tail.data; + this.head = null; + this.tail = null; + } + this.size --; + } + return last; + } + public Iterator iterator(){ + return null; + } + + /** + * 节点类 + */ + private static class Node{ + Object data; + Node next; + + } + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + if(this.head == null || this.head == this.tail){ + return; + } + + Node pre_cursor = null; + Node cursor = this.head; + Node after_cursor = cursor.next; + + while(cursor != null){ + cursor.next = pre_cursor; + pre_cursor = cursor; + cursor = after_cursor; + if(after_cursor != null){ + after_cursor = after_cursor.next; + } + } + + Node tmpNode = this.head; + this.head = this.tail; + this.tail = tmpNode; + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + if(this.head == null || this.head.next == null){ + return; + } + if(this.head.next.next == null){ + this.head = this.head.next; + } + + Node stepOne = this.head; + Node stepTwo = this.head; + + while (stepTwo.next != null){ + stepOne = stepOne.next; + stepTwo = stepTwo.next.next; + } + this.head = stepOne; + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + Node current = head; + Node firstHalf = null; + for (int k = 0; k < i; k ++){ + if(current == null){ + return; + } + firstHalf = current; //记录待删除节点的前一个节点 + current = current.next; + } + + //移动length长度 + for (int j = 0; j < length; j++) { + if(current == null){ + return; + } + current = current.next; + } + + if(i == 0){ + head = current; + }else { + firstHalf.next = current; + } + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + if(this.head == null){ + return; + } + Node current = this.head; + Node current_next = this.head; + while (current_next != null){ + current_next = current_next.next; //如果放到下个while循环后面写,就需要判断一次current_next是不是null了 + while(current_next != null && current_next.data.equals(current.data)){ + //删除重复节点 + current.next = current_next.next; + current_next = current_next.next; + } + current = current_next; + } + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + //怎么才能高效呢 + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + return null; + } + + /** + * 遍历列表 + */ + public void printList(){ + System.out.println(); + for (Node cursor = this.head;cursor!=null;cursor=cursor.next){ + System.out.print(cursor.data+" "); + } + } +} diff --git a/students/769232552/season_one/main/java/season_1/code01/List.java b/students/769232552/season_one/main/java/season_1/code01/List.java new file mode 100644 index 0000000000..95bc37d172 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code01/List.java @@ -0,0 +1,9 @@ +package code01; + +public interface List { + public void add(Object o); + public void add(int index, Object o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/students/769232552/season_one/main/java/season_1/code01/Queue.java b/students/769232552/season_one/main/java/season_1/code01/Queue.java new file mode 100644 index 0000000000..d9956deb9a --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code01/Queue.java @@ -0,0 +1,24 @@ +package code01; + +public class Queue { + + private LinkedList linkedList = new LinkedList(); + + public void enQueue(Object o){ + linkedList.addFirst(o); + } + + public Object deQueue(){ + Object result = linkedList.removeLast(); + return result; + } + + public boolean isEmpty(){ + return linkedList.size() == 0; + } + + public int size(){ + return linkedList.size(); + } + +} diff --git a/students/769232552/season_one/main/java/season_1/code01/Stack.java b/students/769232552/season_one/main/java/season_1/code01/Stack.java new file mode 100644 index 0000000000..dbaeb91a48 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code01/Stack.java @@ -0,0 +1,31 @@ +package code01; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + Object result = null; + if(elementData.size()!=0) { + result = elementData.remove(elementData.size() - 1); + } + return result; + } + + public Object peek(){ + Object result = null; + if(elementData.size()!=0) { + result = elementData.get(elementData.size() - 1); + } + return result; + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/students/769232552/season_one/main/java/season_1/code02/ArrayUtil.java b/students/769232552/season_one/main/java/season_1/code02/ArrayUtil.java new file mode 100644 index 0000000000..23055ef138 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code02/ArrayUtil.java @@ -0,0 +1,257 @@ +package code02; +import org.apache.commons.lang.ArrayUtils; +import java.util.ArrayList; +import java.util.List; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + if (origin == null || origin.length <= 1){ + return; + } + + int head = 0; + int tail = origin.length - 1; + int tmp; + while (head != tail){ + //调换位置 + tmp = origin[head]; + origin[head] = origin[tail]; + origin[tail] = tmp; + + head ++; + tail --; + } + + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + public int[] removeZero(int[] oldArray){ + if (oldArray == null || oldArray.length < 1){ + return null; + } + + List newList = new ArrayList(); + for(int number : oldArray){ + if(number != 0){ + newList.add(number); + } + } + + Integer[] result = new Integer[newList.size()]; + result = (Integer[]) newList.toArray(result); + return ArrayUtils.toPrimitive(result); + + + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + if(array1 == null && array2 == null){ + return null; + } + if(array1 == null){ + return array2; + } + if(array2 == null){ + return array1; + } + int[] newArray = new int[array1.length + array2.length]; + int m = 0,n = 0, k = 0; + while (m < array1.length && n < array2.length){ + if(array1[m] <= array2[n]){ + newArray[k++] = array1[m++]; + }else { + newArray[k++] = array2[n++]; + } + } + if(m >= array1.length){ + while (n < array2.length){ + newArray[k++] = array2[n++]; + } + } + if(n >= array2.length){ + while (m < array1.length){ + newArray[k++] = array1[m++]; + } + } + return newArray; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] newArray = new int[oldArray.length + size]; + int i = 0; + for (; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + for (int j = 0; j < size; j++){ + newArray[i++] = 0; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + //也就是需要生成一个小于max值的fibonacci数组 + public int[] fibonacci(int max){ + if(max < 2){ + return new int[]{}; + } + if(max < 3){ + return new int[]{1,1}; + } + List list = new ArrayList(); + list.add(0,1); + list.add(1,1); + int i=0; + while (list.get(i) + list.get(i+1) < max){ + list.add(i+2,list.get(i) + list.get(i+1)); + i++; + } + + int[] newArray = new int[list.size()]; + for (int j = 0; j < list.size(); j++) { + newArray[j] = list.get(j).intValue(); + } + return newArray; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + * + * 原理: + * 1,判断一个数字是否为素数,一个数 n 如果是合数,那么它的所有的因子不超过sqrt(n) + * 2,当i是素数的时候,i的所有的倍数必然是合数。 + */ + public int[] getPrimes(int max){ + + if(max <= 2){ + return null; + } + boolean[] prime = new boolean[max + 1]; + for (int i = 2; i <= max; i++) { + if(i%2 == 0){ + prime[i] = false; //偶数 + }else { + prime[i] = true; + } + } + + for (int i = 2; i <= Math.sqrt(max) ; i++) { + if(prime[i]){//奇数 + //如果i是素数,那么把i的倍数标记为非素数 + for(int j = i+i; j <= max; j += i){ + prime[j] = false; + } + } + } + + List num = new ArrayList(); + for (int i = 2; i <= max; i++) { + if(prime[i]){ + num.add(i); + } + } + + Integer[] result = new Integer[num.size()]; + result = (Integer[]) num.toArray(result); + return ArrayUtils.toPrimitive(result); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + + if(max < 6){ + return null; + } + + List perfectNumlist = new ArrayList(); + + for (int j = 6;j <= max; j++){ + List factorNumlist = new ArrayList(); + factorNumlist.add(1); + for (int i = 2; i < j; i++) { + if(j % i == 0){ + factorNumlist.add(i); + } + } + int sum = 0; + for(Integer num : factorNumlist){ + sum += num; + } + + if(sum == j){ + perfectNumlist.add(j); + } + } + Integer[] result = new Integer[perfectNumlist.size()]; + result = (Integer[]) perfectNumlist.toArray(result); + return ArrayUtils.toPrimitive(result); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < array.length - 1; i++) { + sb.append(array[i]); + sb.append(seperator); + } + sb.append(array[array.length - 1]); + return sb.toString(); + } + + public void printArr(int[] array){ + for(int num : array){ + System.out.print(num + " "); + } + } + +} diff --git a/students/769232552/season_one/main/java/season_1/code02/litestruts/ActionConfig.java b/students/769232552/season_one/main/java/season_1/code02/litestruts/ActionConfig.java new file mode 100644 index 0000000000..b5e077e7a5 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code02/litestruts/ActionConfig.java @@ -0,0 +1,28 @@ +package code02.litestruts; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by yaoyuan on 2017/3/22. + */ +public class ActionConfig { + String name; + String clzName; + Map viewResult = new HashMap(); + + + public ActionConfig(String actionName, String clzName) { + this.name = actionName; + this.clzName = clzName; + } + public String getClassName(){ + return clzName; + } + public void addViewResult(String name, String viewName){ + viewResult.put(name, viewName); + } + public String getViewName(String resultName){ + return viewResult.get(resultName); + } +} diff --git a/students/769232552/season_one/main/java/season_1/code02/litestruts/Configuration.java b/students/769232552/season_one/main/java/season_1/code02/litestruts/Configuration.java new file mode 100644 index 0000000000..85d3d98a1f --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code02/litestruts/Configuration.java @@ -0,0 +1,64 @@ +package code02.litestruts; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +/** + * Created by yaoyuan on 2017/3/21. + */ +public class Configuration { + + + private String path; + private final Map actionMap = new HashMap(); + + Configuration(String path){ + parseXML(path); + } + + //解析xml文件 + private void parseXML(String path){ + //读取文件 + File file = new File(path); + SAXReader reader = new SAXReader(); + Document document = null; + try { + document = reader.read(file); + } catch (DocumentException e) { + e.printStackTrace(); + } + Element root = document.getRootElement(); + + for (Iterator iterator = root.elementIterator("action"); iterator.hasNext();) { + Element e = iterator.next(); + String actionName = e.attributeValue("name"); + String clazName = e.attributeValue("class"); + ActionConfig actionConfig = new ActionConfig(actionName,clazName); + for(Iterator childIterator = e.elementIterator();childIterator.hasNext();){ + Element child = childIterator.next(); + String jspKey = child.attributeValue("name"); + String jspValue = child.getTextTrim(); + actionConfig.addViewResult(jspKey,jspValue); + } + actionMap.put(actionName,actionConfig); + } + } + + public String getView(String actionName, String result){ + String jspKey = actionName + "." + result; + return actionMap.get(actionName).getViewName(result); + } + + + public Map getActionMap() { + return actionMap; + } + +} diff --git a/students/769232552/season_one/main/java/season_1/code02/litestruts/LoginAction.java b/students/769232552/season_one/main/java/season_1/code02/litestruts/LoginAction.java new file mode 100644 index 0000000000..0799eae71a --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code02/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package code02.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/students/769232552/season_one/main/java/season_1/code02/litestruts/ReflectionUtil.java b/students/769232552/season_one/main/java/season_1/code02/litestruts/ReflectionUtil.java new file mode 100644 index 0000000000..c4a8ed34fc --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code02/litestruts/ReflectionUtil.java @@ -0,0 +1,120 @@ +package code02.litestruts; + +import org.slf4j.LoggerFactory; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by yaoyuan on 2017/3/21. + */ +public class ReflectionUtil { + private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ReflectionUtil.class); + + private static final Map> clazzMap = new HashMap>(); + + //加载xml文件中的类 + public void initiateClazz(Configuration cfg){ + Map actionMap = cfg.getActionMap(); + + for (Map.Entry entry : actionMap.entrySet()) { + String actionName = entry.getKey(); //login + ActionConfig actionConfig =entry.getValue(); + String className = actionConfig.getClassName(); //code02.litestruts.LoginAction + Class cls; + try { + cls = Class.forName(className, true, Thread.currentThread().getContextClassLoader()); + clazzMap.put(actionName,cls); + } catch (Exception e) { + logger.warn("加载类 " + className + "出错!"); + } + } + + } + + + //返回实例对象 + public Object getInstance(String actionName){ + Object instance = null; + for (Map.Entry> entry : clazzMap.entrySet()) { + String action = entry.getKey(); //login + Class cls = entry.getValue(); //code02.litestruts.LoginAction.class + if(actionName.equals(action)){ + try { + instance = cls.newInstance(); + } catch (Exception e) { + logger.error("生成实例出错!", e); + throw new RuntimeException(e); + } + } + } + return instance; + } + + + //参数赋值 + public void setParameters(Object o, Map params) { + + List methods = getSetterMethods(o.getClass()); + for (String name : params.keySet()) { + String methodName = "set" + name; + for (Method m : methods) { + if (m.getName().equalsIgnoreCase(methodName)) { + try { + m.invoke(o, params.get(name)); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } + } + } + + //运行无参方法 + public Object runMethodWithoutParams(Object o , String methodName){ + Class clz = o.getClass(); + Object result = null; + try { + Method method = clz.getDeclaredMethod(methodName); + try { + result = method.invoke(o); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + return result; + } + + //返回以set开头的方法 + public List getSetterMethods(Class clz){ + return getMethods(clz,"set"); + } + + //返回以get开头的方法 + public List getGetterMethods(Class clz){ + return getMethods(clz,"get"); + } + + private List getMethods(Class clz, String startWithName){ + List methodsList = new ArrayList(); + Method[] methods = clz.getDeclaredMethods(); + for (int i = 0; i < methods.length; i++) { + String methodName = methods[i].getName(); + if(methodName.startsWith(startWithName)){ + methodsList.add(methods[i]); + } + } + return methodsList; + } + +} diff --git a/students/769232552/season_one/main/java/season_1/code02/litestruts/Struts.java b/students/769232552/season_one/main/java/season_1/code02/litestruts/Struts.java new file mode 100644 index 0000000000..de2c22ea94 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code02/litestruts/Struts.java @@ -0,0 +1,76 @@ +package code02.litestruts; + +import org.slf4j.LoggerFactory; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +public class Struts { + private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Struts.class); + /* + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + + + + */ + public static View runAction(String actionName, Map parameters) { + View view = new View(); + Configuration cfg = new Configuration("src/main/resources/struts.xml"); + ReflectionUtil reflectionUtil = new ReflectionUtil(); + reflectionUtil.initiateClazz(cfg); + /* 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象)*/ + Object o = reflectionUtil.getInstance(actionName); + /*2. 根据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") ,那就应该调用 setName和setPassword方法*/ + reflectionUtil.setParameters(o,parameters); + /*3. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success"*/ + String result = (String) reflectionUtil.runMethodWithoutParams(o,"execute"); + /* 4. 通过反射找到对象的所有getter方法(例如 getMessage),通过反射来调用, + 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} ,放到View对象的parameters*/ + Map params = new HashMap(); + List methods = reflectionUtil.getGetterMethods(o.getClass()); + for(Method method : methods){ + String key = method.getName().substring(3); + String value = null; + try { + value = (String) method.invoke(o); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + params.put(key,value); + } + /*5. 根据struts.xml中的 配置,以及execute的返回值,确定哪一个jsp,放到View对象的jsp字段中。*/ + String jsp = cfg.getView(actionName,result); + view.setParameters(params); + view.setJsp(jsp); + + return view; + } + + public static void main(String[] args) throws InvocationTargetException, IllegalAccessException { + + String actionName = "login"; + HashMap params = new HashMap(); + params.put("name","test"); + params.put("password","12345"); + + View view = Struts.runAction(actionName,params); + System.out.println(view.getJsp()); + System.out.println(view.getParameters()); + + + } + +} diff --git a/students/769232552/season_one/main/java/season_1/code02/litestruts/View.java b/students/769232552/season_one/main/java/season_1/code02/litestruts/View.java new file mode 100644 index 0000000000..c7e630587c --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code02/litestruts/View.java @@ -0,0 +1,23 @@ +package code02.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/students/769232552/season_one/main/java/season_1/code03/v1/DownloadThread.java b/students/769232552/season_one/main/java/season_1/code03/v1/DownloadThread.java new file mode 100644 index 0000000000..f91bff3bf1 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code03/v1/DownloadThread.java @@ -0,0 +1,51 @@ +package code03.v1; + +import code03.v1.api.Connection; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CountDownLatch; + +/** + * 定义线程类 + */ + + +public class DownloadThread extends Thread{ + + private static final Logger logger = LoggerFactory.getLogger(DownloadThread.class); + + private Connection conn; + private int startPos; + private int endPos; + private CountDownLatch finished; + private String fileName; + + + public DownloadThread(Connection conn, int startPos, int endPos, CountDownLatch finished,String fileName){ + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.finished = finished; + this.fileName = fileName; + } + + @Override + public void run(){ + logger.debug("thread {} begin to download from start {} to end {} ",Thread.currentThread().getName(),startPos,endPos); + + try { + byte[] data = conn.read(startPos,endPos); + RandomAccessFile rfile = new RandomAccessFile(fileName,"rw"); + rfile.seek(startPos); + rfile.write(data); + rfile.close(); + } catch (IOException e) { + e.printStackTrace(); + } + finished.countDown(); + logger.debug("thread {} end to download from start {} to end {} ",Thread.currentThread().getName(),startPos,endPos); + } +} diff --git a/students/769232552/season_one/main/java/season_1/code03/v1/FileDownloader.java b/students/769232552/season_one/main/java/season_1/code03/v1/FileDownloader.java new file mode 100644 index 0000000000..542fd978c2 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code03/v1/FileDownloader.java @@ -0,0 +1,115 @@ +package code03.v1; + +import code03.v1.api.Connection; +import code03.v1.api.ConnectionException; +import code03.v1.api.ConnectionManager; +import code03.v1.api.DownloadListener; +import code03.v1.impl.ConnectionManagerImpl; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; + +/** + * 线程启动类 + */ + +public class FileDownloader { + private final static int THREAD_NUM = 5; + + private static final String fileHolder = "D:/test.png"; + private String url; + private DownloadListener listener; + private ConnectionManager cm; + private static boolean downloadFinished = false; + + static final CountDownLatch finished = new CountDownLatch(THREAD_NUM); //v2的版本中使用了CyclicBarrier方式 + + + public FileDownloader(String _url) { + this.url = _url; + } + + /* + (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。*/ + public void execute(){ + Connection conn = null; + try { + //启动线程 + int startPos = 0, endPos = 0; + List threads = new ArrayList(); + for (int i = 0; i < THREAD_NUM; i++) { + conn = cm.open(this.url); //每次都要重新获取一个connection.imputstream + int length = conn.getContentLength(); + startPos = length/THREAD_NUM * i; + endPos = length/THREAD_NUM * (i + 1)- 1; + DownloadThread downloadThread = new DownloadThread(conn,startPos,endPos,finished, fileHolder); + threads.add(downloadThread); + downloadThread.start(); + } + finished.await(); + //调用join方法,确保所有线程的工作已经完成 + /*for (int i = 0; i < THREAD_NUM; i++) { + try { + threads.get(i).join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }*/ + listener.notifyFinished(); + } catch (ConnectionException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + if(conn != null){ + conn.close(); + } + } + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + + + public static void main(String[] args) { + + String url = "http://litten.me/assets/blogImg/litten.png"; + FileDownloader fileDownloader = new FileDownloader(url); + ConnectionManager cm = new ConnectionManagerImpl(); + fileDownloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + }); + fileDownloader.setConnectionManager(cm); + fileDownloader.execute(); + + + while (!downloadFinished){ + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("download finished ! "); + + + } + +} diff --git a/students/769232552/season_one/main/java/season_1/code03/v1/api/Connection.java b/students/769232552/season_one/main/java/season_1/code03/v1/api/Connection.java new file mode 100644 index 0000000000..92a3d4725a --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code03/v1/api/Connection.java @@ -0,0 +1,23 @@ +package code03.v1.api; + +import java.io.IOException; + +public interface Connection { + /** + * 给定开始和结束位置, 读取数据, 返回值是字节数组 + * @param startPos 开始位置, 从0开始 + * @param endPos 结束位置 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 得到数据内容的长度 + * @return + */ + public int getContentLength(); + + /** + * 关闭连接 + */ + public void close(); +} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code03/v1/api/ConnectionException.java b/students/769232552/season_one/main/java/season_1/code03/v1/api/ConnectionException.java new file mode 100644 index 0000000000..bee02c3717 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code03/v1/api/ConnectionException.java @@ -0,0 +1,9 @@ +package code03.v1.api; + +public class ConnectionException extends Exception { + + public ConnectionException(String message,Throwable e){ + super(message,e); + } + +} diff --git a/students/769232552/season_one/main/java/season_1/code03/v1/api/ConnectionManager.java b/students/769232552/season_one/main/java/season_1/code03/v1/api/ConnectionManager.java new file mode 100644 index 0000000000..4ec1b87667 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code03/v1/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package code03.v1.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/students/769232552/season_one/main/java/season_1/code03/v1/api/DownloadListener.java b/students/769232552/season_one/main/java/season_1/code03/v1/api/DownloadListener.java new file mode 100644 index 0000000000..8cd24bfd57 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code03/v1/api/DownloadListener.java @@ -0,0 +1,5 @@ +package code03.v1.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/students/769232552/season_one/main/java/season_1/code03/v1/impl/ConnectionImpl.java b/students/769232552/season_one/main/java/season_1/code03/v1/impl/ConnectionImpl.java new file mode 100644 index 0000000000..75c91bb5bb --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code03/v1/impl/ConnectionImpl.java @@ -0,0 +1,95 @@ +package code03.v1.impl; + +import code03.v1.api.Connection; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URLConnection; + + +public class ConnectionImpl implements Connection{ + + private static final Logger logger = LoggerFactory.getLogger(ConnectionImpl.class); + + + private URLConnection urlConnection; + private int length = -1; + + + public ConnectionImpl(URLConnection urlConnection){ + this.urlConnection = urlConnection; + } + + /** + * 读取urlConnection.getInputStream()中的数据,返回byte[] + */ + @Override + public byte[] read(int startPos, int endPos) throws IOException { + int contentLength = getContentLength(); + if(startPos < 0 || endPos > contentLength || contentLength <= 0){ + logger.info("index out of range !"); + return null; + } + + InputStream raw = null; + BufferedInputStream in = null; + int size = endPos - startPos + 1; + byte[] data = new byte[size]; + try{ + raw = urlConnection.getInputStream(); + in = new BufferedInputStream(raw); + in.skip(startPos); + + int offset = 0; + while(offset < size){ + int bytesRead = in.read(data, offset, size - offset); + while (bytesRead == -1){break;} + offset += bytesRead; + } + //用这种方式比较好 + /* + int BUFFER_SIZE = 1024; + byte[] buff = new byte[BUFFER_SIZE]; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + while(baos.size() < size){ + int bytesRead = in.read(buff); //缓存读取的数据,其SIZE大小不一定要等于总的SIZE + if(bytesRead<0) break; + baos.write(buff,0,bytesRead); + } + byte[] data = baos.toByteArray(); + Arrays.copyOf(data,size); + */ + + } catch (IOException e) { + e.printStackTrace(); + }finally { + raw.close(); + in.close(); + } + return data; + } + + @Override + public int getContentLength() { + if(length != -1){ + return length; + } + length = urlConnection.getContentLength(); + //if without content-length header + if(length == -1) { + throw new RuntimeException("content-length error"); + } + return length; + } + + @Override + public void close() { + if(urlConnection != null){ + urlConnection = null; + } + } + +} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code03/v1/impl/ConnectionManagerImpl.java b/students/769232552/season_one/main/java/season_1/code03/v1/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..b4c9a1b90d --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code03/v1/impl/ConnectionManagerImpl.java @@ -0,0 +1,34 @@ +package code03.v1.impl; + +import code03.v1.api.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +/** + * 获取Connection实例 + */ + +public class ConnectionManagerImpl implements ConnectionManager { + private static final Logger logger = LoggerFactory.getLogger(ConnectionManagerImpl.class); + + @Override + public Connection open(String url) throws ConnectionException { + Connection connection = null; + try { + URL _url = new URL(url); + URLConnection urlConnection = _url.openConnection(); + connection = new ConnectionImpl(urlConnection); + } catch (MalformedURLException e) { + logger.error("url {} format error",url); + } catch (IOException e) { + e.printStackTrace(); + } + return connection; + } + +} diff --git a/students/769232552/season_one/main/java/season_1/code03/v2/DownloadThread.java b/students/769232552/season_one/main/java/season_1/code03/v2/DownloadThread.java new file mode 100644 index 0000000000..54cc1d3d0b --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code03/v2/DownloadThread.java @@ -0,0 +1,52 @@ +package code03.v2; + +import code03.v2.api.Connection; + +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + CyclicBarrier barrier; + String localFile; + public DownloadThread( Connection conn, int startPos, int endPos, String localFile, CyclicBarrier barrier){ + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.localFile = localFile; + this.barrier = barrier; + } + + + + public void run(){ + + + try { + System.out.println("Begin to read [" + startPos +"-"+endPos+"]"); + + byte[] data = conn.read(startPos, endPos); + + RandomAccessFile file = new RandomAccessFile(localFile,"rw"); + + file.seek(startPos); + + file.write(data); + + file.close(); + + conn.close(); + + barrier.await(); //等待别的线程完成 + + } catch (Exception e) { + e.printStackTrace(); + + } + + } +} diff --git a/students/769232552/season_one/main/java/season_1/code03/v2/FileDownloader.java b/students/769232552/season_one/main/java/season_1/code03/v2/FileDownloader.java new file mode 100644 index 0000000000..12c750145f --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code03/v2/FileDownloader.java @@ -0,0 +1,133 @@ +package code03.v2; + +import code03.v2.api.Connection; +import code03.v2.api.ConnectionManager; +import code03.v2.api.DownloadListener; + +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + + + +public class FileDownloader { + + private String url; + private String localFile; + + DownloadListener listener; + + ConnectionManager cm; + + + private static final int DOWNLOAD_TRHEAD_NUM = 3; + + public FileDownloader(String _url, String localFile) { + this.url = _url; + this.localFile = localFile; + + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + + CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_TRHEAD_NUM , new Runnable(){ + public void run(){ + listener.notifyFinished(); + } + }); + + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + + createPlaceHolderFile(this.localFile, length); + + int[][] ranges = allocateDownloadRange(DOWNLOAD_TRHEAD_NUM, length); + + for(int i=0; i< DOWNLOAD_TRHEAD_NUM; i++){ + + + DownloadThread thread = new DownloadThread( + cm.open(url), + ranges[i][0], + ranges[i][1], + localFile, + barrier); + thread.start(); + } + + } catch (Exception e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + + } + + private void createPlaceHolderFile(String fileName, int contentLen) throws IOException{ + + RandomAccessFile file = new RandomAccessFile(fileName,"rw"); + + for(int i=0; i totalLen){ + byte[] data = baos.toByteArray(); + return Arrays.copyOf(data, totalLen); + } + + return baos.toByteArray(); + } + + @Override + public int getContentLength() { + + URLConnection con; + try { + con = url.openConnection(); + + return con.getContentLength(); + + } catch (IOException e) { + e.printStackTrace(); + } + + return -1; + + + } + + @Override + public void close() { + + + } + +} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code03/v2/impl/ConnectionManagerImpl.java b/students/769232552/season_one/main/java/season_1/code03/v2/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..596beaa191 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code03/v2/impl/ConnectionManagerImpl.java @@ -0,0 +1,16 @@ +package code03.v2.impl; + + +import code03.v2.api.Connection; +import code03.v2.api.ConnectionException; +import code03.v2.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + + return new ConnectionImpl(url); + } + +} diff --git a/students/769232552/season_one/main/java/season_1/code04/LRUPageFrame.java b/students/769232552/season_one/main/java/season_1/code04/LRUPageFrame.java new file mode 100644 index 0000000000..0ffce58b83 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code04/LRUPageFrame.java @@ -0,0 +1,162 @@ +package code04; + +/** + * 用双向链表实现LRU算法 + */ +public class LRUPageFrame { + + private class Node { + + Node prev; + Node next; + int pageNum; + Node() { + } + } + + private int size; + private int capacity; + + private Node first;// 链表头 + private Node last;// 链表尾 + + public LRUPageFrame(int capacity) { + this.capacity = capacity; + this.size = 0; + } + + /** + * 获取缓存中对象 + * 1、如果缓存中不存在,则直接加入表头 + * 2、如果缓存中存在,则把该对象移动到表头 + * @param pageNum + * @return + */ + public void access(int pageNum) { + Node node = hasContains(pageNum); + if(node != null){ + moveToHead(node); + }else { + addToHead(pageNum); + } + + } + + /** + * 对象是否存在缓存中,如存在则返回该对象在链表中的位置,否则返回null + * @return + */ + private Node hasContains(int key){ + Node node = this.first; + while (node != null){ + if(node.pageNum == key){ + return node; + } + node = node.next; + } + return node; + } + + /** + * 对象加入表头,先先判断缓存是否已经满了 + * @return + */ + private void addToHead(int key){ + Node node = new Node(); + node.pageNum = key; + if(size < capacity){ + addFirst(node); + }else { + removeLast(); + addFirst(node); + } + + } + + + /** + * 对象移动到表头 + * @return + */ + private void moveToHead(Node node){ + if(node == first){ + return; + } + if(node == last){ + node.next = first; + first.prev = node; + first = node; + last = node.prev; + last.next = null; + node.prev = null; + return; + } + node.prev.next = node.next; + node.next.prev = node.prev; + node.next = first; + node.prev = null; + first.prev = node; + first = node; + } + + /** + * 删除表尾 + * @return + */ + private void removeLast(){ + if(last != null){ + Node newLast = last.prev; + last.prev = null; + last = newLast; + last.next = null; + size --; + } + } + /** + * 添加元素到表头 + * @return + */ + private void addFirst(Node node){ + //0个节点 + if(first == null){ + first = node; + last = node; + size ++; + return; + } + //一个节点 + else if(first == last){ + first = node; + first.next = last; + last.prev = first; + size ++; + return; + }else { + node.next = first; + first.prev = node; + first = node; + size ++; + } + } + /** + * 当前链表空间 + * @return + */ + public int getSize() { + return size; + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + Node node = first; + while(node != null){ + buffer.append(node.pageNum); + node = node.next; + if(node != null){ + buffer.append(","); + } + } + return buffer.toString(); + } + +} diff --git a/students/769232552/season_one/main/java/season_1/code05/Stack.java b/students/769232552/season_one/main/java/season_1/code05/Stack.java new file mode 100644 index 0000000000..04e1f1aebb --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code05/Stack.java @@ -0,0 +1,54 @@ +package code05; + +import code01.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + if(o == null){ + return; + } + elementData.add(o); + } + + + public Object pop(){ + Object last = null; + int last_index = elementData.size() - 1; + if(last_index >= 0){ + last = elementData.get(last_index); + elementData.remove(last_index); + } + return last; + } + + public Object peek(){ + Object last = null; + int last_index = elementData.size() - 1 ; + if(last_index >= 0){ + last = elementData.get(last_index); + } + return last; + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } + + @Override + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append("["); + int i = 0; + for (; i < size() - 1; i++) { + sb.append(elementData.get(i)); + sb.append(", "); + } + sb.append(elementData.get(i)); + sb.append("]"); + return sb.toString(); + } +} diff --git a/students/769232552/season_one/main/java/season_1/code05/StackUtil.java b/students/769232552/season_one/main/java/season_1/code05/StackUtil.java new file mode 100644 index 0000000000..fb9e90631a --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code05/StackUtil.java @@ -0,0 +1,148 @@ +package code05; + +public class StackUtil { + + + /** + * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + */ + + /** + * 此处是传值,将原有栈的地址 复制给 变量 s, s在函数内相当于局部变量,因此 s 重新赋值并没有什么用 + * @param s + */ + public static void bad_reverse(Stack s) { + if(s == null){ + return; + } + // need a new stack + Stack newStack = new Stack(); + while (s.peek() != null){ + newStack.push(s.pop()); + } + s = newStack; + } + + + public static void reverse(Stack s) { + if(s == null || s.isEmpty()){ + return; + } + + Stack tmp = new Stack(); + while(!s.isEmpty()){ + tmp.push(s.pop()); + } + while(!tmp.isEmpty()){ + int top = (Integer) tmp.pop(); + addToBottom(s,top);//加入到原来栈的栈底 + } + + + } + + public static void addToBottom(Stack s, int value){ + if(s.isEmpty()){ + s.push(value); + } else{ + int top = (Integer) s.pop(); + addToBottom(s,value); + s.push(top); + } + + } + + /** + * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param o + */ + public static void remove(Stack s,Object o) { + if(s == null || o == null){ + return; + } + Stack newStack = new Stack(); + while (s.peek() != null){ + Object e = s.pop(); + if(!e.equals(o)){ + newStack.push(e); + } + } + while (newStack.peek() != null){ + s.push(newStack.pop()); + } + } + + /** + * 从栈顶取得len个元素, 原来的栈中元素保持不变 + * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 + * @param len + * @return + */ + public static Object[] getTop(Stack s,int len) throws Exception { + if(s == null || s.isEmpty() || s.size() < len || len <= 0){ + return null; + } + Stack newStack = new Stack(); + for (int i = 0; i < len; i++) { + Object o = s.pop(); + newStack.push(o); + } + Object[] objects = new Object[len]; + for (int i = len - 1; i >= 0; i--) { + Object o = newStack.pop(); + s.push(o); + objects[i] = o; + } + return objects; + } + /** + * 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz + * 使用堆栈检查字符串s中的括号是不是成对出现的。 + * 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true + * 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; + * @param s + * @return + */ + public static boolean isValidPairs(String s){ + char tag_1_start = '{'; + char tag_1_end = '}'; + char tag_2_start = '['; + char tag_2_end = ']'; + char tag_3_start = '('; + char tag_3_end = ')'; + + int length = s.length(); + Stack stack = new Stack(); + for (int i = 0; i < length; i++) { + char c = s.charAt(i); + if(c == tag_1_start || c == tag_2_start || c == tag_3_start){ + stack.push(c); + } + else if(c == tag_1_end){ + //pop all element after tag_1_start + Object t= stack.pop(); + if(!t.equals(tag_1_start)){ + return false; + } + } + else if(c == tag_2_end){ + //pop all element after tag_1_start + Object t= stack.pop(); + if(!t.equals(tag_2_start)){ + return false; + } + } + else if(c == tag_3_end){ + //pop all element after tag_1_start + Object t= stack.pop(); + if(!t.equals(tag_3_start)){ + return false; + } + } + } + return stack.size() == 0; + } + + +} diff --git a/students/769232552/season_one/main/java/season_1/code06/InfixExpr.java b/students/769232552/season_one/main/java/season_1/code06/InfixExpr.java new file mode 100644 index 0000000000..a13e190091 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code06/InfixExpr.java @@ -0,0 +1,126 @@ +package code06; + +import code05.Stack; + +import java.util.ArrayList; +import java.util.List; + +public class InfixExpr { + String expr = null; + + public InfixExpr(String expr) { + this.expr = expr; + } + + private float calc(float a, float b, String ops){ + float result = 0.0f; + if (ops.equals("+")) { + return b + a; + } + if (ops.equals("-")) { + return b - a; + } + if (ops.equals("*")) { + return b * a; + } + if (ops.equals("/")) { + return b / a; + } + return result; + } + + private boolean isOperator(String ch){ + boolean isOperator = (ch.equals("+") || ch.equals("-") || ch.equals("*") || ch.equals("/")); + return isOperator; + } + + //parse string to list + private List parser(){ + char[] chs = expr.toCharArray(); + List values = new ArrayList(); + int currentCharPos = 0; // 当前字符的位置 + while (currentCharPos < chs.length) { + String currentStr = String.valueOf(chs[currentCharPos]); + if(isOperator(currentStr)){ + values.add(currentStr); + currentCharPos ++; + } + else { + int numberOffset = 0; + while (currentCharPos + numberOffset < chs.length && !isOperator(String.valueOf(chs[currentCharPos+numberOffset]))){ + numberOffset ++ ; + } + if(numberOffset == 1){ + values.add(currentStr); + currentCharPos++; + }else { + String number = new String(chs,currentCharPos,numberOffset); + values.add(number); + currentCharPos = currentCharPos + numberOffset; + } + } + } + return values; + } + + private int morePriority(String peek, String e) { + boolean hasMorePriority = ((e.equals("+") || e.equals("-")) && (peek.equals("*") || peek.equals("/"))); + boolean hasLessPriority = ((e.equals("*") || e.equals("/")) && (peek.equals("+") || peek.equals("-"))); + if(hasMorePriority) { + return 1; + } + if(hasLessPriority){ + return -1; + } + return 0; + } + + public float evaluate() { + float result = 0.0f; + + Stack numberStack = new Stack(); + Stack opsStack = new Stack(); + + List elements = this.parser(); + for(String e : elements){ + if(!isOperator(e)){ //数字直接入栈 + float number = Float.valueOf(e); + numberStack.push(number); + }else {//操作符 + if(opsStack.isEmpty()){ + opsStack.push(e); + }else { + //栈顶符号有着相等或者更高的优先级 + if(morePriority((String) opsStack.peek(),e) >= 0){ + float a = (Float) numberStack.pop(); + float b = (Float) numberStack.pop(); + String ops = (String) opsStack.pop(); + + float value = calc(a,b,ops); + + numberStack.push(value); + opsStack.push(e); + }else { + opsStack.push(e); + } + } + } + } + while (!opsStack.isEmpty()) { + float a = (Float) numberStack.pop(); + float b = (Float) numberStack.pop(); + String ops = (String) opsStack.pop(); + float value = calc(a, b, ops); + numberStack.push(value); + } + + result = (Float) numberStack.pop(); + return result; + } + + public static void main(String[] args) { + InfixExpr expr = new InfixExpr("20+30.8*400/500"); + List elements = expr.parser(); + System.out.println(expr.evaluate()); + } +} diff --git a/students/769232552/season_one/main/java/season_1/code07/InfixToPostfix.java b/students/769232552/season_one/main/java/season_1/code07/InfixToPostfix.java new file mode 100644 index 0000000000..7da7c7d58d --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code07/InfixToPostfix.java @@ -0,0 +1,44 @@ +package code07; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class InfixToPostfix { + + public static List convert(String expr) { + + List tokens = new ArrayList(); + + Stack tokenStack = new Stack(); + List infixTokens =TokenParser.parseInfix(expr); + + for(Token t : infixTokens){ + if(t.isNumber()){ + tokens.add(t); + }else { + while (!tokenStack.isEmpty() && !t.hasHigherPriority(tokenStack.peek())){ + tokens .add(tokenStack.pop()); + } + tokenStack.push(t); + } + } + + while(!tokenStack.isEmpty()){ + tokens.add(tokenStack.pop()); + } + + return tokens; + } + + public static void main(String[] args) { + String expr = "2+3*4+5"; + List tokens = InfixToPostfix.convert(expr); + for(Token t : tokens){ + System.out.println(t.getStringValue()); + } + + } + + +} diff --git a/students/769232552/season_one/main/java/season_1/code07/PostfixExpr.java b/students/769232552/season_one/main/java/season_1/code07/PostfixExpr.java new file mode 100644 index 0000000000..cf9e3e6722 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code07/PostfixExpr.java @@ -0,0 +1,47 @@ +package code07; + +import java.util.List; +import java.util.Stack; + +public class PostfixExpr { +String expr = null; + + public PostfixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + + Stack numStack = new Stack(); + List tokens = TokenParser.parseNoInfix(this.expr); + + for(Token token : tokens){ + if(token.isNumber()){ + numStack.push(new Float(token.getIntValue())); + } else{ + Float f2 = numStack.pop(); + Float f1 = numStack.pop(); + numStack.push(calc(f1,f2,token.getStringValue())); + } + } + + return numStack.pop().floatValue(); + } + + //如下类似排比句的代码写法就是卫语句 + private Float calc(Float f1, Float f2, String op){ + if(op.equals("+")){ + return f1+f2; + } + if(op.equals("-")){ + return f1-f2; + } + if(op.equals("*")){ + return f1*f2; + } + if(op.equals("/")){ + return f1/f2; + } + throw new RuntimeException(op + " is not supported"); + } +} diff --git a/students/769232552/season_one/main/java/season_1/code07/PrefixExpr.java b/students/769232552/season_one/main/java/season_1/code07/PrefixExpr.java new file mode 100644 index 0000000000..1f8bcff595 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code07/PrefixExpr.java @@ -0,0 +1,55 @@ +package code07; + +import java.util.List; +import java.util.Stack; + +public class PrefixExpr { + String expr = null; + + public PrefixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + Stack exprStack = new Stack(); + Stack numberStack = new Stack(); + + List tokens = TokenParser.parseNoInfix(this.expr); + + for(Token t: tokens){ + exprStack.push(t); + } + + while (!exprStack.isEmpty()){ + Token t = exprStack.pop(); + if(Token.OPERATOR == t.getType()){ + float a = numberStack.pop(); + float b = numberStack.pop(); + float result = calc(a,b,t.getStringValue()); + numberStack.push(result); + }else if(Token.NUMBER == t.getType()){ + numberStack.push(Float.parseFloat(t.getStringValue())); + }else { + System.out.println("char :["+t.getStringValue()+"] is not number or operator,ignore"); + } + } + + return numberStack.pop().floatValue(); + } + + private Float calc(Float f1, Float f2, String op){ + if(op.equals("+")){ + return f1+f2; + } + if(op.equals("-")){ + return f1-f2; + } + if(op.equals("*")){ + return f1*f2; + } + if(op.equals("/")){ + return f1/f2; + } + throw new RuntimeException(op + " is not supported"); + } +} diff --git a/students/769232552/season_one/main/java/season_1/code07/Token.java b/students/769232552/season_one/main/java/season_1/code07/Token.java new file mode 100644 index 0000000000..a5d6735427 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code07/Token.java @@ -0,0 +1,63 @@ +package code07; + +import java.lang.reflect.Array; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by yyglider on 2017/4/19. + */ +public class Token { + + public static final List OPERATORS = Arrays.asList("+","-","*","/"); + + private static final Map priorities = new HashMap(); + + static { + priorities.put("+",1); + priorities.put("-",1); + priorities.put("*",2); + priorities.put("/",2); + } + + public static final int OPERATOR = 1; + public static final int NUMBER = 2; + + private String value; + private int type; + + public Token(int type, String value) { + this.value = value; + this.type = type; + } + + public boolean isNumber(){ + return type == NUMBER; + } + + public boolean isOperator(){ + return type == OPERATOR; + } + + public int getIntValue(){ + return Integer.valueOf(value).intValue(); + } + + public String getStringValue(){ + return value; + } + + public int getType() { + return type; + } + + public boolean hasHigherPriority(Token t){ + if(!this.isOperator() && !t.isOperator()){ + throw new RuntimeException("numbers can't compare priority"); + } + return priorities.get(this.value) - priorities.get(t.value) > 0; + } + +} diff --git a/students/769232552/season_one/main/java/season_1/code07/TokenParser.java b/students/769232552/season_one/main/java/season_1/code07/TokenParser.java new file mode 100644 index 0000000000..df91e4d3dc --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code07/TokenParser.java @@ -0,0 +1,89 @@ +package code07; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by yyglider on 2017/4/20. + * 解析表达式成各个Token + */ +public class TokenParser { + + public static List parseNoInfix(String expr){ + List tokens = new ArrayList(); + + String[] chs = expr.split(" "); + + for (int i = 0; i < chs.length; i++) { + if(isOperator(chs[i])){ + tokens.add(new Token(Token.OPERATOR,chs[i])); + }else { + String value = chs[i]; + tokens.add(new Token(Token.NUMBER,value)); + } + } + return tokens; + } + + + public static List parseInfix(String expr){ + List tokens = new ArrayList(); + + char[] chs = expr.toCharArray(); + + int i = 0; + while (i < chs.length){ + if(isOperator(chs[i])){ + tokens.add(new Token(Token.OPERATOR,String.valueOf(chs[i]))); + i++; + }else if(Character.isDigit(chs[i])){ + + int nextOperatorPos = findNextOperatorPos(i,chs); + String value = expr.substring(i,nextOperatorPos); + tokens.add(new Token(Token.NUMBER,value)); + i = nextOperatorPos; + + }else { + System.out.println("char :["+chs[i]+"] is not number or operator,ignore"); + i++; + } + } + return tokens; + } + + private static int findNextOperatorPos(int i, char[] chs) { + while (Character.isDigit(chs[i])) { + i++; + if (i == chs.length) { + break; + } + } + return i; + } + + private static boolean isOperator(char c) { + String sc = String.valueOf(c); + return Token.OPERATORS.contains(sc); + } + + private static boolean isOperator(String c) { + return Token.OPERATORS.contains(c); + } + + public static void main(String[] args) { +/* + String expr = "20+30.8*400/500"; + List tokens = TokenParser.parseInfix(expr); + for(Token t: tokens){ + System.out.println(t.getStringValue()); + } +*/ + + + String expr2 = "- + + 6 / * 2 9 3 * 4 2 8"; + List tokens2 = TokenParser.parseNoInfix(expr2); + for(Token t: tokens2){ + System.out.println(t.getStringValue()); + } + } +} diff --git a/students/769232552/season_one/main/java/season_1/code08/CircleQueue.java b/students/769232552/season_one/main/java/season_1/code08/CircleQueue.java new file mode 100644 index 0000000000..99ccd2cc57 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code08/CircleQueue.java @@ -0,0 +1,93 @@ +package code08; + +/** + * 用数组实现循环队列 - 杜绝“假上溢” + * 在入队和出队的操作中,头尾指针只增加不减小,致使被删除元素的空间永远无法重新利用。 + * + * 因此,尽管队列中实际的元素个数远远小于向量空间的规模,但也可能由于尾指针巳超出向量空间的上界而不能做入队操作。 + * 为充分利用向量空间。克服上述假上溢现象的方法是将向量空间想象为一个首尾相接的圆环,并称这种向量为循环队列。 + * 在循环队列中进行出队、入队操作时,头尾指针仍要加1,朝前移动。 + * 只不过当头尾指针指向向量上界(QueueSize-1)时,其加1操作的结果是指向向量的下界0。 + * @param + */ +public class CircleQueue { + + private int maxSize; + //用数组来保存循环队列的元素 + private Object[] elementData; + //队头 + private int front; + //队尾 + private int rear; + //元素个数 + private int count; + + public CircleQueue(int maxSize) { + this.maxSize = maxSize+1; //队列需要一个空的位置用于区分队列满和队列空 + this.elementData = new Object[maxSize+1]; + this.front = 0; + this.rear = 0; + } + + public int size() { + return count; + } + + public void enQueue(E data) { + if(this.isFull()){ + System.out.println("queue is full, enQueue failed"); + return; + } + elementData[rear] = data; + rear = (rear + 1) % this.maxSize; + count ++; + } + + public E deQueue() { + if(this.isEmpty()){ + System.out.println("queue is empty, deQueue failed"); + return null; + } + E e = (E) elementData[front]; + front = (front + 1) % this.maxSize; + count --; + return e; + } + + public boolean isFull(){ + return ((rear + 1) % this.maxSize == front); + } + + public boolean isEmpty() { + return rear == front; + } + + public static void main(String[] args) { + CircleQueue q = new CircleQueue(11); + q.enQueue("a1"); + q.enQueue("a2"); + q.enQueue("a3"); + q.enQueue("a4"); + q.enQueue("a5"); + q.enQueue("a6"); + q.enQueue("a7"); + q.enQueue("a8"); + q.enQueue("a9"); + q.enQueue("a10"); + System.out.println("current size is : " + q.size()); + + System.out.println("dequeue : " + q.deQueue()); + System.out.println("after deQueue , current size is : " + q.size()); + + q.enQueue("new1"); + System.out.println("after enQueue , current size is : " + q.size()); + + /*while(!q.isEmpty()){ + System.out.println(q.deQueue()); + System.out.println("current size is : " + q.size()); + }*/ + + + + } +} diff --git a/students/769232552/season_one/main/java/season_1/code08/Josephus.java b/students/769232552/season_one/main/java/season_1/code08/Josephus.java new file mode 100644 index 0000000000..4c964631e2 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code08/Josephus.java @@ -0,0 +1,55 @@ +package code08; + +import java.util.ArrayList; +import java.util.List; + +/** + * 用Queue来实现Josephus问题 + * 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来 + * 该方法返回一个List, 包含了被杀死人的次序 + + 0 1 2 3 4 5 6 7 8 9 m=3, n=10 + + 第一个人(2)出列后的序列为: + 3 4 5 6 7 8 9 0 1 (0,1,2 出队列,0,1 再入队列) + + 第二个人(5)出列后的序列为: +   6 7 8 9 0 1 3 4 (3,4,5 出队列,3,4 再入队列) + + ... + + 如果剩余队列长度小于等于m则依次出队列 + + */ +public class Josephus { + + public static List execute(int n, int m){ + List kList = new ArrayList(); + CircleQueue circleQueue = new CircleQueue(n); + + for (int i = 0; i < n ; i++) { + circleQueue.enQueue(i); + } + + Queue tmpQueue = new Queue(); + while(!circleQueue.isEmpty()){ + + if(m > circleQueue.size()) { + m = m % circleQueue.size(); + } + + for (int j = 0; j < m-1; j++) { + tmpQueue.enQueue(circleQueue.deQueue()); + } + + Integer kill = circleQueue.deQueue(); + kList.add(kill); + + for (int j = 0; j < m-1; j++) { + circleQueue.enQueue(tmpQueue.deQueue()); + } + } + return kList; + } + +} diff --git a/students/769232552/season_one/main/java/season_1/code08/Queue.java b/students/769232552/season_one/main/java/season_1/code08/Queue.java new file mode 100644 index 0000000000..f4e1977b73 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code08/Queue.java @@ -0,0 +1,61 @@ +package code08; + +import java.util.NoSuchElementException; + +public class Queue { + private Node first; + private Node last; + private int size; + + + private static class Node { + private E item; + private Node next; + } + + + public Queue() { + first = null; + last = null; + size = 0; + } + + + public boolean isEmpty() { + return first == null; + } + + public int size() { + return size; + } + + + + public void enQueue(E data) { + Node oldlast = last; + last = new Node(); + last.item = data; + last.next = null; + if (isEmpty()) { + first = last; + } + else{ + oldlast.next = last; + } + size++; + } + + public E deQueue() { + if (isEmpty()) { + throw new NoSuchElementException("Queue underflow"); + } + E item = first.item; + first = first.next; + size--; + if (isEmpty()) { + last = null; + } + return item; + } + +} diff --git a/students/769232552/season_one/main/java/season_1/code08/QueueWithTwoStacks.java b/students/769232552/season_one/main/java/season_1/code08/QueueWithTwoStacks.java new file mode 100644 index 0000000000..7f9700f573 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code08/QueueWithTwoStacks.java @@ -0,0 +1,66 @@ +package code08; + +import java.util.Stack; + +/** + * 用两个栈来实现一个队列 + * @param + */ +public class QueueWithTwoStacks { + private Stack stack1; + private Stack stack2; + + + public QueueWithTwoStacks() { + stack1 = new Stack(); + stack2 = new Stack(); + } + + + + public boolean isEmpty() { + return stack1.isEmpty() && stack2.isEmpty(); + } + + + public int size() { + return stack1.size() + stack2.size(); + } + + + public void enQueue(E item) { + stack1.push(item); + } + + public E deQueue() { + if(!stack2.isEmpty()){ + return stack2.pop(); + } + if(!stack1.isEmpty()){ + while (!stack1.isEmpty()){ + E e = stack1.pop(); + stack2.push(e); + } + return stack2.pop(); + } + return null; + } + + + public static void main(String[] args) { + QueueWithTwoStacks q = new QueueWithTwoStacks(); + q.enQueue("a"); + q.enQueue("b"); + q.enQueue("c"); + while (!q.isEmpty()){ + System.out.println(q.deQueue()); + } + q.enQueue("d"); + q.enQueue("e"); + while (!q.isEmpty()){ + System.out.println(q.deQueue()); + } + } + + } + diff --git a/students/769232552/season_one/main/java/season_1/code09/QuickMinStack.java b/students/769232552/season_one/main/java/season_1/code09/QuickMinStack.java new file mode 100644 index 0000000000..7951a2bde1 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code09/QuickMinStack.java @@ -0,0 +1,40 @@ +package code09; + +import code05.Stack; + +/** + * 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素 + * finMin操作最坏的情形下时间复杂度应该是O(1),简单来讲,操作一次就可以得到最小值 + * + * 构造一个辅助栈,每次压入当前主栈中最小的元素 + */ +public class QuickMinStack { + + Stack mainStack = new Stack(); + Stack subStack = new Stack(); //辅助栈 + + public void push(int data){ + mainStack.push(data); + + //每次压入当前主栈中最小的元素 + if(subStack.isEmpty()){ + subStack.push(data); + } + else if(data >= (Integer)subStack.peek()){ + subStack.push(subStack.peek()); + } + else { + subStack.push(data); + } + + } + + public int pop(){ + subStack.pop(); + return (Integer) mainStack.pop(); + } + + public int findMin(){ + return (Integer) subStack.peek(); + } +} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code09/StackWithTwoQueues.java b/students/769232552/season_one/main/java/season_1/code09/StackWithTwoQueues.java new file mode 100644 index 0000000000..926977502e --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code09/StackWithTwoQueues.java @@ -0,0 +1,42 @@ +package code09; + +import code08.Queue; + +/** + * 思路和2个栈实现一个队列是一致的,入的时候简单,出的时候麻烦 + */ + +public class StackWithTwoQueues { + + + Queue q1 = new Queue(); + Queue q2 = new Queue(); + + public void push(int data) { + q1.enQueue(data); + } + + public int pop() { + if(q1.isEmpty()){ + return -1; + } + + if(q1.size() == 1){ + return (Integer) q1.deQueue(); + } + + while (q1.size()>1){ + q2.enQueue(q1.deQueue()); + } + + int result = (Integer) q1.deQueue(); + + while (!q2.isEmpty()){ + q1.enQueue(q2.deQueue()); + } + + return result; + } + + +} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code09/TwoStackInOneArray.java b/students/769232552/season_one/main/java/season_1/code09/TwoStackInOneArray.java new file mode 100644 index 0000000000..9390e16246 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code09/TwoStackInOneArray.java @@ -0,0 +1,152 @@ +package code09; + +import code01.ArrayList; + +/** + * 用一个数组实现两个栈 + * 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。 + */ +public class TwoStackInOneArray { + + Object[] data = new Object[10]; + int top1 = -1, end1 = 0; + int top2 = data.length, end2 = data.length - 1; + + /** + * 向第一个栈中压入元素 + * @param o + */ + public void push1(Object o){ + if(top1+1 < top2){ + data[++top1] = o; + return; + } + resize(); + data[++top1] = o; + } + + /** + * 从第一个栈中弹出元素 + * @return + */ + public Object pop1(){ + if(top1 < 0){ + return null; + } + return data[top1--]; + } + + /** + * 获取第一个栈的栈顶元素 + * @return + */ + + public Object peek1(){ + if(top1 < 0){ + return null; + } + return data[top1]; + } + + /** + * 向第二个栈压入元素 + */ + public void push2(Object o){ + + if(top2 - 1 > top1){ + data[--top2] = o; + return; + } + resize(); + data[--top2] = o; + } + + /** + * 从第二个栈弹出元素 + * @return + */ + public Object pop2(){ + if(top2 > data.length - 1){ + return null; + } + return data[top2++]; + } + /** + * 获取第二个栈的栈顶元素 + * @return + */ + + public Object peek2(){ + if(top2 > data.length - 1){ + return null; + } + return data[top2]; + } + + /** + * 重新分配空间 + */ + private void resize() { + System.out.println("resize data array ..."); + Object[] newData = new Object[20]; + //copy stack 1 + for (int i = end1; i <= top1; i++) { + newData[i] = this.data[i]; + } + //copy stack 2 + int newDataTop = newData.length; + for (int j = end2; j >= top2 ; j--) { + newData[--newDataTop] = this.data[j]; + } + this.top2 = newDataTop; + this.end2 = newData.length -1; + this.data = newData; + System.out.println("data array resize to " + this.data.length); + } + + @Override + public String toString(){ + StringBuilder sb = new StringBuilder(); + int i = this.end1; + sb.append("stack 1 : "); + for(; i <= this.top1; i++){ + sb.append(data[i].toString()+" "); + } + sb.append(", stack 2 : "); + int j = this.end2; + for(; j >= this.top2; j--){ + sb.append(data[j].toString()+" "); + } + return sb.toString(); + } + + public static void main(String[] args) { + TwoStackInOneArray s = new TwoStackInOneArray(); + s.push1(10); + s.push1(11); + s.push1(12); + s.push1(13); + s.push1(14); + + s.push2(20); + s.push2(21); + s.push2(22); + s.push2(23); + s.push2(24); + + System.out.println(s); + + s.push2(25); + + System.out.println(s); + + s.pop2(); + s.pop2(); + s.pop1(); + s.pop1(); + + System.out.println(s); + + } + +} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code10/BinaryTreeNode.java b/students/769232552/season_one/main/java/season_1/code10/BinaryTreeNode.java new file mode 100644 index 0000000000..a7f242c984 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code10/BinaryTreeNode.java @@ -0,0 +1,39 @@ +package code10; + +/** + * Created by on 2017/5/9. + */ + +public class BinaryTreeNode { + + public T data; + public BinaryTreeNode left; + public BinaryTreeNode right; + + public BinaryTreeNode(T data){ + this.data=data; + } + public T getData() { + return data; + } + public void setData(T data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code10/BinaryTreeUtil.java b/students/769232552/season_one/main/java/season_1/code10/BinaryTreeUtil.java new file mode 100644 index 0000000000..144758651c --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code10/BinaryTreeUtil.java @@ -0,0 +1,125 @@ +package code10; + +import code01.BinaryTree; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +/** + * Created by on 2017/5/9. + */ +public class BinaryTreeUtil { + /** + * 用递归的方式实现对二叉树的前序遍历 + * + * @param root + * @return + */ + public static List preOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + preOrder(root,result); + return result; + } + + public static void preOrder(BinaryTreeNode root, List result){ + if(root == null){ + return; + } + result.add(root.getData()); + preOrder(root.getLeft(),result); + preOrder(root.getRight(),result); + } + + /** + * 用递归的方式实现对二叉树的中遍历 + * + * @param root + * @return + */ + public static List inOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + inOrder(root,result); + return result; + } + + public static void inOrder(BinaryTreeNode root, List result){ + if(root == null){ + return; + } + inOrder(root.getLeft(),result); + result.add(root.getData()); + inOrder(root.getRight(),result); + } + + /** + * 用递归的方式实现对二叉树的后遍历 + * + * @param root + * @return + */ + public static List postOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + postOrder(root,result); + return result; + } + + public static void postOrder(BinaryTreeNode root, List result){ + if(root == null){ + return; + } + postOrder(root.getLeft(),result); + postOrder(root.getRight(),result); + result.add(root.getData()); + } + + /** + * 用非递归的方式实现对二叉树的前序遍历(后序便历类似) + * @param root + * @return + */ + public static List preOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + Stack visitStack = new Stack(); + + visitStack.push(root); + + while(!visitStack.isEmpty()){ + BinaryTreeNode node = (BinaryTreeNode) visitStack.pop(); + result.add((T) node.getData()); + if(node.getRight() != null){ + visitStack.push(node.getRight()); + } + if(node.getLeft() != null){ + visitStack.push(node.getLeft()); + } + + } + + return result; + } + /** + * 用非递归的方式实现对二叉树的中序遍历 + */ + public static List inOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + + Stack visitStack = new Stack(); + + BinaryTreeNode node = root; + + while(node != null ||!visitStack.isEmpty()){ + while (node != null){ + visitStack.push(node); + node = node.left; + } + BinaryTreeNode pNode = (BinaryTreeNode) visitStack.pop(); + result.add((T) pNode.getData()); + node = pNode.right; + } + return result; + } + +} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code10/FileList.java b/students/769232552/season_one/main/java/season_1/code10/FileList.java new file mode 100644 index 0000000000..669d8e32e3 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code10/FileList.java @@ -0,0 +1,38 @@ +package code10; + +import java.io.File; + +/** + * Created by on 2017/5/9. + */ +public class FileList { + + public void list(File file){ + list(file,0); + } + + public void list(File file,int layer){ + showFileName(file,layer); + if(file.isDirectory()){ + File[] files = file.listFiles(); + for(File f : files){ + list(f,layer+1); + } + } + } + + public void showFileName(File file,int i){ + for (int j = 0; j < i; j++) { + System.out.print(" - "); + } + System.out.println(file.getName()); + } + + public static void main(String[] args) { + FileList fileList = new FileList(); + String path = "D:\\dir1"; + File file = new File(path); + fileList.list(file); + } + +} diff --git a/students/769232552/season_one/main/java/season_1/code11/BinarySearchTree.java b/students/769232552/season_one/main/java/season_1/code11/BinarySearchTree.java new file mode 100644 index 0000000000..6b97488945 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/code11/BinarySearchTree.java @@ -0,0 +1,305 @@ +package code11; + +import code10.BinaryTreeNode; + +import java.util.*; + +/** + * Created by yyglider on 2017/5/16. + */ +public class BinarySearchTree { + + BinaryTreeNode root; + + public BinarySearchTree(BinaryTreeNode root) { + this.root = root; + } + + public BinaryTreeNode getRoot() { + return root; + } + + public T findMin() { + if (this.root == null) { + return null; + } + + BinaryTreeNode pNode = this.root; + while (pNode != null && pNode.left != null) { + pNode = pNode.left; + } + return (T) pNode.getData(); + } + + public T findMax() { + if (this.root == null) { + return null; + } + + BinaryTreeNode pNode = this.root; + while (pNode != null && pNode.right != null) { + pNode = pNode.right; + } + return (T) pNode.getData(); + } + + public int height() { + if (this.root == null) { + return 0; + } + return getHeight(this.root); + } + + private int getHeight(BinaryTreeNode node) { + if (node == null) { + return 0; + } + int left = getHeight(node.left); + int right = getHeight(node.right); + return 1 + Math.max(left, right); + } + + public int size() { + if (this.root == null) { + return 0; + } + return getSize(this.root); + } + + private int getSize(BinaryTreeNode node) { + if (node == null) { + return 0; + } + int left = getSize(node.left); + int right = getSize(node.right); + return 1 + left + right; + } + + + public void remove(T e) { + if (this.root == null || e == null) { + return; + } + + if (e.equals(this.root.getData())) { + this.root = null; + return; + } + + BinaryTreeNode pNode = this.root; //记录待删除的元素位置 + BinaryTreeNode pPreNode = this.root; //记录待删除的元素的前一个位置 + + //find e + while (pNode != null) { + if (e.compareTo(pNode.getData()) > 0) { + pPreNode = pNode; + pNode = pNode.right; + } else if (e.compareTo(pNode.getData()) < 0) { + pPreNode = pNode; + pNode = pNode.left; + } else { + break; + } + + } + + //delete e + if (pNode != null && e.equals(pNode.getData())) { + BinaryTreeNode node; + BinaryTreeNode preNode; + + //如果其结点只包含左子树,或者右子树的话,此时直接删除该结点 + boolean hasOneChild = ((pNode.right != null && pNode.left == null) && (pNode.right == null && pNode.left != null)); + if (hasOneChild) { + if (pNode.left != null) { + node = pNode.left; + pNode.setData(node.getData()); + pNode.left = null; + return; + } + node = pNode.right; + pNode.setData(node.getData()); + pNode.right = null; + + } + + //如果其结点既包含左子树,也包含右子树 + //找到其右子树的的最小元素(实际上用左子树最大元素代替也是可以的) + if (pNode.right != null && pNode.left != null) { + preNode = pNode; + node = pNode.right; + while (node != null && node.left != null) { + preNode = node; + node = node.left; + } + //如果是叶子节点 + if (node.right == null) { + pNode.setData(node.getData()); + + preNode.right = null; + return; + } + + pNode.setData(node.getData()); + + //非叶子结点,用其右孩子的元素来替代 + preNode = node; + node = node.right; + preNode.setData(node.getData()); + preNode.right = null; + return; + } + + //其本身就是叶子节点 + pPreNode = null; + + } + } + + //层序遍历,使用队列的方式 + public List levelVisit() { + if (this.root == null) { + return null; + } + List results = new ArrayList(); + Queue queue = new ArrayDeque(); + + queue.add(this.root); + + while (!queue.isEmpty()) { + BinaryTreeNode node = queue.poll(); + results.add((T) node.getData()); + + if (node.left != null) { + queue.add(node.left); + } + if (node.right != null) { + queue.add(node.right); + } + } + return results; + } + + + /** + * 验证是否是二叉查找树 + */ + public boolean isValid() { + if (this.root == null) { + return true; + } + return checkValid(this.root); + } + + public boolean checkValid(BinaryTreeNode node) { + + if (node == null) { + return true; + } + T nodeData = (T) node.getData(); + boolean currentFlag = (((node.left != null && nodeData.compareTo(node.left.getData()) > 0) + || node.left == null) + && ((node.right != null && nodeData.compareTo(node.right.getData()) < 0 + || node.right == null))); + boolean leftFlag = checkValid(node.left); + boolean rightFlag = checkValid(node.right); + return currentFlag && leftFlag && rightFlag; + } + + + + /** + * 最近公共祖先(LCA)问题 + * 基本思想为:从树根开始,该节点的值为t, + * 如果t大于t1和t2,说明t1和t2都位于t的左侧,所以它们的共同祖先必定在t的左子树中,从t.left开始搜索; + * 如果t小于t1和t2,说明t1和t2都位于t的右侧,那么从t.right开始搜索; + * 如果t1<=t<= t2,说明t1和t2位于t的两侧(或t=t1,或t=t2),那么该节点t为公共祖先。 + */ + public T getLowestCommonAncestor(T n1, T n2) { + if (this.root == null || n1 == null || n2 == null) { + return null; + } + + BinaryTreeNode pNode = this.root; + + while (pNode != null) { + if (n1.compareTo(pNode.getData()) > 0 && n2.compareTo(pNode.getData()) > 0) { + pNode = pNode.right; + } else if (n1.compareTo(pNode.getData()) < 0 && n2.compareTo(pNode.getData()) < 0) { + pNode = pNode.left; + } else { + return (T) pNode.getData(); + } + } + return null; + } + + + /** + * 两个节点之间的最短路径 + */ + public List getNodesBetween(T n1, T n2) { + + if (this.root == null || n1 == null || n2 == null) { + return null; + } + + final List list1 = new ArrayList(); + List list2 = new ArrayList(); + BinaryTreeNode pNode1 = this.root; + BinaryTreeNode pNode2 = this.root; + + //find n1 path + while (pNode1 != null) { + list1.add((T) pNode1.getData()); + + if (n1.compareTo(pNode1.getData()) > 0) { + pNode1 = pNode1.right; + } else if (n1.compareTo(pNode1.getData()) < 0) { + pNode1 = pNode1.left; + } else { + break; + } + } + //find n2 path + while (pNode2 != null) { + list2.add((T) pNode2.getData()); + + if (n2.compareTo(pNode2.getData()) > 0) { + pNode2 = pNode2.right; + } else if (n2.compareTo(pNode2.getData()) < 0) { + pNode2 = pNode2.left; + } else { + break; + } + } + + //join path1 and path2 + int i = 0, j = 0; + T first = null; + List results = new ArrayList(); + + while (i < list1.size() && i < list2.size()) { + if (list1.get(i) != list2.get(i)) { + break; + } + i++; + } + if (i > 0) { + i--; + } + int resultsLen = list1.size() + list2.size() - 2 * i - 1; + for (int k = list1.size() - 1; k > i; k--) { + results.add(list1.get(k)); + } + for (int k = i; k < list2.size(); k++) { + results.add(list2.get(k)); + } + + Collections.reverse(list1); + + return results; + } + + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/attr/AttributeInfo.java b/students/769232552/season_one/main/java/season_1/mini_jvm/attr/AttributeInfo.java new file mode 100644 index 0000000000..386305bbda --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/attr/AttributeInfo.java @@ -0,0 +1,19 @@ +package mini_jvm.attr; + +public abstract class AttributeInfo { + public static final String CODE = "Code"; + public static final String CONST_VALUE = "ConstantValue"; + public static final String EXCEPTIONS = "Exceptions"; + public static final String LINE_NUM_TABLE = "LineNumberTable"; + public static final String LOCAL_VAR_TABLE = "LocalVariableTable"; + public static final String STACK_MAP_TABLE = "StackMapTable"; + int attrNameIndex; + int attrLen ; + public AttributeInfo(int attrNameIndex, int attrLen) { + + this.attrNameIndex = attrNameIndex; + this.attrLen = attrLen; + } + + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/attr/CodeAttr.java b/students/769232552/season_one/main/java/season_1/mini_jvm/attr/CodeAttr.java new file mode 100644 index 0000000000..4817c1eee8 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/attr/CodeAttr.java @@ -0,0 +1,119 @@ +package mini_jvm.attr; + + +import mini_jvm.clz.ClassFile; +import mini_jvm.cmd.ByteCodeCommand; +import mini_jvm.cmd.CommandParser; +import mini_jvm.constant.ConstantPool; +import mini_jvm.loader.ByteCodeIterator; + +public class CodeAttr extends AttributeInfo { + private int maxStack ; + private int maxLocals ; + private int codeLen ; + private String code; + public String getCode() { + return code; + } + + private ByteCodeCommand[] cmds ; + public ByteCodeCommand[] getCmds() { + return cmds; + } + + private LineNumberTable lineNumTable; + private LocalVariableTable localVarTable; + private StackMapTable stackMapTable; + + public CodeAttr(int attrNameIndex, int attrLen, int maxStack, int maxLocals, int codeLen,String code ,ByteCodeCommand[] cmds) { + super(attrNameIndex, attrLen); + this.maxStack = maxStack; + this.maxLocals = maxLocals; + this.codeLen = codeLen; + this.code = code; + this.cmds = cmds; + } + + public void setLineNumberTable(LineNumberTable t) { + this.lineNumTable = t; + } + + public void setLocalVariableTable(LocalVariableTable t) { + this.localVarTable = t; + } + + + public static CodeAttr parse(ClassFile clzFile, ByteCodeIterator iter){ + + int attrNameIndex = iter.nextU2ToInt(); + int attrLen = iter.nextU4ToInt(); + int maxStack = iter.nextU2ToInt(); + int maxLocals = iter.nextU2ToInt(); + int codeLen = iter.nextU4ToInt(); + + String code = iter.nextUxToHexString(codeLen); + + ByteCodeCommand[] cmds = CommandParser.parse(clzFile,code); + CodeAttr codeAttr = new CodeAttr(attrNameIndex,attrLen, maxStack,maxLocals,codeLen,code,cmds); + + int exceptionTableLen = iter.nextU2ToInt(); + //TODO 处理exception + if(exceptionTableLen>0){ + String exTable = iter.nextUxToHexString(exceptionTableLen); + System.out.println("Encountered exception table , just ignore it :" + exTable); + + } + + + int subAttrCount = iter.nextU2ToInt(); + + for(int x=1; x<=subAttrCount; x++){ + int subAttrIndex = iter.nextU2ToInt(); + String subAttrName = clzFile.getConstantPool().getUTF8String(subAttrIndex); + + //已经向前移动了U2, 现在退回去。 + iter.back(2); + //line item table + if(AttributeInfo.LINE_NUM_TABLE.equalsIgnoreCase(subAttrName)){ + LineNumberTable t = LineNumberTable.parse(iter); + codeAttr.setLineNumberTable(t); + } + else if(AttributeInfo.LOCAL_VAR_TABLE.equalsIgnoreCase(subAttrName)){ + LocalVariableTable t = LocalVariableTable.parse(iter); + codeAttr.setLocalVariableTable(t); + } + else if (AttributeInfo.STACK_MAP_TABLE.equalsIgnoreCase(subAttrName)){ + StackMapTable t = StackMapTable.parse(iter); + codeAttr.setStackMapTable(t); + } + else{ + throw new RuntimeException("Need code to process " + subAttrName); + } + + + } + return codeAttr; + } + + + public String toString(ConstantPool pool){ + StringBuilder buffer = new StringBuilder(); + //buffer.append("Code:").append(code).append("\n"); + /*for(int i=0;i items = new ArrayList(); + + public void addLineNumberItem(LineNumberItem item){ + this.items.add(item); + } + + public LineNumberTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + } + + public static LineNumberTable parse(ByteCodeIterator iter){ + + int index = iter.nextU2ToInt(); + int len = iter.nextU4ToInt(); + + LineNumberTable table = new LineNumberTable(index,len); + + int itemLen = iter.nextU2ToInt(); + + for(int i=1; i<=itemLen; i++){ + LineNumberItem item = new LineNumberItem(); + item.setStartPC(iter.nextU2ToInt()); + item.setLineNum(iter.nextU2ToInt()); + table.addLineNumberItem(item); + } + return table; + } + + public String toString(){ + StringBuilder buffer = new StringBuilder(); + buffer.append("Line Number Table:\n"); + for(LineNumberItem item : items){ + buffer.append("startPC:"+item.getStartPC()).append(","); + buffer.append("lineNum:"+item.getLineNum()).append("\n"); + } + buffer.append("\n"); + return buffer.toString(); + + } + + private static class LineNumberItem{ + int startPC; + int lineNum; + public int getStartPC() { + return startPC; + } + public void setStartPC(int startPC) { + this.startPC = startPC; + } + public int getLineNum() { + return lineNum; + } + public void setLineNum(int lineNum) { + this.lineNum = lineNum; + } + } + + + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/attr/LocalVariableTable.java b/students/769232552/season_one/main/java/season_1/mini_jvm/attr/LocalVariableTable.java new file mode 100644 index 0000000000..946ef5fa5d --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/attr/LocalVariableTable.java @@ -0,0 +1,98 @@ +package mini_jvm.attr; + + +import mini_jvm.constant.ConstantPool; +import mini_jvm.loader.ByteCodeIterator; + +import java.util.ArrayList; +import java.util.List; + + + +public class LocalVariableTable extends AttributeInfo{ + + List items = new ArrayList(); + + public LocalVariableTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + } + + + private void addLocalVariableItem(LocalVariableItem item) { + this.items.add(item); + } + + public static LocalVariableTable parse(ByteCodeIterator iter){ + + int index = iter.nextU2ToInt(); + int len = iter.nextU4ToInt(); + + LocalVariableTable table = new LocalVariableTable(index,len); + + int itemLen = iter.nextU2ToInt(); + + for(int i=1; i<=itemLen; i++){ + LocalVariableItem item = new LocalVariableItem(); + item.setStartPC(iter.nextU2ToInt()); + item.setLength(iter.nextU2ToInt()); + item.setNameIndex(iter.nextU2ToInt()); + item.setDescIndex(iter.nextU2ToInt()); + item.setIndex(iter.nextU2ToInt()); + table.addLocalVariableItem(item); + } + return table; + } + + + public String toString(ConstantPool pool){ + StringBuilder buffer = new StringBuilder(); + buffer.append("Local Variable Table:\n"); + for(LocalVariableItem item : items){ + buffer.append("startPC:"+item.getStartPC()).append(","); + buffer.append("name:"+pool.getUTF8String(item.getNameIndex())).append(","); + buffer.append("desc:"+pool.getUTF8String(item.getDescIndex())).append(","); + buffer.append("slotIndex:"+ item.getIndex()).append("\n"); + } + buffer.append("\n"); + return buffer.toString(); + } + + private static class LocalVariableItem { + private int startPC; + private int length; + private int nameIndex; + private int descIndex; + private int index; + public int getStartPC() { + return startPC; + } + public void setStartPC(int startPC) { + this.startPC = startPC; + } + public int getLength() { + return length; + } + public void setLength(int length) { + this.length = length; + } + public int getNameIndex() { + return nameIndex; + } + public void setNameIndex(int nameIndex) { + this.nameIndex = nameIndex; + } + public int getDescIndex() { + return descIndex; + } + public void setDescIndex(int descIndex) { + this.descIndex = descIndex; + } + public int getIndex() { + return index; + } + public void setIndex(int index) { + this.index = index; + } + } + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/attr/StackMapTable.java b/students/769232552/season_one/main/java/season_1/mini_jvm/attr/StackMapTable.java new file mode 100644 index 0000000000..65b3b24c82 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/attr/StackMapTable.java @@ -0,0 +1,30 @@ +package mini_jvm.attr; + + +import mini_jvm.loader.ByteCodeIterator; + +public class StackMapTable extends AttributeInfo{ + + private String originalCode; + + public StackMapTable(int attrNameIndex, int attrLen) { + super(attrNameIndex, attrLen); + } + + public static StackMapTable parse(ByteCodeIterator iter){ + int index = iter.nextU2ToInt(); + int len = iter.nextU4ToInt(); + StackMapTable t = new StackMapTable(index,len); + + //后面的StackMapTable太过复杂, 不再处理, 只把原始的代码读进来保存 + String code = iter.nextUxToHexString(len); + t.setOriginalCode(code); + + return t; + } + + private void setOriginalCode(String code) { + this.originalCode = code; + + } +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/clz/AccessFlag.java b/students/769232552/season_one/main/java/season_1/mini_jvm/clz/AccessFlag.java new file mode 100644 index 0000000000..b6717402da --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/clz/AccessFlag.java @@ -0,0 +1,25 @@ +package mini_jvm.clz; + +public class AccessFlag { + private int flagValue; + + public AccessFlag(int value) { + this.flagValue = value; + } + + public int getFlagValue() { + return flagValue; + } + + public void setFlagValue(int flag) { + this.flagValue = flag; + } + + public boolean isPublicClass(){ + return (this.flagValue & 0x0001) != 0; + } + public boolean isFinalClass(){ + return (this.flagValue & 0x0010) != 0; + } + +} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/clz/ClassFile.java b/students/769232552/season_one/main/java/season_1/mini_jvm/clz/ClassFile.java new file mode 100644 index 0000000000..d0884893aa --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/clz/ClassFile.java @@ -0,0 +1,121 @@ +package mini_jvm.clz; + +import mini_jvm.constant.ClassInfo; +import mini_jvm.constant.ConstantPool; +import mini_jvm.field.Field; +import mini_jvm.method.Method; + +import java.util.ArrayList; +import java.util.List; + +public class ClassFile { + + private int minorVersion; + private int majorVersion; + + private AccessFlag accessFlag; + private ClassIndex clzIndex; + private ConstantPool pool; + private List fields = new ArrayList(); + private List methods = new ArrayList(); + + public ClassIndex getClzIndex() { + return clzIndex; + } + public AccessFlag getAccessFlag() { + return accessFlag; + } + public void setAccessFlag(AccessFlag accessFlag) { + this.accessFlag = accessFlag; + } + + + + public ConstantPool getConstantPool() { + return pool; + } + public int getMinorVersion() { + return minorVersion; + } + public void setMinorVersion(int minorVersion) { + this.minorVersion = minorVersion; + } + public int getMajorVersion() { + return majorVersion; + } + public void setMajorVersion(int majorVersion) { + this.majorVersion = majorVersion; + } + public void setConstPool(ConstantPool pool) { + this.pool = pool; + + } + public void setClassIndex(ClassIndex clzIndex) { + this.clzIndex = clzIndex; + } + + public void setFields(List f){ + this.fields = f; + } + public List getFields(){ + return this.fields; + } + public void addField(Field f){this.fields.add(f);} + + + public void setMethods(List m){ + this.methods = m; + } + public List getMethods() { + return methods; + } + public void addMethod(Method m){this.methods.add(m);} + + + + public void print(){ + if(this.accessFlag.isPublicClass()){ + System.out.println("Access flag : public "); + } + System.out.println("Class Name:"+ getClassName()); + System.out.println("Super Class Name:"+ getSuperClassName()); + } + + private String getClassName(){ + int thisClassIndex = this.clzIndex.getThisClassIndex(); + ClassInfo thisClass = (ClassInfo)this.getConstantPool().getConstantInfo(thisClassIndex); + return thisClass.getClassName(); + } + public String getSuperClassName(){ + ClassInfo superClass = (ClassInfo)this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex()); + return superClass.getClassName(); + } + + public Method getMethod(String methodName, String paramAndReturnType){ + + for(Method m :methods){ + + int nameIndex = m.getNameIndex(); + int descriptionIndex = m.getDescriptorIndex(); + + String name = this.getConstantPool().getUTF8String(nameIndex); + String desc = this.getConstantPool().getUTF8String(descriptionIndex); + if(name.equals(methodName) && desc.equals(paramAndReturnType)){ + return m; + } + } + return null; + } + public Method getMainMethod(){ + for(Method m :methods){ + int nameIndex = m.getNameIndex(); + int descIndex = m.getDescriptorIndex(); + String name = this.getConstantPool().getUTF8String(nameIndex); + String desc = this.getConstantPool().getUTF8String(descIndex); + if(name.equals("main") && desc.equals("([Ljava/lang/String;)V")){ + return m; + } + } + return null; + } +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/clz/ClassIndex.java b/students/769232552/season_one/main/java/season_1/mini_jvm/clz/ClassIndex.java new file mode 100644 index 0000000000..dcc59908f0 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/clz/ClassIndex.java @@ -0,0 +1,19 @@ +package mini_jvm.clz; + +public class ClassIndex { + private int thisClassIndex; + private int superClassIndex; + + public int getThisClassIndex() { + return thisClassIndex; + } + public void setThisClassIndex(int thisClassIndex) { + this.thisClassIndex = thisClassIndex; + } + public int getSuperClassIndex() { + return superClassIndex; + } + public void setSuperClassIndex(int superClassIndex) { + this.superClassIndex = superClassIndex; + } +} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/BiPushCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/BiPushCmd.java new file mode 100644 index 0000000000..bc2be8f9f8 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/BiPushCmd.java @@ -0,0 +1,29 @@ +package mini_jvm.cmd; + + +import mini_jvm.clz.ClassFile; +import mini_jvm.engine.ExecutionResult; +import mini_jvm.engine.Heap; +import mini_jvm.engine.JavaObject; +import mini_jvm.engine.StackFrame; + +public class BiPushCmd extends OneOperandCmd { + + public BiPushCmd(ClassFile clzFile, String opCode) { + super(clzFile,opCode); + } + + @Override + public String toString() { + + return this.getOffset()+":"+ this.getOpCode()+" " + this.getReadableCodeText() + " " + this.getOperand(); + } + public void execute(StackFrame frame, ExecutionResult result){ + int value = this.getOperand(); + JavaObject jo = Heap.getInstance().newInt(value); + frame.getOprandStack().push(jo); + + } + + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/ByteCodeCommand.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/ByteCodeCommand.java new file mode 100644 index 0000000000..cc877430c0 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/ByteCodeCommand.java @@ -0,0 +1,158 @@ +package mini_jvm.cmd; + +import mini_jvm.clz.ClassFile; +import mini_jvm.constant.ConstantInfo; +import mini_jvm.constant.ConstantPool; +import mini_jvm.engine.ExecutionResult; +import mini_jvm.engine.StackFrame; + +import java.util.HashMap; +import java.util.Map; + + + + +public abstract class ByteCodeCommand { + + String opCode; + ClassFile clzFile; + private int offset; + + public static final String aconst_null = "01"; + public static final String new_object = "BB"; + public static final String lstore = "37"; + public static final String invokespecial = "B7"; + public static final String invokevirtual = "B6"; + public static final String getfield = "B4"; + public static final String putfield = "B5"; + public static final String getstatic = "B2"; + public static final String ldc = "12"; + public static final String dup = "59"; + public static final String bipush = "10"; + public static final String aload_0 = "2A"; + public static final String aload_1 = "2B"; + public static final String aload_2 = "2C"; + public static final String iload = "15"; + public static final String iload_1 = "1B"; + public static final String iload_2 = "1C"; + public static final String iload_3 = "1D"; + public static final String fload_3 = "25"; + + public static final String voidreturn = "B1"; + public static final String ireturn = "AC"; + public static final String freturn = "AE"; + + public static final String astore_1 = "4C"; + public static final String if_icmp_ge = "A2"; + public static final String if_icmple = "A4"; + public static final String goto_no_condition = "A7"; + public static final String iconst_0 = "03"; + public static final String iconst_1 = "04"; + public static final String istore_1 = "3C"; + public static final String istore_2 = "3D"; + public static final String iadd = "60"; + public static final String iinc = "84"; + private static Map codeMap = new HashMap(); + + static{ + codeMap.put("01", "aconst_null"); + + codeMap.put("A2", "if_icmp_ge"); + codeMap.put("A4", "if_icmple"); + codeMap.put("A7", "goto"); + + codeMap.put("BB", "new"); + codeMap.put("37", "lstore"); + codeMap.put("B7", "invokespecial"); + codeMap.put("B6", "invokevirtual"); + codeMap.put("B4", "getfield"); + codeMap.put("B5", "putfield"); + codeMap.put("B2", "getstatic"); + + codeMap.put("2A", "aload_0"); + codeMap.put("2B", "aload_1"); + codeMap.put("2C", "aload_2"); + + codeMap.put("10", "bipush"); + codeMap.put("15", "iload"); + codeMap.put("1A", "iload_0"); + codeMap.put("1B", "iload_1"); + codeMap.put("1C", "iload_2"); + codeMap.put("1D", "iload_3"); + + codeMap.put("25", "fload_3"); + + codeMap.put("1E", "lload_0"); + + codeMap.put("24", "fload_2"); + codeMap.put("4C", "astore_1"); + + codeMap.put("A2", "if_icmp_ge"); + codeMap.put("A4", "if_icmple"); + + codeMap.put("A7", "goto"); + + codeMap.put("B1", "return"); + codeMap.put("AC", "ireturn"); + codeMap.put("AE", "freturn"); + + codeMap.put("03", "iconst_0"); + codeMap.put("04", "iconst_1"); + + codeMap.put("3C", "istore_1"); + codeMap.put("3D", "istore_2"); + + codeMap.put("59", "dup"); + + codeMap.put("60", "iadd"); + codeMap.put("84", "iinc"); + + codeMap.put("12", "ldc"); + } + + + + + + protected ByteCodeCommand(ClassFile clzFile, String opCode){ + this.clzFile = clzFile; + this.opCode = opCode; + } + + protected ClassFile getClassFile() { + return clzFile; + } + + public int getOffset() { + return offset; + } + + public void setOffset(int offset) { + this.offset = offset; + } + protected ConstantInfo getConstantInfo(int index){ + return this.getClassFile().getConstantPool().getConstantInfo(index); + } + + protected ConstantPool getConstantPool(){ + return this.getClassFile().getConstantPool(); + } + + + + public String getOpCode() { + return opCode; + } + + public abstract int getLength(); + + public String getReadableCodeText(){ + String txt = codeMap.get(opCode); + if(txt == null){ + return opCode; + } + return txt; + } + + public abstract void execute(StackFrame frame, ExecutionResult result); +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/CommandParser.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/CommandParser.java new file mode 100644 index 0000000000..3effa7ae5d --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/CommandParser.java @@ -0,0 +1,149 @@ +package mini_jvm.cmd; + +import mini_jvm.clz.ClassFile; + +import java.util.ArrayList; +import java.util.List; + + +public class CommandParser { + + + + public static ByteCodeCommand[] parse(ClassFile clzFile, String codes) { + + if ((codes == null) || (codes.length() == 0) || (codes.length() % 2) != 0) { + throw new RuntimeException("the orignal code is not correct"); + + } + + codes = codes.toUpperCase(); + + CommandIterator iter = new CommandIterator(codes); + List cmds = new ArrayList(); + + while (iter.hasNext()) { + String opCode = iter.next2CharAsString(); + + if (ByteCodeCommand.new_object.equals(opCode)) { + NewObjectCmd cmd = new NewObjectCmd(clzFile, opCode); + + cmd.setOprand1(iter.next2CharAsInt()); + cmd.setOprand2(iter.next2CharAsInt()); + + cmds.add(cmd); + } else if (ByteCodeCommand.invokespecial.equals(opCode)) { + InvokeSpecialCmd cmd = new InvokeSpecialCmd(clzFile, opCode); + cmd.setOprand1(iter.next2CharAsInt()); + cmd.setOprand2(iter.next2CharAsInt()); + // System.out.println( cmd.toString(clzFile.getConstPool())); + cmds.add(cmd); + } else if (ByteCodeCommand.invokevirtual.equals(opCode)) { + InvokeVirtualCmd cmd = new InvokeVirtualCmd(clzFile, opCode); + cmd.setOprand1(iter.next2CharAsInt()); + cmd.setOprand2(iter.next2CharAsInt()); + + cmds.add(cmd); + } else if (ByteCodeCommand.getfield.equals(opCode)) { + GetFieldCmd cmd = new GetFieldCmd(clzFile, opCode); + cmd.setOprand1(iter.next2CharAsInt()); + cmd.setOprand2(iter.next2CharAsInt()); + cmds.add(cmd); + } else if (ByteCodeCommand.getstatic.equals(opCode)) { + GetStaticFieldCmd cmd = new GetStaticFieldCmd(clzFile, opCode); + cmd.setOprand1(iter.next2CharAsInt()); + cmd.setOprand2(iter.next2CharAsInt()); + cmds.add(cmd); + } else if (ByteCodeCommand.putfield.equals(opCode)) { + PutFieldCmd cmd = new PutFieldCmd(clzFile, opCode); + cmd.setOprand1(iter.next2CharAsInt()); + cmd.setOprand2(iter.next2CharAsInt()); + cmds.add(cmd); + } else if (ByteCodeCommand.ldc.equals(opCode)) { + LdcCmd cmd = new LdcCmd(clzFile, opCode); + cmd.setOperand(iter.next2CharAsInt()); + cmds.add(cmd); + } else if (ByteCodeCommand.bipush.equals(opCode)) { + BiPushCmd cmd = new BiPushCmd(clzFile, opCode); + cmd.setOperand(iter.next2CharAsInt()); + cmds.add(cmd); + }else if(ByteCodeCommand.if_icmp_ge.equals(opCode) + || ByteCodeCommand.if_icmple.equals(opCode) + || ByteCodeCommand.goto_no_condition.equals(opCode)){ + ComparisonCmd cmd = new ComparisonCmd(clzFile,opCode); + cmd.setOprand1(iter.next2CharAsInt()); + cmd.setOprand2(iter.next2CharAsInt()); + cmds.add(cmd); + } else if(ByteCodeCommand.iinc.equals(opCode)){ + IncrementCmd cmd = new IncrementCmd(clzFile,opCode); + cmd.setOprand1(iter.next2CharAsInt()); + cmd.setOprand2(iter.next2CharAsInt()); + cmds.add(cmd); + } + else if (ByteCodeCommand.dup.equals(opCode) + || ByteCodeCommand.aload_0.equals(opCode) + || ByteCodeCommand.aload_1.equals(opCode) + || ByteCodeCommand.aload_2.equals(opCode) + || ByteCodeCommand.iload_1.equals(opCode) + || ByteCodeCommand.iload_2.equals(opCode) + || ByteCodeCommand.iload_3.equals(opCode) + || ByteCodeCommand.fload_3.equals(opCode) + || ByteCodeCommand.iconst_0.equals(opCode) + || ByteCodeCommand.iconst_1.equals(opCode) + || ByteCodeCommand.istore_1.equals(opCode) + || ByteCodeCommand.istore_2.equals(opCode) + || ByteCodeCommand.voidreturn.equals(opCode) + || ByteCodeCommand.iadd.equals(opCode) + || ByteCodeCommand.astore_1.equals(opCode) + || ByteCodeCommand.ireturn.equals(opCode)) { + + NoOperandCmd cmd = new NoOperandCmd(clzFile, opCode); + cmds.add(cmd); + } else { + throw new RuntimeException("Sorry, the java instruction " + opCode + " has not been implemented"); + } + + } + + calcuateOffset(cmds); + + ByteCodeCommand[] result = new ByteCodeCommand[cmds.size()]; + cmds.toArray(result); + return result; + } + + private static void calcuateOffset(List cmds) { + + int offset = 0; + for (ByteCodeCommand cmd : cmds) { + cmd.setOffset(offset); + offset += cmd.getLength(); + } + + } + + private static class CommandIterator { + String codes = null; + int pos = 0; + + CommandIterator(String codes) { + this.codes = codes; + } + + public boolean hasNext() { + return pos < this.codes.length(); + } + + public String next2CharAsString() { + String result = codes.substring(pos, pos + 2); + pos += 2; + return result; + } + + public int next2CharAsInt() { + String s = this.next2CharAsString(); + return Integer.valueOf(s, 16).intValue(); + } + + } +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/ComparisonCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/ComparisonCmd.java new file mode 100644 index 0000000000..e195966a83 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/ComparisonCmd.java @@ -0,0 +1,79 @@ +package mini_jvm.cmd; + + +import mini_jvm.clz.ClassFile; +import mini_jvm.engine.ExecutionResult; +import mini_jvm.engine.JavaObject; +import mini_jvm.engine.StackFrame; + +public class ComparisonCmd extends TwoOperandCmd { + + protected ComparisonCmd(ClassFile clzFile, String opCode) { + super(clzFile, opCode); + + } + + + @Override + public void execute(StackFrame frame, ExecutionResult result) { + + if(ByteCodeCommand.if_icmp_ge.equals(this.getOpCode())){ + //注意次序 + JavaObject jo2 = frame.getOprandStack().pop(); + JavaObject jo1 = frame.getOprandStack().pop(); + + if(jo1.getIntValue() >= jo2.getIntValue()){ + + this.setJumpResult(result); + + } + + } else if(ByteCodeCommand.if_icmple.equals(this.getOpCode())){ + //注意次序 + JavaObject jo2 = frame.getOprandStack().pop(); + JavaObject jo1 = frame.getOprandStack().pop(); + + if(jo1.getIntValue() <= jo2.getIntValue()){ + this.setJumpResult(result); + } + + } else if(ByteCodeCommand.goto_no_condition.equals(this.getOpCode())){ + this.setJumpResult(result); + + } else{ + throw new RuntimeException(this.getOpCode() + "has not been implemented"); + } + + + + + } + + private int getOffsetFromStartCmd(){ + //If the comparison succeeds, the unsigned branchbyte1 and branchbyte2 + //are used to construct a signed 16-bit offset, where the offset is calculated + //to be (branchbyte1 << 8) | branchbyte2. Execution then proceeds at that + //offset from the address of the opcode of this if_icmp instruction + + + int index1 = this.getOprand1(); + int index2 = this.getOprand2(); + short offsetFromCurrent = (short)(index1 << 8 | index2); + return this.getOffset() + offsetFromCurrent ; + } + private void setJumpResult(ExecutionResult result){ + + int offsetFromStartCmd = this.getOffsetFromStartCmd(); + + result.setNextAction(ExecutionResult.JUMP); + result.setNextCmdOffset(offsetFromStartCmd); + } + + @Override + public String toString() { + int index = this.getIndex(); + String text = this.getReadableCodeText(); + return this.getOffset()+":"+ this.getOpCode() + " "+text + " " + this.getOffsetFromStartCmd(); + } + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/GetFieldCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/GetFieldCmd.java new file mode 100644 index 0000000000..b84aa9e15e --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/GetFieldCmd.java @@ -0,0 +1,37 @@ +package mini_jvm.cmd; + + +import mini_jvm.clz.ClassFile; +import mini_jvm.constant.FieldRefInfo; +import mini_jvm.engine.ExecutionResult; +import mini_jvm.engine.JavaObject; +import mini_jvm.engine.StackFrame; + +public class GetFieldCmd extends TwoOperandCmd { + + public GetFieldCmd(ClassFile clzFile, String opCode) { + super(clzFile,opCode); + } + + @Override + public String toString() { + + return super.getOperandAsField(); + } + + @Override + public void execute(StackFrame frame, ExecutionResult result) { + + FieldRefInfo fieldRef = (FieldRefInfo)this.getConstantInfo(this.getIndex()); + String fieldName = fieldRef.getFieldName(); + JavaObject jo = frame.getOprandStack().pop(); + JavaObject fieldValue = jo.getFieldValue(fieldName); + + frame.getOprandStack().push(fieldValue); + + + + } + + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/GetStaticFieldCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/GetStaticFieldCmd.java new file mode 100644 index 0000000000..a1696b7ffd --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/GetStaticFieldCmd.java @@ -0,0 +1,39 @@ +package mini_jvm.cmd; + + +import mini_jvm.clz.ClassFile; +import mini_jvm.constant.FieldRefInfo; +import mini_jvm.engine.ExecutionResult; +import mini_jvm.engine.Heap; +import mini_jvm.engine.JavaObject; +import mini_jvm.engine.StackFrame; + +public class GetStaticFieldCmd extends TwoOperandCmd { + + public GetStaticFieldCmd(ClassFile clzFile, String opCode) { + super(clzFile,opCode); + + } + + @Override + public String toString() { + + return super.getOperandAsField(); + } + + @Override + public void execute(StackFrame frame, ExecutionResult result) { + FieldRefInfo info = (FieldRefInfo)this.getConstantInfo(this.getIndex()); + String className = info.getClassName(); + String fieldName = info.getFieldName(); + String fieldType = info.getFieldType(); + + if("java/lang/System".equals(className) + && "out".equals(fieldName) + && "Ljava/io/PrintStream;".equals(fieldType)){ + JavaObject jo = Heap.getInstance().newObject(className); + frame.getOprandStack().push(jo); + } + //TODO 处理非System.out的情况 + } +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/IncrementCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/IncrementCmd.java new file mode 100644 index 0000000000..3afe4df883 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/IncrementCmd.java @@ -0,0 +1,39 @@ +package mini_jvm.cmd; + + +import mini_jvm.clz.ClassFile; +import mini_jvm.engine.ExecutionResult; +import mini_jvm.engine.Heap; +import mini_jvm.engine.JavaObject; +import mini_jvm.engine.StackFrame; + +public class IncrementCmd extends TwoOperandCmd { + + public IncrementCmd(ClassFile clzFile, String opCode) { + super(clzFile, opCode); + + } + + @Override + public String toString() { + + return this.getOffset()+":"+this.getOpCode()+ " " +this.getReadableCodeText(); + } + + @Override + public void execute(StackFrame frame, ExecutionResult result) { + + int index = this.getOprand1(); + + int constValue = this.getOprand2(); + + int currentValue = frame.getLocalVariableValue(index).getIntValue(); + + JavaObject jo = Heap.getInstance().newInt(constValue+currentValue); + + frame.setLocalVariableValue(index, jo); + + + } + +} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/InvokeSpecialCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/InvokeSpecialCmd.java new file mode 100644 index 0000000000..ae0cd08d9b --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/InvokeSpecialCmd.java @@ -0,0 +1,46 @@ +package mini_jvm.cmd; + + +import mini_jvm.clz.ClassFile; +import mini_jvm.constant.MethodRefInfo; +import mini_jvm.engine.ExecutionResult; +import mini_jvm.engine.MethodArea; +import mini_jvm.engine.StackFrame; +import mini_jvm.method.Method; + +public class InvokeSpecialCmd extends TwoOperandCmd { + + public InvokeSpecialCmd(ClassFile clzFile, String opCode) { + super(clzFile,opCode); + + } + + @Override + public String toString() { + + return super.getOperandAsMethod(); + } + @Override + public void execute(StackFrame frame, ExecutionResult result) { + + + MethodRefInfo methodRefInfo = (MethodRefInfo)this.getConstantInfo(this.getIndex()); + + // 我们不用实现jang.lang.Object 的init方法 + if(methodRefInfo.getClassName().equals("java/lang/Object") + && methodRefInfo.getMethodName().equals("")){ + return ; + + } + Method nextMethod = MethodArea.getInstance().getMethod(methodRefInfo); + + + result.setNextAction(ExecutionResult.PAUSE_AND_RUN_NEW_FRAME); + result.setNextMethod(nextMethod); + + + + } + + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/InvokeVirtualCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/InvokeVirtualCmd.java new file mode 100644 index 0000000000..0414b98f18 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/InvokeVirtualCmd.java @@ -0,0 +1,85 @@ +package mini_jvm.cmd; + + +import mini_jvm.clz.ClassFile; +import mini_jvm.constant.MethodRefInfo; +import mini_jvm.engine.ExecutionResult; +import mini_jvm.engine.JavaObject; +import mini_jvm.engine.MethodArea; +import mini_jvm.engine.StackFrame; +import mini_jvm.method.Method; + +public class InvokeVirtualCmd extends TwoOperandCmd { + + public InvokeVirtualCmd(ClassFile clzFile,String opCode) { + super(clzFile,opCode); + } + + @Override + public String toString() { + + return super.getOperandAsMethod(); + } + + private boolean isSystemOutPrintlnMethod(String className, String methodName){ + return "java/io/PrintStream".equals(className) + && "println".equals(methodName); + } + @Override + public void execute(StackFrame frame, ExecutionResult result) { + + //先得到对该方法的描述 + MethodRefInfo methodRefInfo = (MethodRefInfo)this.getConstantInfo(this.getIndex()); + + String className = methodRefInfo.getClassName(); + String methodName = methodRefInfo.getMethodName(); + + // 我们没有实现System.out.println方法, 所以也不用建立新的栈帧, 直接调用Java的方法, 打印出来即可。 + if(isSystemOutPrintlnMethod(className,methodName)){ + JavaObject jo = (JavaObject)frame.getOprandStack().pop(); + String value = jo.toString(); + System.err.println("-------------------"+value+"----------------"); + + // 这里就是那个out对象, 因为是个假的,直接pop出来 + frame.getOprandStack().pop(); + + return; + } + + //注意:多态, 这才是真正的对象, 先从该对象的class 中去找对应的方法,找不到的话再去找父类的方法 + JavaObject jo = frame.getOprandStack().peek(); + + MethodArea ma = MethodArea.getInstance(); + + Method m = null; + + String currentClassName = jo.getClassName(); + + while(currentClassName != null){ + + ClassFile currentClassFile = ma.findClassFile(currentClassName); + + m = currentClassFile.getMethod(methodRefInfo.getMethodName(), + methodRefInfo.getParamAndReturnType()); + if(m != null){ + + break; + + } else{ + //查找父类 + currentClassName = currentClassFile.getSuperClassName(); + } + } + + if(m == null){ + throw new RuntimeException("Can't find method for :" + methodRefInfo.toString()); + } + + + result.setNextAction(ExecutionResult.PAUSE_AND_RUN_NEW_FRAME); + + result.setNextMethod(m); + } + + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/LdcCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/LdcCmd.java new file mode 100644 index 0000000000..2ada87ca6d --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/LdcCmd.java @@ -0,0 +1,51 @@ +package mini_jvm.cmd; + + +import mini_jvm.clz.ClassFile; +import mini_jvm.constant.ConstantInfo; +import mini_jvm.constant.ConstantPool; +import mini_jvm.constant.StringInfo; +import mini_jvm.engine.ExecutionResult; +import mini_jvm.engine.Heap; +import mini_jvm.engine.JavaObject; +import mini_jvm.engine.StackFrame; + +public class LdcCmd extends OneOperandCmd { + + public LdcCmd(ClassFile clzFile, String opCode) { + super(clzFile,opCode); + } + + @Override + public String toString() { + + ConstantInfo info = getConstantInfo(this.getOperand()); + + String value = "TBD"; + if(info instanceof StringInfo){ + StringInfo strInfo = (StringInfo)info; + value = strInfo.toString(); + } + + return this.getOffset()+":"+this.getOpCode()+" " + this.getReadableCodeText() + " "+ value; + + } + public void execute(StackFrame frame, ExecutionResult result){ + + ConstantPool pool = this.getConstantPool(); + ConstantInfo info = (ConstantInfo)pool.getConstantInfo(this.getOperand()); + + if(info instanceof StringInfo){ + StringInfo strInfo = (StringInfo)info; + String value = strInfo.toString(); + JavaObject jo = Heap.getInstance().newString(value); + frame.getOprandStack().push(jo); + } + else{ + //TBD 处理其他类型 + throw new RuntimeException("Only support StringInfo constant"); + } + + + } +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/NewObjectCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/NewObjectCmd.java new file mode 100644 index 0000000000..ef07cb7dcb --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/NewObjectCmd.java @@ -0,0 +1,39 @@ +package mini_jvm.cmd; + + +import mini_jvm.clz.ClassFile; +import mini_jvm.constant.ClassInfo; +import mini_jvm.engine.ExecutionResult; +import mini_jvm.engine.Heap; +import mini_jvm.engine.JavaObject; +import mini_jvm.engine.StackFrame; + +public class NewObjectCmd extends TwoOperandCmd{ + + public NewObjectCmd(ClassFile clzFile, String opCode){ + super(clzFile,opCode); + } + + @Override + public String toString() { + + return super.getOperandAsClassInfo(); + } + public void execute(StackFrame frame, ExecutionResult result){ + + int index = this.getIndex(); + + ClassInfo info = (ClassInfo)this.getConstantInfo(index); + + String clzName = info.getClassName(); + + //在Java堆上创建一个实例 + JavaObject jo = Heap.getInstance().newObject(clzName); + + frame.getOprandStack().push(jo); + + + + } + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/NoOperandCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/NoOperandCmd.java new file mode 100644 index 0000000000..da490b7931 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/NoOperandCmd.java @@ -0,0 +1,146 @@ +package mini_jvm.cmd; + + +import mini_jvm.clz.ClassFile; +import mini_jvm.engine.ExecutionResult; +import mini_jvm.engine.Heap; +import mini_jvm.engine.JavaObject; +import mini_jvm.engine.StackFrame; + +public class NoOperandCmd extends ByteCodeCommand{ + + public NoOperandCmd(ClassFile clzFile, String opCode) { + super(clzFile, opCode); + } + + @Override + public String toString() { + return this.getOffset()+":" +this.getOpCode() + " "+ this.getReadableCodeText(); + } + + @Override + public void execute(StackFrame frame, ExecutionResult result) { + + String opCode = this.getOpCode(); + + if(ByteCodeCommand.aload_0.equals(opCode)){ + + JavaObject jo = frame.getLocalVariableValue(0); + + frame.getOprandStack().push(jo); + + } else if(ByteCodeCommand.aload_1.equals(opCode)){ + + JavaObject jo = frame.getLocalVariableValue(1); + + frame.getOprandStack().push(jo); + + } else if(ByteCodeCommand.aload_2.equals(opCode)){ + + JavaObject jo = frame.getLocalVariableValue(2); + + frame.getOprandStack().push(jo); + + }else if(ByteCodeCommand.iload_1.equals(opCode)){ + + JavaObject jo = frame.getLocalVariableValue(1); + + frame.getOprandStack().push(jo); + + } else if (ByteCodeCommand.iload_2.equals(opCode)){ + + JavaObject jo = frame.getLocalVariableValue(2); + + frame.getOprandStack().push(jo); + + } else if (ByteCodeCommand.iload_3.equals(opCode)){ + + JavaObject jo = frame.getLocalVariableValue(3); + + frame.getOprandStack().push(jo); + + }else if (ByteCodeCommand.fload_3.equals(opCode)){ + + JavaObject jo = frame.getLocalVariableValue(3); + + frame.getOprandStack().push(jo); + + } + else if (ByteCodeCommand.voidreturn.equals(opCode)){ + + result.setNextAction(ExecutionResult.EXIT_CURRENT_FRAME); + + } else if(ByteCodeCommand.ireturn.equals(opCode)){ + StackFrame callerFrame = frame.getCallerFrame(); + JavaObject jo = frame.getOprandStack().pop(); + callerFrame.getOprandStack().push(jo); + + } else if(ByteCodeCommand.freturn.equals(opCode)){ + + StackFrame callerFrame = frame.getCallerFrame(); + JavaObject jo = frame.getOprandStack().pop(); + callerFrame.getOprandStack().push(jo); + } + + else if(ByteCodeCommand.astore_1.equals(opCode)){ + + JavaObject jo = frame.getOprandStack().pop(); + + frame.setLocalVariableValue(1, jo); + + } else if(ByteCodeCommand.dup.equals(opCode)){ + + JavaObject jo = frame.getOprandStack().peek(); + frame.getOprandStack().push(jo); + + } else if(ByteCodeCommand.iconst_0.equals(opCode)){ + + JavaObject jo = Heap.getInstance().newInt(0); + + frame.getOprandStack().push(jo); + + } else if(ByteCodeCommand.iconst_1.equals(opCode)){ + + JavaObject jo = Heap.getInstance().newInt(1); + + frame.getOprandStack().push(jo); + + } else if(ByteCodeCommand.istore_1.equals(opCode)){ + + JavaObject jo = frame.getOprandStack().pop(); + + frame.setLocalVariableValue(1, jo); + + } else if(ByteCodeCommand.istore_2.equals(opCode)){ + + JavaObject jo = frame.getOprandStack().pop(); + + frame.setLocalVariableValue(2, jo); + + } else if(ByteCodeCommand.iadd.equals(opCode)){ + + JavaObject jo1 = frame.getOprandStack().pop(); + JavaObject jo2 = frame.getOprandStack().pop(); + + JavaObject sum = Heap.getInstance().newInt(jo1.getIntValue()+jo2.getIntValue()); + + frame.getOprandStack().push(sum); + + } else if (ByteCodeCommand.aconst_null.equals(opCode)){ + + frame.getOprandStack().push(null); + + } + else{ + throw new RuntimeException("you must forget to implement the operation :" + opCode); + } + + + } + + + public int getLength(){ + return 1; + } + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/OneOperandCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/OneOperandCmd.java new file mode 100644 index 0000000000..0ffadfbd2b --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/OneOperandCmd.java @@ -0,0 +1,29 @@ +package mini_jvm.cmd; + +import mini_jvm.clz.ClassFile; + + + +public abstract class OneOperandCmd extends ByteCodeCommand { + + private int operand; + + public OneOperandCmd(ClassFile clzFile, String opCode) { + super(clzFile, opCode); + + } + public int getOperand() { + + return this.operand; + } + + public void setOperand(int oprand1) { + this.operand = oprand1; + + } + public int getLength(){ + return 2; + } + + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/PutFieldCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/PutFieldCmd.java new file mode 100644 index 0000000000..94652375fd --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/PutFieldCmd.java @@ -0,0 +1,44 @@ +package mini_jvm.cmd; + + +import mini_jvm.clz.ClassFile; +import mini_jvm.constant.ClassInfo; +import mini_jvm.constant.FieldRefInfo; +import mini_jvm.constant.NameAndTypeInfo; +import mini_jvm.engine.ExecutionResult; +import mini_jvm.engine.JavaObject; +import mini_jvm.engine.StackFrame; + +public class PutFieldCmd extends TwoOperandCmd { + + public PutFieldCmd(ClassFile clzFile, String opCode) { + super(clzFile,opCode); + } + + @Override + public String toString() { + + return super.getOperandAsField(); + } + @Override + public void execute(StackFrame frame, ExecutionResult result) { + + FieldRefInfo fieldRef = (FieldRefInfo)this.getConstantInfo(this.getIndex()); + + ClassInfo clzInfo = (ClassInfo)this.getConstantInfo(fieldRef.getClassInfoIndex()); + NameAndTypeInfo nameTypeInfo = (NameAndTypeInfo)this.getConstantInfo(fieldRef.getNameAndTypeIndex()); + // for example : name + String fieldName = nameTypeInfo.getName(); + // for example : Ljava/lang/String : 注意:我们不再检查类型 + String fieldType = nameTypeInfo.getTypeInfo(); + + JavaObject fieldValue = frame.getOprandStack().pop(); + JavaObject objectRef = frame.getOprandStack().pop(); + + objectRef.setFieldValue(fieldName, fieldValue); + + } + + + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/TwoOperandCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/TwoOperandCmd.java new file mode 100644 index 0000000000..93ea4b02bf --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/TwoOperandCmd.java @@ -0,0 +1,67 @@ +package mini_jvm.cmd; + + +import mini_jvm.clz.ClassFile; +import mini_jvm.constant.ClassInfo; +import mini_jvm.constant.ConstantInfo; +import mini_jvm.constant.FieldRefInfo; +import mini_jvm.constant.MethodRefInfo; + +public abstract class TwoOperandCmd extends ByteCodeCommand{ + + int oprand1 = -1; + int oprand2 = -1; + + public int getOprand1() { + return oprand1; + } + + public void setOprand1(int oprand1) { + this.oprand1 = oprand1; + } + + public void setOprand2(int oprand2) { + this.oprand2 = oprand2; + } + + public int getOprand2() { + return oprand2; + } + + public TwoOperandCmd(ClassFile clzFile, String opCode) { + super(clzFile, opCode); + } + + public int getIndex(){ + int oprand1 = this.getOprand1(); + int oprand2 = this.getOprand2(); + int index = oprand1 << 8 | oprand2; + return index; + } + + protected String getOperandAsClassInfo(){ + int index = getIndex(); + String codeTxt = getReadableCodeText(); + ClassInfo info = (ClassInfo)getConstantInfo(index); + return this.getOffset()+":"+this.getOpCode()+" "+ codeTxt +" "+ info.getClassName(); + } + + protected String getOperandAsMethod(){ + int index = getIndex(); + String codeTxt = getReadableCodeText(); + ConstantInfo constInfo = this.getConstantInfo(index); + MethodRefInfo info = (MethodRefInfo)this.getConstantInfo(index); + return this.getOffset()+":"+this.getOpCode()+" " + codeTxt +" "+ info.toString(); + } + + protected String getOperandAsField(){ + int index = getIndex(); + + String codeTxt = getReadableCodeText(); + FieldRefInfo info = (FieldRefInfo)this.getConstantInfo(index); + return this.getOffset()+":"+this.getOpCode()+" " + codeTxt +" "+ info.toString(); + } + public int getLength(){ + return 3; + } +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ClassInfo.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ClassInfo.java new file mode 100644 index 0000000000..8b974772fe --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ClassInfo.java @@ -0,0 +1,26 @@ +package mini_jvm.constant; + +public class ClassInfo extends ConstantInfo { + private final int type = ConstantInfo.CLASS_INFO; + + private int utf8Index ; + public ClassInfo(ConstantPool pool) { + super(pool); + } + + public int getUtf8Index() { + return utf8Index; + } + public void setUtf8Index(int utf8Index) { + this.utf8Index = utf8Index; + } + public int getType() { + return type; + } + + public String getClassName() { + int index = getUtf8Index(); + UTF8Info utf8Info = (UTF8Info)constantPool.getConstantInfo(index); + return utf8Info.getValue(); + } +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ConstantInfo.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ConstantInfo.java new file mode 100644 index 0000000000..54f89aacd8 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ConstantInfo.java @@ -0,0 +1,28 @@ +package mini_jvm.constant; + +public abstract class ConstantInfo { + public static final int UTF8_INFO = 1; + public static final int FLOAT_INFO = 4; + public static final int CLASS_INFO = 7; + public static final int STRING_INFO = 8; + public static final int FIELD_INFO = 9; + public static final int METHOD_INFO = 10; + public static final int NAME_AND_TYPE_INFO = 12; + + protected ConstantPool constantPool; + + public ConstantInfo(){} + + public ConstantInfo(ConstantPool pool) { + this.constantPool = pool; + } + public abstract int getType(); + + public ConstantPool getConstantPool() { + return constantPool; + } + public ConstantInfo getConstantInfo(int index){ + return this.constantPool.getConstantInfo(index); + } + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ConstantPool.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ConstantPool.java new file mode 100644 index 0000000000..c5d8348d89 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ConstantPool.java @@ -0,0 +1,25 @@ +package mini_jvm.constant; + +import java.util.ArrayList; +import java.util.List; + +public class ConstantPool { + + private List constantInfos = new ArrayList(); + + public ConstantPool(){} + + public void addConstantInfo(ConstantInfo info){ + this.constantInfos.add(info); + } + + public ConstantInfo getConstantInfo(int index){ + return this.constantInfos.get(index); + } + public String getUTF8String(int index){ + return ((UTF8Info)this.constantInfos.get(index)).getValue(); + } + public Object getSize() { + return this.constantInfos.size() -1; + } +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/FieldRefInfo.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/FieldRefInfo.java new file mode 100644 index 0000000000..be474180d8 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/FieldRefInfo.java @@ -0,0 +1,49 @@ +package mini_jvm.constant; + +public class FieldRefInfo extends ConstantInfo{ + private final int type = ConstantInfo.FIELD_INFO; + + private int classInfoIndex; + private int nameAndTypeIndex; + + public FieldRefInfo(ConstantPool pool) { + super(pool); + } + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return getClassName() +" : "+ typeInfo.getName() + ":" + typeInfo.getTypeInfo() +"]"; + } + + public String getClassName(){ + ClassInfo classInfo = (ClassInfo) this.getConstantInfo(this.getClassInfoIndex()); + UTF8Info utf8Info = (UTF8Info)this.getConstantInfo(classInfo.getUtf8Index()); + return utf8Info.getValue(); + } + + public String getFieldName(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getFieldType(){ + NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/MethodRefInfo.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/MethodRefInfo.java new file mode 100644 index 0000000000..8e3789e1b4 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/MethodRefInfo.java @@ -0,0 +1,52 @@ +package mini_jvm.constant; + +public class MethodRefInfo extends ConstantInfo { + + private final int type = ConstantInfo.METHOD_INFO; + + private int classInfoIndex; + private int nameAndTypeIndex; + + public MethodRefInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + + public int getClassInfoIndex() { + return classInfoIndex; + } + public void setClassInfoIndex(int classInfoIndex) { + this.classInfoIndex = classInfoIndex; + } + public int getNameAndTypeIndex() { + return nameAndTypeIndex; + } + public void setNameAndTypeIndex(int nameAndTypeIndex) { + this.nameAndTypeIndex = nameAndTypeIndex; + } + + public String toString(){ + return getClassName() +" : "+ this.getMethodName() + " : " + this.getParamAndReturnType() ; + } + + public String getClassName(){ + ConstantPool pool = this.getConstantPool(); + ClassInfo clzInfo = (ClassInfo)pool.getConstantInfo(this.getClassInfoIndex()); + return clzInfo.getClassName(); + } + + public String getMethodName(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getName(); + } + + public String getParamAndReturnType(){ + ConstantPool pool = this.getConstantPool(); + NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); + return typeInfo.getTypeInfo(); + } +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/NameAndTypeInfo.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/NameAndTypeInfo.java new file mode 100644 index 0000000000..142787ce16 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/NameAndTypeInfo.java @@ -0,0 +1,45 @@ +package mini_jvm.constant; + +public class NameAndTypeInfo extends ConstantInfo{ + public final int type = ConstantInfo.NAME_AND_TYPE_INFO; + + private int index1; + private int index2; + + public NameAndTypeInfo(ConstantPool pool) { + super(pool); + } + + public int getIndex1() { + return index1; + } + public void setIndex1(int index1) { + this.index1 = index1; + } + public int getIndex2() { + return index2; + } + public void setIndex2(int index2) { + this.index2 = index2; + } + public int getType() { + return type; + } + + + public String getName(){ + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info1 = (UTF8Info)pool.getConstantInfo(index1); + return utf8Info1.getValue(); + } + + public String getTypeInfo(){ + ConstantPool pool = this.getConstantPool(); + UTF8Info utf8Info2 = (UTF8Info)pool.getConstantInfo(index2); + return utf8Info2.getValue(); + } + + public String toString(){ + return "(" + getName() + "," + getTypeInfo()+")"; + } +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/NullConstantInfo.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/NullConstantInfo.java new file mode 100644 index 0000000000..6c3bcad2df --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/NullConstantInfo.java @@ -0,0 +1,12 @@ +package mini_jvm.constant; + +public class NullConstantInfo extends ConstantInfo { + + public NullConstantInfo(){} + + @Override + public int getType() { + return -1; + } + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/StringInfo.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/StringInfo.java new file mode 100644 index 0000000000..8b432cffb7 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/StringInfo.java @@ -0,0 +1,25 @@ +package mini_jvm.constant; + +public class StringInfo extends ConstantInfo{ + private final int type = ConstantInfo.STRING_INFO; + private int index; + + public StringInfo(ConstantPool pool) { + super(pool); + } + + public int getType() { + return type; + } + public int getIndex() { + return index; + } + public void setIndex(int index) { + this.index = index; + } + + public String toString(){ + return this.getConstantPool().getUTF8String(index); + } + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/UTF8Info.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/UTF8Info.java new file mode 100644 index 0000000000..2c2253e622 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/UTF8Info.java @@ -0,0 +1,33 @@ +package mini_jvm.constant; + +public class UTF8Info extends ConstantInfo{ + private final int type = ConstantInfo.UTF8_INFO; + + private int length ; + private String value; + + public UTF8Info(ConstantPool pool) { + super(pool); + } + + public int getLength() { + return length; + } + public void setLength(int length) { + this.length = length; + } + public String getValue() { + return value; + } + public void setValue(String value) { + this.value = value; + } + public int getType() { + return type; + } + + @Override + public String toString() { + return "UTF8Info [type=" + type + ", length=" + length + ", value=" + value +")]"; + } +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/ExecutionResult.java b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/ExecutionResult.java new file mode 100644 index 0000000000..58c9d26dca --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/ExecutionResult.java @@ -0,0 +1,57 @@ +package mini_jvm.engine; + + +import mini_jvm.method.Method; + +public class ExecutionResult { + public static final int RUN_NEXT_CMD = 1; + public static final int JUMP = 2; + public static final int EXIT_CURRENT_FRAME = 3; + public static final int PAUSE_AND_RUN_NEW_FRAME = 4; + + private int nextAction = RUN_NEXT_CMD; + + private int nextCmdOffset = 0; + + private Method nextMethod; + + public Method getNextMethod() { + return nextMethod; + } + public void setNextMethod(Method nextMethod) { + this.nextMethod = nextMethod; + } + + + + public void setNextAction(int action){ + this.nextAction = action; + } + public boolean isPauseAndRunNewFrame(){ + return this.nextAction == PAUSE_AND_RUN_NEW_FRAME; + } + public boolean isExitCurrentFrame(){ + return this.nextAction == EXIT_CURRENT_FRAME; + } + + public boolean isRunNextCmd(){ + return this.nextAction == RUN_NEXT_CMD; + } + + public boolean isJump(){ + return this.nextAction == JUMP; + } + + public int getNextCmdOffset() { + return nextCmdOffset; + } + + public void setNextCmdOffset(int nextCmdOffset) { + this.nextCmdOffset = nextCmdOffset; + } + + + + + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/ExecutorEngine.java b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/ExecutorEngine.java new file mode 100644 index 0000000000..d7ef541eb8 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/ExecutorEngine.java @@ -0,0 +1,77 @@ +package mini_jvm.engine; + +import mini_jvm.method.Method; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +public class ExecutorEngine { + + private Stack stack = new Stack(); + + public ExecutorEngine() { + + } + + public void execute(Method mainMethod){ + + StackFrame mainFrame = StackFrame.create(mainMethod); + stack.push(mainFrame); + + while(!stack.empty()){ + + StackFrame frame = stack.peek(); + + ExecutionResult result = frame.execute(); + + if(result.isPauseAndRunNewFrame()){ + + Method nextMethod = result.getNextMethod(); + StackFrame nextFrame = StackFrame.create(nextMethod); + nextFrame.setCallerFrame(frame); + setupFunctionCallParams(frame,nextFrame); + + stack.push(nextFrame); + + } else { + stack.pop(); + } + } + + } + + + + private void setupFunctionCallParams(StackFrame currentFrame,StackFrame nextFrame) { + + Method nextMethod = nextFrame.getMethod(); + + + List paramList = nextMethod.getParameterList(); + + //加上1 是因为要把this也传递过去 + + int paramNum = paramList.size() + 1; + + + List values = new ArrayList(); + + //数据结构知识: 从栈中取出栈顶的x个元素 + while(paramNum>0){ + values.add(currentFrame.getOprandStack().pop()); + paramNum --; + } + //数据结构知识: 把一个列表倒序排列 + List params = new ArrayList(); + + for(int i=values.size()-1; i>=0 ;i--){ + params.add(values.get(i)); + } + + + nextFrame.setLocalVariableTable(params); + + } + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/Heap.java b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/Heap.java new file mode 100644 index 0000000000..4a197b98f6 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/Heap.java @@ -0,0 +1,39 @@ +package mini_jvm.engine; + +public class Heap { + + /** + * 没有实现垃圾回收, 所以对于下面新创建的对象, 并没有记录到一个数据结构当中 + */ + + private static Heap instance = new Heap(); + private Heap() { + } + public static Heap getInstance(){ + return instance; + } + public JavaObject newObject(String clzName){ + + JavaObject jo = new JavaObject(JavaObject.OBJECT); + jo.setClassName(clzName); + return jo; + } + + public JavaObject newString(String value){ + JavaObject jo = new JavaObject(JavaObject.STRING); + jo.setStringValue(value); + return jo; + } + + public JavaObject newFloat(float value){ + JavaObject jo = new JavaObject(JavaObject.FLOAT); + jo.setFloatValue(value); + return jo; + } + public JavaObject newInt(int value){ + JavaObject jo = new JavaObject(JavaObject.INT); + jo.setIntValue(value); + return jo; + } + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/JavaObject.java b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/JavaObject.java new file mode 100644 index 0000000000..51185b1056 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/JavaObject.java @@ -0,0 +1,71 @@ +package mini_jvm.engine; + +import java.util.HashMap; +import java.util.Map; + +public class JavaObject { + public static final int OBJECT = 1; + public static final int STRING = 2; + public static final int INT = 3; + public static final int FLOAT = 4; + + int type; + private String className; + + private Map fieldValues = new HashMap(); + + private String stringValue; + + private int intValue; + + private float floatValue; + + public void setFieldValue(String fieldName, JavaObject fieldValue){ + fieldValues.put(fieldName, fieldValue); + } + public JavaObject(int type){ + this.type = type; + } + public void setClassName(String className){ + this.className = className; + } + public void setStringValue(String value){ + stringValue = value; + } + public String getStringValue(){ + return this.stringValue; + } + public void setIntValue(int value) { + this.intValue = value; + } + public int getIntValue(){ + return this.intValue; + } + public int getType(){ + return type; + } + public JavaObject getFieldValue(String fieldName){ + return this.fieldValues.get(fieldName); + } + public String toString(){ + switch(this.getType()){ + case INT: + return String.valueOf(this.intValue); + case STRING: + return this.stringValue; + case OBJECT: + return this.className +":"+ this.fieldValues; + case FLOAT : + return String.valueOf(this.floatValue); + default: + return null; + } + } + public String getClassName(){ + return this.className; + } + public void setFloatValue(float value) { + this.floatValue = value; + } + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/MethodArea.java b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/MethodArea.java new file mode 100644 index 0000000000..29e7c5a380 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/MethodArea.java @@ -0,0 +1,90 @@ +package mini_jvm.engine; + +import mini_jvm.clz.ClassFile; +import mini_jvm.constant.MethodRefInfo; +import mini_jvm.loader.ClassFileLoader; +import mini_jvm.method.Method; + +import java.util.HashMap; +import java.util.Map; + + + +public class MethodArea { + + public static final MethodArea instance = new MethodArea(); + + /** + * 注意:我们做了极大的简化, ClassLoader 只有一个, 实际JVM中的ClassLoader,是一个双亲委托的模型 + */ + + private ClassFileLoader clzLoader = null; + + Map map = new HashMap(); + + private MethodArea(){ + } + + public static MethodArea getInstance(){ + return instance; + } + + public void setClassFileLoader(ClassFileLoader clzLoader){ + this.clzLoader = clzLoader; + } + + public Method getMainMethod(String className){ + + ClassFile clzFile = this.findClassFile(className); + + return clzFile.getMainMethod(); + } + + + public ClassFile findClassFile(String className){ + + if(map.get(className) != null){ + return map.get(className); + } + // 看来该class 文件还没有load过 + ClassFile clzFile = this.clzLoader.loadClass(className); + + map.put(className, clzFile); + + return clzFile; + + } + + + public Method getMethod(String className, String methodName, String paramAndReturnType){ + + ClassFile clz = this.findClassFile(className); + + Method m = clz.getMethod(methodName, paramAndReturnType); + + if(m == null){ + + throw new RuntimeException("method can't be found : \n" + + "class: " + className + + "method: " + methodName + + "paramAndReturnType: " + paramAndReturnType); + } + + return m; + } + + + public Method getMethod(MethodRefInfo methodRef){ + + ClassFile clz = this.findClassFile(methodRef.getClassName()); + + Method m = clz.getMethod(methodRef.getMethodName(), methodRef.getParamAndReturnType()); + + if(m == null){ + throw new RuntimeException("method can't be found : " + methodRef.toString()); + } + + return m; + + } +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/MiniJVM.java b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/MiniJVM.java new file mode 100644 index 0000000000..6781fed5ad --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/MiniJVM.java @@ -0,0 +1,30 @@ +package mini_jvm.engine; + +import mini_jvm.loader.ClassFileLoader; + +import java.io.FileNotFoundException; +import java.io.IOException; + + + +public class MiniJVM { + + public void run(String[]classPaths , String className) throws FileNotFoundException, IOException{ + + ClassFileLoader loader = new ClassFileLoader(); + for(int i=0;i operands = new ArrayList(); + + public void push(JavaObject jo){ + operands.add(jo); + } + public JavaObject pop(){ + int index = size()-1; + JavaObject jo = (JavaObject)operands.get(index); + operands.remove(index); + return jo; + + } + public JavaObject top(){ + int index = size()-1; + return (JavaObject)operands.get(index); + } + public int size(){ + return operands.size(); + } +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/StackFrame.java b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/StackFrame.java new file mode 100644 index 0000000000..65b4bc119d --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/StackFrame.java @@ -0,0 +1,126 @@ +package mini_jvm.engine; + +import mini_jvm.cmd.ByteCodeCommand; +import mini_jvm.method.Method; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + + +public class StackFrame { + + private List localVariableTable = new ArrayList(); + private Stack oprandStack = new Stack(); + + int index = 0; + + private Method m = null; + + private StackFrame callerFrame = null; + + public StackFrame getCallerFrame() { + return callerFrame; + } + + public void setCallerFrame(StackFrame callerFrame) { + this.callerFrame = callerFrame; + } + + public static StackFrame create(Method m){ + StackFrame frame = new StackFrame(m); + return frame; + } + + + private StackFrame(Method m) { + this.m = m; + } + + + + public JavaObject getLocalVariableValue(int index){ + return this.localVariableTable.get(index); + } + + public Stack getOprandStack(){ + return this.oprandStack; + } + + public int getNextCommandIndex(int offset){ + + ByteCodeCommand[] cmds = m.getCodeAttr().getCmds(); + for(int i=0;i values){ + this.localVariableTable = values; + } + + public void setLocalVariableValue(int index, JavaObject jo){ + //问题: 为什么要这么做?? + if(this.localVariableTable.size()-1 < index){ + for(int i=this.localVariableTable.size(); i<=index; i++){ + this.localVariableTable.add(null); + } + } + this.localVariableTable.set(index, jo); + + + } + + public Method getMethod(){ + return m; + } + + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/field/Field.java b/students/769232552/season_one/main/java/season_1/mini_jvm/field/Field.java new file mode 100644 index 0000000000..1379bd3715 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/field/Field.java @@ -0,0 +1,64 @@ +package mini_jvm.field; + + +import mini_jvm.attr.AttributeInfo; +import mini_jvm.attr.ConstantValue; +import mini_jvm.constant.ConstantPool; +import mini_jvm.constant.UTF8Info; +import mini_jvm.loader.ByteCodeIterator; + +public class Field { + private int accessFlag; + private int nameIndex; + private int descriptorIndex; + + private ConstantPool pool; + private ConstantValue constValue; + + public Field( int accessFlag, int nameIndex, int descriptorIndex,ConstantPool pool) { + + this.accessFlag = accessFlag; + this.nameIndex = nameIndex; + this.descriptorIndex = descriptorIndex; + this.pool = pool; + } + + public String toString() { + String name = ((UTF8Info)pool.getConstantInfo(this.nameIndex)).getValue(); + + String desc = ((UTF8Info)pool.getConstantInfo(this.descriptorIndex)).getValue(); + return name +":"+ desc; + } + + + public static Field parse(ConstantPool pool,ByteCodeIterator iter){ + + int accessFlag = iter.nextU2ToInt(); + int nameIndex = iter.nextU2ToInt(); + int descIndex = iter.nextU2ToInt(); + int attribCount = iter.nextU2ToInt(); + //System.out.println("field attribute count:"+ attribCount); + + Field f = new Field(accessFlag, nameIndex, descIndex,pool); + + for( int i=1; i<= attribCount; i++){ + int attrNameIndex = iter.nextU2ToInt(); + String attrName = pool.getUTF8String(attrNameIndex); + + if(AttributeInfo.CONST_VALUE.equals(attrName)){ + int attrLen = iter.nextU4ToInt(); + ConstantValue constValue = new ConstantValue(attrNameIndex, attrLen); + constValue.setConstValueIndex(iter.nextU2ToInt()); + f.setConstantValue(constValue); + } else{ + throw new RuntimeException("the attribute " + attrName + " has not been implemented yet."); + } + } + + return f; + } + public void setConstantValue(ConstantValue constValue) { + this.constValue = constValue; + } + +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ByteCodeIterator.java b/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ByteCodeIterator.java new file mode 100644 index 0000000000..318c48cf5b --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ByteCodeIterator.java @@ -0,0 +1,63 @@ +package mini_jvm.loader; + +import mini_jvm.util.Util; + +public class ByteCodeIterator { + + private byte[] byteCodes; + private int currentIndex = -1; + private int byteSize = 0; + + ByteCodeIterator(byte[] byteCodes){ + this.byteCodes = byteCodes; + this.byteSize = byteCodes.length; + } + + public boolean hasNext(){ + return currentIndex < byteSize; + } + + + public int nextU1ToInt(){ + return Util.byteToInt(new byte[]{byteCodes[++currentIndex]}); + } + + public int nextU2ToInt(){ + return Util.byteToInt(new byte[]{byteCodes[++currentIndex],byteCodes[++currentIndex]}); + } + + public int nextU4ToInt(){ + return Util.byteToInt(new byte[]{byteCodes[++currentIndex],byteCodes[++currentIndex], + byteCodes[++currentIndex],byteCodes[++currentIndex]}); + } + + public String nextU2ToHexString(){ + return Util.byteToHexString(new byte[]{byteCodes[++currentIndex],byteCodes[++currentIndex]}); + } + + public String nextU4ToHexString(){ + return Util.byteToHexString(new byte[]{byteCodes[++currentIndex],byteCodes[++currentIndex], + byteCodes[++currentIndex],byteCodes[++currentIndex]}); + } + + public String nextBytesLenAsString(int BytesLen) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < BytesLen; i++) { + char c = (char) byteCodes[++currentIndex]; + sb.append(c); + } + return sb.toString(); + } + + public String nextUxToHexString(int len) { + byte[] tmp = new byte[len]; + for (int i = 0; i < len; i++) { + tmp[i] = byteCodes[++currentIndex]; + } + return Util.byteToHexString(tmp).toLowerCase(); + } + + public void back(int n) { + this.currentIndex -= n; + } +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ClassFileLoader.java b/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ClassFileLoader.java new file mode 100644 index 0000000000..974914032e --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ClassFileLoader.java @@ -0,0 +1,144 @@ +package mini_jvm.loader; + +import mini_jvm.clz.ClassFile; +import org.apache.commons.io.IOUtils; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; + + +public class ClassFileLoader { + + private List clzPaths = new ArrayList(); + + public ClassFile loadClass(String className){ + byte[] byteCodes = readBinaryCode(className); + ClassFileParser classFileParser = new ClassFileParser(); + ClassFile clzFile = classFileParser.parse(byteCodes); + return clzFile; + } + + + /** + * 读取class文件的二进制代码 + * @param className + * @return + */ + public byte[] readBinaryCodeV1(String className) { + String clazzPath = getClassPath(className); + BufferedInputStream bins = null; + ByteArrayOutputStream bouts = new ByteArrayOutputStream(); + try { + bins = new BufferedInputStream(new FileInputStream(new File(clazzPath))); + byte[] buffer = new byte[1024]; + int length = -1; + while((length = bins.read(buffer)) != -1){ + bouts.write(buffer, 0, length); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + byte[] codes = bouts.toByteArray(); + //关闭流 + try { + if(bins != null){ + //调用外层流的close方法就关闭其装饰的内层流 + bins.close(); + } + if(bouts != null){ + bouts.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + return codes; + } + + /** + * 使用IOUtils.toByteArray()读取流文件 + */ + public byte[] readBinaryCode(String className){ + String clzPath = getClassPath(className); + File f = new File(clzPath); + try{ + return IOUtils.toByteArray(new FileInputStream(f)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + return null; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + + /** + * 从扫描根目录,获取指定类的绝对路径 + * @param className + * @return + */ + private String getClassPath(String className){ + String clazzPath = null; + //遍历clzPaths中所有路径 + for (String path : this.clzPaths){ + File file = new File(path); + clazzPath = getClassPath(className,file); + if(clazzPath!=null) break; + } + return clazzPath; + } + + private String getClassPath(String className, File file){ + String clazzPath = null; + if(file.exists()){ + //如果是目录,则遍历所有目录下的文件 + if(file.isDirectory()){ + File[] fs = file.listFiles(); + for (File f : fs){ + clazzPath = getClassPath(className,f); + } + }else { + //检查是否是该类对应的class文件 + if(isClazzFile(file.getName(),className)){ + clazzPath = file.getAbsolutePath(); + } + } + } + return clazzPath; + } + + private boolean isClazzFile(String filename , String className){ + String fileClazzName = null; + String [] names = filename.split("\\."); + if(names.length > 0){ + fileClazzName = names[0]; + } + return className.endsWith(fileClazzName); + } + + public void addClassPath(String path) { + clzPaths.add(path); + } + + public String getClassPath(){ + StringBuilder sb = new StringBuilder(); + int i = 0; + for (; i < clzPaths.size() - 1; i++) { + sb.append(clzPaths.get(i)); + sb.append(";"); + } + sb.append(clzPaths.get(i)); + return sb.toString(); + } + + public static void main(String[] args) { + String FULL_QUALIFIED_CLASS_NAME = "mini_jvm/test/EmployeeV1"; + String path1 = "D:\\worksapce\\gitRepo\\java_coding2017\\coding2017\\group23\\769232552\\coding\\target\\test-classes\\mini_jvm"; + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "mini_jvm.test.EmployeeV1"; + } +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ClassFileParser.java b/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ClassFileParser.java new file mode 100644 index 0000000000..6ea12db446 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ClassFileParser.java @@ -0,0 +1,159 @@ +package mini_jvm.loader; + + +import mini_jvm.clz.AccessFlag; +import mini_jvm.clz.ClassFile; +import mini_jvm.clz.ClassIndex; +import mini_jvm.constant.*; +import mini_jvm.field.Field; +import mini_jvm.method.Method; + +public class ClassFileParser { + + public ClassFile parse(byte[] byteCodes) { + ByteCodeIterator iterator = new ByteCodeIterator(byteCodes); + ClassFile clzFile = new ClassFile(); + + //magic number + String magicNumber = iterator.nextU4ToHexString(); + if (!"cafebabe".equals(magicNumber)) { + throw new RuntimeException("not java .class file!"); + } + int minorVersion = iterator.nextU2ToInt(); //次版本号 + int majorVersion = iterator.nextU2ToInt(); //主版本号 + clzFile.setMajorVersion(majorVersion); + clzFile.setMinorVersion(minorVersion); + + ConstantPool constantPool = parseConstantPool(iterator); //常量池 + clzFile.setConstPool(constantPool); + + AccessFlag accessFlag = parseAccessFlag(iterator); //解析访问标识 + clzFile.setAccessFlag(accessFlag); + + ClassIndex clzIndex = parseClassIndex(iterator); //解析类索引 + clzFile.setClassIndex(clzIndex); + + //解析接口,此处暂不支持 + parseInterfaces(iterator); + + //解析字段 + parseField(clzFile,iterator); + + //解析方法 + parseMethod(clzFile,iterator); + + return clzFile; + } + + private void parseMethod(ClassFile clzFile, ByteCodeIterator iterator) { + //方法个数 + int methodCount = iterator.nextU2ToInt(); + for (int i = 0; i < methodCount; i++) { + Method m = Method.parse(clzFile,iterator); + clzFile.addMethod(m); + } + } + + //解析字段 + private void parseField(ClassFile clzFile, ByteCodeIterator iterator) { + //字段个数 + int fieldsCount = iterator.nextU2ToInt(); + for (int i = 0; i < fieldsCount; i++) { + Field f = Field.parse(clzFile.getConstantPool(),iterator); + clzFile.addField(f); + } + } + + + private AccessFlag parseAccessFlag(ByteCodeIterator iterator) { + int accessValue = iterator.nextU2ToInt(); + AccessFlag accessFlag = new AccessFlag(accessValue); + + return accessFlag; + } + + private ClassIndex parseClassIndex(ByteCodeIterator iterator) { + ClassIndex clzIndex = new ClassIndex(); + + int thisClzIndex = iterator.nextU2ToInt(); + int superClzIndex = iterator.nextU2ToInt(); + clzIndex.setThisClassIndex(thisClzIndex); + clzIndex.setSuperClassIndex(superClzIndex); + + return clzIndex; + } + + private ConstantPool parseConstantPool(ByteCodeIterator iterator) { + //常量池 + ConstantPool constantPool = new ConstantPool(); + //常量池成员数 + int constantPoolLength = iterator.nextU2ToInt(); + //第0位补上一个占位符 + NullConstantInfo nullConstantInfo = new NullConstantInfo(); + constantPool.addConstantInfo(nullConstantInfo); + for (int i = 1; i < constantPoolLength; i++) { + int tag = iterator.nextU1ToInt(); + // tag = 1, UTF8_INFO + if (tag == 1) { + UTF8Info utf8Info = new UTF8Info(constantPool); + int length = iterator.nextU2ToInt(); + String value = iterator.nextBytesLenAsString(length); + utf8Info.setLength(length); + utf8Info.setValue(value); + constantPool.addConstantInfo(utf8Info); + } + // tag = 7, CLASS_INFO + else if (tag == 7) { + ClassInfo classInfo = new ClassInfo(constantPool); + int nameIndex = iterator.nextU2ToInt(); + classInfo.setUtf8Index(nameIndex); + constantPool.addConstantInfo(classInfo); + } + // tag = 8, STRING_INFO + else if (tag == 8) { + StringInfo stringInfo = new StringInfo(constantPool); + int stringIndex = iterator.nextU2ToInt(); + stringInfo.setIndex(stringIndex); + constantPool.addConstantInfo(stringInfo); + } + // tag = 9, Fieldref + else if (tag == 9) { + FieldRefInfo fieldRefInfo = new FieldRefInfo(constantPool); + int classInfoIndex = iterator.nextU2ToInt(); + int nameAndTypeIndex = iterator.nextU2ToInt(); + fieldRefInfo.setClassInfoIndex(classInfoIndex); + fieldRefInfo.setNameAndTypeIndex(nameAndTypeIndex); + constantPool.addConstantInfo(fieldRefInfo); + } + // tag = 10, MethodRef + else if (tag == 10) { + MethodRefInfo methodRefInfo = new MethodRefInfo(constantPool); + int classInfoIndex = iterator.nextU2ToInt(); + int nameAndTypeIndex = iterator.nextU2ToInt(); + methodRefInfo.setClassInfoIndex(classInfoIndex); + methodRefInfo.setNameAndTypeIndex(nameAndTypeIndex); + constantPool.addConstantInfo(methodRefInfo); + } + // tag = 12, NameAndType + else if (tag == 12) { + NameAndTypeInfo nameAndTypeInfo = new NameAndTypeInfo(constantPool); + int nameAndTypeIndex = iterator.nextU2ToInt(); + int descriptorIndex = iterator.nextU2ToInt(); + nameAndTypeInfo.setIndex1(nameAndTypeIndex); + nameAndTypeInfo.setIndex2(descriptorIndex); + constantPool.addConstantInfo(nameAndTypeInfo); + } else { + throw new RuntimeException("not realized tag " + tag); + } + } + + return constantPool; + } + + private void parseInterfaces(ByteCodeIterator iterator) { + int interfaceCount = iterator.nextU2ToInt(); + // TODO : 如果实现了interface, 这里需要解析 + System.out.println("interfaceCount:" + interfaceCount); + } +} + diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/method/Method.java b/students/769232552/season_one/main/java/season_1/mini_jvm/method/Method.java new file mode 100644 index 0000000000..6c026d3eb1 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/method/Method.java @@ -0,0 +1,151 @@ +package mini_jvm.method; + + +import mini_jvm.attr.AttributeInfo; +import mini_jvm.attr.CodeAttr; +import mini_jvm.clz.ClassFile; +import mini_jvm.cmd.ByteCodeCommand; +import mini_jvm.constant.ConstantPool; +import mini_jvm.constant.UTF8Info; +import mini_jvm.loader.ByteCodeIterator; + +import java.util.ArrayList; +import java.util.List; + +public class Method { + + private int accessFlag; + private int nameIndex; + private int descriptorIndex; + + private CodeAttr codeAttr; + private ClassFile clzFile; + + + public ClassFile getClzFile() { + return clzFile; + } + + public int getNameIndex() { + return nameIndex; + } + public int getDescriptorIndex() { + return descriptorIndex; + } + + public CodeAttr getCodeAttr() { + return codeAttr; + } + + public void setCodeAttr(CodeAttr code) { + this.codeAttr = code; + } + + public Method(ClassFile clzFile,int accessFlag, int nameIndex, int descriptorIndex) { + this.clzFile = clzFile; + this.accessFlag = accessFlag; + this.nameIndex = nameIndex; + this.descriptorIndex = descriptorIndex; + } + + public static Method parse(ClassFile clzFile, ByteCodeIterator iterator){ + int accessFlag = iterator.nextU2ToInt(); + int nameIndex = iterator.nextU2ToInt(); + int descIndex = iterator.nextU2ToInt(); + int attrCount = iterator.nextU2ToInt(); + + Method m = new Method(clzFile,accessFlag,nameIndex,descIndex); + + for (int i = 0; i < attrCount; i++) { + int attrNameIndex = iterator.nextU2ToInt(); + String attrName = clzFile.getConstantPool().getUTF8String(attrNameIndex); + iterator.back(2); + + if(AttributeInfo.CODE.equalsIgnoreCase(attrName)){ + CodeAttr codeAttr = CodeAttr.parse(clzFile,iterator); + m.setCodeAttr(codeAttr); + }else { + throw new RuntimeException("only CODE attribute is implemented , please implement the "+ attrName); + } + } + return m; + } + + + public String toString() { + + ConstantPool pool = this.clzFile.getConstantPool(); + StringBuilder buffer = new StringBuilder(); + String name = ((UTF8Info)pool.getConstantInfo(this.nameIndex)).getValue(); + String desc = ((UTF8Info)pool.getConstantInfo(this.descriptorIndex)).getValue(); + buffer.append(name).append(":").append(desc).append("\n"); + buffer.append(this.codeAttr.toString(pool)); + return buffer.toString(); + } + + + public ByteCodeCommand[] getCmds() { + return this.getCodeAttr().getCmds(); + } + + + private String getParamAndReturnType(){ + UTF8Info nameAndTypeInfo = (UTF8Info)this.getClzFile() + .getConstantPool().getConstantInfo(this.getDescriptorIndex()); + return nameAndTypeInfo.getValue(); + } + + public List getParameterList(){ + + // e.g. (Ljava/util/List;Ljava/lang/String;II)V + String paramAndType = getParamAndReturnType(); + + int first = paramAndType.indexOf("("); + int last = paramAndType.lastIndexOf(")"); + // e.g. Ljava/util/List;Ljava/lang/String;II + String param = paramAndType.substring(first+1, last); + + List paramList = new ArrayList(); + + if((null == param) || "".equals(param)){ + return paramList; + } + + while(!param.equals("")){ + + int pos = 0; + // 这是一个对象类型 + if(param.charAt(pos) == 'L'){ + + int end = param.indexOf(";"); + + if(end == -1){ + throw new RuntimeException("can't find the ; for a object type"); + } + paramList.add(param.substring(pos+1,end)); + + pos = end + 1; + + } + else if(param.charAt(pos) == 'I'){ + // int + paramList.add("I"); + pos ++; + + } + else if(param.charAt(pos) == 'F'){ + // float + paramList.add("F"); + pos ++; + + } else{ + throw new RuntimeException("the param has unsupported type:" + param); + } + + param = param.substring(pos); + + } + return paramList; + + } +} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/util/Util.java b/students/769232552/season_one/main/java/season_1/mini_jvm/util/Util.java new file mode 100644 index 0000000000..49f6bc0e39 --- /dev/null +++ b/students/769232552/season_one/main/java/season_1/mini_jvm/util/Util.java @@ -0,0 +1,22 @@ +package mini_jvm.util; + +public class Util { + public static int byteToInt(byte[] codes){ + String s1 = byteToHexString(codes); + return Integer.valueOf(s1, 16).intValue(); + } + + public static String byteToHexString(byte[] codes){ + StringBuffer buffer = new StringBuffer(); + for(int i=0;i binaryTree1 = new BinaryTree(); + BinaryTree binaryTree2 = new BinaryTree(); + Integer[] array1 = new Integer[]{3,4,1,2,5}; + Integer[] array2 = new Integer[]{3,1,4,5,2}; + binaryTree1.createBinaryTree(array1); + binaryTree2.createBinaryTree(array2); + binaryTree1.leftOrderScan(binaryTree1.getRoot()); + binaryTree2.leftOrderScan(binaryTree2.getRoot()); + } + + @Test + public void testInsert(){ + BinaryTree binaryTree3 = new BinaryTree(); + } +} \ No newline at end of file diff --git a/students/769232552/season_one/test/java/season_1/code01/LinkedListTest.java b/students/769232552/season_one/test/java/season_1/code01/LinkedListTest.java new file mode 100644 index 0000000000..5481783932 --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/code01/LinkedListTest.java @@ -0,0 +1,174 @@ +package code01; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by yaoyuan on 2017/3/8. + */ +public class LinkedListTest { + + @Test + public void testAdd() throws Exception { + + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + linklist.add(str); + } + + // size() + Assert.assertEquals(array.length,linklist.size()); + + //add(),get() + for (int i = 0; i < linklist.size(); i++){ + Assert.assertEquals(array[i],linklist.get(i)); + } + + } + + @Test + public void testAddWithIndex() throws Exception { + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + linklist.add(str); + } + + //add(),get() + for (int i = 0; i < linklist.size(); i++){ + Assert.assertEquals(array[i],linklist.get(i)); + } + + String str = "new"; + linklist.add(0,str); + Assert.assertEquals(str,linklist.get(0)); + + linklist.add(3,str); + Assert.assertEquals(str,linklist.get(3)); + + linklist.add(linklist.size() ,str); + Assert.assertEquals(str,linklist.get(linklist.size()-1)); + } + + @Test + public void testRemove() throws Exception { + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + linklist.add(str); + } + + //remove(),get() + Assert.assertEquals(linklist.remove(0),array[0]); + Assert.assertEquals(linklist.size(),array.length - 1); + + Assert.assertEquals(linklist.remove(linklist.size() - 1),array[array.length-1]); + Assert.assertEquals(linklist.size(),array.length - 2); + + } + + @Test + public void testAddFirst() throws Exception { + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + linklist.add(str); + } + //addFirst(),get() + String str = "new"; + linklist.addFirst(str); + Assert.assertEquals(str,linklist.get(0)); + Assert.assertEquals(linklist.size(),array.length + 1); + } + + @Test + public void testAddLast() throws Exception { + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + linklist.add(str); + } + //addLast(),get() + String str = "new"; + linklist.addLast(str); + Assert.assertEquals(str,linklist.get(linklist.size()-1)); + Assert.assertEquals(linklist.size(),array.length + 1); + } + + @Test + public void testRemoveFirst() throws Exception { + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + linklist.add(str); + } + //removeFirst(),get() + Assert.assertEquals(linklist.removeFirst(),array[0]); + Assert.assertEquals(linklist.size(),array.length - 1); + } + + @Test + public void testRemoveLast() throws Exception { + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + linklist.add(str); + } + //removeLast(),get() + Assert.assertEquals(linklist.removeLast(),array[array.length-1]); + Assert.assertEquals(linklist.size(),array.length - 1); + + } + + @Test + public void testReverse(){ + LinkedList linklist = new LinkedList(); + String[] array = new String[]{"a","b","c","d","e"}; + for (String str : array){ + linklist.add(str); + } + linklist.reverse(); + for(int i=0; i params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("Message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("Message")); + } +} diff --git a/students/769232552/season_one/test/java/season_1/code03/FileDownloaderTest.java b/students/769232552/season_one/test/java/season_1/code03/FileDownloaderTest.java new file mode 100644 index 0000000000..64d2d93c99 --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/code03/FileDownloaderTest.java @@ -0,0 +1,60 @@ +package code03; + +import code03.v1.FileDownloader; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import code03.v1.api.ConnectionManager; +import code03.v1.api.DownloadListener; +import code03.v1.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "http://litten.me/assets/blogImg/litten.png"; + + FileDownloader downloader = new FileDownloader(url); + + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 等待多线程下载程序执行完毕 + while (!downloadFinished) { + try { + System.out.println("还没有下载完成,休眠五秒"); + //休眠5秒 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("下载完成!"); + + + + } + +} diff --git a/students/769232552/season_one/test/java/season_1/code04/LRUPageFrameTest.java b/students/769232552/season_one/test/java/season_1/code04/LRUPageFrameTest.java new file mode 100644 index 0000000000..7448e3ee56 --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/code04/LRUPageFrameTest.java @@ -0,0 +1,38 @@ +package code04; + +import org.junit.Assert; + +import org.junit.Test; + + +public class LRUPageFrameTest { + + @Test + public void testAccess() { + LRUPageFrame frame = new LRUPageFrame(3); + frame.access(7); + frame.access(0); + frame.access(1); + //1,0,7 + Assert.assertEquals("1,0,7", frame.toString()); + frame.access(2); + //2,1,0 + Assert.assertEquals("2,1,0", frame.toString()); + frame.access(0); + //0,2,1 + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(0); + //0,2,1 + Assert.assertEquals("0,2,1", frame.toString()); + frame.access(3); + //3,0,2 + Assert.assertEquals("3,0,2", frame.toString()); + frame.access(0); + //0,3,2 + Assert.assertEquals("0,3,2", frame.toString()); + frame.access(4); + //4,0,3 + Assert.assertEquals("4,0,3", frame.toString()); + } + +} diff --git a/students/769232552/season_one/test/java/season_1/code05/StackUtilTest.java b/students/769232552/season_one/test/java/season_1/code05/StackUtilTest.java new file mode 100644 index 0000000000..e745e60fbc --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/code05/StackUtilTest.java @@ -0,0 +1,79 @@ +package code05; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +/** + * Created by yaoyuan on 2017/4/6. + */ +public class StackUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAddToBottom() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + + StackUtil.addToBottom(s, 0); + Assert.assertEquals("[0, 1, 2, 3]", s.toString()); + + } + + @Test + public void testReverse() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); + StackUtil.reverse(s); + Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString()); + } + + @Test + public void testRemove() { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + StackUtil.remove(s, 2); + Assert.assertEquals("[1, 3]", s.toString()); + } + + @Test + public void testGetTop() throws Exception { + Stack s = new Stack(); + s.push(1); + s.push(2); + s.push(3); + s.push(4); + s.push(5); + { + Object[] values = StackUtil.getTop(s, 3); + Assert.assertEquals(5, values[0]); + Assert.assertEquals(4, values[1]); + Assert.assertEquals(3, values[2]); + } + } + + @Test + public void testIsValidPairs() { + Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])")); + Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})")); + } + +} \ No newline at end of file diff --git a/students/769232552/season_one/test/java/season_1/code06/InfixExprTest.java b/students/769232552/season_one/test/java/season_1/code06/InfixExprTest.java new file mode 100644 index 0000000000..af0ea63370 --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/code06/InfixExprTest.java @@ -0,0 +1,49 @@ +package code06; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class InfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); + { + InfixExpr expr = new InfixExpr("2+3*4+5"); + expr.evaluate(); + Assert.assertEquals(19.0, expr.evaluate(), 0.001f); + } + { + InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); + Assert.assertEquals(100.0, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("3*20/2"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("20/2*3"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + { + InfixExpr expr = new InfixExpr("10-30+50"); + Assert.assertEquals(30, expr.evaluate(), 0.001f); + } + + } + +} diff --git a/students/769232552/season_one/test/java/season_1/code07/PostfixExprTest.java b/students/769232552/season_one/test/java/season_1/code07/PostfixExprTest.java new file mode 100644 index 0000000000..ace42b0db2 --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/code07/PostfixExprTest.java @@ -0,0 +1,42 @@ +package code07; + + + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class PostfixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + { + PostfixExpr expr = new PostfixExpr("6 5 2 3 + 8 * + 3 + *"); + Assert.assertEquals(288, expr.evaluate(),0.0f); + } + + { + //9+(3-1)*3+10/2 + PostfixExpr expr = new PostfixExpr("9 3 1 - 3 * + 10 2 / +"); + Assert.assertEquals(20, expr.evaluate(),0.0f); + } + + { + //10-2*3+50 + PostfixExpr expr = new PostfixExpr("10 2 3 * - 50 +"); + Assert.assertEquals(54, expr.evaluate(),0.0f); + } + } + +} diff --git a/students/769232552/season_one/test/java/season_1/code07/PrefixExprTest.java b/students/769232552/season_one/test/java/season_1/code07/PrefixExprTest.java new file mode 100644 index 0000000000..6626b3ba01 --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/code07/PrefixExprTest.java @@ -0,0 +1,45 @@ +package code07; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class PrefixExprTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testEvaluate() { + { + // 2*3+4*5 + PrefixExpr expr = new PrefixExpr("+ * 2 3 * 4 5"); + Assert.assertEquals(26, expr.evaluate(),0.001f); + } + { + // 4*2 + 6+9*2/3 -8 + PrefixExpr expr = new PrefixExpr("- + + 6 / * 2 9 3 * 4 2 8"); + Assert.assertEquals(12, expr.evaluate(),0.001f); + } + { + //(3+4)*5-6 + PrefixExpr expr = new PrefixExpr("- * + 3 4 5 6"); + Assert.assertEquals(29, expr.evaluate(),0.001f); + } + { + //1+((2+3)*4)-5 + PrefixExpr expr = new PrefixExpr("- + 1 * + 2 3 4 5"); + Assert.assertEquals(16, expr.evaluate(),0.001f); + } + + + } + +} diff --git a/students/769232552/season_one/test/java/season_1/code08/JosephusTest.java b/students/769232552/season_one/test/java/season_1/code08/JosephusTest.java new file mode 100644 index 0000000000..2212b059c1 --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/code08/JosephusTest.java @@ -0,0 +1,32 @@ +package code08; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class JosephusTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testExecute() { + Assert.assertEquals("[1, 3, 5, 0, 4, 2, 6]", Josephus.execute(7, 2).toString()); + } + + @Test + public void testExecute2() { + + Assert.assertEquals("[2, 5, 1, 6, 4, 0, 3]", Josephus.execute(7, 3).toString()); + + } + +} diff --git a/students/769232552/season_one/test/java/season_1/code09/QuickMinStackTest.java b/students/769232552/season_one/test/java/season_1/code09/QuickMinStackTest.java new file mode 100644 index 0000000000..8eac5ec714 --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/code09/QuickMinStackTest.java @@ -0,0 +1,34 @@ +package code09; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by yyglider on 2017/5/4. + */ +public class QuickMinStackTest { + QuickMinStack stack = new QuickMinStack(); + + @Before + public void before(){ + stack.push(3); + stack.push(4); + stack.push(2); + stack.push(1); + } + + @Test + public void findMin() throws Exception { + Assert.assertEquals(1,stack.findMin()); + stack.pop(); + Assert.assertEquals(2,stack.findMin()); + stack.pop(); + Assert.assertEquals(3,stack.findMin()); + stack.push(0); + Assert.assertEquals(0,stack.findMin()); + } + +} \ No newline at end of file diff --git a/students/769232552/season_one/test/java/season_1/code09/StackWithTwoQueuesTest.java b/students/769232552/season_one/test/java/season_1/code09/StackWithTwoQueuesTest.java new file mode 100644 index 0000000000..a01b6a5a07 --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/code09/StackWithTwoQueuesTest.java @@ -0,0 +1,27 @@ +package code09; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by yyglider on 2017/5/4. + */ +public class StackWithTwoQueuesTest { + @Test + public void pop() throws Exception { + StackWithTwoQueues stack = new StackWithTwoQueues(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + + Assert.assertEquals(4,stack.pop()); + Assert.assertEquals(3,stack.pop()); + Assert.assertEquals(2,stack.pop()); + Assert.assertEquals(1,stack.pop()); + + } + +} \ No newline at end of file diff --git a/students/769232552/season_one/test/java/season_1/code10/BinaryTreeUtilTest.java b/students/769232552/season_one/test/java/season_1/code10/BinaryTreeUtilTest.java new file mode 100644 index 0000000000..827ff1794b --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/code10/BinaryTreeUtilTest.java @@ -0,0 +1,79 @@ +package code10; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.*; + +/** + * Created by yyglider on 2017/5/9. + */ +public class BinaryTreeUtilTest { + + BinaryTreeNode root = null; + @Before + public void setUp() throws Exception { + root = new BinaryTreeNode(1); + root.setLeft(new BinaryTreeNode(2)); + root.setRight(new BinaryTreeNode(5)); + root.getLeft().setLeft(new BinaryTreeNode(3)); + root.getLeft().setRight(new BinaryTreeNode(4)); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testPreOrderVisit() { + + List result = BinaryTreeUtil.preOrderVisit(root); + Assert.assertEquals("[1, 2, 3, 4, 5]", result.toString()); + + + } + @Test + public void testInOrderVisit() { + + + List result = BinaryTreeUtil.inOrderVisit(root); + Assert.assertEquals("[3, 2, 4, 1, 5]", result.toString()); + + } + + @Test + public void testPostOrderVisit() { + + + List result = BinaryTreeUtil.postOrderVisit(root); + Assert.assertEquals("[3, 4, 2, 5, 1]", result.toString()); + + } + + + @Test + public void testInOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.inOrderWithoutRecursion(root); + Assert.assertEquals("[3, 2, 6, 4, 7, 1, 5]", result.toString()); + + } + @Test + public void testPreOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.preOrderWithoutRecursion(root); + Assert.assertEquals("[1, 2, 3, 4, 6, 7, 5]", result.toString()); + + } + +} \ No newline at end of file diff --git a/students/769232552/season_one/test/java/season_1/code11/BinarySearchTreeTest.java b/students/769232552/season_one/test/java/season_1/code11/BinarySearchTreeTest.java new file mode 100644 index 0000000000..e2f3d6a05d --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/code11/BinarySearchTreeTest.java @@ -0,0 +1,93 @@ +package code11; + +import static org.junit.Assert.fail; + +import code10.BinaryTreeNode; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + + +public class BinarySearchTreeTest { + + BinarySearchTree tree = null; + + @Before + public void setUp() throws Exception { + BinaryTreeNode root = new BinaryTreeNode(6); + root.left = new BinaryTreeNode(2); + root.right = new BinaryTreeNode(8); + root.left.left = new BinaryTreeNode(1); + root.left.right = new BinaryTreeNode(4); + root.left.right.left = new BinaryTreeNode(3); + tree = new BinarySearchTree(root); + } + + @After + public void tearDown() throws Exception { + tree = null; + } + + @Test + public void testFindMin() { + Assert.assertEquals(1, tree.findMin().intValue()); + + } + + @Test + public void testFindMax() { + Assert.assertEquals(8, tree.findMax().intValue()); + } + + @Test + public void testHeight() { + Assert.assertEquals(4, tree.height()); + } + + @Test + public void testSize() { + Assert.assertEquals(6, tree.size()); + } + + @Test + public void testRemoveLeaf() { + tree.remove(3); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(4, root.left.right.data.intValue()); + + } + + @Test + public void testRemoveMiddleNode() { + tree.remove(2); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(3, root.left.data.intValue()); + Assert.assertEquals(4, root.left.right.data.intValue()); + } + + @Test + public void testLevelVisit(){ + Assert.assertEquals(Arrays.asList(6, 2, 8, 1, 4, 3), tree.levelVisit()); + } + + @Test + public void testGetLowestCommonAncestor(){ + Assert.assertEquals(new Integer(6),tree.getLowestCommonAncestor(2,8)); + Assert.assertEquals(new Integer(2),tree.getLowestCommonAncestor(1,4)); + } + + @Test + public void testGetNodesBetween(){ + Assert.assertEquals(Arrays.asList(1,2,4), tree.getNodesBetween(1,4)); + } + + @Test + public void testIsValid(){ + Assert.assertTrue(tree.isValid()); + tree.root.left = new BinaryTreeNode(12); + Assert.assertFalse(tree.isValid()); + } +} \ No newline at end of file diff --git a/students/769232552/season_one/test/java/season_1/mini_jvm/ClassFileloaderTest.java b/students/769232552/season_one/test/java/season_1/mini_jvm/ClassFileloaderTest.java new file mode 100644 index 0000000000..dca7123987 --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/mini_jvm/ClassFileloaderTest.java @@ -0,0 +1,335 @@ +package mini_jvm; + +import mini_jvm.clz.ClassFile; +import mini_jvm.clz.ClassIndex; +import mini_jvm.cmd.BiPushCmd; +import mini_jvm.cmd.ByteCodeCommand; +import mini_jvm.cmd.OneOperandCmd; +import mini_jvm.cmd.TwoOperandCmd; +import mini_jvm.constant.*; +import mini_jvm.field.Field; +import mini_jvm.loader.ClassFileLoader; +import mini_jvm.method.Method; +import mini_jvm.util.Util; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + + +public class ClassFileloaderTest { + + + private static final String FULL_QUALIFIED_CLASS_NAME = "mini_jvm/test/EmployeeV1"; + static String path1 = "D:\\worksapce\\gitRepo\\coding2017\\group23\\769232552\\coding\\src\\test\\resources"; + + static String path2 = "C:\\temp"; + + static ClassFile clzFile = null; + static { + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "mini_jvm.test.EmployeeV1"; + + clzFile = loader.loadClass(className); + clzFile.print(); + } + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testClassPath(){ + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + loader.addClassPath(path2); + + String clzPath = loader.getClassPath(); + + Assert.assertEquals(path1+";"+path2,clzPath); + + } + + @Test + public void testClassFileLength() { + + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + + String className = "com.coderising.jvm.test.EmployeeV1"; + + byte[] byteCodes = loader.readBinaryCode(className); + + // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 + Assert.assertEquals(1056, byteCodes.length); + + } + + + @Test + public void testMagicNumber(){ + ClassFileLoader loader = new ClassFileLoader(); + loader.addClassPath(path1); + String className = "com.coderising.jvm.test.EmployeeV1"; + byte[] byteCodes = loader.readBinaryCode(className); + byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; + + + String acctualValue = Util.byteToHexString(codes); + + Assert.assertEquals("cafebabe", acctualValue); + } + + + + + + /** + * ---------------------------------------------------------------------- + */ + + + @Test + public void testVersion(){ + + Assert.assertEquals(0, clzFile.getMinorVersion()); + Assert.assertEquals(52, clzFile.getMajorVersion()); + + } + + @Test + public void testConstantPool(){ + + + ConstantPool pool = clzFile.getConstantPool(); + + Assert.assertEquals(53, pool.getSize()); + + { + ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(1); + Assert.assertEquals(2, clzInfo.getUtf8Index()); + + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(2); + Assert.assertEquals("com/coderising/jvm/test/EmployeeV1", utf8Info.getValue()); + } + { + ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(3); + Assert.assertEquals(4, clzInfo.getUtf8Index()); + + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(4); + Assert.assertEquals("java/lang/Object", utf8Info.getValue()); + } + { + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(5); + Assert.assertEquals("name", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(6); + Assert.assertEquals("Ljava/lang/String;", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(7); + Assert.assertEquals("age", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(8); + Assert.assertEquals("I", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(9); + Assert.assertEquals("", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(10); + Assert.assertEquals("(Ljava/lang/String;I)V", utf8Info.getValue()); + + utf8Info = (UTF8Info) pool.getConstantInfo(11); + Assert.assertEquals("Code", utf8Info.getValue()); + } + + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(12); + Assert.assertEquals(3, methodRef.getClassInfoIndex()); + Assert.assertEquals(13, methodRef.getNameAndTypeIndex()); + } + + { + NameAndTypeInfo nameAndType = (NameAndTypeInfo) pool.getConstantInfo(13); + Assert.assertEquals(9, nameAndType.getIndex1()); + Assert.assertEquals(14, nameAndType.getIndex2()); + } + //抽查几个吧 + { + MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(45); + Assert.assertEquals(1, methodRef.getClassInfoIndex()); + Assert.assertEquals(46, methodRef.getNameAndTypeIndex()); + } + + { + UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(53); + Assert.assertEquals("EmployeeV1.java", utf8Info.getValue()); + } + } + @Test + public void testClassIndex(){ + + ClassIndex clzIndex = clzFile.getClzIndex(); + ClassInfo thisClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); + ClassInfo superClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex()); + + + Assert.assertEquals("com/coderising/jvm/test/EmployeeV1", thisClassInfo.getClassName()); + Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); + } + + /** + * 下面是第三次JVM课应实现的测试用例 + */ + @Test + public void testReadFields(){ + + List fields = clzFile.getFields(); + Assert.assertEquals(2, fields.size()); + { + Field f = fields.get(0); + Assert.assertEquals("name:Ljava/lang/String;", f.toString()); + } + { + Field f = fields.get(1); + Assert.assertEquals("age:I", f.toString()); + } + } + @Test + public void testMethods(){ + + List methods = clzFile.getMethods(); + ConstantPool pool = clzFile.getConstantPool(); + + { + Method m = methods.get(0); + assertMethodEquals(pool,m, + "", + "(Ljava/lang/String;I)V", + "2ab7000c2a2bb5000f2a1cb50011b1"); + + } + { + Method m = methods.get(1); + assertMethodEquals(pool,m, + "setName", + "(Ljava/lang/String;)V", + "2a2bb5000fb1"); + + } + { + Method m = methods.get(2); + assertMethodEquals(pool,m, + "setAge", + "(I)V", + "2a1bb50011b1"); + } + { + Method m = methods.get(3); + assertMethodEquals(pool,m, + "sayHello", + "()V", + "b2001c1222b60024b1"); + + } + { + Method m = methods.get(4); + assertMethodEquals(pool,m, + "main", + "([Ljava/lang/String;)V", + "bb000159122b101db7002d4c2bb6002fb1"); + } + } + + private void assertMethodEquals(ConstantPool pool,Method m , String expectedName, String expectedDesc,String expectedCode){ + String methodName = pool.getUTF8String(m.getNameIndex()); + String methodDesc = pool.getUTF8String(m.getDescriptorIndex()); + String code = m.getCodeAttr().getCode(); + Assert.assertEquals(expectedName, methodName); + Assert.assertEquals(expectedDesc, methodDesc); + Assert.assertEquals(expectedCode, code); + } + + @Test + public void testByteCodeCommand(){ + { + Method initMethod = this.clzFile.getMethod("", "(Ljava/lang/String;I)V"); + ByteCodeCommand [] cmds = initMethod.getCmds(); + + assertOpCodeEquals("0: aload_0", cmds[0]); + assertOpCodeEquals("1: invokespecial #12", cmds[1]); + assertOpCodeEquals("4: aload_0", cmds[2]); + assertOpCodeEquals("5: aload_1", cmds[3]); + assertOpCodeEquals("6: putfield #15", cmds[4]); + assertOpCodeEquals("9: aload_0", cmds[5]); + assertOpCodeEquals("10: iload_2", cmds[6]); + assertOpCodeEquals("11: putfield #17", cmds[7]); + assertOpCodeEquals("14: return", cmds[8]); + } + + { + Method setNameMethod = this.clzFile.getMethod("setName", "(Ljava/lang/String;)V"); + ByteCodeCommand [] cmds = setNameMethod.getCmds(); + + assertOpCodeEquals("0: aload_0", cmds[0]); + assertOpCodeEquals("1: aload_1", cmds[1]); + assertOpCodeEquals("2: putfield #15", cmds[2]); + assertOpCodeEquals("5: return", cmds[3]); + + } + + { + Method sayHelloMethod = this.clzFile.getMethod("sayHello", "()V"); + ByteCodeCommand [] cmds = sayHelloMethod.getCmds(); + + assertOpCodeEquals("0: getstatic #28", cmds[0]); + assertOpCodeEquals("3: ldc #34", cmds[1]); + assertOpCodeEquals("5: invokevirtual #36", cmds[2]); + assertOpCodeEquals("8: return", cmds[3]); + + } + + { + Method mainMethod = this.clzFile.getMainMethod(); + + ByteCodeCommand [] cmds = mainMethod.getCmds(); + + assertOpCodeEquals("0: new #1", cmds[0]); + assertOpCodeEquals("3: dup", cmds[1]); + assertOpCodeEquals("4: ldc #43", cmds[2]); + assertOpCodeEquals("6: bipush 29", cmds[3]); + assertOpCodeEquals("8: invokespecial #45", cmds[4]); + assertOpCodeEquals("11: astore_1", cmds[5]); + assertOpCodeEquals("12: aload_1", cmds[6]); + assertOpCodeEquals("13: invokevirtual #47", cmds[7]); + assertOpCodeEquals("16: return", cmds[8]); + } + + } + + private void assertOpCodeEquals(String expected, ByteCodeCommand cmd){ + + String acctual = cmd.getOffset()+": "+cmd.getReadableCodeText(); + + if(cmd instanceof OneOperandCmd){ + if(cmd instanceof BiPushCmd){ + acctual += " " + ((OneOperandCmd)cmd).getOperand(); + } else{ + acctual += " #" + ((OneOperandCmd)cmd).getOperand(); + } + } + if(cmd instanceof TwoOperandCmd){ + acctual += " #" + ((TwoOperandCmd)cmd).getIndex(); + } + Assert.assertEquals(expected, acctual); + } + +} diff --git a/students/769232552/season_one/test/java/season_1/mini_jvm/EmployeeV1.java b/students/769232552/season_one/test/java/season_1/mini_jvm/EmployeeV1.java new file mode 100644 index 0000000000..b06f72b9e2 --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/mini_jvm/EmployeeV1.java @@ -0,0 +1,28 @@ +package mini_jvm; + +public class EmployeeV1 { + + + private String name; + private int age; + + public EmployeeV1(String name, int age) { + this.name = name; + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + public void setAge(int age){ + this.age = age; + } + public void sayHello() { + System.out.println("Hello , this is class Employee "); + } + public static void main(String[] args){ + EmployeeV1 p = new EmployeeV1("Andy",29); + p.sayHello(); + + } +} \ No newline at end of file diff --git a/students/769232552/season_one/test/java/season_1/mini_jvm/EmployeeV2.java b/students/769232552/season_one/test/java/season_1/mini_jvm/EmployeeV2.java new file mode 100644 index 0000000000..54a7ce4713 --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/mini_jvm/EmployeeV2.java @@ -0,0 +1,54 @@ +package mini_jvm; + +public class EmployeeV2 { + + public final static String TEAM_NAME = "Dev Team"; + private String name; + private int age; + public EmployeeV2(String name, int age) { + this.name = name; + this.age = age; + } + + public void sayHello() { + System.out.println("Hello , this is class Employee "); + System.out.println(TEAM_NAME); + System.out.println(this.name); + } + + public void setName(String name) { + this.name = name; + } + + public void setAge(int age) { + this.age = age; + } + + + + public void isYouth() { + if (age < 40) { + System.out.println("You're still young"); + } else { + System.out.println("You're old"); + } + } + + + + public void testAdd() { + int sum = 0; + for (int i = 1; i <= 100; i++) { + sum += i; + } + System.out.println(sum); + } + + + public static void main(String[] args) { + EmployeeV2 p = new EmployeeV2("Andy", 35); + p.sayHello(); + p.isYouth(); + p.testAdd(); + } +} \ No newline at end of file diff --git a/students/769232552/season_one/test/java/season_1/mini_jvm/Example.java b/students/769232552/season_one/test/java/season_1/mini_jvm/Example.java new file mode 100644 index 0000000000..01526633b6 --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/mini_jvm/Example.java @@ -0,0 +1,16 @@ +package mini_jvm; + +public class Example{ + public void disp(char c){ + System.out.println(c); + } + public void disp(int c){ + System.out.println(c ); + } + public static void main(String args[]){ + Example obj = new Example(); + obj.disp('a'); + obj.disp(5); + } +} + diff --git a/students/769232552/season_one/test/java/season_1/mini_jvm/HourlyEmployee.java b/students/769232552/season_one/test/java/season_1/mini_jvm/HourlyEmployee.java new file mode 100644 index 0000000000..289ece0647 --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/mini_jvm/HourlyEmployee.java @@ -0,0 +1,27 @@ +package mini_jvm; + +public class HourlyEmployee extends EmployeeV2 { + + int hourlySalary; + + public HourlyEmployee(String name, + int age, int hourlySalary) { + super(name, age); + this.hourlySalary = hourlySalary; + } + + public void sayHello(){ + System.out.println("Hello , this is Hourly Employee"); + } + public static void main(String[] args){ + EmployeeV2 e = new HourlyEmployee("Lisa", 20, 40); + e.sayHello(); + } + + public int getHourlySalary(){ + return this.hourlySalary; + } + + + +} diff --git a/students/769232552/season_one/test/java/season_1/mini_jvm/MiniJVMTest.java b/students/769232552/season_one/test/java/season_1/mini_jvm/MiniJVMTest.java new file mode 100644 index 0000000000..2ecc54f150 --- /dev/null +++ b/students/769232552/season_one/test/java/season_1/mini_jvm/MiniJVMTest.java @@ -0,0 +1,28 @@ +package mini_jvm; + + +import mini_jvm.engine.MiniJVM; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class MiniJVMTest { + + static final String PATH = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\answer\\bin"; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testMain() throws Exception{ + String[] classPaths = {PATH}; + MiniJVM jvm = new MiniJVM(); + jvm.run(classPaths, "com.coderising.jvm.test.HourlyEmployee"); + + } + +} diff --git a/students/769232552/season_two/pom.xml b/students/769232552/season_two/pom.xml new file mode 100644 index 0000000000..e71b8c044d --- /dev/null +++ b/students/769232552/season_two/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + com + season_two + 1.0-SNAPSHOT + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + + \ No newline at end of file diff --git a/students/769232552/season_two/src/main/java/work01/srp/DBUtil.java b/students/769232552/season_two/src/main/java/work01/srp/DBUtil.java new file mode 100644 index 0000000000..89801f6621 --- /dev/null +++ b/students/769232552/season_two/src/main/java/work01/srp/DBUtil.java @@ -0,0 +1,25 @@ +package work01.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/769232552/season_two/src/main/java/work01/srp/Mail.java b/students/769232552/season_two/src/main/java/work01/srp/Mail.java new file mode 100644 index 0000000000..79ddb9c94a --- /dev/null +++ b/students/769232552/season_two/src/main/java/work01/srp/Mail.java @@ -0,0 +1,73 @@ +package work01.srp; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; + +public class Mail { + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + private Product product; + private String toAddress; + private String subject; + private String message; + private String sendMailQuery; + + + public Mail(Product product){ + this.product = product; + } + + public List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + public void generateMail(HashMap userInfo) throws IOException { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0){ + setMessage(userInfo); + setLoadQuery(); + setToAddress(toAddress); + } + } + + public String getMessage() { + return message; + } + + public void setMessage(HashMap userInfo) throws IOException { + String name = (String) userInfo.get(NAME_KEY); + this.message = "尊敬的 "+name+", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!" ; + this.subject = "您关注的产品降价了"; + } + + public String getSubject() { + return subject; + } + + public void setLoadQuery() { + this.sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + product.getProductID() +"' " + + "and send_mail=1 "; + } + + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getToAddress() { + return toAddress; + } + + public Product getProduct() { + return product; + } + + public void setProduct(Product product) { + this.product = product; + } +} diff --git a/students/769232552/season_two/src/main/java/work01/srp/MailBox.java b/students/769232552/season_two/src/main/java/work01/srp/MailBox.java new file mode 100644 index 0000000000..a7fccfaae9 --- /dev/null +++ b/students/769232552/season_two/src/main/java/work01/srp/MailBox.java @@ -0,0 +1,43 @@ +package work01.srp; + +public class MailBox { + + private String smtpHost = null; + private String altSmtpHost = null; + private String fromAddress = null; + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getSmtpHost() { + return smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public String getFromAddress() { + return fromAddress; + } + + public void sendEmail(Mail mail, String smtpHost, boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(mail.getToAddress()).append("\n"); + buffer.append("Subject:").append(mail.getSubject()).append("\n"); + buffer.append("Content:").append(mail.getMessage()).append("\n"); + buffer.append("SMTPHost:").append(smtpHost).append("\n"); + System.out.println(buffer.toString()); + } +} diff --git a/students/769232552/season_two/src/main/java/work01/srp/MailBoxConfiguration.java b/students/769232552/season_two/src/main/java/work01/srp/MailBoxConfiguration.java new file mode 100644 index 0000000000..024119cd18 --- /dev/null +++ b/students/769232552/season_two/src/main/java/work01/srp/MailBoxConfiguration.java @@ -0,0 +1,62 @@ +package work01.srp; +import java.util.HashMap; +import java.util.Map; + +public class MailBoxConfiguration { + + static Map configurations = new HashMap(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + + } + + private MailBox mailBox; + + public MailBoxConfiguration(){} + + public void config(){ + setSMTPHost(); + setAltSMTPHost(); + setFromAddress(); + } + + public void setMailBox(MailBox mailBox) { + this.mailBox = mailBox; + } + + private void setSMTPHost() { + this.mailBox.setSmtpHost(getProperty(ConfigurationKeys.SMTP_SERVER)); + } + + + private void setAltSMTPHost() { + this.mailBox.setAltSmtpHost(getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + } + + + private void setFromAddress() { + this.mailBox.setFromAddress(getProperty(ConfigurationKeys.EMAIL_ADMIN)); + } + + + + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + private String getProperty(String key) { + + return configurations.get(key); + } + + public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + + } +} diff --git a/students/769232552/season_two/src/main/java/work01/srp/Product.java b/students/769232552/season_two/src/main/java/work01/srp/Product.java new file mode 100644 index 0000000000..2eff922d22 --- /dev/null +++ b/students/769232552/season_two/src/main/java/work01/srp/Product.java @@ -0,0 +1,28 @@ +package work01.srp; + + +public class Product { + + protected String productID = null; + + + protected String productDesc = null; + + + public void setProductID(String productID) { + this.productID = productID; + } + + public void setProductDesc(String desc) { + this.productDesc = desc; + } + + + public String getProductDesc() { + return productDesc; + } + + public String getProductID() { + return productID; + } +} diff --git a/students/769232552/season_two/src/main/java/work01/srp/PromotionMail.java b/students/769232552/season_two/src/main/java/work01/srp/PromotionMail.java new file mode 100644 index 0000000000..52222aea0d --- /dev/null +++ b/students/769232552/season_two/src/main/java/work01/srp/PromotionMail.java @@ -0,0 +1,92 @@ +package work01.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + public static void main(String[] args) throws Exception { + + File f = new File("D:\\worksapce\\gitRepo\\coding2017\\students\\769232552\\season_two\\src\\main\\resources\\work01\\srp\\product_promotion.txt"); + boolean emailDebug = false; + + //配置邮箱 + MailBox mailBox = new MailBox(); + MailBoxConfiguration mailBoxConfiguration = new MailBoxConfiguration(); + mailBoxConfiguration.setMailBox(mailBox); + mailBoxConfiguration.config(); + + //将促销信息发送邮件 + List products = readProductFile(f);//获取促销产品信息 + for (Product product : products){ + Mail mail = new Mail(product); //生成邮件(邮件内容,收发人信息) + sendPromotionMails(emailDebug,mail,mailBox); + } + + } + + + + public static void sendPromotionMails(boolean debug, Mail mail, MailBox mailBox) throws Exception { + + System.out.println("开始发送邮件"); + + List mailingList = mail.loadMailingList(); //获取收件人列表 + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + mail.generateMail((HashMap) iter.next()); //生成邮件内容 + try { + if (mail.getToAddress().length() > 0) + mailBox.sendEmail(mail, mailBox.getSmtpHost(), debug); + } catch (Exception e) { + try { + mailBox.sendEmail(mail, mailBox.getAltSmtpHost(), debug); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + } else { + System.out.println("没有邮件发送"); + + } + } + + + public static List readProductFile(File file) throws IOException + { + List list = new ArrayList(); + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp; + while ((temp = br.readLine()) != null){ + String[] data = temp.split(" "); + + Product product = new Product(); + product.setProductID(data[0]); + product.setProductDesc(data[1]); + list.add(product); + + System.out.println("产品ID = " + product.getProductID() + "\n"); + System.out.println("产品描述 = " + product.getProductDesc() + "\n"); + } + + return list; + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + +} diff --git a/students/769232552/season_two/src/main/resources/work01/srp/product_promotion.txt b/students/769232552/season_two/src/main/resources/work01/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/769232552/season_two/src/main/resources/work01/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 1ef7aaf4deb42061fa405a3d6396e01bd435d623 Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Thu, 15 Jun 2017 11:20:08 +0800 Subject: [PATCH 111/214] =?UTF-8?q?2017.6.15=20=E7=AC=AC=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E9=82=AE=E4=BB=B6=E5=8F=91=E9=80=81=E7=A8=8B?= =?UTF-8?q?=E5=BA=8FSRP=E9=87=8D=E6=9E=84=20=E7=AC=AC=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SRP\346\265\201\347\250\213.png" | Bin 0 -> 22058 bytes .../srp_restructure_1/PromotionMail.java" | 67 ++++++++++++++++++ .../pojo/Configuration.java" | 26 +++++++ .../pojo/ConfigurationKeys.java" | 9 +++ .../srp_restructure_1/pojo/Mail.java" | 38 ++++++++++ .../pojo/MailServiceConfiguration.java" | 35 +++++++++ .../srp_restructure_1/pojo/Product.java" | 23 ++++++ .../srp_restructure_1/pojo/User.java" | 23 ++++++ .../srp_restructure_1/product_promotion.txt" | 4 ++ .../service/ProductService.java" | 39 ++++++++++ .../service/UserService.java" | 18 +++++ .../srp_restructure_1/util/DBUtil.java" | 36 ++++++++++ .../srp_restructure_1/util/MailUtil.java" | 34 +++++++++ ...7\345\247\213\346\265\201\347\250\213.png" | Bin 0 -> 36208 bytes 14 files changed, 352 insertions(+) create mode 100644 "students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/SRP\346\265\201\347\250\213.png" create mode 100644 "students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/PromotionMail.java" create mode 100644 "students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/Configuration.java" create mode 100644 "students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/ConfigurationKeys.java" create mode 100644 "students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/Mail.java" create mode 100644 "students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/MailServiceConfiguration.java" create mode 100644 "students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/Product.java" create mode 100644 "students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/User.java" create mode 100644 "students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/product_promotion.txt" create mode 100644 "students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/service/ProductService.java" create mode 100644 "students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/service/UserService.java" create mode 100644 "students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/util/DBUtil.java" create mode 100644 "students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/util/MailUtil.java" create mode 100644 "students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/\345\216\237\345\247\213\346\265\201\347\250\213.png" diff --git "a/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/SRP\346\265\201\347\250\213.png" "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/SRP\346\265\201\347\250\213.png" new file mode 100644 index 0000000000000000000000000000000000000000..638b75e7ebfdd9d9b4fa37a4825a11011de67e84 GIT binary patch literal 22058 zcmd43XEdB&^e--m_$CA?%BVqt=nSHZPDTj{qIX7(-dog=QKF8TC?h)236fy+I(kiX z(V}*VNHz7B%u&@XfM-pDBA+IJkH6E)puwYe$)Q$9UFg=x;`*P;pzIl z_SPoj-icN{Q{Cb1zCz9-a>W)yG&$yG+mM!G&5(P$C4nvD+R|%{=l7mNkM7JuuZv2& zEp7J?uelEG|KyuVxw2{H>?fWZNi?=1_mL&buQ0z@%mql%f2y2U41|%uYKLNB(f#%R z$X{aCV3>j3^yl>#C_`;SbKR%q>1*YMZjWPQ1nj7~%|mkwtzTK0dA&FvLl?2pV&jt| z@v!D{NTsdF{MwQ}J`B{5{?zojB~jzUr`(7)g2pRbg_H+Nb?tfi2lmJVDb&+3uufv( zr7B4M6T?m@I$k={()I^8rouY6%y!SgYM|_`tSKZ20mH#6TS(=pC^d9%A|=hH%3`{i zLPfuPGiAZS_`G9C*(6bkfZ~_*X2R!7^4l9?5}H@j()JJx;^SA+Dkb`f8fs0zvqz{+ zs-J%xRyNr{IYLxIcaE)%^|Wy z$WvJ$8x1{YgQ6Wf-^k5T^n(bQkivh(=NCz@c z^H9o$>?v6Pjo4{zq988}`1%MV-@}85@7``5Qf6?17nZ%H?|Curyd0~M z>uU1GUm^yMMQ1*-cx`pWQZQ`D{M|NkrD{K!6D-)l>()r2JX*}gqNLl8Jz5|-}7 zP_$bLHe?cyK{;NBa zut~dXYNz^pTG`}P7cq7AJGJ_qXN0U1!b=IHqcDb-CFwU#PP!d3H7Gp%^e5o&Xg`q3 z=<@b-yj@k-x{KJ5WUc3?JrwvrbtS3e!45jDGO)K=fKTX21!nAwQDAz=0Qx;FPcVBM z^`)#)7UjU0i%!%)!P#wGh!l$0)u7x=f1SD}@4Kj{E$9gyiq=*ZwA-?JmO73}2;H?k z3{H9gR9#EiK#NT?(#LktGU1aRbYJ9ywZ6G6N1n2Ym@W^(~I%!p*SZ$plN44Wq6gD4=eL#2JgM&QNTA_J-he#ZyH)Lk z;&!{QFnJ$#KU%JwoEOoiZwCpYgT{?$SOzQ@9CgF~Q95$HX+>{WA67T+e@(4fQUxNFh z9=AQxCtq3Su0Fleo7SnF-I%-{NB%ywzqb}p-6t6 z+*ZjLZrSA+Pra~H0@o<@3>Sn>T?@KStu8+!2o`XEi35N}3_#WcFnd58a$m~Vd zO`mOK77$`=5s{VD`tu*tP*%+^ROjZ>(7yfpfyJoL(}YR?I8V79UB{=P%4f-bniWDr?Bnl%?)VqQjc7& zwm?dki_LB0vTS@7k+JGa=UHWS@gS$pek;R?;Lt{<6nvSux7=5vWs2(*QZ>BLyG18+ zb<$sYnR2i7vw$bK_NN^KhQ2Z`?EcPIs`-1V`mqHE_a=7OJ#YiwXeM!5Sh&6b#)a(a z<&xdM4zZlWf!X=iWiXr4cs^F=y`!;OdAO-QsNP~Pa^mNkCj<8N2SR_k@zqpfVqE%5 znSKh0!Asxx9O+l>(C1f+``xRnoiUrgA|5WmkN}(YP32r?Y3fwVWHU`>zVa9DIJFOB zOe#hfJGXzg;GD~0J@_LTP;pgJ-<$?lhz<^&)|IkpEv}gPRefsU(2Ts{_{_8M8Zph4 zl!beRj~(9~c&<-uu21J$t>I+jg0*0yROod@eO*qz)2n>O&3p>P^zUI8r$tWO z)HUL?vwd{zwF_S>lLNOLFUnKa`o8&B;W~ZNgyk8w4Hp-0K9!b`lG&2?^dJ4wUgi1{ z``1_CZ0P8Bb?%egmX;=d_0+?3Ti@<}hW8ee%=2AVrzjC$H4=Z{YTw`J+TdR07Z05B zIjd(*o{o4npG6`oP^IZ>UH2xawF*tA6sf+~#W$)J+KCo6Hkw)7uH-7zoXjy}oeade zGNknH)5b|U=uXEIc9Q0*VLihqT|g&AeFp=k276l1?eH)a#t3f`0W_|lPLgI7?oE?q zu56N&X@NrwKPG7>)WOF|BJbR0TyG=RoQMCXys~-KY6pM)gXyce>3IU=@q%Z?HPWA- zC4hBjF3n8p0i*b3Ph9bM&1*UX>ckqdk;&LZx1IDbC+l!>~bZu%Rcx;ZN0cBm^PC1b%`&2Ipo z6S3ICjBzio4b*t=04IXC5-&G0RlDBzFsNkUE3l~6-@QAajAMT#VWt^maW%U<^W{ue z+YpVRV3NVOujA0SS5~;;6Y+KNJjuzUt|@CZ7>T=yFcYh6xG8?^YKwVp&(|#5+2SF4UCnoigv8a0H9buX;%q11eVK9* zQQDQLbpP4U%)mLQVed5?yZmLbHZ9l{;tmWPaZH?P@^MioxZqwh>Y>`2Rv5m?*NN5E zyTNgAT{MP#PUYkH#T>bmW97>Hc_UM8!tD;J+29SMePDJ21i&35QBXi3b$l;IX*s+y zy3rx7IR<@Z6-N&Urz;O|@2^)M#=x_$)!RU#XOVy&vFVW-Hx@fcxWrNf>I%5hS1NCP zv}{Cfw;S>rgP$tFi?9_|YIxtvB6UiD(sN(&`utW^nu7dlNiFuMB=&OYbb9$pL8h~T?hU&eJUnO)&5NwD2=zsr2_Q?oIqyZ-mDj#6mVE5Evi z;2qzDUg|T}mcsQJ2$wm;+Ph4T`~^zT%gltU>CQn1t|73Z#4&Ve%#cue<909ms7q!S zR40mP%a*1v{`O+<*4gfaa7LkwoAx#6AE69TNHP0WmpewS#FkcRs{F@~!eQmlPfkL` z4bCq@yS9{z&BU}Rx=YwL8vAEFS*uget2rM1?F0P7jyj=tG0Qg=lyf|p;vR;PC)*ZE zS+txOy~}+yeX5;}4DMMTM(u~*CvgDYVQyNSR_82t&45J|BcXWp@$A6HTy6S1nFP;U zh-ch~mWxQX_h<<{^cXHx6J_GUY?D2NusPE;A{SLgrFmS>*CsiUPQ!->X zZSt$9$U_Kjs8@9M*}rVmVzUs_&YL)Hx-i=?@KyKpasGKoc|UiT%cCHBBm9mJ36prI z-){4x%Bduvm9+8*FXF1qVMzkm^=+HA~njd=c~Vkm`a8M4ZykC2eBy4Dj#8!WZC& z+Ugs%rpYHdl>tv?PkJYpMv}D~1;q#Sgb_#g4*7{@(savhb31z1?=5weL~)$CZhT#3 z>{rezjfUR=A^legWiD{f^$P^bEXQSTaZqm^^UWer{O?fX$%CF7sqhLHh~D$1`b_Lj zXna=UTu@vmCoEv8)#&BI7sth;`tiBFjka&+l34CQiC%xMIF-~5kwfvT7-+@aedp`v zNat+<5eyy@iid1)@FP);OY$bQB45xRuglCljiZpV#1>0(8O4c2w-+U1)5ZrfyA954 zSPqZv{Fe#+gSL;ae%gChp#YywxA*%YjFaj$z8t#9OMYJIN zS;f0~gCPq_ix*Lr!V(T+2s;z-I?Q_#Iu{pyw}8{n&C&K~ znJ7~*2g?))cU-~D3vCpN*6B2uy;UXGA?$~g^%}%f@W-Yv=5&#pbt`6n=;275sB$b|H2eDaY*7doZY~~iObF*CFB`1rGA0-z?$DLDIrF~3T zcXV(p>88O}VFE5rdG-ONcLVd)pC4rC9WaS_~ZV#ug zP0K5wfP;fZ>(Hd*TZJb}E&;ei?@RhxENMiB%_z^N#4M_+&d8@s8DJ^C7ISSQUi&Z8 z+P!RSUy3Ka{nBhWMlDku(+rpXe6^H;dbn)tdziYX#(7r;yNj=I^)qv>N7$mCV+Oq` zWX`L`c3+A|Zx5Z6Ro#^;93V38qz!)OL&qJAw-z$MW!|8{m&!x}_%3D$KH{x!0u5EF;cV8(!eLumH=9`**7;czYjBe1I^U25}njY&)=N^8&&5hd$ zI&SY?6d4XZ!jLm)w0uKTJ!eCrrA!&;_c(Y6iLF6!xiY+=ufhu<>5^uvIaBJc5Q-G2AbT zUA-sZgTXMImQj$R%n40w_^{!JM>liQm(B*qwXEw)KK%-Dm`s{`$z#?#DC%( z>U0+NFepc5QH0(38(W)O4t5k}`UPnaE#=e+uT&mHVk47J)b+$jZWGm5zrpgiWlr5S zs%zdDjbY)R*|IFBFbu~c%PO~&1lTDm_Kikzh(UwAtdTw#4B_e2i6X|EI3>o9SZd_PkAT~*NWlU??ha4Ch9- zL`~2*)xLwrbZsa_R{kD5e9;~j$VdO4#i?D+r2~roU_{+JwXSLXa=d>$=hCnjN)v+N z`hW@rSBo6kc&??pa|_@h_y2mne>B`mNd!`XPKJm?$CK}KE5o*1?9gP8kk&3POU)Og z;i)f=+{uNR*yWbZ_!sF75R!aP_3RecA@Z2ZNkh??W>51OR2c^kA(v(jhsJ{W-a=2} z7s!WoP1_$9Z$S42ZmF&Bh+6%{4MGfKqtlL<8r-Flt%rr>ENP#4h3S*RXHoch)Ei&@ zgb~)L7~klS+d4XC*=ZF!(xQ*#*Q)xU`xo@bcCXb9km;yYUWER``T&8Q$=ieq31ZCk zl$it>(Rj(y0D=0|GvhPSI=dH^79l{RkMx38pLv^Fm1j{OS>}Q?8aDsB2Z^4-Vz@y% z&ux8cV>TY*;@zuaGBz0Ztvkz9nU6OL+F$K*qsn*8e6G9;I^xS}y5L;jN=G>YErVs+jMHDO;0XIU1eE z-aQO9kOkwnuZn(;BzCQCu#V`Y612sON3BNpW=KF(7@{f$s<>ZxRDZJ|7i+y;nw| z*Avs3Cj+hR`}t$pOd%52Y*PzsejHPKho^ry6axDq5QM}O&nSEmyZvaQd@oX(yKv3h$`J(;*{zNvm@pxS_`-ag`3nxoc2z9Yac=#wOvIo zX8}ZU65&TLAD`X8(*e#(8W@;4B4@iXfCu?@7XHsLY^e_c%IS*a5>PNJ7e17W1S!i% z3BW})kZ3wT^V+-?fcFU89I!e6{um*FqhrBoKlMsj^=~{uLNgVmfrBpI=$-Mo0v^MG z59sACf$0A3!wCa;`U-Ju{Mt%-Oi}TS;q=+a3-^!~0Fzq5eel{UFBSxG1QjUm*nXSc0p;(ImP* zWbP~K0jl>=;E+iajXtC{hr$eE&tGl(kb<@icP)AcE!mi2M#daf) zE)L71E+|SiS+*-ugdZfNd!#&sKklm!eC6CGl07S5b+$e|xYO(pAyL(xaIq>7vVg#U zD2cY2{v&PWboUVH4n>Y_s1cQO~3q1QTFdf%#Ak4wv8k&VxE` zo=U;$v#DABB|LJ$QZ3DLzv}T%^GP7D)1Vfu0GqfAHZcW&9nS%LB-(wco{`OglC@W3 z2uTu-?=}fZJ6IA)M}K()#0AxnCwKhxBg8)g7`wsNx4?1feN+H*@Y1xLtCZHrepS5r z#Ot&k*}AQ~N*o@-_Yp9hF_T6^+)U52d~qF9e?j7FJcNcSXSVDY-!=4ZF~9G` zZpw=`39Lx-Eivl)ssf;xJ0M%(HP3ol`nN6k2SO(gy9ycW&KKleWTKhVrUnSV0!+d1 z^)|X004hLuh4PA1WB0j$e*k*zvl-W0?gE}JUI&|)4xOCQnfi<$-@TPp!G(qXCcq8K z_dI(nT)wMe_sV0P4nvm*wm7h61_b;0)&W6TX?XbHh&~y08e*-um_j~`uy94|Jl2eS zsfml#FUUjq`u%_90IWJm)YA#>W*RI;93&o`3A^XYmQ`Y5IY+<(U$Ay3mh%b&03Qn< z3yHV-vX}}4k33LvtoH%1z&%KPl`sgCfy`KMo}!*AZl6(O$&>RCs##fjyA(Npsw2hn z{~8UCmi@0lfJG;73dz1*j)x-wqSnt#g_`Zoj;eIOw52>?&MtdWrn< zQpn3=elNHcY@EtRRW&UT8vtY4s`+hd$H^}K262NOuaZ&Y8M^lRq58 z9;#E0Y=r{f7~tn|&HwEdOud*Y1@C&(@jU0{KL2wt6*L@Q!2Z<c7XOj@iJw<|N z4r|D~454==Tv+|by-*Y<%Rid}>>Zt=DdcAG9SvYN!tNk}yg@IoFiUCFM^$E{`#gmH z0BB{tr>OOR>(xN4`#ibMNXgOD)vlsZ1J`oarclH$tyP(S1;AG8dr%||;K(&74SA+a zjiA)f=k(OfC#2AZ6nK>wx!7TkZ;|)ew}y7s*IP@~tsg_}eT{t9c2{LvZDwsX#YA)c zik)UJOj%EyPFtC1PlY^v&R117eEGzWM0RE)bOu;Qdc?_XnRb?4j`q`1wtl3h9Iz$S zpOISWj8o-kNTu*{Uc7SMBl?^It{Ab^INhfwhTRlD!*{g$_v}zHoH#R_&mHgVXsws% z7iFcC+|>(V_i$>vm8-viHDttXa4+ZP69ep=SbKzlD^fPXe?!rUX~R!R{K)&_h8^oz zxjO$cEW~WG<8$uA@mG>hAMma9A0ew?ymN& zu#3TD-t7DDSZmE7p^BCH)_!c4E+KBKJ@XcNn>=z%VsyRUok&}bTyd8bAWH}?MK*hD^B zxg(F_-emNL&D)MCt-Z0zS9?AF$rsW$#i5iRt$F2djcfTe>x96v z{C>MO+e|yfPs`nUf*H!6D9kSL!!w2K-Gc``OY`NdcCiW9TLKEeCS^xpqMNNWX%5*{ zOh;oX@_4_j<-y9=M1}^|h}*Hyn6WAQ#dFEwjUP0VkFSp?XDxp}5v(|Pj~fFQIS}>e zgKm;tDDxm1lckn0;=$D)%#SReS|lr9QUIUt#3pcuulQ3Vh9>;$yk4mRxF3S6s^k4hVvOwygmRZPlhRW=C4kEYJo zSLIfH-<1x+=KHrzkKD8_-ORkin`V#ULXdUii}fl&{hwTr2Jq3?_Pe4HFih2x%QBiK z*W_%oWT)MPZKy5k3oqFvatUWLl1cp>!GK!yg=HX7E=%X#CGsF5ey9SYTDa#IrZvH% zR2r|)?VfyO4)L)p<-@Ev$k9BuzHm9EVL02ao{kOor3+1o_clvDs7~p~sgz2A&7b>Vg!y1G=6d7f=$;xX=7PMk`$E6|tGwpPX1ekZt!}cVF<|uBZu>O~NBVHT9Lw{O z*S>f=_EHXkJHbMIaWvV}Oi?mE_$hIMg^&x=J`YbWLqpp4a9+_o`N4!aA~&|FmTEp` z0u?hNbI96j!?hnu)+h&ircZuN_4@ZK3TjsAjLd z0aI-XjQ4zX853eqQ8DEsj)Kjv>DwI#wNqaFNR5j>pV~9s)hW$uT3e6g4>X_3xNnuF zr1kX5$Eb0dB`Yg_1!!XL?>!=H_bJtpdHX^oa~3 z!rkkm;>(N3%PZN&UGD=1>^1_1s=C{jOPl1kH3Bm^4}R={76u4!oF@xr=u+|GD6nt3 z_@TB=q_wcp+TdW_NHPzi$FLi%Vc_kE8Ppwc5JrSXt*<%+#vpPXJpKLk`%Z2y+tpRk zj%{|TO{5N@@l%#ugn~e=rsu?ark`QtC5ro^G5OFH85>$#(z9{6xf92{tb1SP0p>#y z2wE{scNK5Qq{Cftrk zX?OnF-HM^!bW(L#pS5!*lw))P9mtzwe4jIK}} zY8(5!2yLI(5UEf5gkPW84t?!bULy_&93sSwAEM#VhE(_$T@sU2_(C(%YVL5gXXmz# z--@tk^S6r&mU@L{?FO5DhUC8d448>>CJiq_eO5gTxpZS>vZ_aJO9bL=Y0}+vTaB90x?3j^ zDyS4ugUIqv6r9@Vl#B-5uX|B+kLjv0@+IP21SysJ>S@K5A-|4oaveILJ3Dk+i~I=7 zDnDGgP$c?yo*w#aBACWHW>jFh*i1)?whvB}4?di&D@fDM{xZrSuNePMFf8j4CMjX8 z_p^OrpodoRbhOIphaX0-KXC_>w_%_mZ4(uDfWZkVs4RQ_%|`_B2F5V`+^qJ%Ed`$H z>$7RAe3092bnmr~2y)k6*JjZ+T7wHjG)w@I22pK6@%8tpL|(+ayUyBfEKdDaN6lsA zX?k~5=$^ZU>W22BV=Le0xHc81`OQFzML2=$TUc*%TQtsi;m)}#pRG0rH1JMQCiG|y6-Wj}4?~6ol zd#<`aET8P$c6wXqQA^tiKPuIoEaKd(J^VI)$PIT%=*>6l4z(>q@{KLoxc5NyFOf3w zaGS^#Pu*RQqcN_}ohGK$tOI+1uVu(8T&|wP33l%evmg>Z_|E=|e2a~UQgn`zeVo1x z#D%Shj3c&hM643SOnk1%uq-*hTT9^LwB|IR9MiN7olacgRWh-s$^Bklo5%h>B!S}= z60ZuBfO0HACLn>mFL4zEifXq*OYgt|HxB&9^ItGp@ZDQbxIC+lRQ?eUVX)+11<`QB zR=p{ZG5r(6mO>x~daTrdr}X|UOzNZRcfhvNYH+X%SPdk(^WTuq0F42p1CR=m{yUIs z)x4@faw(2OedWt-#QEOh$Qoz;c>HX$t8?MLu0g}+LEnXp2Mich=D5`>exF%mpV8!o0Sg=GIp9Q!v>;Ao1u{Z?(>0K)Gc+Gu$PELfQH$+11<8|HkgI$`=$X*P27NAJ15|^>ZAk z#l;BQ+nfXvoW{r&)_{t+@D3V*<1UyGDm3bbSthoGbR~!K92O3{Js8c3e&#Ct-@ZoM ziVH%zOO9r`A7Zr7pT_jiDeQD7&#ZgFl`39HfKxpl&M!VM^B%2D*ZcJm{aSk7H@V>1 zv;FXFIPvX&*BSjcpJJ~8$@03L?f)1_RrNYjzRTm0_2l`ySybydCpYh$0>3b+Ej;tO zN_ono;dFpj^2E=l@6xpwL0@c3bHRhSbcNI}Cd7MJVQS2h%_Ee`$vS?D<&Tt7b=9+2 zmTsrP0W)EdGcT_;w6jUzDmCgVn!9^k4@fTr^Q;7-*}F`l$Ys-kt;MQHrLrz6TV8}& zdk;&B9i;~W!UV-#vH%xBc%>26$3UzJ+@5y0RgX%(r=xyFlLA9=lh;?V`l*tigxVw~`-|6dl&ab!s01 z(X*6qFe>(mG<SKA)xD_#CAX>!PDNv;Mk2BWRVIa0ZSmKoZgdhD6gRf ziK3^xU)3LbL|U~_oT5+ba%T~T*0{|LMFIe7lzqY0dG^t&ePiag5J?4(+H&pmWG_pr zk0e+0l^+KBE9~?=EuOG=TQ}8`fs@R^30ZZF0xFND2~z59@tpZyxGhMl-)!ZX#5|!^ zR>nCR097nd4eV@U2D`2j2jG%7=M$2gcQgsnZy9W;&68tK41a>O^?39l5cX;?b^sp4 zp`*Qb{S&Y+#k(+uIMy}*CNf{nWUDfBvjLoK#qo{Zj)uZLU>Cjvg4$Je71(nNj-rUB zjyOMr8uPtM8EGWQ&CA^~Tra2JFgl!QK6n$k<5zKSaxj7d>JP(_wt*jBKODenXchXY zzqpbkFNmsLW!Yu{kSje-ZO&kMK7d7jE1pr)UqA-@3Tc{zoZvp|6LyWIxo2}qW$ah) z?e*X^wC=gdFZcOkr^bLFGieQbh21_?=MzoSp#JPD;U}Q>YwHsYFq6IfbPrd8XitA8nBr;y?Olt@5=9L#8&rCDuHdI&z{pR(SqY}Dv2>BlW83qZvb9bYk?$~++7 zlqHcYSQns@!nI`FuLjMXZUcm-bKu$V{cAtut<#P2MFEL9FuhIvLqHOS=H1s+wJWl* zs7m??wgkbxMFc3wjmSB90d}>p%dZy(y*qxj2ePOipKTvtv_4i9uO-W41LV%1g46$r z*G9uBQ$m}TCrXCQ+MT59Iu9Z62J+Yw6#-*|s55@iosKANs?{#jZlrWeJgnDA?E&VB zq^dyI2yM;qpHjKj5`EyG%YlbK%By6jXG)%TsnErY%2u*DOQMcHSD_ND+Mxh7e=7Mpg_&4o;xa`K7B$`tRpWqHjX!yK zzdH3MZHpe|hf>0SH)Ce#pGx4hwriqWyrzLo8otp54Lkw>WxfL&A~ zig;lUHvC{4uA7mr{hd&4oOe)NEyc%n^6_GbC3LdI6=*w?5+vO2z@jl$2U?Ma~pLdU6Y;ZF?7!X7)_ue z0`PY%_Hkqy(S@zW`b6=saJpl#kI5V%^w{__pt^bKXl8P9U;?Ti0QroDUVVJAJ7?cb z?3Kw0v$Q`fFguT`LBz)>Cg!^Y49rK~&d%G1O)r6ZijDX~jC|q3jx|KEkf1MK{WXIk z48V+kRs8E(>0GL4XlEuxxCkvz4SegE!}A|M03eh6BiWlcx8GIE-Z0JD+b8mCJUJ&y zoK4g1@~+@XGI%xN^RZ!n!t3yu5@!HlqeuYGF3vpvs{9*i?y_YaV@}GSp_wv5t0CP2 zDoCkAyoj#yF;I(dx#_@~sc|ix7L+yuP4j_4YG;pUZM&jSPtoT4-P<zGDEBzh)bvyWxQ~!B)4nqm$bm zu}I-!tNGi)WoxZlj`gLm-(eTR+O6w+JahH3C8gDe#I5D~agic05F+0IOCt~#*fO`R zB7cKD5qekQoar2ng}!kkDcx+N;*B_Olh+Pr+&0G0cYFMN_juM)<_qaFvE#X)fb=Xb zY{1r?xW6x>OWsh|bD4w>a9xFjrpC7Q9C@MLqa-v#Btc)E9a~AsNH;W+^~lM7_s4Vx z-=u5udUtP|z`;o$S!Dnp6M?f-JJ{;az#{}6-v{`rq?xaf$-KogNw_&V3udh3hYB!z zM@@w9-6@v3ED$nmQfq;ITumaOl4Tcq%)x*X5y3D7b0-rwO?7 zuXz*Sy;aZSAF8*ZK(|&Wqi*dkic>U{ERvgRNq}|-`6o-WQiE`iuCBH`u|q%6l|B7t zCW5%sNPZNfhqud0=3BYjK}ICb=KWCLdb)9RbA+eoFCt?pMle+DLP$CjPLV^Zz*f!K zQO&pN(Yo50+lzxh=?bPdoH_%;-;xOIa}}4t*s0yYvn7qI!jya?DBpro9l*MSTBZ(% zl-&q<#Lk^v55*DeGi;kt`qPms(ei-ks%FI1^xY&NMpcYgDLZCqVLg`;8KntlmARVa z`pHg=ZaS}0x!2=5X(z{>Uh$IiE!q;t(+7YI`t)&s8WHn$(>dnX?1E-e0@;;zvKM!Y z*16Ar1lNCchfG}Om$*Up^50L~UtkkAY@M5K*N5NO7ntt~#XuF+q ziS>8xgslCh^O3G-re44Hj{TQ+f2HrGPvU*}dYdb%l~_i2Vu}(79fX4HrGlJuktlC- zsbko0XO;|oIm;2cEWi+Ue8Q4&3uMaM+W@N_)m-f5*x08UoPZ7GW5UTJY3+KsGK6^u zKaeW%tvuM(i3e!=;&yA*mw26niY33M%laEPrqe=B;-RtJxnvFsJqseUZ8P0aUyI$p zf*tUdHxGx$8QAB13VVS8pb?{sgYklO}PDvO#e(31QWA>D! zJBkIoxwRq8s#iq@@J*)FhA$2lMO~z~_b;6HF8pc-7ujZd z@C_S7!bd}3m|t>MqI;d@B8cYSRD$a(ADd6u*lV%*L;DZw)4T4cx~?6Rs;)`ZWaqkd zJV&|EOMyqhRPUcw$oIj{C7&& zP8SmVE{;9Wi!~}hI38R9#~+9)5D$oti|#3E_G*rqjuQVWE; zh|72r813UCP_o7SaeBOXxlfy5u_d-)OuG|b;d&kya_iZs!s(=+F2GY3+xC5$v7g0# zwk>`=TjY}!W}On+1ebOpzO zQO*0#EUh#i1eCwU?p^6N4{Ow;1i5M<#*ZhDe5ywMnoHj=xkDzWAt_-5TUu?XF|$Y@ zH_rl^3*G9O4k!&C4^`&j6GMU`pp$L1%zLIe;Mw*v^YFHzo-U{64hAml zZ(=ytL5wnGLoQew)nlui9t1REcqt(x3g#l{fk#~W?M%^_#`OGh%J=N#^cGY1t^1;r zn;)_nFmxM#Gx34xfxd_`0GZ}f7j|rrA2%J$oX4JgwTKzEB$fFD%!_cc6)`KlQABD{ z9cci`rJv7f+bxjtZhL|%A&6GL9)l4F0I`-ELvT`!H}0FbAKzuwQi^ef9tL zU6nbe{2?KTCJI#QZ(Oy+k#lL0-+bt(Q3p6^fK)n!n#Pd?a0T~m%sMf z@EJq!tw8*1c`A(ys?iNWO3UHtTz|OS*>$AXDC?_EqcZ$l4t7HRj-C7)92ec`s$YOM z0|4m6EGX5usb##}qP;k0B^iC8iF?(6MfGs!(Y=-g{Co~>b+}eqXt{dk)~TcN*!%8TiStWEPCa{gBYI?~ zQMVWge>dKK5?eub-nLoaNDPX=KVH%=7s+`u9?>SbGPvcsk${MmT_5V(|2~{X`dx!s z(LO=f+G>j^W@yx&#$Gg$Rf{#C2GA$<-->gCmN}0vT$1nGZ!lO^H0CO2^uwtbrf!m= zlZ;m1TRHQsCES35 z8Uo;n0P@B~y1DENTxCl66!{a?kC79ZU(6sW+t3N3LHtx%fdrRSgb1{)lopNoL*qy} zQlm+C|JrO#{2^OxT_Gzfdv(RvB?`IJ@?-~{>YDs4r|}_XLlyNCy;PqH5fCS_nR^GW z)tioBlEOa&MA17zByfDt3b;ay17@il{0UC%mv)fMCFO@d42NvQ-K!QD>cOQ+`CmQN zVOpOlxfWCsf56?Z`qWl*!MzsQrpr6br&Cn$@z0U93)Y_Sd2mtkT!)qsu+CS)kKT~_ z`41*Uay;}3N|3($-z7*RR_mkrK$c{j7O`1 zVX||)2)OJwTycEtn^!*qJNR~M<;r-lzR{0AiYZ53pd1&q*lJ%*ktu9~W+0{#M&XZW zQY0RJ$PuM8U``yNH1tUJvIhz#=deraNFRI_i_?*&+RmHpWM}Q8{S%mD1%QA*jMEN% zD+}lCR)m99SY{gDHJ~O>6%VFa@uLnFtbpHsK6iuQ!EK<|Uu$x4sO5i4)$&czgy92D z%8zX(ma~6*A?c-oLW6q<5+p{jL-^O-+?o?dvsZQQu#6K`NW#GTrHmKKq_{bbd!NO1 zUg$-7Bt{|4}Rv1UjFB!fKCxwEoMX z3R3o#-BtiLSV0yHeJSY4Voa#A9vA_<^^sg=9qGq%<@y`AK(T^~K~qw(R!5^c*%jB; z=!eL6Fe3AVs9=B=jt;;$fR%a)J_O?fnW~Fj%;A zg)(ypK{Mk+Cly{9!mCN>a|kKYuUHrKVKH$q5j|1geL$VKoGb!XV}Frg9&wXm_s$PM z36fSxsYV}Aw94oH%_;;Y@cF-NC=w)|K48c20h9=SgrU+8^CMvCr~$?oF!H_aq)`Ey zfQv^F19w;BRvwIl3)Q97>!C=*EqShc+@1eE@gDY6r?Qgf4sc+f7#n9nGZn9qcPH>w zDATa`qZ?b|ZT04m;~vT4i%c5JY-V*ooo9mC$RJ=Y!-2(%c%}`~m0#f!$Vg99&%wW2 zPsIeRQsD2N=Zuh=*p%g+o0i{Y1TJ57t9K&cDAVFs@{pnmYIS9Vsl-PG@z7HVc=s!o@gD#J}L~gNXOvVj-#V7rRp9YW!FSx3Y`OZQ8!H72vBHD zMqMfNWBKzsp=ktR#R3g0Z(I7OU$jFFR`YVIs%F9&5=O}0!_@)+!?z1Gl!;J0wgr@x zJ%UfaUJsf|MZKp|F06h+F=7`&$)g0CZvDpwP|+$K2rgpKCEWPyv2-XCBOQo`=ptJT zHb535((>e?T0C4?ZGUa)Ux^rAEojTek0AuN|nqD*gnM@ zdmtYZ@T{PXnZ`5WjcXdSov1p^lT4P0PY_Ovr9Wj7*Rf^sbm>A5K&ljJ{FAoh?BzQy zJ`pZwxMNk`kWTBxa!+p1gB?hq@eAJ7+6i)iJlp7T-<)8yhyY{ zCodr}9cT`~1-%YeT-JwG9U-v!1;EdI^bQXjbQ8XpqO^V7nO2E2?841}c>-uonJG9| z{af;o7#m=V!Y0?7O(9G`4?ww%4hb5%z~uDfQ*4`UM&C7f5V2?b4ItTW7Q2*yfQ@9l z+hM-<>i{Gv<4EdC-hEK6u_N@}?XgD`B_*n4T%z5I&n707eMJur1R6bj>GMIe0H3I{ ziWsp4nEv<*3w-GW*ijO8a1ci96+(Qdh1#K&HlUeEii2Ag@z!mID(Qe!?yBLqnw4R; z?3oN0cK}W#JPgXfgOKN^p9S3mkVRMv;7hIU&Ymo#%pNUGGBi~NMa}BxHM#g2YTp!9 zMnD^Rlx+KY7-ZkWL1)1%+h(ZGMF14AbKw<$HV05u(4ncP%78CGeJHB*W!F5b5_Mn9 zN{J=p5mkyTXpA`yc{U&*V4ghjb6sGY%o#LUJedt3x^k`2Rue5}^iu?g5d@bqRL?04RQLfcR+WLH0-&W) zFxy54J0q|kfFQnANR69he`c1f?d@P|R#x$02wRb%QvN5J52K~+mMS+m#Di#h-i-R+ z8>nJvi<0vJU|j%dNcnDumRsif3_d5O`id=Nc;w`kKT5 z3Qyk&T5-KWBuiokK%BWwQE201{S2@^rMCr%V1>qJMMu3*gm6l>&GSAeLlfWa3%-iR zp%kaHcb+7*%Q~}pd>!w?u!g4M`5)IFB+L*En=?o0pg;$i(C&cZaPRE7qf{zAkJ1OL z)u&Mp_L&EIhMSsUCZB>MumAcnO9CITNg5Ptr zj2rb5qNlvimy%^v!Lg!a{bc*ODdc$w0381}@a-bX6Ek|b zH*>x(_c&JyMrz`}aK2)Pi7ZY7H||;+T&I6VyN6( z$$EVTld`eOt*!R>y}6KVD~#ZuoLS4d)r6SkmGeqaM-9A}*=RFwkMYV|_%WzGli0<_ zi{Pew<@f#|^KwjUZbZv;BbH}~*92zT(+s(7FI3lThk0|={RWn`rb?b-JexVsg)f_t z?mn01=^ev^*jRuf&?(BiN{cIthQ2xf1Hl8$U6aeC#pmfBL4n5LF0c8_-`L zypp?R)}jX=r8sm!^^^S|0#Yt900NA~I$y!;?|l16WeEa;i7eLBa+&llYN~m1qvfaU zDAISbOtG~YpZZY%S1{;!qzFw>Plb3TdpWKP$Ie)CREXA{|N9-YWKURL&dusjZay8i z?oKp^4Lp@8$5_+ zcgnUU5&&`Z%bBg`y_X70b?Gh=sFJImwbce>U+Is#FcX^Hf@Mml6>f_(*Lk=IU?rtn z!PDew{sR}YDUM}xn~rniFLwYj`RkLdD1{5W-P#%Nk$2_4paeoc#>j@e<-`&i6DrF;H7aD-(wB?=cN)Gt2r-t| zlaskAPQw+nrgvSo)8Q9et3_LBoFP6J>dhu7LGH*e+_`B>YI$yL&cboX{uL`n66?_M@CBF)9HO6)r+BtpD0Ut&ip1A> z5O+b=fA&4|Fc*gP`k$Y!JkaIYKqr6!O8u@&%z=1NIsi6r3hEVUe){!1hs^5+*P^fb zAfMA=p5%#=uO+5{#P)x_3;bagk=P`NT!;@!=i(WV_kQmC z`(|FueCB!n&+}h@-_1;Lx*KreP+7c@>s_Um=o-fP3^lAQSkHyUeGb<2550%|7w}`@ zVVP}ZCswkS40c>i@!f7@BBzvmHR{~3>S>d5PkF_%H4>p_TILlDhSy1>^pa-on!PLI zNUdT>V3E%#TVujEI3HWs+0Pw(_SM_ZlkIFD<25b13nxUw{L{9frc5UHe*&G5U4&WAPsDeJWSTlNxt*5dBd_VKg~+VuyqH5>^^ zde4#yt5QZQy+@dzxr@-FMfe_LNpUF}1Rti=vpl;-aAine`W>8|5Ml!b5A(vk@5&-J z|83jaXA+&_2q%hTd!=s#iP*<@oHJta>5z7|S)C5ZDW@{Cbd4+7!wlw|$|RT4Nt89H z4Z39GzY2T>Zx2(2bAD9pV)!xurQpFC;_N4`xwGOFR7G~MDf~N>0A@Q@RaD+(=)j^- zIz^cntdp46c+A|0{!hx4I>rW*kZT%!sCq$>L(>O16e7<2Smw9gvYS+?C{ACI(7>0B_T) z&I``esVbYW7nAeEf0+jB0CKmw^HI9HUq7G97*1#&`0)Ti7y>)TdHFsjBVKv-QrnI~ zy@%AzZANKx#r9DWh~M|ev6MDgLhqRx1%I|UvoG;4n6PquFS~R==Z(mcTJ}G!w)t{P zS#w6)_0~?%ctCYzZY?ke$PFSZ?Ub6dQ@vxPtYkwvtQetoB+N^mlPSK|>~I9Gcr%zIM^f>};!`UESp|mfu;o`^e5L*Y4 zffJXL_WIzBgy0;9$T|ip>?(4+9>hk0#glEfW)|gaLPGjlgFEJT`U~rjMkhSdO`*O* z?n3c94QFb4nR$n?5Jfv1)YSz3uEi*i0Ud=pkR``aJbyXtTH3N()Zqpp_wlDwPkf$R zTg$-;hSa?ZyCg)p7hd>KKlo*Qeog#_=x?RMa_{r%+ddE2US_pR7vI`V$nOXUh?u{k zK<}vqrz|4-ZthJg4{^cyE`vH2zfJsIfuz&dOm!5?$3*AHZ&G^5ZM{^t|jaH3{nNtB-9RA9F_Zv7FE)r7H00lRNb(w$~!>UC69R3KgT=j;y~Jl93qLiPR`Ou^)ko4NC3yF&IE;~%eK%eb zF6(rQ={!6n=pXqvcMUfe5&rJ(GsYKE6s5=48)!>Aq;HgCj}9UH@ZQFzWP1-a^Tq31 zZld{8cY!CEcZvp_c>fP9EdUB}goe`iPx3MQv4h)~7b?WghtHuSC;*_fSMm9UdU&WF zcwRjP)J}zDGPK>G3~8=cmp;4T)gj@^`|bw_tpou9P!CZ=`~YG_*sBRKe-`RZtfSeJ zgHi)>LrsK`jB98^-Jv#W=_ij_QE1>AN#IZpFQa3t8#Am!;x%J+5%N~(j|EM23~*$~ zpT0jFJtMB@HB7h>E#h6;_tBQv*peTmcbQoWWR}6Ur(K;^w*qJtp#F1e=wH=q&L4rK z7f8urpfivS)f<@Ne*AnPz*4M10SF)}op&)XqKXa+gLQ~^qo@dQx;(4+McW-ZSX+Mj zx?83p+&?Pf4Yvt(gO?xyXzH9@n1R;@YcwE7p7^b`d%T=%@q*l4!K!?CySV!m^R%SB z4YoSR^%DI|@6W(ML!jJ2A6MV`DT|I0(}sJ-#Ta{nHLi;|1M+11pfsZ-sNmrqG_7}S zC(>1OA`d*endrQ=9@~z>D2ScyD$s}iS24IarSa?0YB#FNSp~s80WYrSp4Nb;rs#}` z+dI>V*~3BueyN=SbH}rQ5I~CJm+CKqm-TmITZ5E?2w1%k31Rc^4Klf88Tv$a4N#%z z-es4yhOG%iaPd1*7zv%ull6U;?uX9S!W))QT#NIm5PtHxr+MFWr(xf+8a4ISL%>>wQ1r?@gO+OT zG?xFyRAa`pebVA`#ul`$1p9g_uv*a|mBHtK{e%A|+CJqrf`VnN&88RmuXEEb|4&jz zRd>h<$QS{sLtt@KldmTk1Kuu#(m-9rncg~D`ti<$Mmu-?yzxZ;@oaQhcVhco{ZLcU zmW^=Si2nFkGj%l(0#WV{xhpcjicQsOR2koNC}IlEJP+e6pZI*6x-SZNL_u2Ac5Hv_ zmW{A>1lgMSt4g*@K|BUxNBO?<@ccuj)Cf8EVefIy=tSwMgEfm2-#A?NVysQ{0YcCW zHK1dc;mH$*GXwdVpzlEJ9M-n&(kR*XZn~zQuV+hXx-?G{>boEpilE)C&+YvnW<~-k z2{H!O<0rg#0 z>P`7)!haof;_cy-W>`=?Um6@M!DH(mOarK(#2SJb5^;4hXtwU{c7dyZd?IPT9T3X| za0cl|6P4!Gum)C%ty8}>H5RDFq!E59|9V^9O zRh%1XdAp9b{seX;zEHo>9+^vr5;x({QE5{fG-eT1O4tia8cg3jnCzjSN9#7lF(wb` z+M$o`!~H1zl(8-glITFMd+eJN!va#L)L0o~RuYrwR|eG`E9ZFOSrzn1AH#~bNfeXv z-VnEHiymg)pxuGAYPx%D22{ME8f{a-sa5C4{xC4VEY2Cc>XjkgsPm~EhMPJ z8#OerD!UJ*bU2E{{oQY6CBk62V_v-R>~TkjLrhc2S$*l@Be&QH^FWYDJ|2LM68 z&|R-4qWt!=E-_-O1?~F#IC=Itj!UkytN5H$vnfE*$cQB-&L2m@aIzl7;BBKsB8*u7 za&56Zll7G!=9YHZ>LS-OsT;Sa;99GZq_Nps>MZQE@cJ+pdD zGN^4JszO4MwM?~Wu?FQFVJ%``tvOBhfw<;WyfgmSR39DEX<3*E5(safyWtcv(|}Pa zh5*v%sZ3GxHjfXgUlg}W>fimxCp`N1Ef~gplW>fn&`>SH3uJ&HwT~ zu-z|iIU^BpZDfl+##f6k9flin+YD@wgDKGKK-bAZ{~{YfO688Kph|dYHAvX%sJ-cH zp-z=VTXG77gt69iPzw_Q1xGN?3x}WZ9R)-WlEebII0szP#c(vs3wa|{ plist = productService.getProductDescList(file); + sendEMails(mailDebug, configuration, plist); + } + + protected void sendEMails(boolean debug, MailServiceConfiguration configuration, List plist) + throws IOException { + System.out.println("开始发送邮件"); + if (plist != null) { + Iterator piterator = plist.iterator(); + while (piterator.hasNext()) { + Product product = piterator.next(); + List ulist = userService.getSendMailUser(product); + if (ulist != null) { + Iterator uiterator = ulist.iterator(); + while (uiterator.hasNext()) { + User user = uiterator.next(); + Mail mail = new Mail("您关注的产品降价了", + "尊敬的 " + user.getName() + ", 您关注的产品 " + plist.get(0).getProductDesc() + " 降价了,欢迎购买!", + user.getEmail()); + MailUtil.sendEmail(debug, configuration, mail); + } + } else { + System.out.println("没有邮件发送"); + } + } + } else { + System.out.println("没有降价商品"); + } + } +} diff --git "a/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/Configuration.java" "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/Configuration.java" new file mode 100644 index 0000000000..56182957fa --- /dev/null +++ "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/Configuration.java" @@ -0,0 +1,26 @@ +package com.coderising.ood.srp_restructure_1.pojo; + +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static { + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git "a/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/ConfigurationKeys.java" "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/ConfigurationKeys.java" new file mode 100644 index 0000000000..6a5cdb35fc --- /dev/null +++ "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/ConfigurationKeys.java" @@ -0,0 +1,9 @@ +package com.coderising.ood.srp_restructure_1.pojo; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git "a/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/Mail.java" "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/Mail.java" new file mode 100644 index 0000000000..560809a8cb --- /dev/null +++ "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/Mail.java" @@ -0,0 +1,38 @@ +package com.coderising.ood.srp_restructure_1.pojo; + +public class Mail { + + private String subject; + private String message; + private String toAddress; + + public Mail(String subject, String message, String toAddress) { + this.subject = subject; + this.message = message; + this.toAddress = toAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } +} diff --git "a/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/MailServiceConfiguration.java" "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/MailServiceConfiguration.java" new file mode 100644 index 0000000000..cbdaaec27b --- /dev/null +++ "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/MailServiceConfiguration.java" @@ -0,0 +1,35 @@ +package com.coderising.ood.srp_restructure_1.pojo; + +public class MailServiceConfiguration { + + private String SMTPHost; + private String AltSMTPHost; + private String FromAddress; + + public String getSMTPHost() { + return SMTPHost; + } + + public MailServiceConfiguration setSMTPHost(String sMTPHost) { + SMTPHost = sMTPHost; + return this; + } + + public String getAltSMTPHost() { + return AltSMTPHost; + } + + public MailServiceConfiguration setAltSMTPHost(String altSMTPHost) { + AltSMTPHost = altSMTPHost; + return this; + } + + public String getFromAddress() { + return FromAddress; + } + + public MailServiceConfiguration setFromAddress(String fromAddress) { + FromAddress = fromAddress; + return this; + } +} diff --git "a/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/Product.java" "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/Product.java" new file mode 100644 index 0000000000..38de6f12e0 --- /dev/null +++ "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/Product.java" @@ -0,0 +1,23 @@ +package com.coderising.ood.srp_restructure_1.pojo; + +public class Product { + + private String productID; + private String productDesc; + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } +} diff --git "a/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/User.java" "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/User.java" new file mode 100644 index 0000000000..ebe5d134f3 --- /dev/null +++ "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/pojo/User.java" @@ -0,0 +1,23 @@ +package com.coderising.ood.srp_restructure_1.pojo; + +public class User { + + private String name; + private String email; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} diff --git "a/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/product_promotion.txt" "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/product_promotion.txt" new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/product_promotion.txt" @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git "a/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/service/ProductService.java" "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/service/ProductService.java" new file mode 100644 index 0000000000..d1353bc72b --- /dev/null +++ "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/service/ProductService.java" @@ -0,0 +1,39 @@ +package com.coderising.ood.srp_restructure_1.service; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import com.coderising.ood.srp_restructure_1.pojo.Product; + +public class ProductService { + + public List getProductDescList(File file) throws IOException { + List plist = new ArrayList(); + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + Product p = new Product(); + p.setProductID(data[0]); + p.setProductDesc(data[1]); + + System.out.println("产品ID = " + p.getProductID() + "\n"); + System.out.println("产品描述 = " + p.getProductDesc() + "\n"); + + plist.add(p); + return plist; + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + + } + +} diff --git "a/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/service/UserService.java" "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/service/UserService.java" new file mode 100644 index 0000000000..799f713ccf --- /dev/null +++ "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/service/UserService.java" @@ -0,0 +1,18 @@ +package com.coderising.ood.srp_restructure_1.service; + +import java.util.ArrayList; +import java.util.List; + +import com.coderising.ood.srp_restructure_1.pojo.Product; +import com.coderising.ood.srp_restructure_1.pojo.User; +import com.coderising.ood.srp_restructure_1.util.DBUtil; + +public class UserService { + + public List getSendMailUser(Product product) { + String sql = "Select name from subscriptions " + "where product_id= '" + product.getProductID() + "' " + + "and send_mail=1 "; + return DBUtil.query(sql); + } + +} diff --git "a/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/util/DBUtil.java" "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/util/DBUtil.java" new file mode 100644 index 0000000000..086893ec60 --- /dev/null +++ "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/util/DBUtil.java" @@ -0,0 +1,36 @@ +package com.coderising.ood.srp_restructure_1.util; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import com.coderising.ood.srp_restructure_1.pojo.User; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * + * @param sql + * @return + */ + public static List query(String sql) { + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + /* + * HashMap userInfo = new HashMap(); userInfo.put("NAME", "User" + + * i); userInfo.put("EMAIL", "aa@bb.com"); userList.add(userInfo); + */ + /* + * 因为在重构的时候使用了bean,所以为了方便直接改为返回beanlist + */ + User user = new User(); + user.setName("User" + i); + user.setEmail("aa@bb.com"); + userList.add(user); + } + + return userList; + } +} diff --git "a/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/util/MailUtil.java" "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/util/MailUtil.java" new file mode 100644 index 0000000000..ca44417827 --- /dev/null +++ "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/srp_restructure_1/util/MailUtil.java" @@ -0,0 +1,34 @@ +package com.coderising.ood.srp_restructure_1.util; + +import com.coderising.ood.srp_restructure_1.pojo.Mail; +import com.coderising.ood.srp_restructure_1.pojo.MailServiceConfiguration; + +public class MailUtil { + + private static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + // 假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + } + + public static void sendEmail(boolean debug, MailServiceConfiguration configuration, Mail mail) { + try { + if (mail.getToAddress().length() > 0) + sendEmail(mail.getToAddress(), configuration.getFromAddress(), mail.getSubject(), mail.getMessage(), + configuration.getSMTPHost(), debug); + } catch (Exception e) { + try { + sendEmail(mail.getToAddress(), configuration.getFromAddress(), mail.getSubject(), mail.getMessage(), + configuration.getAltSMTPHost(), debug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } +} \ No newline at end of file diff --git "a/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/\345\216\237\345\247\213\346\265\201\347\250\213.png" "b/students/949603184/homework01-\351\207\215\346\236\204\351\202\256\344\273\266\345\217\221\351\200\201/\345\216\237\345\247\213\346\265\201\347\250\213.png" new file mode 100644 index 0000000000000000000000000000000000000000..c2d8aceb83073568160503fe84291c5c820ea6f2 GIT binary patch literal 36208 zcmcG$Wn9y3+%`T$MMXhCK^ljY2&kkZl>wtcN`?a|kyaSeA=2qcY0%Ln!aznzsDy;0 zTj`NfBK@56^1AN(d0zeB{9jz3&$S=FIOChgah!%~X((Md$8-(?fm~2V!0$sKXmjx#R4nA51kA;4r z@=ST<+2riH$9>NW-Oh90*$uyCe07WNta9tkgNw4_}DYLjbyht zQHA<2^Vp1H>F!tOL;v0syL`2yL#2HlS9I-@gsc{$*=v@<^NqIr2WbN;R$-4-0>)zY zEjp`At+&ZvkrvKFN!VX>Lwm(Y;PChE^*$9I+)BQ8&TMA9;FyxFr>fway8;K583_xu zlpW*UpgeIEUpct6r6m(t*a*hzW2 z`)|NES}&2l6CslR$S99rw8FrRUawXt zajo+#bpD-(3IUq%%q@V07^g8bRE#E63k*l-!!k!nOK2V;;N&r%b7?y67v6rf%2BgN zB^QoAS&TNmP-_^jo*#vvKr{b&oxpw^HjBzQ@4a}Ka)GQK{DM9#yH`w~AH}I& zppzcp&yJy55F?E=;r-x--tCiI(-qvEh|4ygH=Q1Rh<8w3Dv7)k{PUxqY+sYHqxFkh zP}Uy3|Idb->8XJ#UY0<|(w2vOncd?#J#!ii43Cv6t6XcT-As*pdk;%K1w24bJeAe= zCi-ag>2CV}zC3qbQ~VcIx4m8ca}*6EPytKs_w|AaUi|q=))ErJdm9Ygq7Z*%vr}fB zya2HX170$Wzr8*4v-? zZ?L|vvdV0!gN4SOQIwERz!Uw>uR~e=Ryn{RgTPRVrI4=w{-sEF&}4-v*$MpiegQsh zm}KZ(<~#G8@V1?N6u8n$M~g;^Jsy~~rfFG)cP5LsVOfWmC41Rj>Ch_8r#|u*;^=tA z&8l^9O@!V4*ih-ZwLa-B)(d)K6ZrQgD{+|ZEg`Bc5z-uInKgcjoskJu$&>a9quqWcfG4riFMY~{1+hx`Bd(0(|ytY*wHn$QSEN4 ztjrM!i~Wy&#XgbKE8>~+mYL!gi4YF{6`_>^aUL#*Y|54FYdm~!`u(H(isroERFx|z z!ty<~B`UmY(sOL$G2INg@fvk6khI|uXd|N$Q~-I}GxOK^pM>_vKMMRA<@Y4XL5B2P zSz}wL_Z0-j`dH@GMH7+tuLrIS{MM2gnl#UR#%0uNbdl&Ed3Q3eZ-ARiSQ?RJM5JfH z!P;^wTx%6^1#JShJ|z__9MYi)^Cvvc0(*J?et&2S=_35?YNTR5^6}5^dlq}$4oNa_7>?VrY|I)b36Z1_axb`a8 zL~b!LlQpT^Npr1NlCtz-<1&nvF&b+;@lpJ?mtRd<=y0a}NZx2`BC4vm3V1{v()9_# zZuTr+nV8VW@>8D_*WhP87X&O@y4%KSK>W#*(&>9{C!*|okKRi za_-Hb!UxR?%om)umux(3FZfqR4GuAiKj&vIMz=DPmdniR*YNj+`^lW~Y+CjbFJzK? z?}edh)(FZ($1)KuW_K-SiUN`EqNU8|bURaL*KF;#g(>*C?AmW>oXx_$sKQa3N4v$d zM{>FC=H3*e+oSkzhFwvz67IV`$((gxe?)4XIeq$yNpbi$mBH%-QR7kDpz;wbE+c-F zuneENRp(3f;%8YW2Usv={k2?N+@F#YOI)y&r+*^x7V>ZJMf8(z1d?Ve;qz=8*r0V( z9A0jTsqKpdbFjh^Vlcwqm^W5t?A$!J?EV$bG)sx41Z(`o(aiCvPSsp0x=M930*v5( z;muv#7WbhXkyRNXHm@>Q*=2|&ye#=WUliiqzxmY%tP02c7)>24cRO?vB@cs0b zu2qzKR%XJR_%60bb=0`X7!=tJ!`zNri|g z19|^zZCM5&A#l*u1mzSgl|CFiA=g0i_xbFMI4a0}lGV%uzn_$pn#a>a_RavCggKg3 zdwdsJdq}px2KEwd>~3biaDW0qrALVrq}_@0jHmr{|NL)r;%X$y^U8$JqK)O3tdzJT zzA2uGLs_2%O%R?IYMbZvOVp$>Rl4a8A*ofQxcKTW@$pqcmhGRXDKvyMZWtUoDx}jH zeJ+t^5u9{pyUja(0}ex9_%jeqYsD6cwI$r*+!1ENvWRrPTb=IHwp+NX>P%K;LBI$y}b{n76X~1#|J3nWJ%Kx-c?ny zv#X!*w!`C(+Fc?cmq4AEnKH@O|`;dEuW(KzFnlCIoUyHn_Hs);0{D?ko21FpV^@15tgQV5zG6SYkKWh#Dx_=tc;_aWoFbOIhl{R54-*N$N2}fD zZWeA*q9N_E6U(Q)Sw2l|Hj&@z+hFKbzu9Vmr_f^?s;sNH;>AfR(7v zEp?$^lf1>gsR5R~L`~LuUIEY1-^Qq@uIyAnaHX2%6IJ6g{rYro&K;=;6Y$C3U)nAD z<-LH$NwQwKX(4~-HB+5eSM*Rrod9#=L{k;4V%iVdGO8&zre*qw(kLDKdwS z@jp~|Mtq-mFzZk0<9m#8D?T4%W7DrP+F&%K4Fp0J6!8Mtjo+>;L@*mk1wLShc!!W) zSX;G_laK+6e!>^Fqlh|?l=gDq`lMx;Hqd2hoh|Ie^QC-`A#!Np z?SofM%dDl^#Wow&HU>8O-4!OAQBShT%}6^(D@3?MZAA<-*p;Xx`j#=M(dgTa}4lq0qDe_+W^ZQ*+V}Y-jB}>tsQEmu$6lxod$029!J; zr$O=+B*r2P-0|yQCNt>s1rKAxXmpXdm%FBIY(#8fpZVCwk75Gzft<({9mfV_;^MDL zD9W89kGvQyw-1aEkJW^ZgB)Mwd$R^4=RKawjOf0o2j(5AwbC0X{9K?7v7K;@$8qDi z9XBuHvN7@ukllq)<%JB1bX=Z(8DKs|@e`;PQ2zGU1>8U1ruFRZR$a zfltMs410O`BX}b9o_tfuwU4+=WZJHBZQH{LdOagBy*l~#^Zc4chsxH9Cu_De3%>DY zyQVYcLX<&dvthYmlHH5ZU@NmtX~DW+H%GW%7A)bocgb8pR6!AVFb>Nj&5kT-D&k|f ztF7WkzX^(v@TzO@Wmn=2gUFFfuG=Ax&J=s(;RO`G*b0{il3V=b=E$cS0#*G&asbA` z;b(DGgOgX+2k-6&Gi41j`|&~zhuK0^BLXQ1*F~Gz!eB=SyLyK;Yhtl>`eRQN-{&&{ zb85UJqpqxx=+F9i53ZG~9W@jymu|s~FPx3JMu8yh#Wic}{-^b~&;)N{ae0H5?B1vK zov+XAi|T!gYsG®2HunRIZ_4dc7cZ5Ye*&tYYQH-y>R;@(-cY^I-b_Z?Sb%uh!^;^*Kh}xtsJ*mao(q}mAU30w!=eU}9EDt! z9{UgB-;!6~+hy;|G9MdYtA_jMnG4!S!lkp5B0C2iz0pfkj_fr)Ly$etGsq=chzX^Q z^&%asbQD$5Id1V84@!=lJnX-Jac1T|USX#{`Z39GA}h5!DycUrFX4ilN(&<#LfL%8 zk3W*5NbxWM)WS4q!7;ZRJ|Z?95Zy{oT&D&Bv>k*KtD|lid@$kFCXE zdKF_YVo)HT4FLn7eNgBS2626U(6}C62%TJJN~Trtx>~;t9golFF{3E`&3RgCqO|`S zMkC!`;$;zZQkY z87w5*TQ?5=Ld>u9E(G^~X@1U22sN6Jlrk{%%$luc5EB0t_ZU9oZS}+kIMi1k4#*?0 zY>%FL|JePR?Fp?3dF^yCW#ewv$=o#@)q>HWGR3j=D;B0)CG_vG*x4nT6qiRAo)J)? z5hk29$Vorg)?^q=Z;0DWMbsI`>`$+C$H|!3?*KsHEU*I+w+l`$->OPRtFcKrE1it1 z97v~DW+Kigei-Q)oEw}&>M&ERO$H^_b^|A zHl-q(kA5{s;ocpuIa;kl$T&`bXLX<$(3o+UjRB)zb$lhPLQkyiM}4V18xYS+fAWI~ zg>8dMv%w)^&_=B0ns@0Bgw^JxZ{=3TkpuTq@L32G=oVc|grsD>94vRrh_Q)l9i6I% zsxnegR9OW9XE3FN|FKbrlGibmRfF_gB>MUM5bq$>5TLYJHOMr=2*%RN64LO<-H{({ z5wKFSkD&CvH2HOrlBd%tStbgb)^}x&9dn09ckJR?u)u7N=rjUaf zOAX;pDNaj9(Sbg}OI}8jp)rdG;ZN2Jr5<&eBOjk*A7ZuXyHH>nj)qHL0+!HP1phbj;Xi9GDEZY#d_LD|#@Mk2hH-rZsC=R=+evrD_yHD?^|4Ih-@ zItv-~u&r;hS$jXJgb%8KmLX$#*Q$gmqBUZj=HW|7L>JWIu zQ&i%>X{AK?b=YpW-u&(jYrf-OS$Wk6>sxEO%U-|7RBGNvFc&{Drq@pCn_@+cP7e4p zIBb=tH)$1LF+>pCSD3P$a7(=^!7>0PaI!Yv+-~dG*@+%oO(QE7h(r~hdh05igqLZU zlgj40K6&Skkc|xb$lU!lIPQb`E zLxAv!=(gScAZp>VzH`Ar8hdA7x~a?C!Oi8B#T_|S^_V%t;Pn|8QSdYjwDvR6AgV6l$#z<*yrUGS z-yXM!P#ya0 zr&>KlTZ53o+%`I)|2Q1&!H;sAqY>)iJ*z_Py|?p2)!~zMcHz7P5#1^DQkQG{nv!d+ zms zGyZJEtvT^KrTK$J_9DL5MTuOgi5hqAhiC3FJB=n|G?}I^s!30oR5H4$V+6;nnM=|c z?%N9Ox803@K|{i8dwRJO3PBC>fLz7|p1!&F1vEaEL#nIM+amR+8AKwLhc#Bq`=(95 zJ}-b5Dm@YU5P1LbD^yjYiq8_#?P;MCYS*Vib!1J>BT>Vrqf1h;@O-38D-U>Y4am=4 zKfk77g`66zH(HX#r`te8hQ{$oiv8N(&)H_&8k;`F$k);4pQM$n{s2Sgb&YY|X5mXG zj9&rW%p|nxE!V+v4w%wb9$3*RlZTz#h>-eO*Xl4WL7$M4Lh?)@5Pj1*%~H>tw4N*q z_5?Y@@jiZWuWxk?!zN}$2rrgZ89x4@F)){!}zRk z67)=$`8q)t>}KK_S$go4j0ERNaQpO9r!{zPYXxdZ>;w0TdRMnD7Xio?4G9wkWytM%9D6Gz0wTu%)^hb~_x5EPBK#c* zM?CZgnCw~il1YG9bUW2S@F0z$$=yq+L9PqFd-Kn4i`Czs`YFZPYD!MwBQqrZ96&f7 zW~%x1^fy6z5a&n!RqT9b3ix^uDWAH{MJ*veM)K=uQe_xD`}+lp>!agST>GUe!Rq527k|d5Iqjc`4?L=PfwkKjr*e<3VtTbXeJ7|f1 zETGm-dQ`y~04!y&AmuD|jFWC*C2qs7^${-9s!z?b0BIA;d<>;!1ujkpmdgDfJY)e+ z|J3ogbp^P4Ta-4k=f?TYr!GNf^fkSGp4+xmK8~^V8j=z_<8-#$YhiK~mZ95T}<;*jc=|SF%PC|JSr%u94`|q zDBC~}?F8Hu4Jq_#0s(-8f>p9?a6@i996p+c0V@m=qceZWff z$F}1J3Q|Bm;xTEY^A0|x06~+1TS9XfT6EpuBnv%>S55CosYpK73Wjz2E7Bhg47eKXY<^)pYp?_ThY)7;A@+p~$U-ru1GHGqqgy0e zKwP!FONRkeW!J!VLCO;{)hh^OtpJD%A))_kKUvBXAP|p>;CHy70Yt7F_)Zy!NUM^! z*1%vOufdW%fTz>tll&EO6D(fwt+Jbd0Rq?Q!DK+lCgFYtCm|4iu)|HDG6bdihmune zh&CAKCGI!mkQq=9zcX7Ag2)+z@KC5@z^@ia4QT~n+oQ^jC*Gjw0o7Fr{|ShkEgbf7 z`5!18I5PxN2JV0PKduUy0;3~^*1BeA+kW-I%VZD^K`3h`b3NK|c|;ySh!74k@EdeW zt9+d|ub+Ut5`x3z?$WVBIHJL5GJ%(a8LOiKc?f*(MdP>Z^5@OV#2(K>%J(iHkU?NUh+v-OvK`g(#FZ+%4eC&zI{W2uHysZZ30$}!Q__fIMIG%UyS$}sH#tt7~hPEdANB#p-XF4 zYFlIgM##@-r4SdqIFEd`c>Ym1Sh}0fSAKxT6xf3WHzzDkYrvZjIH=q~|EFNr|zqyC$)+ypjCCryL_NS+H zPHX>dLZES#&V*wul|6W6npvId;F~T;WeA#AY);iu(gg<%0~;DBpL)WB8jJ*2w(Fs3{kRxU1Uo?1+M$hh zdTb6!yN8uG1-8JS#oJM9*TVgM(((=C6B7d!00s4q84Qds>|sy-JKmrx|M~8dq9FfA2*)FoM;xGgr>>wXrX2 z%v~vNpx9KUdMV<9fl!-z07HDUefc=FE_gO*5kYFPuD_w7T$e!(aH`~)wa4* zhvCm3a)w^40eKv1rzhR#cc|7YPX<#cAxq;kyN(W1{EN})BRqS8ja-K#Jx#%5P{LKv z!<<*Z0p4It9De9!nHlst^KwTA?0!tFr{IOOtaf*7RX=dNy-=^G(cN>O#W&pFdWoa9VZmEa44b^W?kvKYZ7FhQ1KG4 zql+!qy`ERHiws?Q811mFmitg!TGQFpy3mBi_@n$)ut?@$k>1cAYtbA(46Uh3sItha zuK<9HqdoRB4_8ShAAzQ*- zth@#d@s$A>_zBhRFOnhw6?fUB7r(?~F&Z+FzaK2J#8)LEU2EGV`O2A$lxi!B$_Eg9 zJR07wi*=bhIH5gPtH9X#*C{m*Mu1au(ACv{wC!Rp$KTmu>k);+W@zh5cq=E2zU!@4 zecfla8hhQAx*0S~xCLjg7(lh4W#nK7qq%Ur!=hz(^llY2dkoiwP=E{sQRNjgfSIUF z@PICrn>*One$JL;VRzE+HR*x5UZsqm&~c=TGZ1YIx{$qMW(`4uTsI|@2|@!!_ZW>Y z*_{mDkb7AYe1MOIU!?K99MLVaIj1WRn+?TfZMr7uG0Q<$6k+QI-GJom@=es@jWgzt zP}k@TM}xABMW$*BF9>ns^u~f#lnG>lBionvvbO91sGZ`TrSsvRKB89Y90w*26v4!lQPf4|v!%~6hfR$Yl_BNgFNvUGw&{JtV=WS;ku%2(*o?w5C+@LI$dEC8~V z>4=~yXE`$3FFiJW_Q7ih@Q}`h-QY&{gbhdb^n*`}lMxeVj}M;$c=*fl#`T-)A6iVx zm4n=b5-J^I?S%wJWLbGtNBgC#+R69miF9kNBicuNkSzcrgF)p-2D|OQH9Gc+yrz_; z#iU?S;}~T8_0e$jZ%`-+Xcn#fX_Z?f?@?MA2MH@xAw_JxdI7A%PDk`JPLv>kB-neD zlVcfmFYC#yLAkBTvDR)CyKVgEd6(){rsRT>hA`KFJE@|v2D~ZRq3CuTs1T`DScpMF z^uB?Y2>^HaOOY;fsuB5zxP5K*@r}`F96S#_ zO;DlO!UGWe0L6JMdN>?EdFQzEZkCqEyNBssc^6_@r%K=5qxJ1HPV$MF31wvNJ8;XX zS7lgM`ogcp8_GFelEt@MYQ0`nxjD&l>0CAiiO*f(io9`jjvot(l!jD~WD8=*07m1i zO(X)K@aMRmVSA6Fp8wt8tAyBZ+983xjR)M%#G$6!d4If(@sBcM4p{F-z`tX;REu!^zBSe2 z)bE0V%$&u54YkkzZvtNibgrr*ouAT}<`h^=@V0sfT~iFkxTPXTDvOXs)> z?_pK5q{@C(nRkF9VxkHV7r|fjQ~$YRgPE8hJK;OiQB%7?{9=NxJ7MzBUFV_4alA>8 z?4DPV_O0pM;&#pG-#acdo!tpF$9BHE!?~d)#*@CwxjF2euCi-;-CPB~FWb&jN4V1N z`UuF@bJFppS3iGTCOws_CE5No9(mz{rtv|U+BU^|Q243dh2xKXo?9Qc+OKaTU3wi$ z$o-z|Mxr+y8-F)s&2n5Rf9>l|VKC^cUYR61+gx1TDCBgcYPhAwq4zNzf(>TZ$jCv= zbA8v(c8?j5%~|u2Ss%}1jnI^V5)8X@c&-x_7hMM>a(#1)IZ;o8GMO5+3Bu7*dRnGj zb-Tx@J)7tvcH1wM7{OCCM5^>U=kHr@cD4aMP-1(MgWW@#-~xM(zD_tbe@o;|>pE9K zKDUD9)^(S~-2l#HW1Bl1@0;#w@j78CMRSSY3M{e0hvpxDe^D-uNoba7*vPrF#~&@| zb~KQugk8D=kguqN&30hpErUf{q*4miVHSpVlJBrGx7K`+iQthrG#ggO>cr}7Dkf*G zY!1k#?J1Z8F_+ndi@dr+F{mld7yd|)-OTVkWT)OJ<{OMz&89i2&cH?3zD`3t?OQn` z+Eo^G((m`4I6IY8DvM^$yP*y9U&l9148%eJv~i z$6uO3RM|c$_(x$fBVVe7*GPF=$G%hDuTePkSTpK#G1pPAS%c{A)#ZkbULo$7gIf#tD>(_-Pd7FWUs}O_Ehv{;0fy^kDKv6T2HUsqGAtH|(^mVbqbrb0Y=<8uF3sPAx@L9n@Crf;XSwfe#i=+Q~i0-eT`SeA+RZyEEWzCp(^p*=NdfyaCOsvC%% zva&(F!Uos__-c#h6{0^+h6U0Zio$>@;_#DIIBA%F>!p5>+7Ic8Fr%cVTGhcCOc(oQ z8n9s_u}WtbWLI07Ic2-jm0w9{9$Ve0>c3`Nv1%C-00d z%^L>P>DA8OKdMPpOr(?oET0crF{8b*Q+NqIViePX+#1&=P4QN^YiUxgWvt zO+Puu|8{eryb*W0LTuu^+O$?2%!xwtYab38%?K$7wLEEfN!h#3#B;ILkjDYLNp&Eh zZ4_rv7?NRao`tEV^_FO~eeg{v>Q*SCYTvUL^jXT>*+w_)CoAno ze-Ym7zI@@-Qa1bIGZoJJP^iK=H0`{VI6KwHNG}E0ta#zZDkgboVvOCxu}&DHLCFuC z0zVf#rh#SNC(fIGxTtRn_6gGIu2)jg)s>p>JW2>6W}q zyrCX}>_~EH6FQt{i$ecke-iV90S-$f5$M^Y{3t9X zX~I$pcn{{bTy|f!?ZQ6Wws5;_N}ux6+VbJ(pq(COrNfPK{;baheZ}Zo4tEC5EulOU zyDDuGOuF}|luF>0Y>b+huM4nQPp{w~tF>_utnHIBGXeSf5l(#(?~>zZ?csLEh^!$yGw?U-dmbj8%FQ@~RZ@A$i|E z@JR;*hW$81ICmP2xumd`-ai#{p~iYw5as40Y5z7adZfgf*@SjG^(~t&!@gA13Yg98R4#AWg_ zlfG0wO>&ZY+)P*Q z1hZQ|Et=RFFVnz%hc58N@U_BKW`tf z&Y4E5-^ZQ3;7VkH!>n)E^X^qJ6YXT{7wDBXIp3f6%WF`zISvhfJ5`rCoCE?)V>tS+ zHglijj%7a~QRMzT6{J&@-OtFYv3!y?Jfx!VdN-yE)+e`jYM4kkvfz>xk_nM|P5(^U)y-kyV0@zmx%@INZdUY@b!nfd>?{9tX@YyM*6q}|0Z^Aa zwwq#w8B`^Mi3%d44yXupr^);+y4?E|?z0gA9n@D;i(OrYjmkt`yQ*IhY>z$b(0F@F zukan(){%`d+%qLD3LO+wxbTB!7>=)=6Y)%twIi?#FVSh`8amu~hpD~U|5OuM=-@LQ zn-3r#3Z+EQ6n>J?P&dR4FU&XsRrRQ9c8EUA(xBPPHM0B4`nj#RCOB+*4MAkESW|%6 zK74ls6=$`q6Vk4d;54=8?aoL{#nZKcL`PH}VwTdg=BowU-*zJSHG9;GH(yV$B4})xAB`|`0!P!Sq%Vn-a zm?0AVY41}Hmcgbb?159}^TA5-j|c3LEaCm4?(uj97YUDO-r*YbO5&;#{`0U|7sT#L z*I4}HN-pagB+xT13y!TiwKML<nMk97bO0s-2< zE67EY8q6waRJ(lbK|eon1CGbSv7S~70{~G4U?iXk0cKKDLgP1vU?Em8#wdPSlRU=# z(_E%`TsubHeW}9&+}V42p|4(ZULhSQCwFy{Aabgpu>`!~hDooytjY7Els`@F?sEIu zOauSS?8Qf%x5sKS)DFL`69K;sBubtyuR%CSO0I)u?IYX0C3X$Mj!KqR#ckZdR!TPG z?%Fx-=@5B1-ZjYKaDB%Xv--0-9d z)AvC~-FNhcBdz5sG|f#P#9N@AoB&+wSOyE?Nlk-e1ib6$?gO|<+nM9*6zdx zP99qz1MaMMKmL4%LB(Gm|Jk)O6TsgOiJWPGDv<(o^`;5)pN<~Te_K`DpsuY^$_4a% z!{5U?{deaViqTF$liMw4-|&ebm;Z2t?ed}DyGm%9(m}s4AeVkqWdCNU-kgM9I`_$* zM)@yYUUlrM$_`wPSt+;0B=^4(w&xBIe2UCf*c*=&=GMkA9NI{VK{<-iA%#HK+e-BV z;%&JW{kTC|iXS?jUwBy?sMyn{mGFv^DSaw~b{`9mOX~sOyG}#w>HQZXJJrMG`_yi6 zRQ5FxNL&61+R00cna*Yv1m*p<5OfG&U()q~-Wi-$`8;O}cs)GmNF5Kyi&~whOm0t2 z3YQeVj5=Efv+_tRQM7p<-sK|)Q1QA|Q09it768m`{3pd^%3d#fS195O`@TP)=UcX~ z_Gl-4Kx`AiNS564&m2yJSmpcc?VWXsiIil}%=GXXr=Ow*_z2uh1u!)y%f(PI#-!0_Po}zkM8x+BgNsg|CP5ocnNnq9 z=%bQ<{<<&+yifDk$dW`t{uBOYAgZt5YK9{YLO2Mo@-qLNUP#VY(|Tl#_1uZg|FI#RGkE$HILuewsLRbO zCQCS68FB*RO|X#VOu`IVN5$K$ zV8$=OEA60!y4`OxYreO)Wqm7K8jK(1D&U{`Yl<2?szBfyD z9Mwk+G|ZXv#d_jEi3&&^4t~JzOfV*oJX@Fppc+uz4EEpcz7HM$^@2>gwqHM!Hy#Ox zWv~+?Zt>lgw6y=`L8npJUjrr{3xfKmQlJWf+`eiyR`M2y_!1|5YG%jkG!55S_JYC{ z-Eho^X4(T1kcVwl?0&OW&tZTXa-g)Kjo@_nWf!SGCI!zFw1wGsIR zV&|{Q%n_B`I#}KqGG;yroP(Wd`??qoCPhNa147Vi=hat^Cbs?p?I1J+Y=$LA%>>;R z&3k?N<`dS^yTb7YNyXg+Sutg7xI=chv(ScyGV`b83pN`o0U2>bKqyurHiBPEBfJ^YnAyF8#}t>e?g@@UM2zuddX+2 z*|U~Y(sIYQ9yBHEmt(tycfh(Qk-+CkvcPN5@h)nf;{^#!$%lhE*k-1h=gC`D*ZElS zETrR?Ju+kMzP5WZ>oaeeYRvkUUI2wrts)f2oUcVSSOy0a|Y0#UHa#{lM zWLzit8Hxd|`%^H5mVZVpH}A}weQ0%s1xD8 zpYrh2>vX$j&b}|-lJsFqn_q$Y3=p830FB^VHvzE+R>Vf+ZrCK+Ls37j%y(RfzK2t| zxNU3+4c8Emuk`0z&Gs%rPn*Y0N!&MvF(9yNWlzA!UG0aC z2gj20%u%PVR!@4!je1yVQ&h_j4oJOL1-nh%fyjIX$BqKSH%T*|vDdz&X8!rdgmqRd z>^)`dJgA{t`SjK9t-AqaBI8ESnF}01W#L3z{{0f9XCSHQQ3mt^EfInRbcx`92eagO zfKV8m)hwJKL%cu)*V1A%PD%nFfz9=H?9~5ioOz)W4h|9UfyhUa*2+pAeFTJV6r3mBdXNZcdO0f)+_tHCCXelaSX8stVxSC>_>?@^%e_=5_Xkr#aGshsa@gt4`8(7V6<1Dgu@A?s^`9r z_ziTk3#vyu2!U;3J1iFQ#P*K9x8aEd{d^sMK%#~(JDYgZUM#OIH>{|Vme^N^{ajPI z*hk_W|GWr}huSK#S7SPFr*j}o6Z+nIPhJNShf`LAg?)-%_nCX4g~cwli{f+zHv!-S zcGB~|BYRHb z3C7A~Lcn>BI$^XeIF$z^ry7*pzi;jceU3&i8a*C89NbuL=4@Ich{ovTshpk6rep4h z4*+M|#ledt>sJjeH;vfc{psjdJwvRxdF>mc@v zt37~5JM^cVz}fJ#gR`~OHd80jwcLQ(UnNOu0O?4^7C zA_MV{9ES!{4J`LrDjrGm3HVCf#ID;SsE0|e2h!`aqJPgs%Bg@{sEl3Wq~UxJxe0Oo zO~91XS@C=-4x|@-c?w7&-v6gHvOWNoGI8*eUZB&vxso=qJ^}tlgia{QaOaaTE_efgTUEPD`=}h2#TXsVOwVnaD;*~c7zY8)7o`t-BT;N+U)X}6O zd7XjUL_-p_Ab6wdaeif0^?Cr-()1ADHh8{ETlSsi^i`l*07(8H_C>(2h18Hd3CmtN z$G2c!WJxXbbO`W5?$_f~YC9Wg8b_Zr-id5eqjI_{cKcNq%{8IpnT`AouHMuP|8;{c8Dh9^xm;K@dTC`C+5PNgRCY9{<7C*LPD2SK!1qX0kwg9D4(?m%am{GhyS4BDABedfyp{agyQPrHXv8jCXYw}Xp4ZA>XNqO{2+Eq#pJz6U zaSTB8zF6eAv<>2WSm>w5S3$)*zp~Ty z%?Gb_>eMRkOXklNjvN)8OfDN~R|4Abl4J=o)>65$cM?}N6b>liphJ^Q;wEG{kEBcs z=I(9eNCQ<29bMm5la`DjahVUhp6u!bd_))Vk4qtQx0fiu4fNm!rCNRb{(HCqTNip|Fg9MJXkp3}&%VbEPDttl*>_yCrH~|2ApqHtb zQ@{gJBiIzbS|}l_mHxWWpXQ%3Q*_;GyO3S&Zw_l*VXu}h-)jOcMh_B?Bo%N+AiXHzlZoe&VDU5@eks<~uILan%s=Nk=dCyXI#9U#5qJUEI68p<*BtGv`!utIU=<4}6 zqla&;plROkYa=r5gZ@X$3pBLgR6}*isf(XL%Z^Uv(maPS9u%qADqwJ!$#;s6e~^6{ zY3mu3XghxH_I(MOHr0%T(|y9hl}ZEc-H%jUZW#3~Eu{!7Nmh8MjHs0QjL%3?k-Ted z3V4_Ii}-6j(>9pZmnr))mVVD~HCayn4TCp~_y>Bk1Gh-MBc!JiCDO*-`IppaI@H0cJH_GUl-Xn2Zli zrV2huYT?qIk8+mjVL7{zglN7r)wRjEZkrJ{!@|#Ze}p7>DEB{pne1iPr*oe(4WHQO zW_8CP0|Zo|n}`$A4qFD)lM-scELG3vQ*K`rYYu;(BJi{gRjERe=QA?5Q@Qd3XuCLdxaWy|fw;k?y{Uu#lR>%SUpjf&ap(ivJIiA6j8;B5K=CqNM% zCV5QZ>HWwsB}35W_Jk4d0_=wcgq592?-zprCBNjVVB5>+dZy?KyKjyiJ!qbt#h{TZ z{WPV29F4WL1_2_)lN-BX=|HCoYm?bWy#W2G2^>q;NK)92CmkPx0|OOzS9{c@FN#ls zj|zQ#>_4$Zob`kfE+no2D}H74d3DJLXjXhp#pt$j2=j%GEXh*tyY)+9kX9hV-J>c` z%B1i<5aO4l8lTbR_5)dPK@z-$4Gs=1a)TSb{rB&BoG``$ad_FkPtt%V2i-7HzSa;# zB#?uO{;R+C3u@TS1=3fhMY>f*s^N;xw@Vg2?NXCNJ>*5BG%4z(;( zF)kVDYANKDgjYIDRa{boQc6n< zQ@!xShN?h;fh$8)M%ygH2_e0F@>?g~JLTDnqQ|HiSiEZIShmBrwZ??4^DjD{b}UNo zW4dIK9<-JF_rTGIy%7+qniC)RpIrsO;peQB+Ot}^D)ndU5cC1!+3Y$anroAGY^I}Z z9-q|f&89P-!d2^T+mzo`ml0zjVzqMMFc+cx^)4sDAs|m0B>e*e>?w~&OeJ`r<3LTw zuPF{h)k!^^X~~)uV*G&&c3bkvlcSZkNr+X7cu$Fx(f*$di|3=1?bXTyM4MTIn;y4# zUj^F9Lx=z5Q-`ag&E<|>ASV=o;G08z5MpkJdk;(;=DB1vpS(0vSjkL$lC2sAw693 zhe-%HVD=>Mr6(N!#&+vTvSRJ&=BrHt=65T!6^m3@F>@WuNY7V}%! zl-@4yZ9+b|I|I=z68s|>Jz5kyB=59vIgAIUSocL{pOthXF(~N&H1^hUQMGN`D0ZMC zDxe@;N(u$`@G-Y zzy19;3~N?g*BQqd$GKpYP)-Qyt_0nXq|QMO^Uo`N!6=Mg@DqTV!E7h_OYk0R5IWok z-vs#`-2u#ABarz2{I^RABa!AcI|+JqiIxDt{siUx&$ba-{O<6$0)R)a%|Ms`Co0#c z;2z$oK%L@J)XCs5$Qh}F^RbQ{{_YuPI@v3IMk{U7b^2 znxi`XrkBgEru%{x;75QU3+P4Qf4)-(rqA#X{(7IU8m0<->e>6f@7uw;g3Sqd zlT_8lEbcVKkU}|CHoY`GHJw&b$MfKW_7%8*Hm#JGR*`+7p9}YqH8TiP2FeBMw7GPl zKqVhMMk!#I9k*jV_mf1k^Gdx84MlpL(p!qQL~BJM>@XV%S${(lVmTNg9|BmKrPjMJ zdeG1Q2=fnqg?LgI3vb2jH)Fp>4*aZ~*>*;2h42}|_q zkVXTjQIB9QDplDC7@#K)co@;`;PS5GA;Q4d`j#N1PCC zzi;GBER^cW4BBlac#qqx@A=<^<&YDu;6)A`$pD-S?`3WX*aVKjjv40)s-W`WPS@r* zxN#AOp)!i!qZgILq-9~yoGtnMKMa%wg?FGmQzpA$)G$o$P_Wt6@WtuO+vY=0Og42) z?&z8r@RtzxRo1<*=sxGEpl`FI=U6%jH#+RAFHAmRA0tf(lsE$LI9_BZUkU>YNR|?3 zS)<-7FA(ukJM6wmB+5mrbXCdWG<#fPjsl*+{bgd;%rwiM&O?3-lc>~BO*Q%-GySoY zg#C;cFhll`x8T7P|8?|tPIBYmSOrHvjk~RK{yZIfvc9YN-F>&N=yU2S*r%#2ux)bN z)F>)vWwth=R1qv2|3DxLO|X@Bc~q!jO;oF17K#4!H)5`tJVdE31?%kb9xTn1wvjJ= zZytJ!WI89;qcfpJ)x+^vrIz2m`Y5H~jXW51ht~V2cmB1vmb)p?9!^ViX+^xu{eipZ z3_C4tG7(3X!%$|-6=Y?t+d)?oh^oPet*Imm0szAxE)V|_j9e48-1Mz6XR%@(A)lUp z@OfxTZ%A3M8(~XH^PZ)ovkYhO=b4S5Xz-tbVN-x&J2xSXC>Y zcoU|$1tz6^KN0RHBMhdvkfI8qY=?~cO=nY^FpZMssq{CXaCi##4hRHDe)};qf>bd~ zZT|fd{=?fa#Tkf<9}WtK&wFal2HXe35QYKG1~xM_y$8w`paFSp)|8dzI*7zW009i( zy#-Lh@oc45I9PY2fdX{s>o>m(Y5?a;Ns*3}8@JkWRzetTbkM(yhpASOL6?z+L2khj z_?~sp&Pi3h_1%^8HI;akSDJj#AdHu}NMyWNWchvvwy62SYfvH@HdhBq-8q>FV*-He z+FeGt$10Z)PQmJJKbex##G}@dIUS=AWD2-l-UcPtV8DQ3Dv3l!Aii8S-qN9EV5Eit zA7(!NZ!5L)&)@ZhAfW9~5^W2}!nrvnXmgXb`o43fd=tgV570n$;|~QZ)PVKofLTN^ za^``h@kB$(@{83_T!$_Dr(MB2ruRYmSz_WbF;ELBP!E*`B*zUbqc~t_kR`_Hz117o zzt7l2&wsmbE!;i0q?X1doe}PP^wT!8#D5D+%$q5fQ1a4pRKpjVXjF?5= zLTns?Ywk+~aMI=okY@#TEQs~N9x%2keuR?Ov-A3D>sRDJ68J%OzPq9#6d8LjmIF8R z<8@fNK$ZbWBEwyQPHYxF28fxrMg4*_BT(^z6wVt2FvDVai|)=4BYdO)nbVTdR?+K# z&WPax-sAh_|DrOcA8qx>Fu>pE6rpmsTWyM)ew8Hb2PCY`B-GUVT$mPV{gJ6<(nS0t z=&{Gk#ILX(MXOqbKG<0ICp?9^F?9uQv`%*e$YCBJ)zFH#SV2{NhB6BaNFmALQCgfKXQeroQ?;Nwh*)+YAyp< zr?ME(?m)9AOWtP?sY|eegRMY>O_FTF)c07;W@Z6`OQ3^;3pG!--?|+F^bM^2ZN|Rw zsHm<*CLDcXq%xMu+iP6;juvZ+Fw^EW3y5rADVMw8;{UsrASjNV#*%*xdkeC&bc3;V z<_6`xN^$4j4$bDf%c2p0)3|Y36E41YkfG)1tBjUXHju_PdsdSsE=F&F>|PmEmJgJ7 z1yyUlQUzKFzk#|22%}3TGvj|@^VWuoOxjR;Qq#x`&Y5{%wB^TN?l^-f9epW8O&f(q ze_G$3>_MqD0yB(thzJwY*A2MWukQ6=IPs(3=2ZLHS#{mUB`UDAK_ClfRfiP=at!14 z|0>BP&#GCY@mZBK%_Hc}$U-qQ3=AS6Db!`#vx>6Ij2=FkVi(wc8+9ix0RNBzw=A4I z*4y&lK@agk^a|lU{}wULo6WmW2>sniI(5YTNh*eU_aZQ(8~eTCth+`QhKDvx9JSdc zqhK%_t~7xTm&59i%$?jdY%rTY^p1s=)<9}{tG8C!9Uqrc32txJgZ)+U6}uykg$$z~ z4?sQYp6K5FdcBGu>_w`(vOmVya0b}hMsl!vm^15#oIL?+1_U2Jh@WFWX5mw@@WRra zKX(%*oh$4y(+PDMkVy*Gcuw`x`Z*2*yGCs!Y;*^A%ZO06B>AgY4y#>&Y;fv6#k@a* zRSnO6O)Ci{sbB+SM32frZsaKv^l1WQK<5G=k(Md3mg$?GC&A;>JUf9r(<;0N5rWfj zEFEi;XPl}hyK3!|B|h#?t=;S%1)NCwrMk*#cK}schayowwKi4RyPbhLoY%FvfW?f(<#Sxpgj>i!mqB?vo)=y9%rnFCmVplKpPo;!!Y z&q~+0z)|JO9C~2drSMZPCy+fo!_MFa(d{kf71GbG^?i;$C7#+oXLD-qIyko#aLhm4 zpNQ3$_J?TCi+H%c2gJ)J(_%eo?1~R8jbL^TBE-@8gIfSY>q70`R--zXeS4n)?HnQ|j3@JHL0u zrN|X>z3pB#JSzD*>XiD8Dv0%He&bh=N-ilM(DFjlKg5u!6h^@6YbV)0>Uu}I0}uB|+-IZ9z|)fc?r>c-ZCw#t;l$=lXLqja zx{WkFQfTQy# zTM_Sx>h#3T8?G)3c1D9_Lu-Qu1GVAYhT=v_a#Tf+>p~1-ni%xoy#)y%V7+UF+Ap9w zU&#-UG_u#^x=h*<*Y84I046S=K(9e5 zDQfxmY@UI*{{yp2YVRXvZujyE5N1PceYB)sCVg8V$FJs^=sjukEk|GqT~>iO*Zn+K z4FaCRc?oo>SOUm>drkZGchPp7)&y z>uD5d0ch`VJ(|adBK5X?c9x26r}b1sQ6J!pto%t=nCc%Kj>XmaAkm=B=X*!#(cWWl z=H)v@otoXTU}i89W4(E(!yKEVfS5X{*bZ+}G9yK)iZ6}+G|Ezsw*dJpGtPxs zlI(zn_*l{+zfO6|q%xNlnpL(G*87V0_pq%aN(I6u(aaJFs& z7CFdCgTaj09_u(3Tm6d*(r zroZCaUD;2AyBz?&+BCt7_W_ov1ZtcDFlPwP?%J~Ob#1!NsX?<*Rq7o{+ZThhy)pY&gR2rnr?7=x4~q~ zrnU@M)DoC01lTN~v8Z!s^<0WAR*9ToOW-Vt;OzC&ZMTZ}&SFvhRi46jq_kc}FAUjw zR@oZ0tU4`zxW6)cv=LP5?`iuOj7VdE0oLrVG@$YlNdS-ML-jmh8-d|rZ~=p#?$ptw z`IhVAmEFs?3<=VT-j2UKq}k!M>;j`Xl#IB(k@@*-vZ{irD6wzpweo`gE8^1eNyc8~ z-DMv2Y}ZmN96)$@Jc95E>G&jQWmzex20ek?q(cf_JjltuZg+F$JMhR?LPyJgY;JqM0>l}S**6p9i6$%K54l2uRB@lT^ z;|oPP)T#qD(E=y(Ma)^@Hi6R2FwpE9wOpT+t})3PiY=UwJ$|Z11eG{f%kyFxr39bJ8N}kW=o`< z_~~K#4aSv$o+NIqx?0z0I0j(sO(wp3AAOCG($|d6 zwt3C3MqOSpV#AOUdSxH1j}JGR@N}-MAUZ7-Is-tQ45nSy%Im4wVARPlM2J%Rq9K=I z6sU~gWYO0F#RCkwh&(8&PCHmTBx$d|ZtCsn5GJn&IEGq|TWi-NXb6dt@?oM8j(wR% zwZ0Tua#(fR-07$OrJR|!gv~fb%ii~y*G79KQHj=WW&~6d7l=?LXZX>rs)HFh-2O_QUO4d^mglxH&+Z2P5W^$Ds@fQ-6C&;Cj9r0ul!Uh}y}vc9;*Til0J9zJ8Ut>} zu{Ut??HHX^M}Q50eh=b5hIHN}*n!itGAGdW=Acw<&h|_u^{e+^(!Xm&s8P;dwi6$H zP{tSusywwk-|y*u24#K|%}IDT+cqaOV^je!d@|__JE?HV8HJm~Zg?(f*!{bNj9c?> z<+Gz6;4M8uRG^-8v`?HWts#}GhY@wMtyv;-{+VjZlBA;)6B3xopV6Do6_lL;L(iJG zh9=w1|J5tS(^OMt2zpt{yFi<@_MY*DBQ-F}G$3gm2Guf{fy)FyE)W+az!%~v7X8j$ z=awd^S7xVA$W4A!;yg|ks+Jz7s#R~H5h;a5X~3?6xdZ?U=pkQyVj{VGPxWj6H}3a0 z%p;|}x(b*V}XK0=K9>YMVP9-bonI6a6mJ3!D2}~*&15e zg`%gpKYw2W$?5dI)?HAuKmO`UvPrUjq2in$h4o#9{0^$y)|2^XZl*VhMvs2u^kA!+vmd02tZi zIp6l-pSf#)T5xX7f;QtXd(mL*a4vSc3592?u`$@j21d z^Rj_q2ee`p*Ghdmr9ix8HijcV6J0UT?|+&5D*5oRB|4fhk?@qWky+k&eyHtKo1JsN zA)wxXxRVHk1~OI=NH7v^;}=Unhs52odk^gq2I4A4C{_EHe6(DF`SG*c+x%9gOjeR$ zkPFe%aIn2w^4Ue&^O^Lb*ZJ!O^S>u@;R1KnIfM3+?C*9A-a-)G$IObws?OUxY-}hZ zYIflj!(OF+?p%nPd0-2ELiw*U!M>WZo^T~+e=|lQn-hw=x*d&5shCSZ~V=pS8iCW}eRkhhuk_Qu86(W#r?p{RT z4GMphs)lsyP5o5AoqDO@S;}VH-zZhQl5I_x#&q33PfbBhnA*ZVdV!UwZTsbC)aXvw zSS{5Es_1LTfl?!eAM@y|cYY8y-%aX(4Ab#K4KUz53DT5z>8%pKKluJg%IGs=%@)k1 zUlVgHWh%)A>f@mRaF!^Nln`2IfgV|FIscpX40qS98NLgDkSc|K;pVI@LN@6;lglpiH~#MCZw~80pA4_4-6oXxb{D)C zkahi%x29o^j$YxtuX_{P6)@O{$ujMr&=Dx%Cy-vg`eO7w0ztC~YU$9H1zq~;o2KDg z?8&fA5ZXvTpj&;d84&J4;sa?OV?Ygr4 zBCvr#C@gfq4W`Xw!~s5Ha$8>}OsmZs62(LBbzlO|{2kiLYamjZ0}4kJLP{u%9h|3f zW`=vNUCQEr#0_rp^-a`rZpcdCRUgpU0}4t+^5OD=j)$F>Snzff91^@q7V`HP{+zCw zjV)>PTW!5{rj*N{1Ko9uv&!4fI7eJD0L4{s9WaOp%g%s1WfZpt>>$u$q7crgu1E)Q zRXA~YXRwv-lRQdqxf&_Zt_F@Rp z^HzEB=YD5YE!h0OCZQVIUa&b5w96xuCB5~9tR!z==H5y?o98gz#OI_dg6md}gI?)z zP1x7V$=>M=mi%T^sEj1qg`hlqd&5iS^BTjDfAHO&q~f;fyrX2v8xm>&blZBn%xO7)UkUIfz1>!p}kye>>ApX;V$%SJF% z&x0gn6tmzVs?R}Lfjgt2=ssGwLGc8WTEX)hxJbxxT&!x`_fc9)}T0X75j-iX+)rK{_1s?b4HIG z9f_-DzW5A8{QNQl{&1XhezFZ(FUI#rgvepjWd6c{$ zV$AT9H$2k)ooldQ(`}mFR`Xh`FX>JuJlk0XW8!u+C>qB5O47 zXUO_$Lqm;%)F{2Z^A5~G_zZO?CGnFH`XwK2H&6v-Rh#2I*UI3=PwnArwyBCj zCmC&S6LOQn!b@z8@Yph(jvRKW>Pgni)QxvNa3n8jac!Jc9&Sz0bD zGv{&US>E{Y#MA~sX5R1-QwKu|&xiJ=oJ4tw+VZ6x6$ZSb>Rhl4jECsyYXYgQ`S5hN zhX{`GMqcx`kns?mqDCy{08NI9B#;t4<-ba>EO?AIB51g~ol^7JPw`yJU8K)XUZu2A z4wZ)C+brE{XgJ7Mb+N-T%HQ0$UAXLjn^eSAMjw2)t4SZ$BOM#|!Fizh5HRS~GGHe^ zlVxZ=mh)~^kQ$ULT7cf6BSeQAZ_61loyC&zy`sHL?Pt> zP3O;2A?pRkSxKltlDV>55Y!UY36|;8%m01EiK;{UzSOLaOC_(8jNQNFrRreEZuaX> zRAe>6V-E*MLg{b)J$rsQCuOgg_CdO0i#$cqn~MD8u~!K9FJqPhCD#~ny&vR!@o z0Ccx*kki(%!ipmR*ZX6V1N z#COXtmRPV{S(+vw z>S=h&?YgWs8}V4x%S}d}J&Z^Os;n5U-Di1u!}0*jJ-Y;CwZqtYz_rfn)K>;R6CE4a z$FCaheuI20FTJimu1z3U$fUoiP+KK-6Pav2Ze}(7&6zHuduVp+&I35@$`#t@H?(P07#foNyGyOa`%m}%% zMTK+2cwY6%Ut@m^H?^WxB(mE3L&2wS;%29hwP3dXaIu+NTmitHmL=!ha-+-T{xQ|> zLsQf~3Ky5{))cSl&>j+j&4DQGWJ9qKg53k(-(U^%rG<4 z0ENMAccD9W*Loy7?Jc>T#!8RdlfX7ES)qC9Kf; z8|8!mWIkJ0=xjD-g27j0a0nTfyMGmvZ<87Jvak@<=@^@l;Gq4FJRTC2Pn0fphi@QW z(dYfnk2+^IDJC;eRh+?{)g7iybUr zXMnHh!ZglO7yWZs>CCHgLb21@?_FZSU*vqk z+dsjf|BqOs;oFnF!t4gN9-wcR==InLNows{b5IWAIqI!%E_;1YSvTgo+C#hZ<=n@+ z^}Bm#(+H{1p@-Hg8;>!kzYo$G|5w%yieJHaW+L?V&yBp5gQ*w27zy3{B`xpO*xf#( zZ_AH$Ir9?2k3}#=thTcJ0T2i#@*nPqtB3uhWK^CJ&-jDnAU%Q1*b+A`ftkdw`sa+Z z)jeMP`9F5mVWv%0mBONTERP*eLTQ)D)@l*96scXm)3G$gRvy9Mj)zyh^{$&&^TTX% zmHE}%dgiD)nPWBnVv6KeyK>{VOXj@S3<|8e%kTG%PHXBr^-^oII`R(8R0?JlPNf@h z;1c@AUWs^88Pu1sti^8BMuK_8ke1DGjP~ouW%2&(m4o??w?$gD%Oi}PKrgdS1#YNs zYyPgP=9%3-7To2(T4`B0cY_S2z=t(tM5)qhI<+T#noqkO<~4r6#sJs1uYYiBQ+k9N z<$ye1_Qm}jcmIu~Ud*SjZ8O++#T~vs3Sv4}mdlo}E&L=iO{&c<2Gpj9P7>5vldiPT zas*PC*2ZBk-VCCe(UrZ$bKb$F_p%N`G`5q(=!tbwP5ai<&oavUJTA`RaCLjew1O8i z&o4SDM-74zP(97?3l;pPYCcCxzdcJ7*_Rutl&ry)Y8rV z`cceJLTjk#!L`oBPw$}1g6?!pHonoT9ag%h7?mzZK~HNyhkMMG`|4r|{UAuaA0K1+ zsAA$CJ{J&-i0FvH6Z-!5hXHcdY&C9Ru|H`;Hs$~22x-{qZ~UQ~Vpki69MXb5xy5lI zTpyd!3%RU^7Lqg!Gw*=`z0Wv6zdwdPeoW_^;dPJN*p=t?p+yyiMQmDjd;I!n;=(6^ z;|CCmA`eEMKN)xHxquZl&F+sv_ff;s1KKjMVwPXlsLqjd;mU%biS~F^tSrPo{P3T@ z(pHFby>rdpdYFr!5TRMo%^Ch&3ScBNs$~T#&(q>fC_*!&_8i9bo4mtclly}trEDkQnK<2 zf{J=O+N>;w?6RpYhs%W=>tY1Rybh7}feLPHLG_5iQt%~`gI?%ZB7sigtUZ($LP?a{-SYe(GVgyqJc#&J?*%Dom>CCtnZ9xF z)jD@PO)+RUCHmS(;=j6Dw0uPG%6adsOVgu)9m-BR{#~mjk6Heg5jO}MjFoG-sqLZx z-vVCb!^6)*>pMQTH95U%0K)OlPjddN&gKiy^wF zb9~v3>g+i29K?x7xql_?nb`mSV3&mPW~aEiRnM!(>epa6>%JE_R-_|j+$QsCvbw7I zitA@AYsz1Fj7M45&Ne=e`}u_GDM8KaGZ*z*+oa@B43(<1yGz7Cz3x-8yzB0egXGws zG^*bl@jto`3hi&mLIi|ucTA-j;P+43cfv$A>fv#<)>T3JRi76inUV@u9spr1QOQRU zF=6b%1F%YwC0aczmt4^E{; zf$PKkm2gWXmXzuAMNN*9&^=Ic;^@?L;^<$cq`I${tTV%-C;NZMmmcX^$!gJSm_O@+ zw+oE0;x!BzytZPxSr{UVosf3?Y3vzW{V_N=f+3~bPam(CP5T|+X)CvJj_54KH3B8% za)4aA=zhtEBQLPVowe6zFQrz#zbM0)QfUT6?W(@OF6MuVn$A_iD+m??g`6p}%eY8c zJi5&``ek?vwDOU4X#3B;PrS=xh~_gQ^yU;RR3mHLj+Yc5A|-AX&CZLjJG5mhd7h+E zzOn4`=)+e%X2<7Rvk>n+oeuP6OyP-j*dE3BwkSuCRcBqM$)oDvY)Gt@~^0TY|#8n{nzk3 znx&@-aD}`axOt+D|Nf9Hva_})eqaw}kl2nzc!SV{X;S$2Z_onuzeE;%6IF42QJ>h$ z&Oyt-!>-|n6ZuKYGtq_6wF67nXMY@YI|9phH_*sz>G8tLP571ExDC7&+AL*!89FBH zk71X;D}x@ft0x=T$jf42)c!tl8Hf~=UvQR0Cl!>C=&V$;k+eDV+pCvc>nUHd^)4LQ z6dUB!O)VEL-KT5kNNj9Wo(%;Vfh(@94XbU2t--qP(%BDq;lhv}ok2OInwuDMwO!Kw zLbmNXsK{EWCC&0?+K&6Y+DK*|-e5U7W`0zAM}w?7r&&^c>1QQ8Zz7>F?omBC`_ptV z;Dcx?KweM{=3Y?H^V*?k#K-pgYOY%Mbg)vdO#^?^IkE(41HZR#G7VeU5t!;bJ{QJO z?+hX>vAV}k_&);3JCvhtRV_*FhCbc*a8+-wCu^YZx*BpkZRIlRJ26jTn>lYczOqa5+)SctPj)ER6+9v=ozpZ)Y==RBCJXyy)h-JTs{|?5s$s3 z|M_87J|QjLn>U$MLvWH`vr^Tm57lX>xIU}5cyaU9kHD7ChJk7nbuYi>>+j5p=C=!i zRv74lSM+F;H-mlwvYiuokq8RZXi=Mnvx&>;e;M!Pru5jeJprB@5qo6S3iQtiX z#D=`!eqKDDyyWjXmbY=L!C(Dyf8bi3zKp%;9wNVj#9kIP*$An(6G z+~|{qEtvSljP?-r?)#H%jg^!rAD~OyAhh$fX~9vfYQ1w7?BM%W>r&&l__vSuV!IXH zcNDJd4WGH0V3wcWC`{>E$)PXJr*S4>Fn`j*z@lhD@r+nbjg?2yAmZ*7tRlZJ*($5% zi7xIZ0>@I3dH?M`BjG&xV$#g&^h8sPytWQ!Nubqi;} zP;C>Fm_~|^2X#Yv_g1d;J``*FmPmlkL(G7;BRhDWexl8~1Edg!d%BlJ zdc-Y1OuYEG$!d=au$7#+GZbNIw?ONw&Y=4$U)nz4!V!f7wQ#Yh2I-Xuee{SdJC7*c zup&jRnF@6%iHQwL7q*I-_lS^%t((X)FEGL<^)&o`F{XgwPBkZqnt^7s3O43_yZR(~ zaK<{`xJGScixqJ-UPs$QZXuZR_Vz=49-)gJg-?43q%*bQ12`sJlWE|Av=;%kd7BS` zhCsi-a?Sbwz1ZWUkr{=Hn1-b4sqqK zeuF_3PUw7DE=j;5Y8TxQ`BM<8tJ0@QqsIFs14{S%IxEelJdUQ+#}2iHfRIEq1qiae zZDM^oOB@DHx}75&&_=&b%Ki^P9_Xljk-t_Sa+$^uD51;`n3siv728zrL)&y-B}H)90&A{?Gu zTs6vcW4DS8d!`KDrU6++DP}T40On_+9IQg@X_Rx}_X#>J$eaTWRL_CoV&)rT2Zxa# zoL@r_7&2d4&Pf^-Ha4Yj)9Ki}u&n~*R)3+hR&VF@Yl1U$@8LDS=WBa)KK(5B{5opg zZl8@2xsiZySGI4(DEQ3iW7CPmLlXowt4p@-SjqfAI6zNF%7DL3uvg#wc9NZ(%--tI zowEfuB&ex^5N#VuQAD?c#%Chlhv+0m>W&(NESZIPo}FG2V`>JtM}`Bc^TV%D`8deB zn!l76lzWUO4tNdI_xaU%Bf^*3a6tNQZqrAEP=gCS=m;e;pZYh4rf;(0WHw#?!?Rc+ zye8Vpfutgl<}v<5`^Nd|+mwHw1$gE=b{)Jd6|tbUYUEls;@ae^EV^kWfkmNkU z{_fGyp9i3B&fYcgzVVO>er#@0agH@dt1I=OUhyp@^~J~ussIZ>G9bED<+m`tnsJQH z0G*TwdIg2VF*f}7!&i1kpWjSB&`On`V$^7i^vQb}r67#tBizDlc zeBwnNo4J@*cpRH;;It)$yRoi?Y93V8q+=BH+iC6cKL8k#?sBbR@@`q{1K)JH7I{4l zW{m3)QQMiC0?-JqyVlGo_Zi(0V)nEd$X-wC(qn3fX{qjs!+qa>RLpG;jN4 zt7}E-gTHI8HVxjY3H$9E=0MR053=OI*)w)+At?g#>(t$SN*x!s*i#xB)zaT$*L!&UcRdXu79yRRdw)gU}x3T`M zmDYB7J(Hz+!O6gU%bsmTNH!r@Jn+K@Dl=#gE~3qJZr#cMdab-}N2;T-B#eVOiW>oX zy>hZ-(0sIPK5l!m$H_C6D1n9eD_tct_dLSDo_%^9+hPUMXCPhwO84}QZa4I;gT!K# z87%2E$aHi!iq-o_`Mal|hlXWAsF9+i@9-s&%48{c&z!(`*Zud1><@)5}GrORfTCds;j#EwPkly&RrFn?7v7pNh!n;(cQUlcX+&ilO`Y^S>;AjHFrh$xG7WB$z zPtVmp@0>iMKMDcjo@ZAe&4~LCCK9^Fz7&c5{i+`Qab&Z>&|G?1QbS|)E9_I;8OpG@ zGSe=m@*3kb+mRCZHn<#-XI1$FAzv>Ps7ptMUdxcf(w!y~>Z)Jx3iUsM9CcjZ~@P;qhYznXDX#0MHa)#~Vy?ltxP<~(K3tkLd1 zS}es>!#jx`uGp})?f&dxuiL3!JwjWxP^pI-%=hE*-#ceE9%{*yGWrGMhF-tu&Z;8= zW4x7Ht%JdNa@vofe$tzvRLh4KyMsnQ37Ou!P&F3XWgDtAZace-HjZuM(3r@E`S+YjPO0%J(TK{ z73h8@as|nO$;*wQ5t*e7diH4IS0g!2k0vy$eOz~Tt9jeBWw9~ICZz4M6NRE#a`9)d z&mDOg`(?4^ef^VH`tsU7q98w>Q02M3vBnA_P!5-VbTaunFZvN_Sdm>3ea?frVCm-c z4kLApM{J>;ID8r(KCg{DVIc9cFt(WfCeb_8^y*Oe zQVUp3!o595o@dHNlTRSY|Bnh((296ZN02}TlVh904XP(aKib?qNS}#7rWC^D)R3{^ z2FasBPE{y>^yffn?9eUq3G{Hn#u>yCN$r4S{_CejeGaaC6m&KQ!&%`kY&dJ)fCdGk zsp*NGiXWa!c#vs()*Os{e95qA)LY#p<1LD-@}04*#}h=LIh1oA`lF`LPF$uXOwFt> z?|qh;q}nXfT(Af@AfDpN5n7Ii9ece_CAsac`a>45zBdxmrm&w(KIIIbb}L=|5RA0t z8(1SZQ73NCx6X87*6y`09nZ;q<)_)LsAr-*CaWh4;xRlCvKVJ+Y>WY@q_O%(8p39tz72 zkMCijf`&#MrUrl`piNN+w23h9F}wT59Jb0vWm^~V&D^?a(eGZftK*<~dExo!QoBnq z(hP!}ZYG|SpTXuSfnR18#;8(ri+Qk)beJui{Gxn;wEY`zwG8%30djsHPYN?>0i6ZU z7H{MIYDC12fAEAOg*NHSu=5jGCAB$ zQUek;_;D{31;}ee=2o6QZpb|+D0l;JY$7tBJt$6*;&tEMo^*)ma(X6I!90=p?Cch@iddB)z zylsnA0juB^7TK}I6*jl;=yeR}GX98GN2gAG@hcNpen;(|EcVyLrfu0d{`!@3!AM%4 zwU+jK+sksM#y4pEy!;Y=Mbf6Y-*qrX*)zFq{bD(1%J86Cp2F@sPXzRW=x4WmS6FH9 zFCP*Mc8&gUq+74BXnFGGa9wyoG=n_C|GnFO-(Ee%k$b|}nD^S^Lt)XqHC}yFk)v|g zkjdOAqN;y~rDpj#Z=H5Xp=U@7p)z~;y6 z1C`|>oP!gkyC&0m^Od6#y}|}7E6ZfV6*0>%)DAs%G{DrEhk^jhWqJ`1>C#BKP*a7XDJassGieK|j(@k5p1jtSb z!LSD&o<9AH#+y`#17aJHvK7tzQ_aV($gk$9CYu>kff?v^aq=%sc>Gvu_Dd_sz-)l( zFtDB@N1J=~b?d7Z&-SwYYq#|T4_)?Lc4pfd{f}tVu6*Q6@{wzo`6bp~lxGsDti5Xy zJNo12BS&0e)$)|y-Moe$ep4P#-&a@Y^bJ<$9uVIlBSDCmc{JSLH33wu*BSK(+iFLv z1wZ(O^^R-@%$~6|SCL&7YETz(Rr$V~RTn&(F;x47TagS=9yG<=@7Pmv(U%eL@4vCw z6;aVx`_HOTu6dmY2t|u?eE%uks%Azhp9erG-6^3~JU%Nq`c|558cjZu31S8y) zDnvuqKF=sl7dtOsA*vU?vON*l#`HoO+c#r>)Fdt<;=L8~%>|%Z)3Xa)O-*kvKis+$ z>w%7D4NBm=WByw(2d2}(^R(%J&2ZAN9JhnfqCE>9m=Cvlk?5vslD$ci49tuvdhbBz zIw2jjN7O5zjan~I7t^hMzf5$S4p*QgB<8)-yeGa}(E{!6VaJ+3*O7>Rs*72JwsLgq z2i~l&(bG3ehUEwLR4Dnkv*qSA{EB&8mL03sOanI!7jv&r5b~`qam7m?ZdH`?q;a-)u^kBQRu_Tp}pW z*gdSdh_}t%+neB#A$w;i|H7p0@ej1ldS*p%i1*c_dfIS*PRtQoSL48!58BfWd3 zZx6eozW#_@xKos>l>~;|5}D_AoW1sG>_c*VoJPsOeJ{8kwB-FzHR^Hm%&E_z7~*UJ z*}<|FQA?=HFu}HT_};U;V_@NR(820Hwj%<@oG1g@BN1<(r45Wo4+7=9n;`5fs#$#u zSbddolJRGj~*`LVU{(WnIb_U-gc^Jvnu5ie)|7|gQ zL|hdzjSIlcwhNA_9THo{iu;#%6t&uLAM_nawv7j+&4`2Z#M4tgFWD_wT1e}=Y(-Z&|L_{o5BOuW|K-l=d c`N@H}AwB=KBS!Hh@KYiM8CB`RhmQjO9{?MLx&QzG literal 0 HcmV?d00001 From aa10e1759512191e51db3d21334b85e0e36955df Mon Sep 17 00:00:00 2001 From: 506359831 <506359831@qq.com> Date: Thu, 15 Jun 2017 12:28:03 +0800 Subject: [PATCH 112/214] github test --- .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../src/com/coderising/ood/srp/DBUtil.java | 25 +++ .../src/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 6 files changed, 278 insertions(+) create mode 100644 students/506359831/src/com/coderising/ood/srp/Configuration.java create mode 100644 students/506359831/src/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/506359831/src/com/coderising/ood/srp/DBUtil.java create mode 100644 students/506359831/src/com/coderising/ood/srp/MailUtil.java create mode 100644 students/506359831/src/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/506359831/src/com/coderising/ood/srp/product_promotion.txt diff --git a/students/506359831/src/com/coderising/ood/srp/Configuration.java b/students/506359831/src/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/506359831/src/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/506359831/src/com/coderising/ood/srp/ConfigurationKeys.java b/students/506359831/src/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/506359831/src/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/506359831/src/com/coderising/ood/srp/DBUtil.java b/students/506359831/src/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/506359831/src/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/506359831/src/com/coderising/ood/srp/MailUtil.java b/students/506359831/src/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/506359831/src/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/506359831/src/com/coderising/ood/srp/PromotionMail.java b/students/506359831/src/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..781587a846 --- /dev/null +++ b/students/506359831/src/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/506359831/src/com/coderising/ood/srp/product_promotion.txt b/students/506359831/src/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/506359831/src/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From c86742705512d195711a481fc797e8a8ae866b11 Mon Sep 17 00:00:00 2001 From: Mori <1363044717@qq.com> Date: Thu, 15 Jun 2017 15:48:21 +0800 Subject: [PATCH 113/214] first commit --- students/1363044717/ood-assignment/pom.xml | 32 ++++++ .../com/coderising/ood/srp/Configuration.java | 23 +++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 ++ .../java/com/coderising/ood/srp/DBUtil.java | 23 +++++ .../java/com/coderising/ood/srp/FileUtil.java | 23 +++++ .../java/com/coderising/ood/srp/Mail.java | 28 ++++++ .../java/com/coderising/ood/srp/MailUtil.java | 16 +++ .../java/com/coderising/ood/srp/Product.java | 34 +++++++ .../com/coderising/ood/srp/PromotionMail.java | 97 +++++++++++++++++++ .../java/com/coderising/ood/srp/User.java | 34 +++++++ .../com/coderising/ood/srp/UserService.java | 17 ++++ .../coderising/ood/srp/product_promotion.txt | 4 + 12 files changed, 340 insertions(+) create mode 100644 students/1363044717/ood-assignment/pom.xml create mode 100644 students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java create mode 100644 students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java create mode 100644 students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/User.java create mode 100644 students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/UserService.java create mode 100644 students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/1363044717/ood-assignment/pom.xml b/students/1363044717/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/1363044717/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..dbf48911d3 --- /dev/null +++ b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * + * @param sql + * @return + */ + public static List query(String sql) { + List userList = new ArrayList<>(); + for (int i = 1; i <= 3; i++) { + User userInfo = new User("User" + i, "aa@bb.com"); + userList.add(userInfo); + } + return userList; + } +} diff --git a/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java new file mode 100644 index 0000000000..e7b12edd30 --- /dev/null +++ b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +/** + * Created by Mori on 2017/6/15. + */ +public class FileUtil { + + public static String readFile(File file) throws IOException { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + return temp; + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } +} diff --git a/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java new file mode 100644 index 0000000000..c18227852c --- /dev/null +++ b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java @@ -0,0 +1,28 @@ +package com.coderising.ood.srp; + +/** + * Created by Mori on 2017/6/15. + */ +public class Mail { + private String subject; + private String message; + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + protected void setPromotionMessage(String userName, String productDesc) { + this.setSubject("您关注的产品降价了"); + this.setMessage("尊敬的 " + userName + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"); + } +} diff --git a/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..129ffdd207 --- /dev/null +++ b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,16 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress,Mail mail, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(mail.getSubject()).append("\n"); + buffer.append("Content:").append(mail.getMessage()).append("\n"); + System.out.println(buffer.toString()); + } + +} diff --git a/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..5ff6a9665f --- /dev/null +++ b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,34 @@ +package com.coderising.ood.srp; + +/** + * Created by Mori on 2017/6/15. + */ +public class Product { + + private String productID; + private String productDesc; + + public Product() { + } + + public Product(String productID, String productDesc) { + this.productID = productID; + this.productDesc = productDesc; + } + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } +} diff --git a/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..43d0b264bc --- /dev/null +++ b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,97 @@ +package com.coderising.ood.srp; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +public class PromotionMail { + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected Mail mail = new Mail(); + protected Product product = null; + protected boolean debug = false; + protected List mailingList = null; + + private static Configuration config; + + public static void main(String[] args) throws Exception { + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + PromotionMail pe = new PromotionMail(f, emailDebug); + pe.sendEMails(); + } + + public PromotionMail(File file, boolean mailDebug) throws Exception { + debug = mailDebug; + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + loadProduct(file); + loadConfig(); + loadMailingList(); + } + + protected void loadProduct(File file) throws Exception { + String fileStr = FileUtil.readFile(file); + String data[] = fileStr.split(" "); + product = new Product(data[0], data[1]); + } + + protected void loadMailingList() { + mailingList = UserService.loadMailingListByProductId(product.getProductID()); + } + + protected void loadConfig() { + config = new Configuration(); + setSMTPHost(); + setAltSMTPHost(); + setFromAddress(); + } + + protected void setSMTPHost() { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + protected void setAltSMTPHost() { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + protected void setFromAddress() { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected boolean validateToAddress() { + return toAddress.length() > 0; + } + + protected void setToAddress(String address) { + toAddress = address; + } + + protected void sendEMails() throws IOException { + System.out.println("开始发送邮件"); + if (mailingList == null) { + System.out.println("没有邮件发送"); + return; + } + for (User user : mailingList) { + this.setToAddress(user.getEmail()); + if (!this.validateToAddress()) { + continue; + } + //设置促销消息 + mail.setPromotionMessage(user.getName(), product.getProductDesc()); + try { + MailUtil.sendEmail(toAddress, fromAddress, mail, smtpHost, debug); + } catch (Exception e) { + try { + MailUtil.sendEmail(toAddress, fromAddress, mail, altSmtpHost, debug); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + } +} diff --git a/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/User.java b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/User.java new file mode 100644 index 0000000000..71fcd1ea85 --- /dev/null +++ b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/User.java @@ -0,0 +1,34 @@ +package com.coderising.ood.srp; + +/** + * Created by Mori on 2017/6/15. + */ +public class User { + + private String email; + private String name; + + public User() { + } + + public User(String email, String name) { + this.email = email; + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/UserService.java b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/UserService.java new file mode 100644 index 0000000000..3b2bd5aac5 --- /dev/null +++ b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/UserService.java @@ -0,0 +1,17 @@ +package com.coderising.ood.srp; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Mori on 2017/6/15. + */ +public class UserService { + + public static List loadMailingListByProductId(String productID) { + String sql = "Select name from subscriptions " + + "where product_id= '" + productID + "' " + + "and send_mail=1 "; + return DBUtil.query(sql); + } +} diff --git a/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/1363044717/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 430e3a51e2bdcdcdbe4bbe99842ea4974343878d Mon Sep 17 00:00:00 2001 From: chunyan123b <115615290@qq.com> Date: Thu, 15 Jun 2017 15:51:59 +0800 Subject: [PATCH 114/214] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 第一次提交 --- .../config/product_promotion.txt | 4 + students/115615290/ood-assignment/pom.xml | 32 ++++++++ .../com/coderising/ood/srp/Configuration.java | 23 ++++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 +++ .../java/com/coderising/ood/srp/DBUtil.java | 27 +++++++ .../java/com/coderising/ood/srp/MailUtil.java | 21 ++++++ .../java/com/coderising/ood/srp/Product.java | 48 ++++++++++++ .../com/coderising/ood/srp/PromotionMail.java | 74 +++++++++++++++++++ .../java/com/coderising/ood/srp/User.java | 23 ++++++ 9 files changed, 261 insertions(+) create mode 100644 students/115615290/ood-assignment/config/product_promotion.txt create mode 100644 students/115615290/ood-assignment/pom.xml create mode 100644 students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/User.java diff --git a/students/115615290/ood-assignment/config/product_promotion.txt b/students/115615290/ood-assignment/config/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/115615290/ood-assignment/config/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/115615290/ood-assignment/pom.xml b/students/115615290/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/115615290/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..d11d29787e --- /dev/null +++ b/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..9f7cd5433f --- /dev/null +++ b/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,27 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + User user = new User(); + user.setName("User" + i); + user.setEmail("aa@bb.com"); + + userList.add(user); + } + + return userList; + } +} diff --git a/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..22584f3d95 --- /dev/null +++ b/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,21 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + if(toAddress==null||toAddress.equals("")){ + throw new RuntimeException("发送地址不能为空!"); + } + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..4208ee6d34 --- /dev/null +++ b/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,48 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class Product { + + private String productID; + private String productDesc; + + public Product (File file) throws IOException{ + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + public String getProductID() { + return productID; + } + public void setProductID(String productID) { + this.productID = productID; + } + public String getProductDesc() { + return productDesc; + } + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + + + +} diff --git a/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..e14524f666 --- /dev/null +++ b/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,74 @@ +package com.coderising.ood.srp; + +import java.io.File; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String subject = null; + protected String message = null; + + + private static Configuration config; + + + + public static void main(String[] args) throws Exception { + + File f = new File("config/product_promotion.txt"); + PromotionMail pe = new PromotionMail(); + List users = DBUtil.query(pe.sendMailQuery); + Product product = new Product(f); + pe.sendEMails(users, product); + + } + + + public PromotionMail() throws Exception { + config = new Configuration(); + setSMTPHost(); + setAltSMTPHost(); + setFromAddress(); + } + + protected void setSMTPHost() { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + protected void setAltSMTPHost() { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + } + + + protected void setFromAddress() { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + public void setMessage(User user,Product product){ + subject = "您关注的产品降价了"; + message = "尊敬的 "+user.getName()+", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!" ; + } + + public void sendEMails(User user,Product product){ + setMessage(user,product); + System.out.println("开始发送邮件"); + MailUtil.sendEmail(user.getEmail(), fromAddress, subject, message, smtpHost, true); + System.out.println("邮件发送完毕"); + } + + public void sendEMails(List users,Product product){ + System.out.println("开始发送邮件"); + for(User user:users){ + setMessage(user,product); + MailUtil.sendEmail(user.getEmail(), fromAddress, subject, message, smtpHost, true); + } + System.out.println("邮件发送完毕"); + } +} diff --git a/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/User.java b/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/User.java new file mode 100644 index 0000000000..42e4c81f89 --- /dev/null +++ b/students/115615290/ood-assignment/src/main/java/com/coderising/ood/srp/User.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; + +public class User { + + private String name; + private String email; + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getEmail() { + return email; + } + public void setEmail(String email) { + this.email = email; + } + + + +} From 4e65eca89df94ba5affda6f78b9807b8404a2154 Mon Sep 17 00:00:00 2001 From: macvis Date: Thu, 15 Jun 2017 16:15:46 +0800 Subject: [PATCH 115/214] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/java/tree/BinaryTreeNodeTest.java | 2 +- .../srp/{ => original}/PromotionMail.java | 11 +- .../main/java/srp/refactor/PromotionMail.java | 96 +++++++++++++++++ .../configuration}/Configuration.java | 2 +- .../configuration}/ConfigurationKeys.java | 2 +- .../src/main/java/srp/refactor/mail/Mail.java | 100 ++++++++++++++++++ .../java/srp/{ => refactor/util}/DBUtil.java | 4 +- .../main/java/srp/refactor/util/FileUtil.java | 39 +++++++ .../srp/{ => refactor/util}/MailUtil.java | 9 +- .../ood_demo_file}/product_promotion.txt | 0 .../ood/src/test/java/srp/SrpTest.java | 15 ++- 11 files changed, 268 insertions(+), 12 deletions(-) rename students/75939388/ood/src/main/java/srp/{ => original}/PromotionMail.java (92%) create mode 100644 students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java rename students/75939388/ood/src/main/java/srp/{ => refactor/configuration}/Configuration.java (94%) rename students/75939388/ood/src/main/java/srp/{ => refactor/configuration}/ConfigurationKeys.java (85%) create mode 100644 students/75939388/ood/src/main/java/srp/refactor/mail/Mail.java rename students/75939388/ood/src/main/java/srp/{ => refactor/util}/DBUtil.java (86%) create mode 100644 students/75939388/ood/src/main/java/srp/refactor/util/FileUtil.java rename students/75939388/ood/src/main/java/srp/{ => refactor/util}/MailUtil.java (73%) rename students/75939388/ood/src/main/{java/srp => resources/ood_demo_file}/product_promotion.txt (100%) diff --git a/students/75939388/datastrure/src/test/java/tree/BinaryTreeNodeTest.java b/students/75939388/datastrure/src/test/java/tree/BinaryTreeNodeTest.java index 8de899c994..90705ddca2 100644 --- a/students/75939388/datastrure/src/test/java/tree/BinaryTreeNodeTest.java +++ b/students/75939388/datastrure/src/test/java/tree/BinaryTreeNodeTest.java @@ -17,6 +17,6 @@ public void init(){ @Test public void test1(){ - + } } diff --git a/students/75939388/ood/src/main/java/srp/PromotionMail.java b/students/75939388/ood/src/main/java/srp/original/PromotionMail.java similarity index 92% rename from students/75939388/ood/src/main/java/srp/PromotionMail.java rename to students/75939388/ood/src/main/java/srp/original/PromotionMail.java index 446f82e9ad..4ef4df0f7a 100644 --- a/students/75939388/ood/src/main/java/srp/PromotionMail.java +++ b/students/75939388/ood/src/main/java/srp/original/PromotionMail.java @@ -1,4 +1,9 @@ -package srp; +package srp.original; + +import srp.refactor.configuration.Configuration; +import srp.refactor.configuration.ConfigurationKeys; +import srp.refactor.util.DBUtil; +import srp.refactor.util.MailUtil; import java.io.BufferedReader; import java.io.File; @@ -24,7 +29,7 @@ public class PromotionMail { protected String productID = null; protected String productDesc = null; - private static Configuration config; + private static Configuration config; @@ -91,7 +96,7 @@ protected void setLoadQuery() throws Exception { protected void setSMTPHost() { - smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); } diff --git a/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java b/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java new file mode 100644 index 0000000000..ea14d1c94a --- /dev/null +++ b/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java @@ -0,0 +1,96 @@ +package srp.refactor; + +import org.apache.commons.lang3.StringUtils; +import srp.refactor.configuration.Configuration; +import srp.refactor.configuration.ConfigurationKeys; +import srp.refactor.mail.Mail; +import srp.refactor.util.DBUtil; + +import java.util.HashMap; +import java.util.List; + +/** + * 在原有的Mail邮件功能的基础上修改而来的促销邮件发送端 + * + * Created by Tee on 2017/6/15. + */ +public class PromotionMail extends Mail { + + protected String productID = null; + protected String productDesc = null; + protected String sendMailQuery = null; + + private static Configuration config = new Configuration(); + + private final static String NAME_KEY = "NAME"; + private final static String EMAIL_KEY = "EMAIL"; + + private void init(){ + super.initMailSettings( + config.getProperty(ConfigurationKeys.SMTP_SERVER), + config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER), + config.getProperty(ConfigurationKeys.EMAIL_ADMIN) + ); + } + + public PromotionMail(){ + init(); + } + + @Override + public void createMail(String toAddress, String subject, String message){ + this.toAddress = toAddress; + this.subject = subject; + this.message = message; + super.write(); + } + + public void setProductPromotion(String productID, String productDesc) throws Exception{ + this.productID = productID; + this.productDesc = productDesc; + } + + /** + * 模拟数据库查询 + */ + protected void setLoadQuery() throws Exception { + if(StringUtils.isBlank(productID)){ + throw new RuntimeException("没有获取到productID"); + } + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + System.out.println("loadQuery set, productID -> " + productID); + } + + public void batchSetMails(List data) throws Exception{ + if(data.isEmpty()){ + throw new RuntimeException("data不能为空"); + } + for(int i = 0; i < data.size(); i+=2){ + String productId = data.get(i); + String productName = data.get(i+1); + setProductPromotion(productId, productName); + setLoadQuery(); + + List userList = DBUtil.query(sendMailQuery); + for(HashMap userInfo : userList){ + createMail( + (String)userInfo.get(EMAIL_KEY), + "您关注的" + productName + "已降价", + "尊敬的 "+ userInfo.get(NAME_KEY) +", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" + ); + } + } + } + + /** + * 暂时这么实现 + */ + @Override + public void send(){ + super.batchSend(false); + } +} \ No newline at end of file diff --git a/students/75939388/ood/src/main/java/srp/Configuration.java b/students/75939388/ood/src/main/java/srp/refactor/configuration/Configuration.java similarity index 94% rename from students/75939388/ood/src/main/java/srp/Configuration.java rename to students/75939388/ood/src/main/java/srp/refactor/configuration/Configuration.java index 3a17cdd457..b3b24944d2 100644 --- a/students/75939388/ood/src/main/java/srp/Configuration.java +++ b/students/75939388/ood/src/main/java/srp/refactor/configuration/Configuration.java @@ -1,4 +1,4 @@ -package srp; +package srp.refactor.configuration; import java.util.HashMap; import java.util.Map; diff --git a/students/75939388/ood/src/main/java/srp/ConfigurationKeys.java b/students/75939388/ood/src/main/java/srp/refactor/configuration/ConfigurationKeys.java similarity index 85% rename from students/75939388/ood/src/main/java/srp/ConfigurationKeys.java rename to students/75939388/ood/src/main/java/srp/refactor/configuration/ConfigurationKeys.java index ef3a07a354..9b1cfe8dcc 100644 --- a/students/75939388/ood/src/main/java/srp/ConfigurationKeys.java +++ b/students/75939388/ood/src/main/java/srp/refactor/configuration/ConfigurationKeys.java @@ -1,4 +1,4 @@ -package srp; +package srp.refactor.configuration; public class ConfigurationKeys { diff --git a/students/75939388/ood/src/main/java/srp/refactor/mail/Mail.java b/students/75939388/ood/src/main/java/srp/refactor/mail/Mail.java new file mode 100644 index 0000000000..01e1ecbbf3 --- /dev/null +++ b/students/75939388/ood/src/main/java/srp/refactor/mail/Mail.java @@ -0,0 +1,100 @@ +package srp.refactor.mail; + +import org.apache.commons.lang3.StringUtils; +import srp.refactor.util.MailUtil; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * Created by Tee on 2017/6/15. + */ +public abstract class Mail { + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + private static final String TO_ADDRESS_KEY = "toAddress"; + private static final String SUBJECT_KEY = "subject"; + private static final String MESSAGE_KEY = "message"; + + protected List mailList; + + /** + * 初始化邮件设置 + * + * @param smtpHost smtp主机 + * @param altSmtpHost 备用smtp主机 + * @param fromAddress 发件人地址 + */ + protected void initMailSettings(String smtpHost, String altSmtpHost, String fromAddress){ + this.smtpHost = smtpHost; + this.altSmtpHost = altSmtpHost; + this.fromAddress = fromAddress; + this.mailList = new ArrayList<>(); + } + + public abstract void createMail(String toAddress, String subject, String message); + + /** + * 撰写邮件 + */ + protected void write(){ + HashMap mailToSend = new HashMap<>(); + mailToSend.put(TO_ADDRESS_KEY, this.toAddress); + mailToSend.put(SUBJECT_KEY, this.subject); + mailToSend.put(MESSAGE_KEY, this.message); + this.mailList.add(mailToSend); + } + + private boolean isInit(){ + return StringUtils.isNotBlank(this.smtpHost) && + StringUtils.isNotBlank(this.altSmtpHost) && + StringUtils.isNotBlank(this.fromAddress); + } + + /** + * 供子类实现的send方法 + */ + public abstract void send(); + + /** + * 批量发送 + */ + public void batchSend(boolean debug){ + if(!isInit()){ + throw new RuntimeException("邮件客户端还未做配置"); + } + + if(mailList.isEmpty()){ + System.out.println("没有邮件要发送"); + return; + } + int size = mailList.size(); + System.out.println("开始发送邮件, 总邮件数=" + size); + int i = 0; + for(HashMap mail : mailList){ + i++; + String toAddress = (String)mail.get(TO_ADDRESS_KEY); + if(StringUtils.isBlank(toAddress)){ + System.out.println("收件人地址为空,此邮件发送中止"); + continue; + } + + String subject = (String)mail.get(SUBJECT_KEY); + String message = (String)mail.get(MESSAGE_KEY); + + System.out.println("\n正在发送第[" + i + "]封邮件"); + try{ + MailUtil.sendEmail(toAddress, this.fromAddress, subject, message, this.smtpHost, debug); + }catch(Exception e){ + MailUtil.sendEmail(toAddress, this.fromAddress, subject, message, this.altSmtpHost, debug); + } + System.out.println("第[" + i + "]封邮件发送完成"); + } + } +} diff --git a/students/75939388/ood/src/main/java/srp/DBUtil.java b/students/75939388/ood/src/main/java/srp/refactor/util/DBUtil.java similarity index 86% rename from students/75939388/ood/src/main/java/srp/DBUtil.java rename to students/75939388/ood/src/main/java/srp/refactor/util/DBUtil.java index 912bebf080..00aa263606 100644 --- a/students/75939388/ood/src/main/java/srp/DBUtil.java +++ b/students/75939388/ood/src/main/java/srp/refactor/util/DBUtil.java @@ -1,4 +1,4 @@ -package srp; +package srp.refactor.util; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -12,7 +12,7 @@ public class DBUtil { */ public static List query(String sql){ - List userList = new ArrayList(); + List userList = new ArrayList(); for (int i = 1; i <= 3; i++) { HashMap userInfo = new HashMap(); userInfo.put("NAME", "User" + i); diff --git a/students/75939388/ood/src/main/java/srp/refactor/util/FileUtil.java b/students/75939388/ood/src/main/java/srp/refactor/util/FileUtil.java new file mode 100644 index 0000000000..a1fbdca905 --- /dev/null +++ b/students/75939388/ood/src/main/java/srp/refactor/util/FileUtil.java @@ -0,0 +1,39 @@ +package srp.refactor.util; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Tee on 2017/6/15. + */ +public class FileUtil { + + public static List readFile(File file)throws IOException { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String tmp = null; + List data = new ArrayList<>(); + while((tmp = br.readLine()) != null){ + String[] temp = tmp.split(" "); + data.add(temp[0]); + data.add(temp[1]); + } + + return data; + } catch (IOException e) { + throw new IOException(e); + } finally { + try{ + br.close(); + }catch(Exception e){ + e.printStackTrace(); + } + + } + } +} diff --git a/students/75939388/ood/src/main/java/srp/MailUtil.java b/students/75939388/ood/src/main/java/srp/refactor/util/MailUtil.java similarity index 73% rename from students/75939388/ood/src/main/java/srp/MailUtil.java rename to students/75939388/ood/src/main/java/srp/refactor/util/MailUtil.java index 556976f5c9..b6c267b8b6 100644 --- a/students/75939388/ood/src/main/java/srp/MailUtil.java +++ b/students/75939388/ood/src/main/java/srp/refactor/util/MailUtil.java @@ -1,7 +1,10 @@ -package srp; +package srp.refactor.util; public class MailUtil { + private static final String TO_ADDRESS_KEY = "toAddress"; + private static final String MAIL_KEY = "mail"; + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, boolean debug) { //假装发了一封邮件 @@ -10,9 +13,9 @@ public static void sendEmail(String toAddress, String fromAddress, String subjec buffer.append("To:").append(toAddress).append("\n"); buffer.append("Subject:").append(subject).append("\n"); buffer.append("Content:").append(message).append("\n"); - System.out.println(buffer.toString()); + System.out.print(buffer.toString()); } - + } diff --git a/students/75939388/ood/src/main/java/srp/product_promotion.txt b/students/75939388/ood/src/main/resources/ood_demo_file/product_promotion.txt similarity index 100% rename from students/75939388/ood/src/main/java/srp/product_promotion.txt rename to students/75939388/ood/src/main/resources/ood_demo_file/product_promotion.txt diff --git a/students/75939388/ood/src/test/java/srp/SrpTest.java b/students/75939388/ood/src/test/java/srp/SrpTest.java index 7882177794..843d0b03f3 100644 --- a/students/75939388/ood/src/test/java/srp/SrpTest.java +++ b/students/75939388/ood/src/test/java/srp/SrpTest.java @@ -2,12 +2,19 @@ import org.junit.Before; import org.junit.Test; +import srp.refactor.PromotionMail; +import srp.refactor.util.FileUtil; + +import java.io.File; +import java.util.List; /** * Created by Tee on 2017/6/15. */ public class SrpTest { + PromotionMail promotionMail = null; + @Before public void init(){ @@ -17,7 +24,13 @@ public void init(){ * 重构后的代码尝试运行 */ @Test - public void runTrial(){ + public void runTrial() throws Exception{ + File file = new File("/Users/Tee/Code/learning2017/season2/coding2017/" + + "students/75939388/ood/src/main/resources/ood_demo_file/product_promotion.txt"); + List data = FileUtil.readFile(file); + PromotionMail promotionMail = new PromotionMail(); + promotionMail.batchSetMails(data); + promotionMail.send(); } } From 88ae9f8b58a7cda3a8b8a61c419571a8d8465d9e Mon Sep 17 00:00:00 2001 From: macvis Date: Thu, 15 Jun 2017 16:31:35 +0800 Subject: [PATCH 116/214] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ood/src/main/java/srp/refactor/PromotionMail.java | 2 +- .../75939388/ood/src/main/java/srp/refactor/mail/Mail.java | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java b/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java index ea14d1c94a..01da6b3516 100644 --- a/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java +++ b/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java @@ -42,7 +42,7 @@ public void createMail(String toAddress, String subject, String message){ this.toAddress = toAddress; this.subject = subject; this.message = message; - super.write(); + super.addToMailList(); } public void setProductPromotion(String productID, String productDesc) throws Exception{ diff --git a/students/75939388/ood/src/main/java/srp/refactor/mail/Mail.java b/students/75939388/ood/src/main/java/srp/refactor/mail/Mail.java index 01e1ecbbf3..af5600ae21 100644 --- a/students/75939388/ood/src/main/java/srp/refactor/mail/Mail.java +++ b/students/75939388/ood/src/main/java/srp/refactor/mail/Mail.java @@ -8,6 +8,8 @@ import java.util.List; /** + * 具有初始化设置、可以批量发送邮件的邮件客户端 + * * Created by Tee on 2017/6/15. */ public abstract class Mail { @@ -43,7 +45,7 @@ protected void initMailSettings(String smtpHost, String altSmtpHost, String from /** * 撰写邮件 */ - protected void write(){ + protected void addToMailList(){ HashMap mailToSend = new HashMap<>(); mailToSend.put(TO_ADDRESS_KEY, this.toAddress); mailToSend.put(SUBJECT_KEY, this.subject); @@ -89,11 +91,13 @@ public void batchSend(boolean debug){ String message = (String)mail.get(MESSAGE_KEY); System.out.println("\n正在发送第[" + i + "]封邮件"); + System.out.println("=========================================================="); try{ MailUtil.sendEmail(toAddress, this.fromAddress, subject, message, this.smtpHost, debug); }catch(Exception e){ MailUtil.sendEmail(toAddress, this.fromAddress, subject, message, this.altSmtpHost, debug); } + System.out.println("=========================================================="); System.out.println("第[" + i + "]封邮件发送完成"); } } From 733005ff0a8edd06fbfb4efe9ff28ee4516160b1 Mon Sep 17 00:00:00 2001 From: macvis Date: Thu, 15 Jun 2017 16:54:39 +0800 Subject: [PATCH 117/214] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A=E9=83=A8=E5=88=86=E5=AE=8C?= =?UTF-8?q?=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/srp/original/PromotionMail.java | 7 +++--- .../main/java/srp/refactor/PromotionMail.java | 15 ++++++++----- .../src/main/java/srp/refactor/mail/Mail.java | 22 +++++++++++++++---- .../main/java/srp/refactor/util/MailUtil.java | 21 ------------------ 4 files changed, 31 insertions(+), 34 deletions(-) delete mode 100644 students/75939388/ood/src/main/java/srp/refactor/util/MailUtil.java diff --git a/students/75939388/ood/src/main/java/srp/original/PromotionMail.java b/students/75939388/ood/src/main/java/srp/original/PromotionMail.java index 4ef4df0f7a..8deec60166 100644 --- a/students/75939388/ood/src/main/java/srp/original/PromotionMail.java +++ b/students/75939388/ood/src/main/java/srp/original/PromotionMail.java @@ -3,7 +3,6 @@ import srp.refactor.configuration.Configuration; import srp.refactor.configuration.ConfigurationKeys; import srp.refactor.util.DBUtil; -import srp.refactor.util.MailUtil; import java.io.BufferedReader; import java.io.File; @@ -175,14 +174,14 @@ protected void sendEMails(boolean debug, List mailingList) throws IOException configureEMail((HashMap) iter.next()); try { - if (toAddress.length() > 0) - MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + if (toAddress.length() > 0){} +// MailContentUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); } catch (Exception e) { try { - MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); +// MailContentUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); } catch (Exception e2) { diff --git a/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java b/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java index 01da6b3516..4234a0ac6b 100644 --- a/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java +++ b/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java @@ -77,15 +77,20 @@ public void batchSetMails(List data) throws Exception{ List userList = DBUtil.query(sendMailQuery); for(HashMap userInfo : userList){ - createMail( - (String)userInfo.get(EMAIL_KEY), - "您关注的" + productName + "已降价", - "尊敬的 "+ userInfo.get(NAME_KEY) +", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" - ); + createMail((String)userInfo.get(EMAIL_KEY), + generateSubject(productName), generateMessage(userInfo, productDesc)); } } } + private String generateSubject(String productName){ + return "您关注的" + productName + "已降价"; + } + + private String generateMessage(HashMap userInfo, String productDesc){ + return "尊敬的 "+ userInfo.get(NAME_KEY) +", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + } + /** * 暂时这么实现 */ diff --git a/students/75939388/ood/src/main/java/srp/refactor/mail/Mail.java b/students/75939388/ood/src/main/java/srp/refactor/mail/Mail.java index af5600ae21..755fe6c18d 100644 --- a/students/75939388/ood/src/main/java/srp/refactor/mail/Mail.java +++ b/students/75939388/ood/src/main/java/srp/refactor/mail/Mail.java @@ -1,7 +1,6 @@ package srp.refactor.mail; import org.apache.commons.lang3.StringUtils; -import srp.refactor.util.MailUtil; import java.util.ArrayList; import java.util.HashMap; @@ -67,7 +66,7 @@ private boolean isInit(){ /** * 批量发送 */ - public void batchSend(boolean debug){ + protected void batchSend(boolean debug){ if(!isInit()){ throw new RuntimeException("邮件客户端还未做配置"); } @@ -93,12 +92,27 @@ public void batchSend(boolean debug){ System.out.println("\n正在发送第[" + i + "]封邮件"); System.out.println("=========================================================="); try{ - MailUtil.sendEmail(toAddress, this.fromAddress, subject, message, this.smtpHost, debug); + sendEmail(toAddress, this.fromAddress, subject, message, this.smtpHost, debug); }catch(Exception e){ - MailUtil.sendEmail(toAddress, this.fromAddress, subject, message, this.altSmtpHost, debug); + sendEmail(toAddress, this.fromAddress, subject, message, this.altSmtpHost, debug); } System.out.println("=========================================================="); System.out.println("第[" + i + "]封邮件发送完成"); } } + + /** + * 发送邮件客户端的功能和责任,所以移入邮件客户端,但是只能被基础客户端调用 + * 子类只能通过batchSend来发送邮件 + */ + private void sendEmail(String toAddress, String fromAddress, String subject, + String message, String smtpHost, boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.print(buffer.toString()); + } } diff --git a/students/75939388/ood/src/main/java/srp/refactor/util/MailUtil.java b/students/75939388/ood/src/main/java/srp/refactor/util/MailUtil.java deleted file mode 100644 index b6c267b8b6..0000000000 --- a/students/75939388/ood/src/main/java/srp/refactor/util/MailUtil.java +++ /dev/null @@ -1,21 +0,0 @@ -package srp.refactor.util; - -public class MailUtil { - - private static final String TO_ADDRESS_KEY = "toAddress"; - private static final String MAIL_KEY = "mail"; - - public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, - boolean debug) { - //假装发了一封邮件 - StringBuilder buffer = new StringBuilder(); - buffer.append("From:").append(fromAddress).append("\n"); - buffer.append("To:").append(toAddress).append("\n"); - buffer.append("Subject:").append(subject).append("\n"); - buffer.append("Content:").append(message).append("\n"); - System.out.print(buffer.toString()); - - } - - -} From ff755b0b070bf92ec46400cef231a17a14500bc1 Mon Sep 17 00:00:00 2001 From: '1299310140' <'13437282785@163.com'> Date: Thu, 15 Jun 2017 17:13:35 +0800 Subject: [PATCH 118/214] test --- students/1299310140/src/com/Test.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 students/1299310140/src/com/Test.java diff --git a/students/1299310140/src/com/Test.java b/students/1299310140/src/com/Test.java new file mode 100644 index 0000000000..1599b83b36 --- /dev/null +++ b/students/1299310140/src/com/Test.java @@ -0,0 +1,5 @@ +package com; + +public class Test { + +} From 7e796db089ea0bde77080fa62a0bc501ecd7f307 Mon Sep 17 00:00:00 2001 From: '1299310140' <'13437282785@163.com'> Date: Thu, 15 Jun 2017 17:27:09 +0800 Subject: [PATCH 119/214] test --- students/1299310140/src/com/Test.java | 1 - 1 file changed, 1 deletion(-) diff --git a/students/1299310140/src/com/Test.java b/students/1299310140/src/com/Test.java index 1599b83b36..2900598832 100644 --- a/students/1299310140/src/com/Test.java +++ b/students/1299310140/src/com/Test.java @@ -1,5 +1,4 @@ package com; public class Test { - } From eb557b9afffbe366fcb694a96d11b0b5fcfc45bb Mon Sep 17 00:00:00 2001 From: '1299310140' <'13437282785@163.com'> Date: Thu, 15 Jun 2017 17:34:56 +0800 Subject: [PATCH 120/214] test --- students/1299310140/src/com/Test.java | 4 - .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../src/com/coderising/ood/srp/DBUtil.java | 25 +++ .../src/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 7 files changed, 278 insertions(+), 4 deletions(-) delete mode 100644 students/1299310140/src/com/Test.java create mode 100644 students/1299310140/src/com/coderising/ood/srp/Configuration.java create mode 100644 students/1299310140/src/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/1299310140/src/com/coderising/ood/srp/DBUtil.java create mode 100644 students/1299310140/src/com/coderising/ood/srp/MailUtil.java create mode 100644 students/1299310140/src/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/1299310140/src/com/coderising/ood/srp/product_promotion.txt diff --git a/students/1299310140/src/com/Test.java b/students/1299310140/src/com/Test.java deleted file mode 100644 index 2900598832..0000000000 --- a/students/1299310140/src/com/Test.java +++ /dev/null @@ -1,4 +0,0 @@ -package com; - -public class Test { -} diff --git a/students/1299310140/src/com/coderising/ood/srp/Configuration.java b/students/1299310140/src/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..927c7155cc --- /dev/null +++ b/students/1299310140/src/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/1299310140/src/com/coderising/ood/srp/ConfigurationKeys.java b/students/1299310140/src/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..868a03ff83 --- /dev/null +++ b/students/1299310140/src/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/1299310140/src/com/coderising/ood/srp/DBUtil.java b/students/1299310140/src/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..65383e4dba --- /dev/null +++ b/students/1299310140/src/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/1299310140/src/com/coderising/ood/srp/MailUtil.java b/students/1299310140/src/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..373f3ee306 --- /dev/null +++ b/students/1299310140/src/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/1299310140/src/com/coderising/ood/srp/PromotionMail.java b/students/1299310140/src/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..94bfcbaf54 --- /dev/null +++ b/students/1299310140/src/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/1299310140/src/com/coderising/ood/srp/product_promotion.txt b/students/1299310140/src/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/1299310140/src/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From f94b7faa3329e65e9145b8761dbee6fd8ce877d7 Mon Sep 17 00:00:00 2001 From: zhangqing <13161040988@163.com> Date: Thu, 15 Jun 2017 19:31:27 +0800 Subject: [PATCH 121/214] first --- students/1753179526/readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/1753179526/readme.md diff --git a/students/1753179526/readme.md b/students/1753179526/readme.md new file mode 100644 index 0000000000..765ea98ff3 --- /dev/null +++ b/students/1753179526/readme.md @@ -0,0 +1 @@ +Test git commit. push-wary \ No newline at end of file From 3d1db9dfe7871990ba7ab6bf256dbe9f5903a00d Mon Sep 17 00:00:00 2001 From: zhangqing <13161040988@163.com> Date: Thu, 15 Jun 2017 20:22:35 +0800 Subject: [PATCH 122/214] sdfsf --- students/1753179526/readme.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/students/1753179526/readme.md b/students/1753179526/readme.md index 765ea98ff3..8bd27805d5 100644 --- a/students/1753179526/readme.md +++ b/students/1753179526/readme.md @@ -1 +1,3 @@ -Test git commit. push-wary \ No newline at end of file +Test git commit. push-wary + +第二次提交内容,更改编码问题。 \ No newline at end of file From 7219fef06724936ccdc3add25a8d94456f3ea564 Mon Sep 17 00:00:00 2001 From: xiaoyj <346154295@qq.com> Date: Thu, 15 Jun 2017 20:39:13 +0800 Subject: [PATCH 123/214] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/346154295/ood-assignment/pom.xml | 46 ++++++++++++ .../com/coderising/ood/srp/Configuration.java | 23 ++++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 +++ .../java/com/coderising/ood/srp/DBUtil.java | 28 +++++++ .../java/com/coderising/ood/srp/MailUtil.java | 40 ++++++++++ .../com/coderising/ood/srp/ProductInfo.java | 25 +++++++ .../com/coderising/ood/srp/PromotionMail.java | 73 +++++++++++++++++++ .../src/main/resource/product_promotion.txt | 4 + 8 files changed, 248 insertions(+) create mode 100644 students/346154295/ood-assignment/pom.xml create mode 100644 students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java create mode 100644 students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/346154295/ood-assignment/src/main/resource/product_promotion.txt diff --git a/students/346154295/ood-assignment/pom.xml b/students/346154295/ood-assignment/pom.xml new file mode 100644 index 0000000000..0853e4c187 --- /dev/null +++ b/students/346154295/ood-assignment/pom.xml @@ -0,0 +1,46 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.7 + 1.7 + UTF-8 + + + + + diff --git a/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..ad72a12c22 --- /dev/null +++ b/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,28 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param productID + * @return + */ + public static List> loadMailingList(String productID){ + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID + "' " + + "and send_mail=1 "; + System.out.println("loadQuery set"); + List> userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..f9627d307e --- /dev/null +++ b/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,40 @@ +package com.coderising.ood.srp; + +public class MailUtil { + private static String smtpHost; + private static String altSmtpHost; + private static String fromAddress; + + public static void init() { + Configuration config = new Configuration(); + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + private static void sendEmail(String toAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + public static void sendEmail(String toAddress, String subject, String message, boolean debug) { + if(toAddress == null || toAddress.length() == 0) { + return; + } + try { + sendEmail(toAddress,subject,message,smtpHost,debug); + } catch (Exception e) { + try { + sendEmail(toAddress, subject, message, altSmtpHost, debug); + } catch (Exception e2){ + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } +} diff --git a/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java b/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java new file mode 100644 index 0000000000..e3719a503d --- /dev/null +++ b/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; + +/** + * Created by xiaoyj on 2017/6/15. + */ +public class ProductInfo { + private String productID; + private String productDesc; + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } +} diff --git a/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..e7530dbc55 --- /dev/null +++ b/students/346154295/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,73 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; + +public class PromotionMail { + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + boolean emailDebug = false; + File file = new File("src/main/resource/product_promotion.txt"); + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + ProductInfo productInfo = getProductInfo(file); + List> mailingList = DBUtil.loadMailingList(productInfo.getProductID()); + sendEMails(emailDebug, mailingList, productInfo); + + } + + private static String getMessage(HashMap userInfo, ProductInfo productInfo) throws IOException + { + String name = userInfo.get(NAME_KEY); + return "尊敬的 "+name+", 您关注的产品 " + productInfo.getProductDesc() + " 降价了,欢迎购买!" ; + } + private static String getSubject() { + return "您关注的产品降价了"; + } + + protected static void sendEMails(boolean debug, List> mailingList, ProductInfo productInfo) throws IOException { + System.out.println("开始发送邮件"); + MailUtil.init(); + if (mailingList != null) { + for (HashMap userInfo : mailingList) { + String toAddress = userInfo.get(EMAIL_KEY); + if (toAddress == null || toAddress.length() == 0) { + continue; + } + String subject = getSubject(); + String message =getMessage(userInfo, productInfo); + MailUtil.sendEmail(toAddress, subject, message, debug); + } + + } else { + System.out.println("没有邮件发送"); + + } + } + private static ProductInfo getProductInfo(File file) throws IOException { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + ProductInfo productInfo = new ProductInfo(); + productInfo.setProductID(data[0]); + productInfo.setProductDesc(data[1]); + + System.out.println("产品ID = " + productInfo.getProductID() + "\n"); + System.out.println("产品描述 = " + productInfo.getProductDesc() + "\n"); + return productInfo; + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } +} diff --git a/students/346154295/ood-assignment/src/main/resource/product_promotion.txt b/students/346154295/ood-assignment/src/main/resource/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/346154295/ood-assignment/src/main/resource/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 46f1d05a4289fbec35c57d89acf5f793ca41083f Mon Sep 17 00:00:00 2001 From: zhangqing <13161040988@163.com> Date: Thu, 15 Jun 2017 21:30:43 +0800 Subject: [PATCH 124/214] Test command line --- students/1753179526/readme.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/students/1753179526/readme.md b/students/1753179526/readme.md index 8bd27805d5..0eb59149a8 100644 --- a/students/1753179526/readme.md +++ b/students/1753179526/readme.md @@ -1,3 +1,5 @@ Test git commit. push-wary -第二次提交内容,更改编码问题。 \ No newline at end of file +第二次提交内容,更改编码问题。 + +增加new line 测试命令行 \ No newline at end of file From fa6d4c16e5eec61654e54b39398aec0620fae22b Mon Sep 17 00:00:00 2001 From: penglei Date: Thu, 15 Jun 2017 22:41:17 +0800 Subject: [PATCH 125/214] my first git with IDEA --- .../799298900/src/com/leipengzj/myfirstGitFork.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 students/799298900/src/com/leipengzj/myfirstGitFork.java diff --git a/students/799298900/src/com/leipengzj/myfirstGitFork.java b/students/799298900/src/com/leipengzj/myfirstGitFork.java new file mode 100644 index 0000000000..a87099c226 --- /dev/null +++ b/students/799298900/src/com/leipengzj/myfirstGitFork.java @@ -0,0 +1,10 @@ +package com.leipengzj; + +/** + * Created by tyrion on 2017/6/15. + */ +public class myfirstGitFork { + public static void main(String[] args) { + System.out.println("myfirst git"); + } +} From baa3c495777b8ac42e39a4ae14adf50197c7858a Mon Sep 17 00:00:00 2001 From: jyz2017 <734473301@qq.com> Date: Thu, 15 Jun 2017 23:19:52 +0800 Subject: [PATCH 126/214] =?UTF-8?q?jyz=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E4=BD=9C=E4=B8=9A=EF=BC=8C=E9=82=AE=E4=BB=B6=E9=87=8D?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coding2017jyz/product_promotion.txt | 4 ++ students/734473301/coding2017jyz/readme.txt | 4 ++ .../jyz/coding2017/Configuration.java | 32 +++++++++ .../src/homework/jyz/coding2017/Email.java | 54 +++++++++++++++ .../homework/jyz/coding2017/EmailHost.java | 57 ++++++++++++++++ .../homework/jyz/coding2017/MailConfig.java | 68 +++++++++++++++++++ .../src/homework/jyz/coding2017/MailDao.java | 34 ++++++++++ .../src/homework/jyz/coding2017/MailSend.java | 41 +++++++++++ .../homework/jyz/coding2017/WorkStart.java | 26 +++++++ 9 files changed, 320 insertions(+) create mode 100644 students/734473301/coding2017jyz/product_promotion.txt create mode 100644 students/734473301/coding2017jyz/readme.txt create mode 100644 students/734473301/coding2017jyz/src/homework/jyz/coding2017/Configuration.java create mode 100644 students/734473301/coding2017jyz/src/homework/jyz/coding2017/Email.java create mode 100644 students/734473301/coding2017jyz/src/homework/jyz/coding2017/EmailHost.java create mode 100644 students/734473301/coding2017jyz/src/homework/jyz/coding2017/MailConfig.java create mode 100644 students/734473301/coding2017jyz/src/homework/jyz/coding2017/MailDao.java create mode 100644 students/734473301/coding2017jyz/src/homework/jyz/coding2017/MailSend.java create mode 100644 students/734473301/coding2017jyz/src/homework/jyz/coding2017/WorkStart.java diff --git a/students/734473301/coding2017jyz/product_promotion.txt b/students/734473301/coding2017jyz/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/734473301/coding2017jyz/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/734473301/coding2017jyz/readme.txt b/students/734473301/coding2017jyz/readme.txt new file mode 100644 index 0000000000..746bb1fdc8 --- /dev/null +++ b/students/734473301/coding2017jyz/readme.txt @@ -0,0 +1,4 @@ +因为内容不多,也就没有分包,全部放在了一起。 + +发送邮件的实现方法,最后是放在了host上,可以直接new一个host发送, +这个方法应该实现在哪个类,这个还需结合实际考虑。 \ No newline at end of file diff --git a/students/734473301/coding2017jyz/src/homework/jyz/coding2017/Configuration.java b/students/734473301/coding2017jyz/src/homework/jyz/coding2017/Configuration.java new file mode 100644 index 0000000000..01dbf8d408 --- /dev/null +++ b/students/734473301/coding2017jyz/src/homework/jyz/coding2017/Configuration.java @@ -0,0 +1,32 @@ +package homework.jyz.coding2017; +import java.util.HashMap; +import java.util.Map; + +/** + * 主机配置类 + */ +public class Configuration { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + + private Map configurations = new HashMap<>(); + + public Configuration() { + configurations.put(SMTP_SERVER, "smtp.163.com"); + configurations.put(ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(EMAIL_ADMIN, "admin@company.com"); + } + + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/734473301/coding2017jyz/src/homework/jyz/coding2017/Email.java b/students/734473301/coding2017jyz/src/homework/jyz/coding2017/Email.java new file mode 100644 index 0000000000..8e33d1e4d0 --- /dev/null +++ b/students/734473301/coding2017jyz/src/homework/jyz/coding2017/Email.java @@ -0,0 +1,54 @@ +package homework.jyz.coding2017; + +/** + * 邮件实体信息 + * Created by jyz on 2017/6/13. + */ +public class Email { + private String fromAddress; + private String toAddress; + private String subject; + private String message; + + public Email() { + } + + public Email(String fromAddress, String toAddress, String subject, String message) { + this.fromAddress = fromAddress; + this.toAddress = toAddress; + this.subject = subject; + this.message = message; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/students/734473301/coding2017jyz/src/homework/jyz/coding2017/EmailHost.java b/students/734473301/coding2017jyz/src/homework/jyz/coding2017/EmailHost.java new file mode 100644 index 0000000000..4299d466fe --- /dev/null +++ b/students/734473301/coding2017jyz/src/homework/jyz/coding2017/EmailHost.java @@ -0,0 +1,57 @@ +package homework.jyz.coding2017; + +/** + * 邮件服务器主机类 + * Created by jyz on 2017/6/13. + */ +public class EmailHost { + private Configuration config; + private String host; + private String altHost; + private String hostAdmin; + + /** + * 构建主机 + * @param config 主机配置 + */ + public EmailHost(Configuration config) { + this.config = config; + host = this.config.getProperty(Configuration.SMTP_SERVER); + altHost = this.config.getProperty(Configuration.ALT_SMTP_SERVER); + hostAdmin = this.config.getProperty(Configuration.EMAIL_ADMIN); + } + + public boolean send(Email email){ + + if(email != null){ + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(email.getFromAddress()).append("\n"); + buffer.append("To:").append(email.getToAddress()).append("\n"); + buffer.append("Subject:").append(email.getSubject()).append("\n"); + buffer.append("Content:").append(email.getMessage()).append("\n"); + if(send(host,buffer.toString())){ + return true; + } + System.out.println("启用备用主机发送.."); + if(send(altHost,buffer.toString())){ + return true; + } + System.out.println("发送失败"); + return false; + } + System.out.println("邮件为空,发送失败"); + return false; + } + + public boolean send(String host,String message){ + try { + System.out.println("使用主机"+host+"发送邮件"); + System.out.println(message); + return true; + } catch (Exception e) { + System.out.println("使用主机"+host+"发送邮件失败"); + e.printStackTrace(); + return false; + } + } +} diff --git a/students/734473301/coding2017jyz/src/homework/jyz/coding2017/MailConfig.java b/students/734473301/coding2017jyz/src/homework/jyz/coding2017/MailConfig.java new file mode 100644 index 0000000000..2d3a56aac6 --- /dev/null +++ b/students/734473301/coding2017jyz/src/homework/jyz/coding2017/MailConfig.java @@ -0,0 +1,68 @@ +package homework.jyz.coding2017; + + + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * 配置邮件信息类 + * Created by jyz on 2017/6/13. + */ +public class MailConfig { + private MailDao dao; + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + private String productID = ""; + private String productDesc = ""; + + public MailConfig(MailDao dao) { + this.dao = dao; + } + + public List getEmails() { + List mails = dao.getMails(productID); + List list = new ArrayList<>(); + for (HashMap map : mails) { + Email email = new Email(); + setMessage(map, email); + list.add(email); + } + return list; + } + + private void setMessage(HashMap userInfo, Email email) { + String name = (String) userInfo.get(NAME_KEY); + email.setSubject("您关注的产品降价了"); + String message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + email.setMessage(message); + } + + + public void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + productID = data[0]; + productDesc = data[1]; + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + if(br != null){ + br.close(); + } + } + } +} diff --git a/students/734473301/coding2017jyz/src/homework/jyz/coding2017/MailDao.java b/students/734473301/coding2017jyz/src/homework/jyz/coding2017/MailDao.java new file mode 100644 index 0000000000..def5400761 --- /dev/null +++ b/students/734473301/coding2017jyz/src/homework/jyz/coding2017/MailDao.java @@ -0,0 +1,34 @@ +package homework.jyz.coding2017; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * 邮件Dao + * Created by jyz on 2017/6/13. + */ +public class MailDao { + + public List getMails(String productID){ + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + System.out.println("loadQuery set"); + return query(sendMailQuery); + } + + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/734473301/coding2017jyz/src/homework/jyz/coding2017/MailSend.java b/students/734473301/coding2017jyz/src/homework/jyz/coding2017/MailSend.java new file mode 100644 index 0000000000..d9658cb22f --- /dev/null +++ b/students/734473301/coding2017jyz/src/homework/jyz/coding2017/MailSend.java @@ -0,0 +1,41 @@ +package homework.jyz.coding2017; + +import java.util.List; + +/** + * 发送邮件的工作类 + * Created by jyz on 2017/6/13. + */ +public class MailSend { + + private EmailHost host; + + public MailSend(EmailHost host){ + this.host = host; + } + + public MailSend() { + } + + /** + * 默认主机发送 + * @param emails + */ + public void send(List emails){ + emails.forEach(email -> host.send(email)); + } + public void send(Email email){ + host.send(email); + } + /** + * 指定主机发送 + * @param host + * @param emails + */ + public void send(EmailHost host,List emails){ + for (Email email : emails) { + host.send(email); + } + } + +} diff --git a/students/734473301/coding2017jyz/src/homework/jyz/coding2017/WorkStart.java b/students/734473301/coding2017jyz/src/homework/jyz/coding2017/WorkStart.java new file mode 100644 index 0000000000..9759223a94 --- /dev/null +++ b/students/734473301/coding2017jyz/src/homework/jyz/coding2017/WorkStart.java @@ -0,0 +1,26 @@ +package homework.jyz.coding2017; + +import java.io.File; +import java.io.IOException; + +/** + * 主程序类 + * Created by jyz on 2017/6/13. + */ +public class WorkStart { + public static void main(String args[]) throws IOException { + // 发送邮件程序入口 + Configuration confg = new Configuration();// 加载配置 + MailSend ms = new MailSend(new EmailHost(confg));// 创建邮件工作类 + MailConfig mc = new MailConfig(new MailDao());// 组织待发送邮件 + mc.readFile(new File("product_promotion.txt"));// 获取产品信息 + ms.send(mc.getEmails());// 批量发送 + + Email email = new Email("from@qq.com","to@qq.com","coding2017","hello,world!"); + ms.send(email); // 单个发送 + + } + + + +} From a0569f57ae17ea2633270f782e75f16c7cdff22a Mon Sep 17 00:00:00 2001 From: lx520 <740707954@qq,com> Date: Thu, 15 Jun 2017 23:28:08 +0800 Subject: [PATCH 127/214] =?UTF-8?q?update=202017=E5=B9=B46=E6=9C=8815?= =?UTF-8?q?=E6=97=A5=2023:28:01?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/ood/newSrp/Configuration.java | 25 +++ students/740707954/src/ood/newSrp/DBUtil.java | 25 +++ .../740707954/src/ood/newSrp/MailUtil.java | 18 ++ .../src/ood/newSrp/PromotionMail.java | 181 ++++++++++++++++++ .../src/ood/newSrp/product_promotion.txt | 4 + .../src/ood/oldSrp/Configuration.java | 23 +++ .../src/ood/oldSrp/ConfigurationKeys.java | 9 + students/740707954/src/ood/oldSrp/DBUtil.java | 25 +++ .../740707954/src/ood/oldSrp/MailUtil.java | 18 ++ .../src/ood/oldSrp/PromotionMail.java | 180 +++++++++++++++++ .../src/ood/oldSrp/product_promotion.txt | 4 + 11 files changed, 512 insertions(+) create mode 100644 students/740707954/src/ood/newSrp/Configuration.java create mode 100644 students/740707954/src/ood/newSrp/DBUtil.java create mode 100644 students/740707954/src/ood/newSrp/MailUtil.java create mode 100644 students/740707954/src/ood/newSrp/PromotionMail.java create mode 100644 students/740707954/src/ood/newSrp/product_promotion.txt create mode 100644 students/740707954/src/ood/oldSrp/Configuration.java create mode 100644 students/740707954/src/ood/oldSrp/ConfigurationKeys.java create mode 100644 students/740707954/src/ood/oldSrp/DBUtil.java create mode 100644 students/740707954/src/ood/oldSrp/MailUtil.java create mode 100644 students/740707954/src/ood/oldSrp/PromotionMail.java create mode 100644 students/740707954/src/ood/oldSrp/product_promotion.txt diff --git a/students/740707954/src/ood/newSrp/Configuration.java b/students/740707954/src/ood/newSrp/Configuration.java new file mode 100644 index 0000000000..c2721b9a29 --- /dev/null +++ b/students/740707954/src/ood/newSrp/Configuration.java @@ -0,0 +1,25 @@ +package ood.newSrp; +import ood.oldSrp.ConfigurationKeys; + +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/740707954/src/ood/newSrp/DBUtil.java b/students/740707954/src/ood/newSrp/DBUtil.java new file mode 100644 index 0000000000..0022cb74b0 --- /dev/null +++ b/students/740707954/src/ood/newSrp/DBUtil.java @@ -0,0 +1,25 @@ +package ood.newSrp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/740707954/src/ood/newSrp/MailUtil.java b/students/740707954/src/ood/newSrp/MailUtil.java new file mode 100644 index 0000000000..a6ec476715 --- /dev/null +++ b/students/740707954/src/ood/newSrp/MailUtil.java @@ -0,0 +1,18 @@ +package ood.newSrp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/740707954/src/ood/newSrp/PromotionMail.java b/students/740707954/src/ood/newSrp/PromotionMail.java new file mode 100644 index 0000000000..9ca48b6db6 --- /dev/null +++ b/students/740707954/src/ood/newSrp/PromotionMail.java @@ -0,0 +1,181 @@ +package ood.newSrp; + +import ood.oldSrp.ConfigurationKeys; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File(System.getProperty("user.dir") + "/src/ood/oldSrp/product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + protected void setProductID(String productID) { + this.productID = productID; + + } + + protected String getproductID() { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID + "' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + + + } + + + protected void readFile(File file) throws IOException { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } catch (Exception e) { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/740707954/src/ood/newSrp/product_promotion.txt b/students/740707954/src/ood/newSrp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/740707954/src/ood/newSrp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/740707954/src/ood/oldSrp/Configuration.java b/students/740707954/src/ood/oldSrp/Configuration.java new file mode 100644 index 0000000000..9a1a4d075c --- /dev/null +++ b/students/740707954/src/ood/oldSrp/Configuration.java @@ -0,0 +1,23 @@ +package ood.oldSrp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/740707954/src/ood/oldSrp/ConfigurationKeys.java b/students/740707954/src/ood/oldSrp/ConfigurationKeys.java new file mode 100644 index 0000000000..154ea2c77c --- /dev/null +++ b/students/740707954/src/ood/oldSrp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package ood.oldSrp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/740707954/src/ood/oldSrp/DBUtil.java b/students/740707954/src/ood/oldSrp/DBUtil.java new file mode 100644 index 0000000000..2397e15ad1 --- /dev/null +++ b/students/740707954/src/ood/oldSrp/DBUtil.java @@ -0,0 +1,25 @@ +package ood.oldSrp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/740707954/src/ood/oldSrp/MailUtil.java b/students/740707954/src/ood/oldSrp/MailUtil.java new file mode 100644 index 0000000000..71c229b37d --- /dev/null +++ b/students/740707954/src/ood/oldSrp/MailUtil.java @@ -0,0 +1,18 @@ +package ood.oldSrp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/740707954/src/ood/oldSrp/PromotionMail.java b/students/740707954/src/ood/oldSrp/PromotionMail.java new file mode 100644 index 0000000000..2edbd5bf46 --- /dev/null +++ b/students/740707954/src/ood/oldSrp/PromotionMail.java @@ -0,0 +1,180 @@ +package ood.oldSrp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File(System.getProperty("user.dir") + "/src/ood/oldSrp/product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + protected void setProductID(String productID) { + this.productID = productID; + + } + + protected String getproductID() { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID + "' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } catch (Exception e) { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/740707954/src/ood/oldSrp/product_promotion.txt b/students/740707954/src/ood/oldSrp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/740707954/src/ood/oldSrp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From b0b1e977ade3e955e81d8e570cbf7a52d2c4f3fc Mon Sep 17 00:00:00 2001 From: lx520 <740707954@qq,com> Date: Fri, 16 Jun 2017 00:02:26 +0800 Subject: [PATCH 128/214] =?UTF-8?q?update=202017=E5=B9=B46=E6=9C=8816?= =?UTF-8?q?=E6=97=A5=2000:02:20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/740707954/src/ood/newSrp/FileUtil.java | 13 +++++++++++++ .../740707954/src/ood/newSrp/PromotionMail.java | 3 +++ .../src/ood/newSrp/entity/MainSmtpServer.java | 8 ++++++++ .../740707954/src/ood/newSrp/entity/Notify.java | 14 ++++++++++++++ .../740707954/src/ood/newSrp/entity/Product.java | 10 ++++++++++ .../src/ood/newSrp/entity/SmtpServer.java | 8 ++++++++ .../src/ood/newSrp/entity/TempSmtpServer.java | 8 ++++++++ 7 files changed, 64 insertions(+) create mode 100644 students/740707954/src/ood/newSrp/FileUtil.java create mode 100644 students/740707954/src/ood/newSrp/entity/MainSmtpServer.java create mode 100644 students/740707954/src/ood/newSrp/entity/Notify.java create mode 100644 students/740707954/src/ood/newSrp/entity/Product.java create mode 100644 students/740707954/src/ood/newSrp/entity/SmtpServer.java create mode 100644 students/740707954/src/ood/newSrp/entity/TempSmtpServer.java diff --git a/students/740707954/src/ood/newSrp/FileUtil.java b/students/740707954/src/ood/newSrp/FileUtil.java new file mode 100644 index 0000000000..57508724a6 --- /dev/null +++ b/students/740707954/src/ood/newSrp/FileUtil.java @@ -0,0 +1,13 @@ +package ood.newSrp; + +import java.io.File; +import java.io.IOException; + +/** + * Created by Administrator on 2017/6/16 0016. + */ +public class FileUtil { + protected void readFile(File file) throws IOException { + + } +} diff --git a/students/740707954/src/ood/newSrp/PromotionMail.java b/students/740707954/src/ood/newSrp/PromotionMail.java index 9ca48b6db6..018f1aa8b9 100644 --- a/students/740707954/src/ood/newSrp/PromotionMail.java +++ b/students/740707954/src/ood/newSrp/PromotionMail.java @@ -10,6 +10,9 @@ import java.util.Iterator; import java.util.List; +/** + * promotion 提升 + */ public class PromotionMail { diff --git a/students/740707954/src/ood/newSrp/entity/MainSmtpServer.java b/students/740707954/src/ood/newSrp/entity/MainSmtpServer.java new file mode 100644 index 0000000000..0e4cfa40bc --- /dev/null +++ b/students/740707954/src/ood/newSrp/entity/MainSmtpServer.java @@ -0,0 +1,8 @@ +package ood.newSrp.entity; + +/** + * 主要服务器 + * Created by Administrator on 2017/6/15 0015. + */ +public class MainSmtpServer implements SmtpServer { +} diff --git a/students/740707954/src/ood/newSrp/entity/Notify.java b/students/740707954/src/ood/newSrp/entity/Notify.java new file mode 100644 index 0000000000..aa2caa1049 --- /dev/null +++ b/students/740707954/src/ood/newSrp/entity/Notify.java @@ -0,0 +1,14 @@ +package ood.newSrp.entity; + +import java.io.IOException; +import java.util.HashMap; + +/** + * 通知表 + * Created by Administrator on 2017/6/15 0015. + */ +public class Notify { + protected void setMessage(HashMap userInfo) throws IOException { + + } +} diff --git a/students/740707954/src/ood/newSrp/entity/Product.java b/students/740707954/src/ood/newSrp/entity/Product.java new file mode 100644 index 0000000000..e1851c1526 --- /dev/null +++ b/students/740707954/src/ood/newSrp/entity/Product.java @@ -0,0 +1,10 @@ +package ood.newSrp.entity; + +/** + * 产品 + * Created by Administrator on 2017/6/15 0015. + */ +public class Product { + protected String productID = null; + protected String productDesc = null; +} diff --git a/students/740707954/src/ood/newSrp/entity/SmtpServer.java b/students/740707954/src/ood/newSrp/entity/SmtpServer.java new file mode 100644 index 0000000000..0e1ede3dda --- /dev/null +++ b/students/740707954/src/ood/newSrp/entity/SmtpServer.java @@ -0,0 +1,8 @@ +package ood.newSrp.entity; + +/** + * Created by Administrator on 2017/6/15 0015. + */ +public interface SmtpServer { + String address = ""; +} diff --git a/students/740707954/src/ood/newSrp/entity/TempSmtpServer.java b/students/740707954/src/ood/newSrp/entity/TempSmtpServer.java new file mode 100644 index 0000000000..01321fbf7a --- /dev/null +++ b/students/740707954/src/ood/newSrp/entity/TempSmtpServer.java @@ -0,0 +1,8 @@ +package ood.newSrp.entity; + +/** + * 备用服务器 + * Created by Administrator on 2017/6/15 0015. + */ +public class TempSmtpServer implements SmtpServer { +} From ade018b6ec8dc72807404b446c995bc4ef4e3863 Mon Sep 17 00:00:00 2001 From: Ken-W-P-Huang Date: Fri, 16 Jun 2017 09:29:44 +0800 Subject: [PATCH 129/214] Create src --- students/395860968/ood-assignment/src | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/395860968/ood-assignment/src diff --git a/students/395860968/ood-assignment/src b/students/395860968/ood-assignment/src new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/students/395860968/ood-assignment/src @@ -0,0 +1 @@ + From ed59f2dc11fbfc1b16e158792e0aec914cbef278 Mon Sep 17 00:00:00 2001 From: Ken-W-P-Huang Date: Fri, 16 Jun 2017 09:30:44 +0800 Subject: [PATCH 130/214] Add files via upload --- .../ood-assignment/Configuration.java | 24 +++++++++ .../ood-assignment/ConfigurationKeys.java | 9 ++++ students/395860968/ood-assignment/DBUtil.java | 25 +++++++++ .../395860968/ood-assignment/FileUtil.java | 33 ++++++++++++ .../395860968/ood-assignment/MailUtil.java | 52 +++++++++++++++++++ .../395860968/ood-assignment/Product.java | 21 ++++++++ .../ood-assignment/PromotionMail.java | 44 ++++++++++++++++ .../ood-assignment/product_promotion.txt | 4 ++ 8 files changed, 212 insertions(+) create mode 100644 students/395860968/ood-assignment/Configuration.java create mode 100644 students/395860968/ood-assignment/ConfigurationKeys.java create mode 100644 students/395860968/ood-assignment/DBUtil.java create mode 100644 students/395860968/ood-assignment/FileUtil.java create mode 100644 students/395860968/ood-assignment/MailUtil.java create mode 100644 students/395860968/ood-assignment/Product.java create mode 100644 students/395860968/ood-assignment/PromotionMail.java create mode 100644 students/395860968/ood-assignment/product_promotion.txt diff --git a/students/395860968/ood-assignment/Configuration.java b/students/395860968/ood-assignment/Configuration.java new file mode 100644 index 0000000000..44c566fcaa --- /dev/null +++ b/students/395860968/ood-assignment/Configuration.java @@ -0,0 +1,24 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/395860968/ood-assignment/ConfigurationKeys.java b/students/395860968/ood-assignment/ConfigurationKeys.java new file mode 100644 index 0000000000..868a03ff83 --- /dev/null +++ b/students/395860968/ood-assignment/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/395860968/ood-assignment/DBUtil.java b/students/395860968/ood-assignment/DBUtil.java new file mode 100644 index 0000000000..65383e4dba --- /dev/null +++ b/students/395860968/ood-assignment/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/395860968/ood-assignment/FileUtil.java b/students/395860968/ood-assignment/FileUtil.java new file mode 100644 index 0000000000..9a1a468c10 --- /dev/null +++ b/students/395860968/ood-assignment/FileUtil.java @@ -0,0 +1,33 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +/** + * Created by kenhuang on 2017/6/15. + */ +public class FileUtil { + private static final String FILE_PATH="/Users/kenhuang/Desktop/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt"; + private static File f = new File(FileUtil.FILE_PATH); + protected static Product readFile() throws IOException // @02C + { + BufferedReader br = null; + Product resultProduct; + try { + br = new BufferedReader(new FileReader(f)); + String temp = br.readLine(); + String[] data = temp.split(" "); + resultProduct = new Product(data[0],data[1]); + System.out.println("产品ID = " + resultProduct.productID + "\n"); + System.out.println("产品描述 = " + resultProduct.productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + return resultProduct; + } +} diff --git a/students/395860968/ood-assignment/MailUtil.java b/students/395860968/ood-assignment/MailUtil.java new file mode 100644 index 0000000000..e62f40ead8 --- /dev/null +++ b/students/395860968/ood-assignment/MailUtil.java @@ -0,0 +1,52 @@ +package com.coderising.ood.srp; + +import java.io.IOException; + + +public class MailUtil { + protected String smtpHost = null; + protected String altSmtpHost = null; + public MailUtil(String smtpHost, String altSmtpHost) { + this.smtpHost = smtpHost; + this.altSmtpHost = altSmtpHost; + } + private static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + public void sendPromotionMail(boolean debug, PromotionMail mail) throws IOException { + System.out.println("开始发送邮件"); + String toAddress; + String message; + if (mail.toAddressList != null) { + while (mail.hasNextToAddress()) { + toAddress = mail.getNextToAddress(); + if (toAddress.length() > 0){ + message = mail.generateMessageToCurrentToAddress(); + try { + MailUtil.sendEmail(toAddress, mail.fromAddress, mail.subject, message, this.smtpHost, debug); + } + catch (Exception e) { + try { + MailUtil.sendEmail(toAddress, mail.fromAddress, mail.subject, message, this.altSmtpHost, debug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + }else { + System.out.println("目标地址为空"); + } + } + } else { + System.out.println("没有邮件发送"); + } + } +} diff --git a/students/395860968/ood-assignment/Product.java b/students/395860968/ood-assignment/Product.java new file mode 100644 index 0000000000..27ce7b0ef1 --- /dev/null +++ b/students/395860968/ood-assignment/Product.java @@ -0,0 +1,21 @@ +package com.coderising.ood.srp; + +/** + * Created by kenhuang on 2017/6/15. + */ +public class Product { + protected String productID = null; + protected String productDesc = null; + // protected String sendMailQuery = null; + public Product(String productID, String productDesc) { + this.productID = productID; + this.productDesc = productDesc; + } + protected String generateLoadQuery() { + System.out.println("loadQuery set"); + return "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + } +} diff --git a/students/395860968/ood-assignment/PromotionMail.java b/students/395860968/ood-assignment/PromotionMail.java new file mode 100644 index 0000000000..f62a56c295 --- /dev/null +++ b/students/395860968/ood-assignment/PromotionMail.java @@ -0,0 +1,44 @@ +package com.coderising.ood.srp; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; + +public class PromotionMail { + protected String fromAddress = null; + protected List toAddressList = null; + protected String subject = "您关注的产品降价了"; + protected String message = null; + private Product product; + private int toAddressListIndex = -1 ; + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + public static void main(String[] args) throws Exception { + Product product = FileUtil.readFile(); + if (product != null) { + boolean emailDebug = false; + Configuration config = new Configuration(); + MailUtil mailUtil = new MailUtil(config.getProperty(ConfigurationKeys.SMTP_SERVER), + config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + PromotionMail mail = new PromotionMail(config.getProperty(ConfigurationKeys.EMAIL_ADMIN),product); + mailUtil.sendPromotionMail(emailDebug,mail); + } + } + public PromotionMail(String fromAddress,Product product) throws Exception { + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + this.fromAddress = fromAddress; + this.product = product; + this.toAddressList = DBUtil.query(this.product.generateLoadQuery()); + } + public boolean hasNextToAddress(){ + return this.toAddressListIndex < this.toAddressList.size() - 1; + } + public String getNextToAddress(){ + HashMap map = (HashMap)this.toAddressList.get(++this.toAddressListIndex); + return (String)map.get(EMAIL_KEY); + } + protected String generateMessageToCurrentToAddress() throws IOException { + HashMap map = (HashMap)this.toAddressList.get(this.toAddressListIndex); + String name = (String) map.get(NAME_KEY); + return "尊敬的 "+name+", 您关注的产品 " + this.product.productDesc + " 降价了,欢迎购买!" ; + } +} diff --git a/students/395860968/ood-assignment/product_promotion.txt b/students/395860968/ood-assignment/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/395860968/ood-assignment/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 1aa32156840c66d4e3358815cbf5ecc278ed0189 Mon Sep 17 00:00:00 2001 From: macvis Date: Fri, 16 Jun 2017 09:40:52 +0800 Subject: [PATCH 131/214] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BD=9C=E4=B8=9A?= =?UTF-8?q?=E5=AE=8C=E6=88=90=EF=BC=8C=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/srp/refactor/PromotionMail.java | 22 +++++++++---------- .../ood/src/test/java/srp/SrpTest.java | 4 +++- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java b/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java index 4234a0ac6b..c4eb49c1e7 100644 --- a/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java +++ b/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java @@ -65,7 +65,12 @@ protected void setLoadQuery() throws Exception { System.out.println("loadQuery set, productID -> " + productID); } - public void batchSetMails(List data) throws Exception{ + /** + * 批量编写邮件 + * @param data 从文件中获取的产品促销信息的list + * @throws Exception 查询sql时报的异常,这里选择不处理 + */ + public void batchWrite(List data) throws Exception{ if(data.isEmpty()){ throw new RuntimeException("data不能为空"); } @@ -77,20 +82,15 @@ public void batchSetMails(List data) throws Exception{ List userList = DBUtil.query(sendMailQuery); for(HashMap userInfo : userList){ - createMail((String)userInfo.get(EMAIL_KEY), - generateSubject(productName), generateMessage(userInfo, productDesc)); + String add = (String)userInfo.get(EMAIL_KEY); + String subj = "您关注的" + productName + "已降价"; + String msg = "尊敬的 "+ userInfo.get(NAME_KEY) +", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + + createMail(add, subj, msg); } } } - private String generateSubject(String productName){ - return "您关注的" + productName + "已降价"; - } - - private String generateMessage(HashMap userInfo, String productDesc){ - return "尊敬的 "+ userInfo.get(NAME_KEY) +", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; - } - /** * 暂时这么实现 */ diff --git a/students/75939388/ood/src/test/java/srp/SrpTest.java b/students/75939388/ood/src/test/java/srp/SrpTest.java index 843d0b03f3..19d5387df6 100644 --- a/students/75939388/ood/src/test/java/srp/SrpTest.java +++ b/students/75939388/ood/src/test/java/srp/SrpTest.java @@ -15,6 +15,8 @@ public class SrpTest { PromotionMail promotionMail = null; + private static int in = 0; + @Before public void init(){ @@ -30,7 +32,7 @@ public void runTrial() throws Exception{ List data = FileUtil.readFile(file); PromotionMail promotionMail = new PromotionMail(); - promotionMail.batchSetMails(data); + promotionMail.batchWrite(data); promotionMail.send(); } } From fc0dd13723414db4a60d415e6ccd6a5d7ac1bb05 Mon Sep 17 00:00:00 2001 From: GordenChow <513274874@qq.com> Date: Fri, 16 Jun 2017 10:34:33 +0800 Subject: [PATCH 132/214] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E5=AD=A3=E7=AC=AC?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/513274874/ood/ood-assignment/pom.xml | 32 +++++++ .../com/coderising/ood/srp/Configuration.java | 74 +++++++++++++++ .../com/coderising/ood/srp/PromotionMail.java | 89 +++++++++++++++++++ .../ood/srp/constants/ConfigurationKeys.java | 9 ++ .../java/com/coderising/ood/srp/dto/Mail.java | 43 +++++++++ .../com/coderising/ood/srp/dto/Product.java | 29 ++++++ .../java/com/coderising/ood/srp/dto/User.java | 30 +++++++ .../coderising/ood/srp/product_promotion.txt | 4 + .../com/coderising/ood/srp/util/DBUtil.java | 39 ++++++++ .../com/coderising/ood/srp/util/MailUtil.java | 37 ++++++++ 10 files changed, 386 insertions(+) create mode 100644 students/513274874/ood/ood-assignment/pom.xml create mode 100644 students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/constants/ConfigurationKeys.java create mode 100644 students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/dto/Mail.java create mode 100644 students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/dto/Product.java create mode 100644 students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/dto/User.java create mode 100644 students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt create mode 100644 students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/util/DBUtil.java create mode 100644 students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/util/MailUtil.java diff --git a/students/513274874/ood/ood-assignment/pom.xml b/students/513274874/ood/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/513274874/ood/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..b4cacc1958 --- /dev/null +++ b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,74 @@ +package com.coderising.ood.srp; + +import com.coderising.ood.srp.constants.ConfigurationKeys; + +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + + private static Configuration configuration = null; + static Map configurations = new HashMap(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + + + public static Configuration getInstance(){ + + if(configuration == null) { + return new Configuration(); + } + return configuration; + } + + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + + private Configuration() { + setSMTPHost(); + setAltSMTPHost(); + setFromAddress(); + } + + protected void setSMTPHost() + { + smtpHost = getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + protected void setFromAddress() { + fromAddress = getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + public String getSmtpHost() { + return smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public String getFromAddress() { + return fromAddress; + } +} diff --git a/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..3f7d7bbee9 --- /dev/null +++ b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,89 @@ +package com.coderising.ood.srp; + +import com.coderising.ood.srp.dto.Mail; +import com.coderising.ood.srp.dto.Product; +import com.coderising.ood.srp.dto.User; +import com.coderising.ood.srp.util.DBUtil; +import com.coderising.ood.srp.util.MailUtil; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + protected Product product = new Product(); + + private static Configuration config; + + public static void main(String[] args) throws Exception { + + File f = new File("/Users/guodongchow/Desktop/coding2017/projects/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + config = Configuration.getInstance(); + + sendEMails(mailDebug, loadMailingList()); + } + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + product.setProductID(data[0]); + product.setProductDesc(data[1]); + + System.out.println("产品ID = " + product.getProductID()); + System.out.println("产品描述 = " + product.getProductDesc() + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + protected Mail configureEMail(User user, Product product) throws IOException { + return new Mail(user, product); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException { + + System.out.println("开始发送邮件"+ ":\n"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + Mail mail = configureEMail((User) iter.next(), product); + if (mail.getToAddress().length() > 0) + MailUtil.sendEmail(mail, config, debug); + } + + } else { + System.out.println("没有邮件发送"); + + } + } +} diff --git a/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/constants/ConfigurationKeys.java b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/constants/ConfigurationKeys.java new file mode 100644 index 0000000000..9932bf60f4 --- /dev/null +++ b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/constants/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp.constants; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/dto/Mail.java b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/dto/Mail.java new file mode 100644 index 0000000000..e160e39038 --- /dev/null +++ b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/dto/Mail.java @@ -0,0 +1,43 @@ +package com.coderising.ood.srp.dto; + +import java.io.IOException; + +/** + * Created by guodongchow on 2017/6/15. + */ +public class Mail { + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected void setMessage(User userInfo,Product product) throws IOException + { + + String name = userInfo.getName(); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!" ; + + } + + public Mail(User userInfo,Product product){ + try { + setMessage(userInfo,product); + } catch (IOException e) { + e.printStackTrace(); + } + toAddress = userInfo.getMailAddress(); + } + + public String getToAddress() { + return toAddress; + } + + public String getSubject() { + return subject; + } + + public String getMessage() { + return message; + } +} diff --git a/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/dto/Product.java b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/dto/Product.java new file mode 100644 index 0000000000..0684794a72 --- /dev/null +++ b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/dto/Product.java @@ -0,0 +1,29 @@ +package com.coderising.ood.srp.dto; + +/** + * Created by guodongchow on 2017/6/15. + */ +public class Product { + + String productID; + String productDesc; + + public void setProductID(String productID) + { + this.productID = productID; + + } + + public void setProductDesc(String desc) { + this.productDesc = desc; + } + + + public String getProductID() { + return productID; + } + + public String getProductDesc() { + return productDesc; + } +} diff --git a/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/dto/User.java b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/dto/User.java new file mode 100644 index 0000000000..89b98d226d --- /dev/null +++ b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/dto/User.java @@ -0,0 +1,30 @@ +package com.coderising.ood.srp.dto; + +/** + * Created by guodongchow on 2017/6/15. + */ +public class User { + String name; + String mailAddress; + + public User(String name, String mailAddress) { + this.name = name; + this.mailAddress = mailAddress; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getMailAddress() { + return mailAddress; + } + + public void setMailAddress(String mailAddress) { + this.mailAddress = mailAddress; + } +} diff --git a/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/util/DBUtil.java b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/util/DBUtil.java new file mode 100644 index 0000000000..d2848fe5b1 --- /dev/null +++ b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/util/DBUtil.java @@ -0,0 +1,39 @@ +package com.coderising.ood.srp.util; + +import com.coderising.ood.srp.dto.Product; +import com.coderising.ood.srp.dto.User; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + protected String sendMailQuery = null; + /** + * 应该从数据库读, 但是简化为直接生成。 + * @return + */ + public static List query(){ + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + + userList.add(new User("User"+i,"aa@bb.com")); + } + + return userList; + } + + protected void setLoadQuery( Product product) throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + product.getProductID() + "' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"+ "\n"); + } + + + +} diff --git a/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/util/MailUtil.java b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/util/MailUtil.java new file mode 100644 index 0000000000..5d6ec25bbb --- /dev/null +++ b/students/513274874/ood/ood-assignment/src/main/java/com/coderising/ood/srp/util/MailUtil.java @@ -0,0 +1,37 @@ +package com.coderising.ood.srp.util; + +import com.coderising.ood.srp.Configuration; +import com.coderising.ood.srp.dto.Mail; + +public class MailUtil { + + public static void sendEmail(Mail mail,Configuration config, + boolean debug) { + + StringBuilder buffer = new StringBuilder(); + try { + //假装发了一封邮件 + buffer.append("With SmtpHost:").append(config.getSmtpHost()).append("\n"); + + }catch (Exception e){ + try { + //假装发了一封邮件 + buffer.append("With AltSmtpHost:").append(config.getAltSmtpHost()).append(":\n"); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + + buffer.append("From:").append(config.getFromAddress()).append("\n"); + buffer.append("To:").append(mail.getToAddress()).append("\n"); + buffer.append("Subject:").append(mail.getSubject()).append("\n"); + buffer.append("Content:").append(mail.getMessage()).append("\n"); + + System.out.println(buffer.toString()); + + } + + +} From 68cba4bd0fd062920aa7af0e8696ca0a90291752 Mon Sep 17 00:00:00 2001 From: macvis Date: Fri, 16 Jun 2017 11:01:09 +0800 Subject: [PATCH 133/214] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E9=87=8D?= =?UTF-8?q?=E6=9E=84,=20=E8=81=8C=E8=B4=A3=E6=9E=84=E5=BB=BA=E6=9B=B4?= =?UTF-8?q?=E6=98=8E=E7=A1=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/srp/refactor/PromotionMail.java | 101 --------------- .../srp/refactor/PromotionMailClient.java | 72 +++++++++++ .../java/srp/refactor/domain/Product.java | 30 +++++ .../main/java/srp/refactor/domain/User.java | 30 +++++ .../src/main/java/srp/refactor/mail/Mail.java | 118 ------------------ .../java/srp/refactor/mail/MailClient.java | 95 ++++++++++++++ .../srp/refactor/services/MailService.java | 66 ++++++++++ .../srp/refactor/services/ProductService.java | 38 ++++++ .../srp/refactor/services/UserService.java | 31 +++++ .../java/srp/refactor/util/Constants.java | 21 ++++ .../ood/src/test/java/srp/SrpTest.java | 13 +- 11 files changed, 391 insertions(+), 224 deletions(-) delete mode 100644 students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java create mode 100644 students/75939388/ood/src/main/java/srp/refactor/PromotionMailClient.java create mode 100644 students/75939388/ood/src/main/java/srp/refactor/domain/Product.java create mode 100644 students/75939388/ood/src/main/java/srp/refactor/domain/User.java delete mode 100644 students/75939388/ood/src/main/java/srp/refactor/mail/Mail.java create mode 100644 students/75939388/ood/src/main/java/srp/refactor/mail/MailClient.java create mode 100644 students/75939388/ood/src/main/java/srp/refactor/services/MailService.java create mode 100644 students/75939388/ood/src/main/java/srp/refactor/services/ProductService.java create mode 100644 students/75939388/ood/src/main/java/srp/refactor/services/UserService.java create mode 100644 students/75939388/ood/src/main/java/srp/refactor/util/Constants.java diff --git a/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java b/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java deleted file mode 100644 index c4eb49c1e7..0000000000 --- a/students/75939388/ood/src/main/java/srp/refactor/PromotionMail.java +++ /dev/null @@ -1,101 +0,0 @@ -package srp.refactor; - -import org.apache.commons.lang3.StringUtils; -import srp.refactor.configuration.Configuration; -import srp.refactor.configuration.ConfigurationKeys; -import srp.refactor.mail.Mail; -import srp.refactor.util.DBUtil; - -import java.util.HashMap; -import java.util.List; - -/** - * 在原有的Mail邮件功能的基础上修改而来的促销邮件发送端 - * - * Created by Tee on 2017/6/15. - */ -public class PromotionMail extends Mail { - - protected String productID = null; - protected String productDesc = null; - protected String sendMailQuery = null; - - private static Configuration config = new Configuration(); - - private final static String NAME_KEY = "NAME"; - private final static String EMAIL_KEY = "EMAIL"; - - private void init(){ - super.initMailSettings( - config.getProperty(ConfigurationKeys.SMTP_SERVER), - config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER), - config.getProperty(ConfigurationKeys.EMAIL_ADMIN) - ); - } - - public PromotionMail(){ - init(); - } - - @Override - public void createMail(String toAddress, String subject, String message){ - this.toAddress = toAddress; - this.subject = subject; - this.message = message; - super.addToMailList(); - } - - public void setProductPromotion(String productID, String productDesc) throws Exception{ - this.productID = productID; - this.productDesc = productDesc; - } - - /** - * 模拟数据库查询 - */ - protected void setLoadQuery() throws Exception { - if(StringUtils.isBlank(productID)){ - throw new RuntimeException("没有获取到productID"); - } - - sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + productID +"' " - + "and send_mail=1 "; - - System.out.println("loadQuery set, productID -> " + productID); - } - - /** - * 批量编写邮件 - * @param data 从文件中获取的产品促销信息的list - * @throws Exception 查询sql时报的异常,这里选择不处理 - */ - public void batchWrite(List data) throws Exception{ - if(data.isEmpty()){ - throw new RuntimeException("data不能为空"); - } - for(int i = 0; i < data.size(); i+=2){ - String productId = data.get(i); - String productName = data.get(i+1); - setProductPromotion(productId, productName); - setLoadQuery(); - - List userList = DBUtil.query(sendMailQuery); - for(HashMap userInfo : userList){ - String add = (String)userInfo.get(EMAIL_KEY); - String subj = "您关注的" + productName + "已降价"; - String msg = "尊敬的 "+ userInfo.get(NAME_KEY) +", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; - - createMail(add, subj, msg); - } - } - } - - /** - * 暂时这么实现 - */ - @Override - public void send(){ - super.batchSend(false); - } -} \ No newline at end of file diff --git a/students/75939388/ood/src/main/java/srp/refactor/PromotionMailClient.java b/students/75939388/ood/src/main/java/srp/refactor/PromotionMailClient.java new file mode 100644 index 0000000000..6335f4f86e --- /dev/null +++ b/students/75939388/ood/src/main/java/srp/refactor/PromotionMailClient.java @@ -0,0 +1,72 @@ +package srp.refactor; + +import srp.refactor.configuration.Configuration; +import srp.refactor.configuration.ConfigurationKeys; +import srp.refactor.domain.Product; +import srp.refactor.domain.User; +import srp.refactor.mail.MailClient; +import srp.refactor.services.ProductService; +import srp.refactor.services.UserService; + +import java.util.List; + +/** + * 在原有的MailClient邮件功能的基础上修改而来的促销邮件发送端 + * + * 根据SRP原则,这个邮件客户端只有两个职责: + * 解析传进来的List,然后批量保存至超类的待发送邮件列表中 + * 全部解析完成(数据量较大时需要设置阈值)后 + * 由用户发起发送请求 + * + * Created by Tee on 2017/6/15. + */ +public class PromotionMailClient extends MailClient { + + private static Configuration config = new Configuration(); + + private UserService userService = new UserService(); + private ProductService productService = new ProductService(); + + private void init(){ + super.initMailSettings( + config.getProperty(ConfigurationKeys.SMTP_SERVER), + config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER), + config.getProperty(ConfigurationKeys.EMAIL_ADMIN) + ); + } + + public PromotionMailClient(){ + init(); + } + + @Override + public void createMail(String toAddress, String subject, String message){ + this.toAddress = toAddress; + this.subject = subject; + this.message = message; + super.addToMailList(); + } + + /** + * 批量编写邮件 + * @param promotionProducts 促销中的所有产品 + * @throws Exception 查询sql时报的异常,这里选择不处理 + */ + public void batchWrite(List promotionProducts) throws Exception{ + if(promotionProducts.isEmpty()){ + throw new RuntimeException("没有商品待促销不能为空"); + } + for(Product product : promotionProducts){ + String querySql = productService.getLoadQuerySql(product.getProductId()); + List userList = userService.getUserList(querySql); + for(User user : userList){ + String add = user.getEmail(); + String subj = "您关注的" + product.getProductDesc() + "已降价"; + String msg = "尊敬的 "+ user.getName() +", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!"; + + createMail(add, subj, msg); + } + } + } + +} \ No newline at end of file diff --git a/students/75939388/ood/src/main/java/srp/refactor/domain/Product.java b/students/75939388/ood/src/main/java/srp/refactor/domain/Product.java new file mode 100644 index 0000000000..955ab28490 --- /dev/null +++ b/students/75939388/ood/src/main/java/srp/refactor/domain/Product.java @@ -0,0 +1,30 @@ +package srp.refactor.domain; + +/** + * Created by Tee on 2017/6/16. + */ +public class Product { + private String productId; + private String productDesc; + + public Product(String productId, String productDesc){ + this.productId = productId; + this.productDesc = productDesc; + } + + public String getProductId() { + return productId; + } + + public void setProductId(String productId) { + this.productId = productId; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } +} diff --git a/students/75939388/ood/src/main/java/srp/refactor/domain/User.java b/students/75939388/ood/src/main/java/srp/refactor/domain/User.java new file mode 100644 index 0000000000..4167396f89 --- /dev/null +++ b/students/75939388/ood/src/main/java/srp/refactor/domain/User.java @@ -0,0 +1,30 @@ +package srp.refactor.domain; + +/** + * Created by Tee on 2017/6/16. + */ +public class User { + private String name; + private String email; + + public User(String name, String email){ + this.name = name; + this.email = email; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} diff --git a/students/75939388/ood/src/main/java/srp/refactor/mail/Mail.java b/students/75939388/ood/src/main/java/srp/refactor/mail/Mail.java deleted file mode 100644 index 755fe6c18d..0000000000 --- a/students/75939388/ood/src/main/java/srp/refactor/mail/Mail.java +++ /dev/null @@ -1,118 +0,0 @@ -package srp.refactor.mail; - -import org.apache.commons.lang3.StringUtils; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - -/** - * 具有初始化设置、可以批量发送邮件的邮件客户端 - * - * Created by Tee on 2017/6/15. - */ -public abstract class Mail { - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; - protected String toAddress = null; - protected String subject = null; - protected String message = null; - - private static final String TO_ADDRESS_KEY = "toAddress"; - private static final String SUBJECT_KEY = "subject"; - private static final String MESSAGE_KEY = "message"; - - protected List mailList; - - /** - * 初始化邮件设置 - * - * @param smtpHost smtp主机 - * @param altSmtpHost 备用smtp主机 - * @param fromAddress 发件人地址 - */ - protected void initMailSettings(String smtpHost, String altSmtpHost, String fromAddress){ - this.smtpHost = smtpHost; - this.altSmtpHost = altSmtpHost; - this.fromAddress = fromAddress; - this.mailList = new ArrayList<>(); - } - - public abstract void createMail(String toAddress, String subject, String message); - - /** - * 撰写邮件 - */ - protected void addToMailList(){ - HashMap mailToSend = new HashMap<>(); - mailToSend.put(TO_ADDRESS_KEY, this.toAddress); - mailToSend.put(SUBJECT_KEY, this.subject); - mailToSend.put(MESSAGE_KEY, this.message); - this.mailList.add(mailToSend); - } - - private boolean isInit(){ - return StringUtils.isNotBlank(this.smtpHost) && - StringUtils.isNotBlank(this.altSmtpHost) && - StringUtils.isNotBlank(this.fromAddress); - } - - /** - * 供子类实现的send方法 - */ - public abstract void send(); - - /** - * 批量发送 - */ - protected void batchSend(boolean debug){ - if(!isInit()){ - throw new RuntimeException("邮件客户端还未做配置"); - } - - if(mailList.isEmpty()){ - System.out.println("没有邮件要发送"); - return; - } - int size = mailList.size(); - System.out.println("开始发送邮件, 总邮件数=" + size); - int i = 0; - for(HashMap mail : mailList){ - i++; - String toAddress = (String)mail.get(TO_ADDRESS_KEY); - if(StringUtils.isBlank(toAddress)){ - System.out.println("收件人地址为空,此邮件发送中止"); - continue; - } - - String subject = (String)mail.get(SUBJECT_KEY); - String message = (String)mail.get(MESSAGE_KEY); - - System.out.println("\n正在发送第[" + i + "]封邮件"); - System.out.println("=========================================================="); - try{ - sendEmail(toAddress, this.fromAddress, subject, message, this.smtpHost, debug); - }catch(Exception e){ - sendEmail(toAddress, this.fromAddress, subject, message, this.altSmtpHost, debug); - } - System.out.println("=========================================================="); - System.out.println("第[" + i + "]封邮件发送完成"); - } - } - - /** - * 发送邮件客户端的功能和责任,所以移入邮件客户端,但是只能被基础客户端调用 - * 子类只能通过batchSend来发送邮件 - */ - private void sendEmail(String toAddress, String fromAddress, String subject, - String message, String smtpHost, boolean debug) { - //假装发了一封邮件 - StringBuilder buffer = new StringBuilder(); - buffer.append("From:").append(fromAddress).append("\n"); - buffer.append("To:").append(toAddress).append("\n"); - buffer.append("Subject:").append(subject).append("\n"); - buffer.append("Content:").append(message).append("\n"); - System.out.print(buffer.toString()); - } -} diff --git a/students/75939388/ood/src/main/java/srp/refactor/mail/MailClient.java b/students/75939388/ood/src/main/java/srp/refactor/mail/MailClient.java new file mode 100644 index 0000000000..3ca783fe0d --- /dev/null +++ b/students/75939388/ood/src/main/java/srp/refactor/mail/MailClient.java @@ -0,0 +1,95 @@ +package srp.refactor.mail; + +import org.apache.commons.lang3.StringUtils; +import srp.refactor.services.MailService; +import srp.refactor.util.Constants; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * 具有初始化设置、可以批量发送邮件的邮件客户端 + * + * Created by Tee on 2017/6/15. + */ +public abstract class MailClient { + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected List mailList; + + private MailService mailService = new MailService(); + /** + * 初始化邮件设置 + * + * @param smtpHost smtp主机 + * @param altSmtpHost 备用smtp主机 + * @param fromAddress 发件人地址 + */ + protected void initMailSettings(String smtpHost, String altSmtpHost, String fromAddress){ + this.smtpHost = smtpHost; + this.altSmtpHost = altSmtpHost; + this.fromAddress = fromAddress; + this.mailList = new ArrayList<>(); + } + + public abstract void createMail(String toAddress, String subject, String message); + + /** + * 撰写邮件 + */ + protected void addToMailList(){ + HashMap mailToSend = new HashMap<>(); + mailToSend.put(Constants.EmailInfo.TO_ADDRESS_KEY.getKey(), this.toAddress); + mailToSend.put(Constants.EmailInfo.SUBJECT_KEY.getKey(), this.subject); + mailToSend.put(Constants.EmailInfo.MESSAGE_KEY.getKey(), this.message); + this.mailList.add(mailToSend); + } + + private boolean isInit(){ + return StringUtils.isNotBlank(this.smtpHost) && + StringUtils.isNotBlank(this.altSmtpHost) && + StringUtils.isNotBlank(this.fromAddress); + } + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + /** + * 调用邮件邮件服务器来收发邮件 + * @param debug + */ + public void batchSend(boolean debug){ + if(!isInit()){ + throw new RuntimeException("邮件客户端还未做配置"); + } + + mailService.batchSend(debug, this, this.mailList); + } +} diff --git a/students/75939388/ood/src/main/java/srp/refactor/services/MailService.java b/students/75939388/ood/src/main/java/srp/refactor/services/MailService.java new file mode 100644 index 0000000000..27bcb38389 --- /dev/null +++ b/students/75939388/ood/src/main/java/srp/refactor/services/MailService.java @@ -0,0 +1,66 @@ +package srp.refactor.services; + +import org.apache.commons.lang3.StringUtils; +import srp.refactor.mail.MailClient; +import srp.refactor.util.Constants; + +import java.util.HashMap; +import java.util.List; + +/** + * 邮件收发服务 + * + * Created by Tee on 2017/6/16. + */ +public class MailService { + + + /** + * 批量发送 + */ + public void batchSend(boolean debug, MailClient mailClient, List mailList){ + if(mailList.isEmpty()){ + System.out.println("没有邮件要发送"); + return; + } + int size = mailList.size(); + System.out.println("开始发送邮件, 总邮件数=" + size); + int i = 0; + for(HashMap mail : mailList){ + i++; + String toAddress = (String)mail.get(Constants.EmailInfo.TO_ADDRESS_KEY.getKey()); + if(StringUtils.isBlank(toAddress)){ + System.out.println("收件人地址为空,此邮件发送中止"); + continue; + } + + String subject = (String)mail.get(Constants.EmailInfo.SUBJECT_KEY.getKey()); + String message = (String)mail.get(Constants.EmailInfo.MESSAGE_KEY.getKey()); + + System.out.println("\n正在发送第[" + i + "]封邮件"); + System.out.println("=========================================================="); + try{ + sendEmail(toAddress, mailClient.getFromAddress(), subject, message, mailClient.getSmtpHost(), debug); + }catch(Exception e){ + sendEmail(toAddress, mailClient.getFromAddress(), subject, message, mailClient.getAltSmtpHost(), debug); + } + System.out.println("=========================================================="); + System.out.println("第[" + i + "]封邮件发送完成"); + } + } + + /** + * 发送邮件客户端的功能和责任,所以移入邮件客户端,但是只能被基础客户端调用 + * 子类只能通过batchSend来发送邮件 + */ + public void sendEmail(String toAddress, String fromAddress, String subject, + String message, String smtpHost, boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.print(buffer.toString()); + } +} diff --git a/students/75939388/ood/src/main/java/srp/refactor/services/ProductService.java b/students/75939388/ood/src/main/java/srp/refactor/services/ProductService.java new file mode 100644 index 0000000000..a1484520d1 --- /dev/null +++ b/students/75939388/ood/src/main/java/srp/refactor/services/ProductService.java @@ -0,0 +1,38 @@ +package srp.refactor.services; + +import org.apache.commons.lang3.StringUtils; +import srp.refactor.domain.Product; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Tee on 2017/6/16. + */ +public class ProductService { + + public Product setPromotionInfo(String productId, String productDesc){ + return new Product(productId, productDesc); + } + + public String getLoadQuerySql(String productID){ + if(StringUtils.isBlank(productID)){ + throw new RuntimeException("没有获取到productID"); + } + + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + System.out.println("loadQuery set, productID -> " + productID); + return sendMailQuery; + } + + public List getPromotionInfoList(List data){ + List list = new ArrayList<>(); + for(int i = 0; i < data.size(); i += 2){ + list.add(setPromotionInfo(data.get(i), data.get(i + 1))); + } + return list; + } +} diff --git a/students/75939388/ood/src/main/java/srp/refactor/services/UserService.java b/students/75939388/ood/src/main/java/srp/refactor/services/UserService.java new file mode 100644 index 0000000000..403c9d8a95 --- /dev/null +++ b/students/75939388/ood/src/main/java/srp/refactor/services/UserService.java @@ -0,0 +1,31 @@ +package srp.refactor.services; + +import srp.refactor.domain.User; +import srp.refactor.util.DBUtil; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * Created by Tee on 2017/6/16. + */ +public class UserService { + + public User setUser(String userName, String email){ + return new User(userName, email); + } + + public List getUserList(String sql){ + List userMapList = DBUtil.query(sql); + List userList = new ArrayList<>(); + for(HashMap map : userMapList){ + userList.add(setUser( + (String)map.get("NAME"), + (String)map.get("EMAIL")) + ); + } + + return userList; + } +} diff --git a/students/75939388/ood/src/main/java/srp/refactor/util/Constants.java b/students/75939388/ood/src/main/java/srp/refactor/util/Constants.java new file mode 100644 index 0000000000..88633b1faf --- /dev/null +++ b/students/75939388/ood/src/main/java/srp/refactor/util/Constants.java @@ -0,0 +1,21 @@ +package srp.refactor.util; + +/** + * Created by Tee on 2017/6/16. + */ +public class Constants { + public enum EmailInfo{ + TO_ADDRESS_KEY("toAddress"), + SUBJECT_KEY("subject"), + MESSAGE_KEY("message"); + + private String key; + EmailInfo(String key){ + this.key = key; + } + + public String getKey(){ + return this.key; + } + } +} diff --git a/students/75939388/ood/src/test/java/srp/SrpTest.java b/students/75939388/ood/src/test/java/srp/SrpTest.java index 19d5387df6..c55f54b1d0 100644 --- a/students/75939388/ood/src/test/java/srp/SrpTest.java +++ b/students/75939388/ood/src/test/java/srp/SrpTest.java @@ -2,7 +2,9 @@ import org.junit.Before; import org.junit.Test; -import srp.refactor.PromotionMail; +import srp.refactor.PromotionMailClient; +import srp.refactor.domain.Product; +import srp.refactor.services.ProductService; import srp.refactor.util.FileUtil; import java.io.File; @@ -13,7 +15,7 @@ */ public class SrpTest { - PromotionMail promotionMail = null; + PromotionMailClient promotionMail = null; private static int in = 0; @@ -31,8 +33,9 @@ public void runTrial() throws Exception{ "students/75939388/ood/src/main/resources/ood_demo_file/product_promotion.txt"); List data = FileUtil.readFile(file); - PromotionMail promotionMail = new PromotionMail(); - promotionMail.batchWrite(data); - promotionMail.send(); + PromotionMailClient promotionMail = new PromotionMailClient(); + List promotionProducts = new ProductService().getPromotionInfoList(data); + promotionMail.batchWrite(promotionProducts); + promotionMail.batchSend(false); } } From 52d458713db3cecb20abfaafb509902f0da521dd Mon Sep 17 00:00:00 2001 From: doudou Date: Fri, 16 Jun 2017 17:08:59 +0800 Subject: [PATCH 134/214] first --- students/759412759/pom.xml | 32 ++++++++ .../com/coderising/ood/srp/Configuration.java | 24 ++++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 +++ .../java/com/coderising/ood/srp/DBUtil.java | 25 ++++++ .../java/com/coderising/ood/srp/FileUtil.java | 32 ++++++++ .../java/com/coderising/ood/srp/Mail.java | 81 +++++++++++++++++++ .../java/com/coderising/ood/srp/MailUtil.java | 22 +++++ .../java/com/coderising/ood/srp/Product.java | 31 +++++++ .../com/coderising/ood/srp/PromotionMail.java | 63 +++++++++++++++ .../com/coderising/ood/srp/UserService.java | 18 +++++ .../coderising/ood/srp/product_promotion.txt | 1 + 11 files changed, 338 insertions(+) create mode 100644 students/759412759/pom.xml create mode 100644 students/759412759/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/759412759/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/759412759/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/759412759/src/main/java/com/coderising/ood/srp/FileUtil.java create mode 100644 students/759412759/src/main/java/com/coderising/ood/srp/Mail.java create mode 100644 students/759412759/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/759412759/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/759412759/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/759412759/src/main/java/com/coderising/ood/srp/UserService.java create mode 100644 students/759412759/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/759412759/pom.xml b/students/759412759/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/759412759/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/Configuration.java b/students/759412759/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..7616041c05 --- /dev/null +++ b/students/759412759/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,24 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap(); + + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public static String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/759412759/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/759412759/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/759412759/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..0bef2d57c9 --- /dev/null +++ b/students/759412759/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List> query(String sql){ + List> userList = new ArrayList>(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + return userList; + } + + +} diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/FileUtil.java b/students/759412759/src/main/java/com/coderising/ood/srp/FileUtil.java new file mode 100644 index 0000000000..6a7eeba44c --- /dev/null +++ b/students/759412759/src/main/java/com/coderising/ood/srp/FileUtil.java @@ -0,0 +1,32 @@ +package com.coderising.ood.srp; + +import java.io.*; + +/** + * Created by Tudou on 2017/6/16. + */ +public class FileUtil { + + public static Product loadProductFromFile(String filePath) throws IOException { + Product product = new Product(); + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(new File(filePath))); + String temp = br.readLine(); + String[] data = temp.split(" "); + + product.setProductID(data[0]); + product.setProductDesc(data[1]); + + System.out.println("产品ID = " + product.getProductID() + "\n"); + System.out.println("产品描述 = " + product.getProductDesc() + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + return product; + } + +} diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/Mail.java b/students/759412759/src/main/java/com/coderising/ood/srp/Mail.java new file mode 100644 index 0000000000..49f0db842d --- /dev/null +++ b/students/759412759/src/main/java/com/coderising/ood/srp/Mail.java @@ -0,0 +1,81 @@ +package com.coderising.ood.srp; + + +public class Mail { + + private String smtpHost; + private String altSmtpHost; + private String fromAddress; + + private String toAddress; + private String subject; + private String message; + private boolean debug; + + + public Mail() { + + } + + public void init() { + smtpHost = Configuration.getProperty(ConfigurationKeys.SMTP_SERVER); + altSmtpHost = Configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + fromAddress = Configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean getDebug() { + return debug; + } + + public void setDebug(boolean debug) { + this.debug = debug; + } +} diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/759412759/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..2820127c4b --- /dev/null +++ b/students/759412759/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,22 @@ +package com.coderising.ood.srp; + + +public class MailUtil { + + public static boolean sendEmail(Mail mail) { + //假装发了一封邮件 + if(mail.getToAddress().length() < 0){ + return Boolean.FALSE; + } + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(mail.getFromAddress()).append("\n"); + buffer.append("To:").append(mail.getToAddress()).append("\n"); + buffer.append("Subject:").append(mail.getSmtpHost()).append("\n"); + buffer.append("Content:").append(mail.getMessage()).append("\n"); + buffer.append("smtpHost:").append(mail.getSmtpHost()).append("\n"); + buffer.append("isDebug:").append(mail.getDebug() ? "1" : "0").append("\n"); + System.out.println(buffer.toString()); + return Boolean.TRUE; + } + +} diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/Product.java b/students/759412759/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..860f584faf --- /dev/null +++ b/students/759412759/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,31 @@ +package com.coderising.ood.srp; + +/** + * Created by Tudou on 2017/6/16. + */ +public class Product { + + private String productID; + private String productDesc; + + + public Product() { + + } + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } +} diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/759412759/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..5dfa3853c8 --- /dev/null +++ b/students/759412759/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,63 @@ +package com.coderising.ood.srp; + +import java.io.*; +import java.util.*; + +public class PromotionMail { + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + UserService userService = new UserService(); + PromotionMail pe = new PromotionMail(); + + String path = "F:\\IDEA_PRO_01\\coderrising\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"; + Product product = FileUtil.loadProductFromFile(path); + List> list = userService.loadMailingList(product.getProductID()); + + pe.sendEMails(list,product,Boolean.FALSE); + } + + private void sendEMails(List> mailingList, Product product, boolean debug) throws IOException { + System.out.println("开始发送邮件"); + if (mailingList != null) { + Iterator> iter = mailingList.iterator(); + while (iter.hasNext()) { + HashMap userInfo = iter.next(); + Mail mail = getMail(product, debug, userInfo); + try { + boolean flag = MailUtil.sendEmail(mail); + if (!flag) { + mail.setSmtpHost(mail.getAltSmtpHost()); + MailUtil.sendEmail(mail); + } + } catch (Exception e) { + try { + mail.setSmtpHost(mail.getAltSmtpHost()); + MailUtil.sendEmail(mail); + } catch (Exception e2) { + System.out.println("通过备用 SMTP 服务器发送邮件失败: " + e2.getMessage()); + } + } + } + } else { + System.out.println("没有邮件发送"); + } + } + + private Mail getMail(Product product, boolean debug, HashMap userInfo) { + Mail mail = new Mail(); + String subject = "您关注的产品降价了"; + String message = "尊敬的 " + userInfo.get(NAME_KEY) + ", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!"; + + mail.init(); + mail.setToAddress(userInfo.get(EMAIL_KEY)); + mail.setSubject(subject); + mail.setMessage(message); + mail.setDebug(debug); + return mail; + } +} diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/UserService.java b/students/759412759/src/main/java/com/coderising/ood/srp/UserService.java new file mode 100644 index 0000000000..80dc46eda8 --- /dev/null +++ b/students/759412759/src/main/java/com/coderising/ood/srp/UserService.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +import java.util.HashMap; +import java.util.List; + +/** + * Created by Tudou on 2017/6/16. + */ +public class UserService { + + public List> loadMailingList(String productId) throws Exception { + String sql = "Select name from subscriptions " + + "where product_id= '" + productId + "' " + + "and send_mail=1 "; + return DBUtil.query(sql); + } + +} diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/759412759/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..46c64c6e64 --- /dev/null +++ b/students/759412759/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1 @@ +P8756 iPhone8 \ No newline at end of file From e1e0a17e968e2e836a3b44fc6bb68db968c17894 Mon Sep 17 00:00:00 2001 From: Ginkee Date: Fri, 16 Jun 2017 22:27:03 +0800 Subject: [PATCH 135/214] =?UTF-8?q?SRP=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/466199956/ood/ood-assignment/pom.xml | 32 +++++ .../com/coderising/ood/srp/Configuration.java | 23 ++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 ++ .../java/com/coderising/ood/srp/DBUtil.java | 25 ++++ .../java/com/coderising/ood/srp/FileUtil.java | 25 ++++ .../java/com/coderising/ood/srp/Mail.java | 124 ++++++++++++++++++ .../java/com/coderising/ood/srp/MailUtil.java | 17 +++ .../java/com/coderising/ood/srp/Product.java | 32 +++++ .../com/coderising/ood/srp/PromotionMail.java | 85 ++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 10 files changed, 376 insertions(+) create mode 100644 students/466199956/ood/ood-assignment/pom.xml create mode 100644 students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java create mode 100644 students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java create mode 100644 students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/466199956/ood/ood-assignment/pom.xml b/students/466199956/ood/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/466199956/ood/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..af199815a4 --- /dev/null +++ b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java new file mode 100644 index 0000000000..5d59ae261b --- /dev/null +++ b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +/** + * Created by Dell on 2017/6/15. + */ +public class FileUtil { + public static String readFile (File file) throws IOException{ + + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + return br.readLine(); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } +} diff --git a/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java new file mode 100644 index 0000000000..cd2bb049dc --- /dev/null +++ b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java @@ -0,0 +1,124 @@ +package com.coderising.ood.srp; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +/** + * Created by Dell on 2017/6/15. + */ +public class Mail { + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + + + protected String sendMailQuery = null; + + + protected static final String NAME_KEY = "NAME"; + protected static final String EMAIL_KEY = "EMAIL"; + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(this, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(this, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } + + protected void configureEMail(HashMap userInfo) throws IOException + { + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + +} diff --git a/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..c2d46495a0 --- /dev/null +++ b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,17 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(Mail mail, boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(mail.getFromAddress()).append("\n"); + buffer.append("To:").append(mail.getToAddress()).append("\n"); + buffer.append("Subject:").append(mail.getSubject()).append("\n"); + buffer.append("Content:").append(mail.getMessage()).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..6587491c3b --- /dev/null +++ b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,32 @@ +package com.coderising.ood.srp; + +/** + * Created by Dell on 2017/6/15. + */ +public class Product { + + private String productID = null; + private String productDesc = null; + + + + public void setProductID(String productID) + { + this.productID = productID; + + } + + public String getproductID() + { + return productID; + } + + + public void setProductDesc(String desc) { + this.productDesc = desc; + } + + public String getProductDesc() { + return productDesc; + } +} diff --git a/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..1d054470eb --- /dev/null +++ b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,85 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail extends Mail { + protected Product product; + private static Configuration config; + + public static void main(String[] args) throws Exception { + File f = new File("E:\\LandWolf\\coding2017\\students\\466199956\\ood\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"); + boolean emailDebug = false; + PromotionMail pe = new PromotionMail(f, emailDebug); + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + product = new Product(); + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + config = new Configuration(); + + setSmtpHost(config.getProperty(ConfigurationKeys.SMTP_SERVER)); + setAltSmtpHost(config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + + + setFromAddress(config.getProperty(ConfigurationKeys.EMAIL_ADMIN)); + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + product.getproductID() + "' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setMessage(HashMap userInfo) throws IOException { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 " + name + ", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!"; + + + } + + + protected void readFile(File file) throws IOException // @02C + { + String temp = FileUtil.readFile(file); + String[] data = temp.split(" "); + + product.setProductID(data[0]); + product.setProductDesc(data[1]); + + System.out.println("产品ID = " + product.getproductID() + "\n"); + System.out.println("产品描述 = " + product.getProductDesc() + "\n"); + } + + + @Override + protected void configureEMail(HashMap userInfo) throws IOException { + + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + this.setMessage(userInfo); + } +} diff --git a/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/466199956/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 659ff51973951ec1223a90ac22240dad2a7b5eca Mon Sep 17 00:00:00 2001 From: zizifn <515868058@qq.com> Date: Fri, 16 Jun 2017 23:44:38 +0800 Subject: [PATCH 136/214] ood-assignment first assignment. --- students/515868058/.gitignore | 16 +++ students/515868058/ood-assignment/pom.xml | 44 ++++++++ .../com/coderising/ood/srp/Configuration.java | 23 ++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 ++ .../java/com/coderising/ood/srp/DBUtil.java | 22 ++++ .../java/com/coderising/ood/srp/Mail.java | 45 ++++++++ .../java/com/coderising/ood/srp/MailUtil.java | 18 +++ .../java/com/coderising/ood/srp/Product.java | 54 +++++++++ .../com/coderising/ood/srp/PromotionMail.java | 105 ++++++++++++++++++ .../java/com/coderising/ood/srp/UserInfo.java | 31 ++++++ .../src/main/resources/product_promotion.txt | 4 + 11 files changed, 371 insertions(+) create mode 100644 students/515868058/.gitignore create mode 100644 students/515868058/ood-assignment/pom.xml create mode 100644 students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java create mode 100644 students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfo.java create mode 100644 students/515868058/ood-assignment/src/main/resources/product_promotion.txt diff --git a/students/515868058/.gitignore b/students/515868058/.gitignore new file mode 100644 index 0000000000..d1651ea2f6 --- /dev/null +++ b/students/515868058/.gitignore @@ -0,0 +1,16 @@ +/target/ +/bin/ +.classpath +.project +/.project +.idea/libraries/Maven__junit_junit_4_12.xml +.idea/libraries/Maven__org_apache_ant_ant_1_9_6.xml +.idea/libraries/Maven__org_apache_ant_ant_launcher_1_9_6.xml +.idea/libraries/Maven__org_apache_commons_commons_lang3_3_5.xml +.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml +.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_24.xml +.idea/sonarlint/ +.idea/workspace.xml +logfile.log +logfile1.log +/.idea/ diff --git a/students/515868058/ood-assignment/pom.xml b/students/515868058/ood-assignment/pom.xml new file mode 100644 index 0000000000..1a0471dccc --- /dev/null +++ b/students/515868058/ood-assignment/pom.xml @@ -0,0 +1,44 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..fcaa1dd3e2 --- /dev/null +++ b/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,22 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + + userList.add(new UserInfo("User" + i, "aa@bb.com")); + } + + return userList; + } +} diff --git a/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java b/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java new file mode 100644 index 0000000000..32571d3505 --- /dev/null +++ b/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java @@ -0,0 +1,45 @@ +package com.coderising.ood.srp; + +/** + * Created by James on 6/15/2017. + */ +public class Mail { + + private Configuration config; + + private String smtpHost = null; + private String altSmtpHost = null; + private String fromAddress = null; + + public Mail(Configuration config) { + this.config =config; + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + + } + + protected void setSMTPHost() { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + protected void setFromAddress() { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + public void sendEmail(String toAddress, String subject, String message, boolean debug) throws Exception { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + + + } + + public void sendAltEmail(String toAddress, String subject, String message, boolean debug) throws Exception{ + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + } +} diff --git a/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..820f63cacd --- /dev/null +++ b/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,54 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +/** + * Created by James on 6/16/2017. + */ +public class Product { + + + private String productID = null; + private String productDesc = null; + + public Product(String id, String desc) { + this.productID = id; + this.productDesc = desc; + } + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + + public static Product buildProduct(File file) throws IOException { + + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + return new Product(data[0], data[1]); + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + + } +} diff --git a/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..c61f0afdbc --- /dev/null +++ b/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,105 @@ +package com.coderising.ood.srp; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +public class PromotionMail { + private Product product; + private Mail mail = null; + private List mailList; + + + public static void main(String[] args) throws Exception { + + File f = new File(PromotionMail.class.getClassLoader().getResource("product_promotion.txt").getPath()); + + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + pe.sendEMails(emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + mail = new Mail(new Configuration()); + product = Product.buildProduct(file); + + } + + + public void sendEMails(boolean debug) throws IOException { + + System.out.println("开始发送邮件"); + if (getMailList() != null) { + getMailList().forEach( + userInfo -> { + if (userInfo.getEmail().length() > 0) { + try { + mail.sendEmail(userInfo.getEmail(), getSubject(), getMessage(userInfo.getName(), getProduct().getProductDesc()), debug); + } catch (Exception e) { + try { + mail.sendAltEmail(userInfo.getEmail(), getSubject(), getMessage(userInfo.getName(), getProduct().getProductDesc()), debug); + } catch (Exception e1) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e1.getMessage()); + } + } + } + } + ); + } else { + System.out.println("没有邮件发送"); + } + + + } + + private String getSubject() { + return "您关注的产品降价了"; + } + + private String getMessage(String username, String productDesc) { + return "尊敬的 " + username + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + + } + + public List getMailList() { + if (mailList == null) { + try { + return loadMailingList(loadQuery(getProduct().getProductID())); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } else { + return this.mailList; + } + } + + public Product getProduct() { + return product; + } + + public void setProduct(Product product) { + this.product = product; + } + + private String loadQuery(String productID) throws Exception { + + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID + "' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + return sendMailQuery; + } + + + private List loadMailingList(String queryString) throws Exception { + return DBUtil.query(queryString); + } + +} diff --git a/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfo.java b/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfo.java new file mode 100644 index 0000000000..8bbd72f035 --- /dev/null +++ b/students/515868058/ood-assignment/src/main/java/com/coderising/ood/srp/UserInfo.java @@ -0,0 +1,31 @@ +package com.coderising.ood.srp; + +/** + * Created by James on 6/15/2017. + */ +public class UserInfo { + + private String name; + private String email; + + public UserInfo(String name, String email){ + this.name = name; + this.email = email; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} diff --git a/students/515868058/ood-assignment/src/main/resources/product_promotion.txt b/students/515868058/ood-assignment/src/main/resources/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/515868058/ood-assignment/src/main/resources/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 8e96cc1b5d73fae9eafcd794e291c09a09ebc087 Mon Sep 17 00:00:00 2001 From: dtwj03 Date: Tue, 13 Jun 2017 00:56:49 +0800 Subject: [PATCH 137/214] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=AF=B4=E6=98=8E?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/469880403/readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/469880403/readme.md diff --git a/students/469880403/readme.md b/students/469880403/readme.md new file mode 100644 index 0000000000..8fc153b34f --- /dev/null +++ b/students/469880403/readme.md @@ -0,0 +1 @@ +说明文件 \ No newline at end of file From 37aed5857805f4ec2b391585512b468eb74e94da Mon Sep 17 00:00:00 2001 From: dtwj03 Date: Sat, 17 Jun 2017 00:04:16 +0800 Subject: [PATCH 138/214] =?UTF-8?q?=E9=87=8D=E6=9E=84=E9=82=AE=E4=BB=B6?= =?UTF-8?q?=E5=8F=91=E9=80=81=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/469880403/ood-assignment/pom.xml | 32 ++++++ .../com/coderising/ood/srp/PromotionMail.java | 104 ++++++++++++++++++ .../ood/srp/dao/SubscriptionDao.java | 22 ++++ .../ood/srp/entity/MailSetting.java | 36 ++++++ .../ood/srp/entity/ProductInfo.java | 19 ++++ .../ood/srp/properties/Configuration.java | 23 ++++ .../ood/srp/properties/ConfigurationKeys.java | 9 ++ .../ood/srp/properties/product_promotion.txt | 4 + .../com/coderising/ood/srp/util/DBUtil.java | 25 +++++ .../com/coderising/ood/srp/util/FileUtil.java | 34 ++++++ .../com/coderising/ood/srp/util/MailUtil.java | 18 +++ 11 files changed, 326 insertions(+) create mode 100644 students/469880403/ood-assignment/pom.xml create mode 100644 students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/dao/SubscriptionDao.java create mode 100644 students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/entity/MailSetting.java create mode 100644 students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/entity/ProductInfo.java create mode 100644 students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/properties/Configuration.java create mode 100644 students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/properties/ConfigurationKeys.java create mode 100644 students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/properties/product_promotion.txt create mode 100644 students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/util/DBUtil.java create mode 100644 students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/util/FileUtil.java create mode 100644 students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/util/MailUtil.java diff --git a/students/469880403/ood-assignment/pom.xml b/students/469880403/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/469880403/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..a6791a7cfa --- /dev/null +++ b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,104 @@ +package com.coderising.ood.srp; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import com.coderising.ood.srp.dao.SubscriptionDao; +import com.coderising.ood.srp.entity.MailSetting; +import com.coderising.ood.srp.entity.ProductInfo; +import com.coderising.ood.srp.properties.Configuration; +import com.coderising.ood.srp.properties.ConfigurationKeys; +import com.coderising.ood.srp.util.FileUtil; +import com.coderising.ood.srp.util.MailUtil; + +public class PromotionMail { + + private boolean mailDebug; + + + private static SubscriptionDao subscriptionDao = new SubscriptionDao(); + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + public static void main(String[] args) throws Exception { + // 1 读取配置文件,加载产品信息 + File file = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + ProductInfo productInfo = new ProductInfo(); + FileUtil.readFileAndSetProductInfo(file, productInfo); + + // 2 设置邮箱服务信息 + Configuration config = new Configuration(); + MailSetting mailSetting = new MailSetting(); + loadMailSetting(config, mailSetting); + + // 3 查询意向用户信息 + subscriptionDao.setLoadQuery(productInfo.getProductID()); + List sendMailList = subscriptionDao.loadMailingList(); + + // 4 发送邮件 + PromotionMail pe = new PromotionMail(emailDebug); + pe.sendEMails(mailSetting, sendMailList, productInfo); + + } + + public PromotionMail( boolean mailDebug) throws Exception { + + this.mailDebug = mailDebug; + } + + private static void loadMailSetting(Configuration config, MailSetting mailSetting) { + + mailSetting.setAltSmtpHost(config.getProperty(ConfigurationKeys.SMTP_SERVER)); + mailSetting.setAltSmtpHost(config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + mailSetting.setFromAddress(config.getProperty(ConfigurationKeys.SMTP_SERVER)); + } + + + + protected void sendEMails(MailSetting mailSetting, List mailingList, ProductInfo productInfo) throws IOException { + + System.out.println("开始发送邮件"); + String subject = "您关注的产品降价了"; + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + Map userInfo = (HashMap) iter.next(); + String userName = (String) userInfo.get(NAME_KEY); + String toAddress = (String) userInfo.get(EMAIL_KEY); + String productDesc = productInfo.getProductDesc(); + String message = "尊敬的 " + userName + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + + try { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, mailSetting.getFromAddress(), subject, message, + mailSetting.getSmtpHost(), this.mailDebug); + } catch (Exception e) { + + try { + MailUtil.sendEmail(toAddress, mailSetting.getFromAddress(), subject, message, + mailSetting.getAltSmtpHost(), this.mailDebug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + } + + else { + System.out.println("没有邮件发送"); + + } + + } + + +} diff --git a/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/dao/SubscriptionDao.java b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/dao/SubscriptionDao.java new file mode 100644 index 0000000000..f5360f0373 --- /dev/null +++ b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/dao/SubscriptionDao.java @@ -0,0 +1,22 @@ +package com.coderising.ood.srp.dao; + +import java.util.List; + +import com.coderising.ood.srp.util.DBUtil; + +public class SubscriptionDao { + + protected static String sendMailQuery = null; + public List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + public void setLoadQuery(String productID) throws Exception { + + sendMailQuery = "Select name from subscriptions " + "where product_id= '" + productID + "' " + + "and send_mail=1 "; + + System.out.println("loadQuery set"); + } + +} diff --git a/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/entity/MailSetting.java b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/entity/MailSetting.java new file mode 100644 index 0000000000..c3db877aa4 --- /dev/null +++ b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/entity/MailSetting.java @@ -0,0 +1,36 @@ +package com.coderising.ood.srp.entity; + +public class MailSetting { + + private String smtpHost = null; + private String altSmtpHost = null; + private String fromAddress = null; + private String toAddress = null; + + + public String getToAddress() { + return toAddress; + } + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + public String getSmtpHost() { + return smtpHost; + } + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + public String getAltSmtpHost() { + return altSmtpHost; + } + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + public String getFromAddress() { + return fromAddress; + } + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + +} diff --git a/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/entity/ProductInfo.java b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/entity/ProductInfo.java new file mode 100644 index 0000000000..0a6a569f27 --- /dev/null +++ b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/entity/ProductInfo.java @@ -0,0 +1,19 @@ +package com.coderising.ood.srp.entity; + +public class ProductInfo { + private String productID = null; + private String productDesc = null; + public String getProductID() { + return productID; + } + public void setProductID(String productID) { + this.productID = productID; + } + public String getProductDesc() { + return productDesc; + } + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + +} diff --git a/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/properties/Configuration.java b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/properties/Configuration.java new file mode 100644 index 0000000000..73aaa9166e --- /dev/null +++ b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/properties/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp.properties; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/properties/ConfigurationKeys.java b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/properties/ConfigurationKeys.java new file mode 100644 index 0000000000..8b09c99124 --- /dev/null +++ b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/properties/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp.properties; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/properties/product_promotion.txt b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/properties/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/properties/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/util/DBUtil.java b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/util/DBUtil.java new file mode 100644 index 0000000000..a23198fcea --- /dev/null +++ b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/util/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp.util; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/util/FileUtil.java b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/util/FileUtil.java new file mode 100644 index 0000000000..a0bb0f3a85 --- /dev/null +++ b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/util/FileUtil.java @@ -0,0 +1,34 @@ +package com.coderising.ood.srp.util; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import com.coderising.ood.srp.entity.ProductInfo; + +public class FileUtil { + + public static void readFileAndSetProductInfo(File file,ProductInfo productInfo) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + productInfo.setProductID( data[0]); + productInfo.setProductDesc( data[1]); + + + + System.out.println("产品ID = " + data[0] + "\n"); + System.out.println("产品描述 = " + data[1] + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + +} diff --git a/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/util/MailUtil.java b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/util/MailUtil.java new file mode 100644 index 0000000000..bb028c690c --- /dev/null +++ b/students/469880403/ood-assignment/src/main/java/com/coderising/ood/srp/util/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp.util; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} From 8177baf71b6f66d5ba78201bd4ada97c2f18a00e Mon Sep 17 00:00:00 2001 From: Yangming Xie Date: Fri, 16 Jun 2017 22:55:16 -0400 Subject: [PATCH 139/214] first commit --- students/702282822/ood-assignment/pom.xml | 32 +++++ .../com/coderising/ood/srp/Configuration.java | 26 ++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 ++ .../java/com/coderising/ood/srp/DBUtil.java | 29 ++++ .../java/com/coderising/ood/srp/Mail.java | 129 ++++++++++++++++++ .../java/com/coderising/ood/srp/Product.java | 30 ++++ .../com/coderising/ood/srp/PromotionMail.java | 77 +++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 8 files changed, 336 insertions(+) create mode 100644 students/702282822/ood-assignment/pom.xml create mode 100644 students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java create mode 100644 students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/702282822/ood-assignment/pom.xml b/students/702282822/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/702282822/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..44e38129c8 --- /dev/null +++ b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,26 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + static final String NAME_KEY = "NAME"; + static final String EMAIL_KEY = "EMAIL"; + + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public static String getProperty(String key) + { + return configurations.get(key); + } + +} diff --git a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..d131ff0c73 --- /dev/null +++ b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..97909cf560 --- /dev/null +++ b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,29 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } + + + + +} diff --git a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java new file mode 100644 index 0000000000..9773a990ba --- /dev/null +++ b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java @@ -0,0 +1,129 @@ +package com.coderising.ood.srp; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public abstract class Mail { + + private String smtpHost = null; + private String altSmtpHost = null; + private String fromAddress = null; + private String toAddress = null; + protected String subject = null; + protected String message = null; + protected String sendMailQuery = null; + + + public Mail(File file, Boolean mailDebug) throws Exception + { + readFile(file); + setSMTPHost(); + setAltSMTPHost(); + setFromAddress(); + + setSendMailQuery(); + List mailingList = loadMailingList(); + sendEMails(mailDebug, mailingList); + + } + protected abstract void readFile(File fie) throws IOException; + + protected void setSMTPHost() + { + smtpHost = Configuration.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = Configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + } + + + protected void setFromAddress() + { + fromAddress = Configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected abstract void setSendMailQuery() throws Exception; + + + protected List loadMailingList() throws Exception { //user information, name and email address + return DBUtil.query(this.sendMailQuery); + } + + + protected abstract void setMessage(String name) throws IOException; + + + + + + protected void setToAddress(HashMap userInfo){ + toAddress = (String) userInfo.get(Configuration.EMAIL_KEY); + } + + protected void configureEMail(HashMap userInfo) throws IOException + { + if (toAddress.length() > 0) + setMessage((String)userInfo.get(Configuration.NAME_KEY)); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + HashMap userInfo = (HashMap) iter.next(); + setToAddress(userInfo); + configureEMail(userInfo); + try + { + if (toAddress.length() > 0) + sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } + + public void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..5d4fb8dce4 --- /dev/null +++ b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,30 @@ +package com.coderising.ood.srp; + +public class Product { + + private static String productID; + private static String productDesc; + + + protected static void setProductID(String ID) + { + productID = ID; + + } + + protected static String getProductID() + { + return productID; + } + + protected static void setProductDesc(String desc) { + productDesc = desc; + } + + protected static String getProductDesc(){ + return productDesc; + } + + + +} diff --git a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..e126809ad6 --- /dev/null +++ b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,77 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail extends Mail { //inheritance from mail + + + public static void main(String[] args) throws Exception { + + File f = new File("\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + public PromotionMail(File file, boolean mailDebug) throws Exception { + super(file, mailDebug); + } + + + + protected void setSendMailQuery() throws Exception { + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + Product.getProductID() +"' " + + "and send_mail=1 "; + + System.out.println("loadQuery set"); + } + + protected void setMessage(String name) throws IOException + { + subject = "您关注的产品降价了"; + message = "尊敬的 "+ name +", 您关注的产品 " + Product.getProductDesc() + " 降价了,欢迎购买!" ; + } + + + @Override + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + Product.setProductID(data[0]); + Product.setProductDesc(data[1]); + + System.out.println("产品ID = " + Product.getProductID() + "\n"); + System.out.println("产品描述 = " + Product.getProductDesc() + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + + + + + + + + + + +} diff --git a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..a98917f829 --- /dev/null +++ b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo R15 +P4955 Vivo X20 \ No newline at end of file From 86b2580d202350d56445c26589e8fb19e91ec762 Mon Sep 17 00:00:00 2001 From: liubin <932235900@qq.com> Date: Sat, 17 Jun 2017 11:11:15 +0800 Subject: [PATCH 140/214] =?UTF-8?q?srp=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coderising/ood/srp/PromotaionTest.java | 34 +++++++++++ .../ood/srp/common/Configuration.java | 23 ++++++++ .../ood/srp/common/ConfigurationKeys.java | 9 +++ .../com/coderising/ood/srp/common/DBUtil.java | 26 ++++++++ .../ood/srp/common/product_promotion.txt | 4 ++ .../com/coderising/ood/srp/entity/Email.java | 46 +++++++++++++++ .../coderising/ood/srp/entity/Product.java | 31 ++++++++++ .../com/coderising/ood/srp/entity/User.java | 29 +++++++++ .../ood/srp/service/EmailService.java | 43 ++++++++++++++ .../ood/srp/service/ProductService.java | 59 +++++++++++++++++++ .../ood/srp/service/UserService.java | 17 ++++++ 11 files changed, 321 insertions(+) create mode 100644 students/932235900/src/com/coderising/ood/srp/PromotaionTest.java create mode 100644 students/932235900/src/com/coderising/ood/srp/common/Configuration.java create mode 100644 students/932235900/src/com/coderising/ood/srp/common/ConfigurationKeys.java create mode 100644 students/932235900/src/com/coderising/ood/srp/common/DBUtil.java create mode 100644 students/932235900/src/com/coderising/ood/srp/common/product_promotion.txt create mode 100644 students/932235900/src/com/coderising/ood/srp/entity/Email.java create mode 100644 students/932235900/src/com/coderising/ood/srp/entity/Product.java create mode 100644 students/932235900/src/com/coderising/ood/srp/entity/User.java create mode 100644 students/932235900/src/com/coderising/ood/srp/service/EmailService.java create mode 100644 students/932235900/src/com/coderising/ood/srp/service/ProductService.java create mode 100644 students/932235900/src/com/coderising/ood/srp/service/UserService.java diff --git a/students/932235900/src/com/coderising/ood/srp/PromotaionTest.java b/students/932235900/src/com/coderising/ood/srp/PromotaionTest.java new file mode 100644 index 0000000000..04bc17a433 --- /dev/null +++ b/students/932235900/src/com/coderising/ood/srp/PromotaionTest.java @@ -0,0 +1,34 @@ +package com.coderising.ood.srp; + +import java.util.List; + +import com.coderising.ood.srp.common.Configuration; +import com.coderising.ood.srp.entity.Email; +import com.coderising.ood.srp.entity.Product; +import com.coderising.ood.srp.entity.User; +import com.coderising.ood.srp.service.EmailService; +import com.coderising.ood.srp.service.ProductService; +import com.coderising.ood.srp.service.UserService; + +public class PromotaionTest { + + public static void main(String[] args) { + //1.获得客户集合 + List users = new UserService().getUsers(); + //2.获得促销商品 + List products = new ProductService().getPromotionProducts("src\\com\\coderising\\ood\\srp\\common\\product_promotion.txt"); + //3.配置 + Configuration conf = new Configuration(); + //给每个用户发邮件 + EmailService emailService = new EmailService(); + for(User user:users ){ + + Email email = emailService.generateEmail(user, products, conf ); + + emailService.sendEmail(email); + + + } + + } +} diff --git a/students/932235900/src/com/coderising/ood/srp/common/Configuration.java b/students/932235900/src/com/coderising/ood/srp/common/Configuration.java new file mode 100644 index 0000000000..4565927da1 --- /dev/null +++ b/students/932235900/src/com/coderising/ood/srp/common/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp.common; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/932235900/src/com/coderising/ood/srp/common/ConfigurationKeys.java b/students/932235900/src/com/coderising/ood/srp/common/ConfigurationKeys.java new file mode 100644 index 0000000000..a0064e0e4d --- /dev/null +++ b/students/932235900/src/com/coderising/ood/srp/common/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp.common; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/932235900/src/com/coderising/ood/srp/common/DBUtil.java b/students/932235900/src/com/coderising/ood/srp/common/DBUtil.java new file mode 100644 index 0000000000..6e333a508c --- /dev/null +++ b/students/932235900/src/com/coderising/ood/srp/common/DBUtil.java @@ -0,0 +1,26 @@ +package com.coderising.ood.srp.common; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import com.coderising.ood.srp.entity.User; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + User user = new User(); + user.setUserName("User" + i); + user.setEmailAddress("aa"+i+"@bb.com"); + userList.add(user); + } + return userList; + } +} diff --git a/students/932235900/src/com/coderising/ood/srp/common/product_promotion.txt b/students/932235900/src/com/coderising/ood/srp/common/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/932235900/src/com/coderising/ood/srp/common/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/932235900/src/com/coderising/ood/srp/entity/Email.java b/students/932235900/src/com/coderising/ood/srp/entity/Email.java new file mode 100644 index 0000000000..dd13c4fab1 --- /dev/null +++ b/students/932235900/src/com/coderising/ood/srp/entity/Email.java @@ -0,0 +1,46 @@ +package com.coderising.ood.srp.entity; +/** + * + * @author liubin + *电子邮件实体类 + */ +public class Email { + + private String toAddress; + private String fromAddress; + private String subject; + private String message; + private String smtpHost; + + public String getToAddress() { + return toAddress; + } + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + public String getFromAddress() { + return fromAddress; + } + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + public String getSubject() { + return subject; + } + public void setSubject(String subject) { + this.subject = subject; + } + public String getMessage() { + return message; + } + public void setMessage(String message) { + this.message = message; + } + public String getSmtpHost() { + return smtpHost; + } + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + +} diff --git a/students/932235900/src/com/coderising/ood/srp/entity/Product.java b/students/932235900/src/com/coderising/ood/srp/entity/Product.java new file mode 100644 index 0000000000..dffa593397 --- /dev/null +++ b/students/932235900/src/com/coderising/ood/srp/entity/Product.java @@ -0,0 +1,31 @@ +package com.coderising.ood.srp.entity; +/** + * + * @author liubin + * 产品实体类 + * + */ +public class Product { + + private String productId; + private String productDesc; + + public String getProductId() { + return productId; + } + public void setProductId(String productId) { + this.productId = productId; + } + public String getProductDesc() { + return productDesc; + } + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + @Override + public String toString() { + return "Product [产品ID = " + productId + ", 产品描述 = " + productDesc + "]"; + } + + +} diff --git a/students/932235900/src/com/coderising/ood/srp/entity/User.java b/students/932235900/src/com/coderising/ood/srp/entity/User.java new file mode 100644 index 0000000000..bd85ceac8f --- /dev/null +++ b/students/932235900/src/com/coderising/ood/srp/entity/User.java @@ -0,0 +1,29 @@ +package com.coderising.ood.srp.entity; + +public class User { + private String userId; + private String userName; + private String emailAddress; + + public String getUserId() { + return userId; + } + public void setUserId(String userId) { + this.userId = userId; + } + public String getUserName() { + return userName; + } + public void setUserName(String userName) { + this.userName = userName; + } + public String getEmailAddress() { + return emailAddress; + } + public void setEmailAddress(String emailAddress) { + this.emailAddress = emailAddress; + } + + + +} diff --git a/students/932235900/src/com/coderising/ood/srp/service/EmailService.java b/students/932235900/src/com/coderising/ood/srp/service/EmailService.java new file mode 100644 index 0000000000..9455ba57a5 --- /dev/null +++ b/students/932235900/src/com/coderising/ood/srp/service/EmailService.java @@ -0,0 +1,43 @@ +package com.coderising.ood.srp.service; + +import java.util.List; + +import com.coderising.ood.srp.common.Configuration; +import com.coderising.ood.srp.common.ConfigurationKeys; +import com.coderising.ood.srp.entity.Email; +import com.coderising.ood.srp.entity.Product; +import com.coderising.ood.srp.entity.User; + +public class EmailService { + + public Email generateEmail(User user,List products,Configuration configuration){ + Email email = new Email(); + email.setFromAddress(configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN)); + email.setToAddress(user.getEmailAddress()); + email.setSubject("您关注的产品降价了"); + StringBuffer message=new StringBuffer("尊敬的 "+user.getUserName()+", 您关注的产品:"); + for(Product product:products){ + message.append(product.getProductDesc()+" "); + } + message.append("降价了,欢迎购买!"); + email.setMessage(message.toString()); + return email; + } + /** + * 发送一封Email + * @param email + */ + public void sendEmail(Email email){ + if(email != null ){ + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(email.getFromAddress()).append("\n"); + buffer.append("To:").append(email.getToAddress()).append("\n"); + buffer.append("Subject:").append(email.getSubject()).append("\n"); + buffer.append("Content:").append(email.getMessage()).append("\n"); + System.out.println(buffer.toString()); + }else{ + System.out.println("email 为空,发送邮件失败!"); + } + } +} diff --git a/students/932235900/src/com/coderising/ood/srp/service/ProductService.java b/students/932235900/src/com/coderising/ood/srp/service/ProductService.java new file mode 100644 index 0000000000..1762527fa6 --- /dev/null +++ b/students/932235900/src/com/coderising/ood/srp/service/ProductService.java @@ -0,0 +1,59 @@ +package com.coderising.ood.srp.service; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import com.coderising.ood.srp.entity.Product; + +public class ProductService { + + /** + * 从给定的路径文件获取打折商品信息 + * @param path + * @return + */ + public List getPromotionProducts(String path){ + List products = new ArrayList(); + if(path != null && !"".equals(path)){ + File file = new File(path); + try { + products = readFile(file); + } catch (IOException e) { + System.out.println("获取打折商品信息出错:"+ e.getMessage()); + } + } + return products; + } + + private List readFile(File file) throws IOException // @02C + { + List products = new ArrayList(); + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = null; + while((temp = br.readLine()) != null){ + + String[] data = temp.split(" "); + + Product product = new Product(); + product.setProductId(data[0]); + product.setProductDesc(data[1]); + System.out.println(product); + + products.add(product); + } + return products; + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + if(br != null){ + br.close(); + } + } + } +} diff --git a/students/932235900/src/com/coderising/ood/srp/service/UserService.java b/students/932235900/src/com/coderising/ood/srp/service/UserService.java new file mode 100644 index 0000000000..d8bc1b67d6 --- /dev/null +++ b/students/932235900/src/com/coderising/ood/srp/service/UserService.java @@ -0,0 +1,17 @@ +package com.coderising.ood.srp.service; + +import java.util.List; + +import com.coderising.ood.srp.common.DBUtil; +import com.coderising.ood.srp.entity.User; + +public class UserService { + /** + * 获取客户列表 + */ + public List getUsers(){ + String sendMailQuery = "Select name from subscriptions " + + "where send_mail=1 "; + return DBUtil.query(sendMailQuery); + } +} From 6dcfb02aa5887f3312520d581173bbf3e238b7c9 Mon Sep 17 00:00:00 2001 From: peng Date: Sat, 17 Jun 2017 11:38:53 +0800 Subject: [PATCH 141/214] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/coderising/ood/srp/DBUtil.java | 9 +- .../com/coderising/ood/srp/FileUtils.java | 41 ++++ .../java/com/coderising/ood/srp/MailUtil.java | 43 +++- .../com/coderising/ood/srp/ProductInfo.java | 30 +++ .../com/coderising/ood/srp/PromotionMail.java | 222 +++++------------- .../java/com/coderising/ood/srp/MainTest.java | 24 ++ .../src/test/resources/product_promotion.txt | 4 + 7 files changed, 202 insertions(+), 171 deletions(-) create mode 100644 students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/FileUtils.java create mode 100644 students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/ProductInfo.java create mode 100644 students/1395844061/course-pro-1/src/test/java/com/coderising/ood/srp/MainTest.java create mode 100644 students/1395844061/course-pro-1/src/test/resources/product_promotion.txt diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/DBUtil.java index 437a74cb40..01fb57505e 100644 --- a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/DBUtil.java +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; /** * DBUtil @@ -17,11 +18,11 @@ public class DBUtil { * @param sql * @return */ - public static List query(String sql){ - - List userList = new ArrayList(); + public static List> query(String sql){ + System.out.println("sql: "+sql); + List> userList = new ArrayList<>(); for (int i = 1; i <= 3; i++) { - HashMap userInfo = new HashMap(); + Map userInfo = new HashMap<>(); userInfo.put("NAME", "User" + i); userInfo.put("EMAIL", "aa@bb.com"); userList.add(userInfo); diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/FileUtils.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/FileUtils.java new file mode 100644 index 0000000000..74bd9c128d --- /dev/null +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/FileUtils.java @@ -0,0 +1,41 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * FileUtils + * + * @author Chenpz + * @package com.coderising.ood.srp + * @date 2017/6/14/22:43 + */ +public final class FileUtils { + + + public static List readProductInfoFromFile(String filePath) throws IOException{ + + File file = new File(filePath); + BufferedReader br = null; + List productInfoList = new ArrayList<>(); + try { + br = new BufferedReader(new FileReader(file)); + String temp; + while((temp = br.readLine()) != null){ + String[] data = temp.split(" "); + productInfoList.add(new ProductInfo(data[0], data[1])); + } + return productInfoList; + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + if (br != null) + br.close(); + } + } + +} diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/MailUtil.java index 83b3ef844b..c81b972a0d 100644 --- a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/MailUtil.java +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -9,14 +9,38 @@ */ public final class MailUtil { + private static String smtpHost = null; + private static String altSmtpHost = null; + private static String fromAddress = null; + private static Configuration config; + + + static{ + config = new Configuration(); + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } private MailUtil(){ throw new RuntimeException("illegal called!"); } - public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, - boolean debug) { + public static void sendEmail(String toAddress, String subject, String message, boolean debug) { + //假装发了一封邮件 + System.out.println("debug: " + debug); + try { + System.out.println("使用默认host发送邮件"); + sendDefaultMail(toAddress, subject, message); + }catch (Exception e){ + System.out.println("使用备用host发送邮件"); + sendAltMail(toAddress,subject, message); + } + } + + private static void sendDefaultMail(String toAddress, String subject, String message){ + System.out.println("smtpHost: " + smtpHost); StringBuilder buffer = new StringBuilder(); buffer.append("From:").append(fromAddress).append("\n"); buffer.append("To:").append(toAddress).append("\n"); @@ -24,4 +48,19 @@ public static void sendEmail(String toAddress, String fromAddress, String subjec buffer.append("Content:").append(message).append("\n"); System.out.println(buffer.toString()); } + + private static void sendAltMail(String toAddress, String subject, String message){ + System.out.println("altSmtpHost: " + altSmtpHost); + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + } + + + + + } diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/ProductInfo.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/ProductInfo.java new file mode 100644 index 0000000000..1ba8cf2db7 --- /dev/null +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/ProductInfo.java @@ -0,0 +1,30 @@ +package com.coderising.ood.srp; + +/** + * ProductInfo + * + * @author Chenpz + * @package com.coderising.ood.srp + * @date 2017/6/14/22:41 + */ +public class ProductInfo { + + private String productID = null; + private String productDesc = null; + + public ProductInfo(){} + + public ProductInfo(String productID, String productDesc){ + this.productID = productID; + this.productDesc = productDesc; + } + + public String getProductDesc() { + return productDesc; + } + + public String getProductID() { + return productID; + } + +} diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java index 331537f5d9..a805239bef 100644 --- a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -1,12 +1,7 @@ package com.coderising.ood.srp; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; import java.io.IOException; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; +import java.util.*; /** * PromotionMail @@ -16,188 +11,85 @@ * @date 2017/6/12/23:33 */ public class PromotionMail { - protected String sendMailQuery = null; - - - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; - protected String toAddress = null; - protected String subject = null; - protected String message = null; - - protected String productID = null; - protected String productDesc = null; - - private static Configuration config; + private String productID = null; + private String productDesc = null; + private List mailInfoList = new ArrayList<>(); private static final String NAME_KEY = "NAME"; private static final String EMAIL_KEY = "EMAIL"; - - public static void main(String[] args) throws Exception { - - File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); - boolean emailDebug = false; - - PromotionMail pe = new PromotionMail(f, emailDebug); - - } - - - public PromotionMail(File file, boolean mailDebug) throws Exception { - + public PromotionMail(){} + public PromotionMail(String productID, String productDesc) throws Exception { //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - readFile(file); - - - config = new Configuration(); - - setSMTPHost(); - setAltSMTPHost(); - - - setFromAddress(); - - - setLoadQuery(); - - sendEMails(mailDebug, loadMailingList()); - - - } - - - - - protected void setProductID(String productID) - { this.productID = productID; - - } - - protected String getproductID() - { - return productID; - } - - protected void setLoadQuery() throws Exception { - - sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + productID +"' " - + "and send_mail=1 "; - - - System.out.println("loadQuery set"); + this.productDesc = productDesc; + initMailInfoList(loadMailingList()); } - - protected void setSMTPHost() - { - smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); - } - - - protected void setAltSMTPHost() - { - altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); - - } - - - protected void setFromAddress() - { - fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); - } - - protected void setMessage(HashMap userInfo) throws IOException - { - - String name = (String) userInfo.get(NAME_KEY); - - subject = "您关注的产品降价了"; - message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; - - - + /** + * 获取每个型号的手机关注的人员信息列表 + * @return + * @throws Exception + */ + private List> loadMailingList() throws Exception { + String sql = "select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + return DBUtil.query(sql); } - - protected void readFile(File file) throws IOException // @02C - { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - setProductID(data[0]); - setProductDesc(data[1]); - - System.out.println("产品ID = " + productID + "\n"); - System.out.println("产品描述 = " + productDesc + "\n"); - - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); + /** + * 组装促销邮件的内容信息 + * @param mailingList + */ + private void initMailInfoList(List> mailingList) { + if (mailingList!=null && !mailingList.isEmpty()){ + for (Map map : mailingList){ + // 初始化 mailInfoList + mailInfoList.add(buildMailInfo(map)); + } } } - private void setProductDesc(String desc) { - this.productDesc = desc; + /** + * 发送促销邮件 + * @param debug + * @throws IOException + */ + public void sendEMails(boolean debug) throws IOException { + System.out.println("开始发送邮件... ..."); + if (mailInfoList!=null && !mailInfoList.isEmpty()) { + for (MailInfo mailInfo : mailInfoList){ + MailUtil.sendEmail(mailInfo.toAddress, mailInfo.subject, mailInfo.message, debug); + } + }else { + System.out.println("没有邮件发送... ..."); + } } - protected void configureEMail(HashMap userInfo) throws IOException - { - toAddress = (String) userInfo.get(EMAIL_KEY); - if (toAddress.length() > 0) - setMessage(userInfo); - } - protected List loadMailingList() throws Exception { - return DBUtil.query(this.sendMailQuery); + private MailInfo buildMailInfo(Map userInfo){ + String name = userInfo.get(NAME_KEY); + String subject = "您关注的产品降价了"; + String message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + String toAddress = userInfo.get(EMAIL_KEY); + return new MailInfo(toAddress, subject, message); } + class MailInfo{ - protected void sendEMails(boolean debug, List mailingList) throws IOException - { - - System.out.println("开始发送邮件"); - - - if (mailingList != null) { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) { - configureEMail((HashMap) iter.next()); - try - { - if (toAddress.length() > 0) - MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } - catch (Exception e) - { - - try { - MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) - { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } - } - - - } - - else { - System.out.println("没有邮件发送"); + private String toAddress = null; + private String subject = null; + private String message = null; + MailInfo(String toAddress, String subject, String message){ + this.toAddress = toAddress; + this.subject = subject; + this.message = message; } - } + } diff --git a/students/1395844061/course-pro-1/src/test/java/com/coderising/ood/srp/MainTest.java b/students/1395844061/course-pro-1/src/test/java/com/coderising/ood/srp/MainTest.java new file mode 100644 index 0000000000..9780778d36 --- /dev/null +++ b/students/1395844061/course-pro-1/src/test/java/com/coderising/ood/srp/MainTest.java @@ -0,0 +1,24 @@ +package com.coderising.ood.srp; + +import java.util.List; + +/** + * MainTest + * + * @author Chenpz + * @package com.coderising.ood.srp + * @date 2017/6/14/22:45 + */ +public class MainTest { + + public static void main(String[] args) throws Exception{ + String filePath = MainTest.class.getClassLoader().getResource("product_promotion.txt").getFile(); + List productInfoList = FileUtils.readProductInfoFromFile(filePath); + if (productInfoList != null && !productInfoList.isEmpty()){ + for (ProductInfo productInfo: productInfoList){ + new PromotionMail(productInfo.getProductID(), productInfo.getProductDesc()) + .sendEMails(false); + } + } + } +} diff --git a/students/1395844061/course-pro-1/src/test/resources/product_promotion.txt b/students/1395844061/course-pro-1/src/test/resources/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/1395844061/course-pro-1/src/test/resources/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 8cb6450b1654785b610be5f84c0c8e8130d78542 Mon Sep 17 00:00:00 2001 From: peng Date: Sat, 17 Jun 2017 11:49:01 +0800 Subject: [PATCH 142/214] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coderising/ood/srp/PromotionMail.java | 37 ++++++++++--------- .../ood/srp/{ => config}/Configuration.java | 2 +- .../srp/{ => config}/ConfigurationKeys.java | 2 +- .../ood/srp/{ => utils}/DBUtil.java | 2 +- .../{FileUtils.java => utils/FileUtil.java} | 6 ++- .../ood/srp/{ => utils}/MailUtil.java | 5 ++- .../java/com/coderising/ood/srp/MainTest.java | 6 ++- 7 files changed, 35 insertions(+), 25 deletions(-) rename students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/{ => config}/Configuration.java (94%) rename students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/{ => config}/ConfigurationKeys.java (89%) rename students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/{ => utils}/DBUtil.java (95%) rename students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/{FileUtils.java => utils/FileUtil.java} (89%) rename students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/{ => utils}/MailUtil.java (93%) diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java index a805239bef..ec54c4ce8d 100644 --- a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -1,5 +1,8 @@ package com.coderising.ood.srp; +import com.coderising.ood.srp.utils.DBUtil; +import com.coderising.ood.srp.utils.MailUtil; + import java.io.IOException; import java.util.*; @@ -12,19 +15,16 @@ */ public class PromotionMail { - - private String productID = null; - private String productDesc = null; + private ProductInfo productInfo; private List mailInfoList = new ArrayList<>(); private static final String NAME_KEY = "NAME"; private static final String EMAIL_KEY = "EMAIL"; public PromotionMail(){} - public PromotionMail(String productID, String productDesc) throws Exception { + public PromotionMail(ProductInfo productInfo) throws Exception { //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - this.productID = productID; - this.productDesc = productDesc; + this.productInfo = productInfo; initMailInfoList(loadMailingList()); } @@ -35,7 +35,7 @@ public PromotionMail(String productID, String productDesc) throws Exception { */ private List> loadMailingList() throws Exception { String sql = "select name from subscriptions " - + "where product_id= '" + productID +"' " + + "where product_id= '" + productInfo.getProductID() +"' " + "and send_mail=1 "; return DBUtil.query(sql); } @@ -53,6 +53,19 @@ private void initMailInfoList(List> mailingList) { } } + /** + * 组装邮件内容信息 + * @param userInfo + * @return + */ + private MailInfo buildMailInfo(Map userInfo){ + String name = userInfo.get(NAME_KEY); + String subject = "您关注的产品降价了"; + String message = "尊敬的 "+name+", 您关注的产品 " + productInfo.getProductDesc() + " 降价了,欢迎购买!" ; + String toAddress = userInfo.get(EMAIL_KEY); + return new MailInfo(toAddress, subject, message); + } + /** * 发送促销邮件 * @param debug @@ -69,16 +82,6 @@ public void sendEMails(boolean debug) throws IOException { } } - - - private MailInfo buildMailInfo(Map userInfo){ - String name = userInfo.get(NAME_KEY); - String subject = "您关注的产品降价了"; - String message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; - String toAddress = userInfo.get(EMAIL_KEY); - return new MailInfo(toAddress, subject, message); - } - class MailInfo{ private String toAddress = null; diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/Configuration.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/config/Configuration.java similarity index 94% rename from students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/Configuration.java rename to students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/config/Configuration.java index cd447ad225..7d2169eff8 100644 --- a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/Configuration.java +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/config/Configuration.java @@ -1,4 +1,4 @@ -package com.coderising.ood.srp; +package com.coderising.ood.srp.config; import java.util.HashMap; import java.util.Map; diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java similarity index 89% rename from students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java rename to students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java index 7c3aa420b9..702c8cce32 100644 --- a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java @@ -1,4 +1,4 @@ -package com.coderising.ood.srp; +package com.coderising.ood.srp.config; /** * ConfigurationKeys diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/DBUtil.java similarity index 95% rename from students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/DBUtil.java rename to students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/DBUtil.java index 01fb57505e..734590b4cf 100644 --- a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/DBUtil.java +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/DBUtil.java @@ -1,4 +1,4 @@ -package com.coderising.ood.srp; +package com.coderising.ood.srp.utils; import java.util.ArrayList; import java.util.HashMap; diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/FileUtils.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/FileUtil.java similarity index 89% rename from students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/FileUtils.java rename to students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/FileUtil.java index 74bd9c128d..807bc48727 100644 --- a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/FileUtils.java +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/FileUtil.java @@ -1,4 +1,6 @@ -package com.coderising.ood.srp; +package com.coderising.ood.srp.utils; + +import com.coderising.ood.srp.ProductInfo; import java.io.BufferedReader; import java.io.File; @@ -14,7 +16,7 @@ * @package com.coderising.ood.srp * @date 2017/6/14/22:43 */ -public final class FileUtils { +public final class FileUtil { public static List readProductInfoFromFile(String filePath) throws IOException{ diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/MailUtil.java similarity index 93% rename from students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/MailUtil.java rename to students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/MailUtil.java index c81b972a0d..06b0606fc0 100644 --- a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/MailUtil.java +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/MailUtil.java @@ -1,4 +1,7 @@ -package com.coderising.ood.srp; +package com.coderising.ood.srp.utils; + +import com.coderising.ood.srp.config.Configuration; +import com.coderising.ood.srp.config.ConfigurationKeys; /** * MailUtil diff --git a/students/1395844061/course-pro-1/src/test/java/com/coderising/ood/srp/MainTest.java b/students/1395844061/course-pro-1/src/test/java/com/coderising/ood/srp/MainTest.java index 9780778d36..9c80f94619 100644 --- a/students/1395844061/course-pro-1/src/test/java/com/coderising/ood/srp/MainTest.java +++ b/students/1395844061/course-pro-1/src/test/java/com/coderising/ood/srp/MainTest.java @@ -1,5 +1,7 @@ package com.coderising.ood.srp; +import com.coderising.ood.srp.utils.FileUtil; + import java.util.List; /** @@ -13,10 +15,10 @@ public class MainTest { public static void main(String[] args) throws Exception{ String filePath = MainTest.class.getClassLoader().getResource("product_promotion.txt").getFile(); - List productInfoList = FileUtils.readProductInfoFromFile(filePath); + List productInfoList = FileUtil.readProductInfoFromFile(filePath); if (productInfoList != null && !productInfoList.isEmpty()){ for (ProductInfo productInfo: productInfoList){ - new PromotionMail(productInfo.getProductID(), productInfo.getProductDesc()) + new PromotionMail(productInfo) .sendEMails(false); } } From b54ac89a37ec9649f1d77e7cb6bb457eb3c2f3d6 Mon Sep 17 00:00:00 2001 From: peng Date: Sat, 17 Jun 2017 11:50:34 +0800 Subject: [PATCH 143/214] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/coderising/ood/srp/PromotionMail.java | 6 +++--- .../java/com/coderising/ood/srp/config/Configuration.java | 8 +++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java index ec54c4ce8d..6e3d09fcb3 100644 --- a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -18,12 +18,12 @@ public class PromotionMail { private ProductInfo productInfo; private List mailInfoList = new ArrayList<>(); - private static final String NAME_KEY = "NAME"; - private static final String EMAIL_KEY = "EMAIL"; + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; public PromotionMail(){} + public PromotionMail(ProductInfo productInfo) throws Exception { - //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 this.productInfo = productInfo; initMailInfoList(loadMailingList()); } diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/config/Configuration.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/config/Configuration.java index 7d2169eff8..931e93ab3b 100644 --- a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/config/Configuration.java +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/config/Configuration.java @@ -15,19 +15,17 @@ public class Configuration { static Map configurations = new HashMap<>(); static{ - configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); - configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); - configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + configurations.put(ConfigurationKeys.SMTP_SERVER , "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN , "admin@company.com"); } - /** * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 * @param key * @return */ public String getProperty(String key) { - return configurations.get(key); } } From 6dd917cccf012c8f1c618a571b16df8dd6959bad Mon Sep 17 00:00:00 2001 From: peng Date: Sat, 17 Jun 2017 11:52:18 +0800 Subject: [PATCH 144/214] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/coderising/ood/srp/utils/DBUtil.java | 7 ++++++- .../main/java/com/coderising/ood/srp/utils/FileUtil.java | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/DBUtil.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/DBUtil.java index 734590b4cf..2b4e0410ac 100644 --- a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/DBUtil.java +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/DBUtil.java @@ -12,7 +12,12 @@ * @package com.coderising.ood.srp * @date 2017/6/12/23:32 */ -public class DBUtil { +public final class DBUtil { + + private DBUtil(){ + throw new RuntimeException("illegal called!"); + } + /** * 应该从数据库读, 但是简化为直接生成。 * @param sql diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/FileUtil.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/FileUtil.java index 807bc48727..e833bf786d 100644 --- a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/FileUtil.java +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/FileUtil.java @@ -18,6 +18,9 @@ */ public final class FileUtil { + private FileUtil(){ + throw new RuntimeException("illegal called!"); + } public static List readProductInfoFromFile(String filePath) throws IOException{ From 9ecc0e678e8891d6a848c5495baebcd75ce3fe1f Mon Sep 17 00:00:00 2001 From: peng Date: Sat, 17 Jun 2017 11:53:33 +0800 Subject: [PATCH 145/214] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/coderising/ood/srp/utils/FileUtil.java | 1 - .../src/main/java/com/coderising/ood/srp/utils/MailUtil.java | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/FileUtil.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/FileUtil.java index e833bf786d..83b10391e2 100644 --- a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/FileUtil.java +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/FileUtil.java @@ -23,7 +23,6 @@ private FileUtil(){ } public static List readProductInfoFromFile(String filePath) throws IOException{ - File file = new File(filePath); BufferedReader br = null; List productInfoList = new ArrayList<>(); diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/MailUtil.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/MailUtil.java index 06b0606fc0..cff76d7bf0 100644 --- a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/MailUtil.java +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/MailUtil.java @@ -15,11 +15,9 @@ public final class MailUtil { private static String smtpHost = null; private static String altSmtpHost = null; private static String fromAddress = null; - private static Configuration config; - static{ - config = new Configuration(); + Configuration config = new Configuration(); smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); From 9f2285ef1c34f32a90492692881a95f09a50d4d7 Mon Sep 17 00:00:00 2001 From: lorcx <740707954@qq.com> Date: Sat, 17 Jun 2017 12:01:45 +0800 Subject: [PATCH 146/214] =?UTF-8?q?2017=E5=B9=B46=E6=9C=8817=E6=97=A5=2012?= =?UTF-8?q?:01:42?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/740707954/readMe | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/students/740707954/readMe b/students/740707954/readMe index a930ed0c81..f831579269 100644 --- a/students/740707954/readMe +++ b/students/740707954/readMe @@ -1 +1,2 @@ -TEST1 \ No newline at end of file +TEST1 +2017.06.17 \ No newline at end of file From 0f2dcc52965bbec38fb94e0c3386987374361151 Mon Sep 17 00:00:00 2001 From: Wen Date: Fri, 16 Jun 2017 21:04:27 -0700 Subject: [PATCH 147/214] =?UTF-8?q?1417442485=20Wen=20Zhao=20=E7=AC=AC?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E4=BD=9C=E4=B8=9A=20=E8=87=AA=E8=8D=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coderising/ood/srp/product_promotion.txt | 4 ++ students/1417442485/pom.xml | 32 ++++++++++++++++ .../com/coderising/ood/srp/Configuration.java | 23 ++++++++++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 +++++ .../java/com/coderising/ood/srp/DBUtil.java | 20 ++++++++++ .../java/com/coderising/ood/srp/FileUtil.java | 25 +++++++++++++ .../com/coderising/ood/srp/MailMessage.java | 9 +++++ .../com/coderising/ood/srp/MailService.java | 19 ++++++++++ .../com/coderising/ood/srp/MailSetting.java | 11 ++++++ .../java/com/coderising/ood/srp/MailUtil.java | 18 +++++++++ .../java/com/coderising/ood/srp/Product.java | 11 ++++++ .../coderising/ood/srp/ProductDataStore.java | 30 +++++++++++++++ .../com/coderising/ood/srp/PromotionMail.java | 32 ++++++++++++++++ .../ood/srp/PromotionMailMessage.java | 37 +++++++++++++++++++ .../com/coderising/ood/srp/UserDataStore.java | 15 ++++++++ .../java/com/coderising/ood/srp/UserInfo.java | 11 ++++++ .../coderising/ood/srp/product_promotion.txt | 4 ++ 17 files changed, 310 insertions(+) create mode 100644 students/1417442485/out/production/1417442485/main/java/com/coderising/ood/srp/product_promotion.txt create mode 100644 students/1417442485/pom.xml create mode 100644 students/1417442485/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/1417442485/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/1417442485/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/1417442485/src/main/java/com/coderising/ood/srp/FileUtil.java create mode 100644 students/1417442485/src/main/java/com/coderising/ood/srp/MailMessage.java create mode 100644 students/1417442485/src/main/java/com/coderising/ood/srp/MailService.java create mode 100644 students/1417442485/src/main/java/com/coderising/ood/srp/MailSetting.java create mode 100644 students/1417442485/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/1417442485/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/1417442485/src/main/java/com/coderising/ood/srp/ProductDataStore.java create mode 100644 students/1417442485/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/1417442485/src/main/java/com/coderising/ood/srp/PromotionMailMessage.java create mode 100644 students/1417442485/src/main/java/com/coderising/ood/srp/UserDataStore.java create mode 100644 students/1417442485/src/main/java/com/coderising/ood/srp/UserInfo.java create mode 100644 students/1417442485/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/1417442485/out/production/1417442485/main/java/com/coderising/ood/srp/product_promotion.txt b/students/1417442485/out/production/1417442485/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/1417442485/out/production/1417442485/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/1417442485/pom.xml b/students/1417442485/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/1417442485/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/1417442485/src/main/java/com/coderising/ood/srp/Configuration.java b/students/1417442485/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..70b861717d --- /dev/null +++ b/students/1417442485/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package main.java.com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + private static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public static String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/1417442485/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/1417442485/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..e63eb922a4 --- /dev/null +++ b/students/1417442485/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package main.java.com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/1417442485/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/1417442485/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..c34751d344 --- /dev/null +++ b/students/1417442485/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,20 @@ +package main.java.com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(final String sql){ + final List userList = new ArrayList<>(); + for (int i = 1; i <= 3; i++) { + userList.add(new UserInfo("User" + i, "aa@bb.com")); + } + return userList; + } +} diff --git a/students/1417442485/src/main/java/com/coderising/ood/srp/FileUtil.java b/students/1417442485/src/main/java/com/coderising/ood/srp/FileUtil.java new file mode 100644 index 0000000000..aa288e2795 --- /dev/null +++ b/students/1417442485/src/main/java/com/coderising/ood/srp/FileUtil.java @@ -0,0 +1,25 @@ +package main.java.com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class FileUtil { + + public static String[] readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + String[] data; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + data = temp.split(" "); + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + return data; + } +} diff --git a/students/1417442485/src/main/java/com/coderising/ood/srp/MailMessage.java b/students/1417442485/src/main/java/com/coderising/ood/srp/MailMessage.java new file mode 100644 index 0000000000..7e499b7d64 --- /dev/null +++ b/students/1417442485/src/main/java/com/coderising/ood/srp/MailMessage.java @@ -0,0 +1,9 @@ +package main.java.com.coderising.ood.srp; + +public interface MailMessage { + + String getFromAddress(); + String getToAddress(); + String getSubject(); + String getMessageBody(); +} diff --git a/students/1417442485/src/main/java/com/coderising/ood/srp/MailService.java b/students/1417442485/src/main/java/com/coderising/ood/srp/MailService.java new file mode 100644 index 0000000000..2b8bdf73ef --- /dev/null +++ b/students/1417442485/src/main/java/com/coderising/ood/srp/MailService.java @@ -0,0 +1,19 @@ +package main.java.com.coderising.ood.srp; + +public class MailService { + + public static void send(final MailMessage message, final MailSetting mailSetting) { + if(message.getToAddress().length() == 0) return; + try { + MailUtil.sendEmail(message.getToAddress(), message.getFromAddress(), message.getSubject(), + message.getMessageBody(), MailSetting.smtpHost, mailSetting.emailDebug); + } catch (Exception e) { + try { + MailUtil.sendEmail(message.getToAddress(), message.getFromAddress(), message.getSubject(), + message.getMessageBody(), MailSetting.altSmtpHost, mailSetting.emailDebug); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } +} diff --git a/students/1417442485/src/main/java/com/coderising/ood/srp/MailSetting.java b/students/1417442485/src/main/java/com/coderising/ood/srp/MailSetting.java new file mode 100644 index 0000000000..0991bbae02 --- /dev/null +++ b/students/1417442485/src/main/java/com/coderising/ood/srp/MailSetting.java @@ -0,0 +1,11 @@ +package main.java.com.coderising.ood.srp; + +public class MailSetting { + static String smtpHost = Configuration.getProperty(ConfigurationKeys.SMTP_SERVER); + static String altSmtpHost = Configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + final boolean emailDebug; + + public MailSetting(boolean emailDebug) { + this.emailDebug = emailDebug; + } +} diff --git a/students/1417442485/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/1417442485/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..59cc73476e --- /dev/null +++ b/students/1417442485/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package main.java.com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/1417442485/src/main/java/com/coderising/ood/srp/Product.java b/students/1417442485/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..2e807c9d50 --- /dev/null +++ b/students/1417442485/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,11 @@ +package main.java.com.coderising.ood.srp; + +public class Product { + final String productId; + final String productDesc; + + public Product(String productId, String productDesc) { + this.productId = productId; + this.productDesc = productDesc; + } +} diff --git a/students/1417442485/src/main/java/com/coderising/ood/srp/ProductDataStore.java b/students/1417442485/src/main/java/com/coderising/ood/srp/ProductDataStore.java new file mode 100644 index 0000000000..770baa7aff --- /dev/null +++ b/students/1417442485/src/main/java/com/coderising/ood/srp/ProductDataStore.java @@ -0,0 +1,30 @@ +package main.java.com.coderising.ood.srp; + +import java.io.File; +import java.io.IOException; + +public class ProductDataStore { + + private final File mFileDataSource; + + public ProductDataStore(final File file) { + mFileDataSource = file; + } + + public Product getProduct() { + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + String[] data = null; + try { + data = FileUtil.readFile(mFileDataSource); + } catch (IOException e) { + e.printStackTrace(); + } + Product product = null; + if(data != null && data.length >= 2) { + product = new Product(data[0], data[1]); + System.out.println("产品ID = " + product.productId + "\n"); + System.out.println("产品描述 = " + product.productDesc + "\n"); + } + return product; + } +} diff --git a/students/1417442485/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/1417442485/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..881bdb0f7c --- /dev/null +++ b/students/1417442485/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,32 @@ +package main.java.com.coderising.ood.srp; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +public class PromotionMail { + + private static final String PRODUCT_FILE = "src/main/java/com/coderising/ood/srp/product_promotion.txt"; + + public static void main(String[] args) throws Exception { + + final Product product = new ProductDataStore(new File(PRODUCT_FILE)).getProduct(); + final List userList = UserDataStore.getMailingList(product.productId); + final MailSetting mailSetting = new MailSetting(false); + + PromotionMail.sendEmails(product, userList, mailSetting); + } + + protected static void sendEmails(final Product product, final List userList, final MailSetting mailSetting) throws IOException + { + if(userList == null || userList.isEmpty()) { + System.out.println("没有邮件发送"); + return; + } + System.out.println("开始发送邮件"); + for (UserInfo userInfo : userList) { + final PromotionMailMessage message = new PromotionMailMessage(userInfo.email, userInfo.name, product.productDesc); + MailService.send(message, mailSetting); + } + } +} diff --git a/students/1417442485/src/main/java/com/coderising/ood/srp/PromotionMailMessage.java b/students/1417442485/src/main/java/com/coderising/ood/srp/PromotionMailMessage.java new file mode 100644 index 0000000000..e6f1548559 --- /dev/null +++ b/students/1417442485/src/main/java/com/coderising/ood/srp/PromotionMailMessage.java @@ -0,0 +1,37 @@ +package main.java.com.coderising.ood.srp; + +public class PromotionMailMessage implements MailMessage { + private final static String FROM_ADDRESS = Configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN); + private final static String SUBJECT = "您关注的产品降价了"; + private final String toAddress; + private final String message; + + public PromotionMailMessage(final String toAddress, final String userName, final String productDesc){ + this.toAddress = toAddress; + if (toAddress.length() > 0) { + message = "尊敬的 "+userName+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + } else { + message = null; + } + } + + @Override + public String getFromAddress() { + return FROM_ADDRESS; + } + + @Override + public String getToAddress() { + return toAddress; + } + + @Override + public String getSubject() { + return SUBJECT; + } + + @Override + public String getMessageBody() { + return message; + } +} diff --git a/students/1417442485/src/main/java/com/coderising/ood/srp/UserDataStore.java b/students/1417442485/src/main/java/com/coderising/ood/srp/UserDataStore.java new file mode 100644 index 0000000000..b62d9b0072 --- /dev/null +++ b/students/1417442485/src/main/java/com/coderising/ood/srp/UserDataStore.java @@ -0,0 +1,15 @@ +package main.java.com.coderising.ood.srp; + +import java.util.List; + +public class UserDataStore { + + public static List getMailingList(String productId) throws Exception { + final String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productId +"' " + + "and send_mail=1 "; + + return DBUtil.query(sendMailQuery); + } + +} diff --git a/students/1417442485/src/main/java/com/coderising/ood/srp/UserInfo.java b/students/1417442485/src/main/java/com/coderising/ood/srp/UserInfo.java new file mode 100644 index 0000000000..26272ef017 --- /dev/null +++ b/students/1417442485/src/main/java/com/coderising/ood/srp/UserInfo.java @@ -0,0 +1,11 @@ +package main.java.com.coderising.ood.srp; + +public class UserInfo { + final String name; + final String email; + + public UserInfo(final String name, final String email) { + this.name = name; + this.email = email; + } +} diff --git a/students/1417442485/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/1417442485/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/1417442485/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 68bbd3da94f3d1f325eea28543cbb8142ce42ca3 Mon Sep 17 00:00:00 2001 From: peng Date: Sat, 17 Jun 2017 12:09:44 +0800 Subject: [PATCH 148/214] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coderising/ood/srp/PromotionMail.java | 5 ++-- .../coderising/ood/srp/utils/ArgsUtil.java | 28 +++++++++++++++++++ .../coderising/ood/srp/utils/FileUtil.java | 9 +++--- 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/ArgsUtil.java diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java index 6e3d09fcb3..0be9b165c9 100644 --- a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -2,6 +2,7 @@ import com.coderising.ood.srp.utils.DBUtil; import com.coderising.ood.srp.utils.MailUtil; +import com.coderising.ood.srp.utils.ArgsUtil; import java.io.IOException; import java.util.*; @@ -45,7 +46,7 @@ private List> loadMailingList() throws Exception { * @param mailingList */ private void initMailInfoList(List> mailingList) { - if (mailingList!=null && !mailingList.isEmpty()){ + if (ArgsUtil.isNotEmpty(mailingList)){ for (Map map : mailingList){ // 初始化 mailInfoList mailInfoList.add(buildMailInfo(map)); @@ -73,7 +74,7 @@ private MailInfo buildMailInfo(Map userInfo){ */ public void sendEMails(boolean debug) throws IOException { System.out.println("开始发送邮件... ..."); - if (mailInfoList!=null && !mailInfoList.isEmpty()) { + if (ArgsUtil.isNotEmpty(mailInfoList)) { for (MailInfo mailInfo : mailInfoList){ MailUtil.sendEmail(mailInfo.toAddress, mailInfo.subject, mailInfo.message, debug); } diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/ArgsUtil.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/ArgsUtil.java new file mode 100644 index 0000000000..b329a9ade4 --- /dev/null +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/ArgsUtil.java @@ -0,0 +1,28 @@ +package com.coderising.ood.srp.utils; + +import java.util.List; + +/** + * ArgsUtil + * + * @author Chenpz + * @package com.coderising.ood.srp.utils + * @date 2017/6/17/11:56 + */ +public final class ArgsUtil { + + private ArgsUtil(){ + throw new RuntimeException("illegal called!"); + } + + public static boolean isNotNull(Object object){ + + return object != null; + } + + public static boolean isNotEmpty(List list){ + + return isNotNull(list) && !list.isEmpty(); + } + +} diff --git a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/FileUtil.java b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/FileUtil.java index 83b10391e2..43af9df5e4 100644 --- a/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/FileUtil.java +++ b/students/1395844061/course-pro-1/src/main/java/com/coderising/ood/srp/utils/FileUtil.java @@ -2,10 +2,7 @@ import com.coderising.ood.srp.ProductInfo; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; +import java.io.*; import java.util.ArrayList; import java.util.List; @@ -23,6 +20,10 @@ private FileUtil(){ } public static List readProductInfoFromFile(String filePath) throws IOException{ + + if (!ArgsUtil.isNotNull(filePath)) + throw new IllegalArgumentException("illegal arguments"); + File file = new File(filePath); BufferedReader br = null; List productInfoList = new ArrayList<>(); From efa38578b9b4aa98375624204b4df57b2c1d6170 Mon Sep 17 00:00:00 2001 From: peng Date: Sat, 17 Jun 2017 12:17:39 +0800 Subject: [PATCH 149/214] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B7=A5=E7=A8=8B?= =?UTF-8?q?=E6=8F=8F=E8=BF=B0=E5=92=8C=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/1395844061/README.md | 3 ++- .../src/test/java/com/coderising/ood/srp/MainTest.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/students/1395844061/README.md b/students/1395844061/README.md index 8b13789179..32243de0c9 100644 --- a/students/1395844061/README.md +++ b/students/1395844061/README.md @@ -1 +1,2 @@ - +### 1395844061 ood +1. ood代码优化 diff --git a/students/1395844061/course-pro-1/src/test/java/com/coderising/ood/srp/MainTest.java b/students/1395844061/course-pro-1/src/test/java/com/coderising/ood/srp/MainTest.java index 9c80f94619..2bf0df1070 100644 --- a/students/1395844061/course-pro-1/src/test/java/com/coderising/ood/srp/MainTest.java +++ b/students/1395844061/course-pro-1/src/test/java/com/coderising/ood/srp/MainTest.java @@ -1,5 +1,6 @@ package com.coderising.ood.srp; +import com.coderising.ood.srp.utils.ArgsUtil; import com.coderising.ood.srp.utils.FileUtil; import java.util.List; @@ -16,7 +17,7 @@ public class MainTest { public static void main(String[] args) throws Exception{ String filePath = MainTest.class.getClassLoader().getResource("product_promotion.txt").getFile(); List productInfoList = FileUtil.readProductInfoFromFile(filePath); - if (productInfoList != null && !productInfoList.isEmpty()){ + if (ArgsUtil.isNotEmpty(productInfoList)){ for (ProductInfo productInfo: productInfoList){ new PromotionMail(productInfo) .sendEMails(false); From 8ad22887a209da5df8e1d4c7d382e362c4c72da1 Mon Sep 17 00:00:00 2001 From: Yangming Xie Date: Sat, 17 Jun 2017 01:50:40 -0400 Subject: [PATCH 150/214] optimize --- .../ood-assignment/product_promotion.txt | 4 + .../java/com/coderising/ood/srp/Mail.java | 74 ++++--------- .../java/com/coderising/ood/srp/Product.java | 12 +-- .../com/coderising/ood/srp/PromotionMail.java | 101 +++++++++++++----- 4 files changed, 101 insertions(+), 90 deletions(-) create mode 100644 students/702282822/ood-assignment/product_promotion.txt diff --git a/students/702282822/ood-assignment/product_promotion.txt b/students/702282822/ood-assignment/product_promotion.txt new file mode 100644 index 0000000000..a98917f829 --- /dev/null +++ b/students/702282822/ood-assignment/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo R15 +P4955 Vivo X20 \ No newline at end of file diff --git a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java index 9773a990ba..c7c6bc3ba3 100644 --- a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java +++ b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java @@ -8,25 +8,22 @@ public abstract class Mail { - private String smtpHost = null; - private String altSmtpHost = null; - private String fromAddress = null; - private String toAddress = null; + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; protected String subject = null; protected String message = null; protected String sendMailQuery = null; - public Mail(File file, Boolean mailDebug) throws Exception + public Mail(File file) throws Exception { readFile(file); setSMTPHost(); setAltSMTPHost(); - setFromAddress(); - - setSendMailQuery(); - List mailingList = loadMailingList(); - sendEMails(mailDebug, mailingList); + setFromAddress(); + } protected abstract void readFile(File fie) throws IOException; @@ -48,7 +45,7 @@ protected void setFromAddress() fromAddress = Configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN); } - protected abstract void setSendMailQuery() throws Exception; + protected List loadMailingList() throws Exception { //user information, name and email address @@ -56,65 +53,30 @@ protected List loadMailingList() throws Exception { //user information, name a } - protected abstract void setMessage(String name) throws IOException; - - - + //protected abstract void setMessage(String name) throws IOException; protected void setToAddress(HashMap userInfo){ toAddress = (String) userInfo.get(Configuration.EMAIL_KEY); } - protected void configureEMail(HashMap userInfo) throws IOException - { - if (toAddress.length() > 0) - setMessage((String)userInfo.get(Configuration.NAME_KEY)); - } + + protected abstract void emailProcessing(List mailingList, boolean debug) throws IOException, Exception; - protected void sendEMails(boolean debug, List mailingList) throws IOException + protected void sendEMails(boolean debug) throws Exception { - + + List mailingList = loadMailingList(); //persons + System.out.println("开始发送邮件"); - - - if (mailingList != null) { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) { - HashMap userInfo = (HashMap) iter.next(); - setToAddress(userInfo); - configureEMail(userInfo); - try - { - if (toAddress.length() > 0) - sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } - catch (Exception e) - { - - try { - sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) - { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } - } - - - } - - else { - System.out.println("没有邮件发送"); - - } + emailProcessing(mailingList, debug); } public void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, - boolean debug) { + boolean debug) { + //假装发了一封邮件 StringBuilder buffer = new StringBuilder(); buffer.append("From:").append(fromAddress).append("\n"); diff --git a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java index 5d4fb8dce4..d12c24ddba 100644 --- a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java +++ b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -2,26 +2,26 @@ public class Product { - private static String productID; - private static String productDesc; + private String productID; + private String productDesc; - protected static void setProductID(String ID) + protected void setProductID(String ID) { productID = ID; } - protected static String getProductID() + protected String getProductID() { return productID; } - protected static void setProductDesc(String desc) { + protected void setProductDesc(String desc) { productDesc = desc; } - protected static String getProductDesc(){ + protected String getProductDesc(){ return productDesc; } diff --git a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index e126809ad6..22783ba987 100644 --- a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -4,42 +4,92 @@ import java.io.File; import java.io.FileReader; import java.io.IOException; -import java.io.Serializable; import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; public class PromotionMail extends Mail { //inheritance from mail + private LinkedList products; public static void main(String[] args) throws Exception { - File f = new File("\\product_promotion.txt"); + File f = new File("./product_promotion.txt"); boolean emailDebug = false; - PromotionMail pe = new PromotionMail(f, emailDebug); - + } - public PromotionMail(File file, boolean mailDebug) throws Exception { - super(file, mailDebug); + public PromotionMail(File file, boolean mailDebug) throws Exception + { + super(file); + products = new LinkedList<>(); + sendEMails(mailDebug); } - protected void setSendMailQuery() throws Exception { + protected void setSendMailQuery(Product pro) throws Exception { sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + Product.getProductID() +"' " + + "where product_id= '" + pro.getProductID() +"' " + "and send_mail=1 "; System.out.println("loadQuery set"); } - protected void setMessage(String name) throws IOException + protected void setMessage(String name, Product pro) throws IOException { subject = "您关注的产品降价了"; - message = "尊敬的 "+ name +", 您关注的产品 " + Product.getProductDesc() + " 降价了,欢迎购买!" ; + message = "尊敬的 "+ name +", 您关注的产品 " + pro.getProductDesc() + " 降价了,欢迎购买!" ; + } + + protected void emailProcessing(List mailingList, boolean debug) throws Exception + { + + for(Product pro : products){ + + setSendMailQuery(pro); + + if (mailingList != null) + { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) + { + HashMap userInfo = (HashMap) iter.next(); + setToAddress(userInfo); + if (toAddress.length() > 0) + setMessage((String)userInfo.get(Configuration.NAME_KEY), pro); + + try + { + if (toAddress.length() > 0) + sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + } + else { + System.out.println("没有邮件发送"); + } + + } + + } + + @Override @@ -48,15 +98,20 @@ protected void readFile(File file) throws IOException // @02C BufferedReader br = null; try { br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - Product.setProductID(data[0]); - Product.setProductDesc(data[1]); - - System.out.println("产品ID = " + Product.getProductID() + "\n"); - System.out.println("产品描述 = " + Product.getProductDesc() + "\n"); + String sCurrentLine = ""; + while ((sCurrentLine = br.readLine()) != null) + { + Product prod = new Product(); + String[] data = sCurrentLine.split(" "); + prod.setProductID(data[0]); + prod.setProductDesc(data[1]); + + System.out.println("产品ID = " + prod.getProductID() + "\n"); + System.out.println("产品描述 = " + prod.getProductDesc() + "\n"); + products.add(prod); + } + } catch (IOException e) { throw new IOException(e.getMessage()); } finally { @@ -64,14 +119,4 @@ protected void readFile(File file) throws IOException // @02C } } - - - - - - - - - - } From c7e82030836c9f6a40876ac6cd525b9a6b1de579 Mon Sep 17 00:00:00 2001 From: ThomasChant Date: Sat, 17 Jun 2017 14:14:46 +0800 Subject: [PATCH 151/214] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E5=91=A8=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/494800949/pom.xml | 12 ++ .../ood/assignment/srp/Configuration.java | 24 +++ .../ood/assignment/srp/ConfigurationKeys.java | 9 + .../main/java/ood/assignment/srp/DBUtil.java | 25 +++ .../java/ood/assignment/srp/MailUtil.java | 19 ++ .../ood/assignment/srp/PromotionMail.java | 198 ++++++++++++++++++ .../ood/assignment/srp/product_promotion.txt | 4 + .../main/java/ood/work/srp/Configuration.java | 24 +++ .../java/ood/work/srp/ConfigurationKeys.java | 9 + .../src/main/java/ood/work/srp/DBUtil.java | 24 +++ .../src/main/java/ood/work/srp/Email.java | 41 ++++ .../src/main/java/ood/work/srp/Emails.java | 38 ++++ .../src/main/java/ood/work/srp/MailUtil.java | 24 +++ .../src/main/java/ood/work/srp/Product.java | 26 +++ .../java/ood/work/srp/ProductInfoLoader.java | 35 ++++ .../main/java/ood/work/srp/PromotionMail.java | 32 +++ .../main/java/ood/work/srp/SMTPClient.java | 71 +++++++ .../src/main/java/ood/work/srp/User.java | 25 +++ 18 files changed, 640 insertions(+) create mode 100644 students/494800949/pom.xml create mode 100644 students/494800949/src/main/java/ood/assignment/srp/Configuration.java create mode 100644 students/494800949/src/main/java/ood/assignment/srp/ConfigurationKeys.java create mode 100644 students/494800949/src/main/java/ood/assignment/srp/DBUtil.java create mode 100644 students/494800949/src/main/java/ood/assignment/srp/MailUtil.java create mode 100644 students/494800949/src/main/java/ood/assignment/srp/PromotionMail.java create mode 100644 students/494800949/src/main/java/ood/assignment/srp/product_promotion.txt create mode 100644 students/494800949/src/main/java/ood/work/srp/Configuration.java create mode 100644 students/494800949/src/main/java/ood/work/srp/ConfigurationKeys.java create mode 100644 students/494800949/src/main/java/ood/work/srp/DBUtil.java create mode 100644 students/494800949/src/main/java/ood/work/srp/Email.java create mode 100644 students/494800949/src/main/java/ood/work/srp/Emails.java create mode 100644 students/494800949/src/main/java/ood/work/srp/MailUtil.java create mode 100644 students/494800949/src/main/java/ood/work/srp/Product.java create mode 100644 students/494800949/src/main/java/ood/work/srp/ProductInfoLoader.java create mode 100644 students/494800949/src/main/java/ood/work/srp/PromotionMail.java create mode 100644 students/494800949/src/main/java/ood/work/srp/SMTPClient.java create mode 100644 students/494800949/src/main/java/ood/work/srp/User.java diff --git a/students/494800949/pom.xml b/students/494800949/pom.xml new file mode 100644 index 0000000000..521e154bc6 --- /dev/null +++ b/students/494800949/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + season2 + season2 + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/students/494800949/src/main/java/ood/assignment/srp/Configuration.java b/students/494800949/src/main/java/ood/assignment/srp/Configuration.java new file mode 100644 index 0000000000..858e860697 --- /dev/null +++ b/students/494800949/src/main/java/ood/assignment/srp/Configuration.java @@ -0,0 +1,24 @@ +package ood.assignment.srp; + +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/494800949/src/main/java/ood/assignment/srp/ConfigurationKeys.java b/students/494800949/src/main/java/ood/assignment/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..c41514a2fb --- /dev/null +++ b/students/494800949/src/main/java/ood/assignment/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package ood.assignment.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/494800949/src/main/java/ood/assignment/srp/DBUtil.java b/students/494800949/src/main/java/ood/assignment/srp/DBUtil.java new file mode 100644 index 0000000000..26ba6bb524 --- /dev/null +++ b/students/494800949/src/main/java/ood/assignment/srp/DBUtil.java @@ -0,0 +1,25 @@ +package ood.assignment.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/494800949/src/main/java/ood/assignment/srp/MailUtil.java b/students/494800949/src/main/java/ood/assignment/srp/MailUtil.java new file mode 100644 index 0000000000..03aca56c16 --- /dev/null +++ b/students/494800949/src/main/java/ood/assignment/srp/MailUtil.java @@ -0,0 +1,19 @@ +package ood.assignment.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + + +} diff --git a/students/494800949/src/main/java/ood/assignment/srp/PromotionMail.java b/students/494800949/src/main/java/ood/assignment/srp/PromotionMail.java new file mode 100644 index 0000000000..326f56a3d5 --- /dev/null +++ b/students/494800949/src/main/java/ood/assignment/srp/PromotionMail.java @@ -0,0 +1,198 @@ +package ood.assignment.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("I:\\sourceCode\\coding2017_2\\students\\494800949\\src\\main\\java\\ood\\assignment\\srp\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/494800949/src/main/java/ood/assignment/srp/product_promotion.txt b/students/494800949/src/main/java/ood/assignment/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/494800949/src/main/java/ood/assignment/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/494800949/src/main/java/ood/work/srp/Configuration.java b/students/494800949/src/main/java/ood/work/srp/Configuration.java new file mode 100644 index 0000000000..878ae499c2 --- /dev/null +++ b/students/494800949/src/main/java/ood/work/srp/Configuration.java @@ -0,0 +1,24 @@ +package ood.work.srp; + +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/494800949/src/main/java/ood/work/srp/ConfigurationKeys.java b/students/494800949/src/main/java/ood/work/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..6127d61f88 --- /dev/null +++ b/students/494800949/src/main/java/ood/work/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package ood.work.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/494800949/src/main/java/ood/work/srp/DBUtil.java b/students/494800949/src/main/java/ood/work/srp/DBUtil.java new file mode 100644 index 0000000000..48f108e054 --- /dev/null +++ b/students/494800949/src/main/java/ood/work/srp/DBUtil.java @@ -0,0 +1,24 @@ +package ood.work.srp; +import java.util.ArrayList; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + User userInfo = new User(); + userInfo.setName("User" + i); + userInfo.setEmail("aa"+ i +"@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/494800949/src/main/java/ood/work/srp/Email.java b/students/494800949/src/main/java/ood/work/srp/Email.java new file mode 100644 index 0000000000..2d3c4a4995 --- /dev/null +++ b/students/494800949/src/main/java/ood/work/srp/Email.java @@ -0,0 +1,41 @@ +package ood.work.srp; + +/** + * Created by Administrator on 2017/6/17 0017. + */ +public class Email { + + private String toAddress; + private String subject; + private String message; + + public Email(String toAddress, String subject, String message) { + this.toAddress = toAddress; + this.subject = subject; + this.message = message; + } + + public String getToAddress() { + return toAddress; + } + + public String getSubject() { + return subject; + } + + public String getMessage() { + return message; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/students/494800949/src/main/java/ood/work/srp/Emails.java b/students/494800949/src/main/java/ood/work/srp/Emails.java new file mode 100644 index 0000000000..768e513c88 --- /dev/null +++ b/students/494800949/src/main/java/ood/work/srp/Emails.java @@ -0,0 +1,38 @@ +package ood.work.srp; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Administrator on 2017/6/17 0017. + */ +public class Emails { + + public static Email newEmail(User user, Product product) { + String subject = "您关注的产品降价了"; + String message = "尊敬的 "+user.getName()+", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!" ; + return new Email(user.getEmail(), subject, message); + } + + public static List createEmails(String path) throws IOException { + List products = ProductInfoLoader.readFile(path); + List mails = new ArrayList<>(); + for (Product product : products) { + String sql = getSql(product.getProductId()); + List users = DBUtil.query(sql); + for (User user : users) { + Email mail = Emails.newEmail(user, product); + mails.add(mail); + } + } + return mails; + } + + private static String getSql(String productId) { + System.out.println("loadQuery set"); + return "Select name from subscriptions " + + "where product_id= '" + productId +"' " + + "and send_mail=1 "; + } +} diff --git a/students/494800949/src/main/java/ood/work/srp/MailUtil.java b/students/494800949/src/main/java/ood/work/srp/MailUtil.java new file mode 100644 index 0000000000..43ab9a4339 --- /dev/null +++ b/students/494800949/src/main/java/ood/work/srp/MailUtil.java @@ -0,0 +1,24 @@ +package ood.work.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + public static void sendEmail(Email mailInfo, String fromAddress, String smtphost, boolean debug) { + String toAddress = mailInfo.getToAddress(); + String subject = mailInfo.getSubject(); + String message = mailInfo.getMessage(); + sendEmail(toAddress, fromAddress, subject, message, smtphost, debug); + } + +} diff --git a/students/494800949/src/main/java/ood/work/srp/Product.java b/students/494800949/src/main/java/ood/work/srp/Product.java new file mode 100644 index 0000000000..b3b4400014 --- /dev/null +++ b/students/494800949/src/main/java/ood/work/srp/Product.java @@ -0,0 +1,26 @@ +package ood.work.srp; + +/** + * Created by Administrator on 2017/6/17 0017. + * 商品类 + */ +public class Product { + private String productId; + private String productDesc; + + public String getProductId() { + return productId; + } + + public void setProductId(String productId) { + this.productId = productId; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } +} diff --git a/students/494800949/src/main/java/ood/work/srp/ProductInfoLoader.java b/students/494800949/src/main/java/ood/work/srp/ProductInfoLoader.java new file mode 100644 index 0000000000..8a484e9524 --- /dev/null +++ b/students/494800949/src/main/java/ood/work/srp/ProductInfoLoader.java @@ -0,0 +1,35 @@ +package ood.work.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Administrator on 2017/6/17 0017. + */ +public class ProductInfoLoader { + + public static List readFile(String path) throws IOException // @02C + { + File file = new File(path); + List products = new ArrayList<>(); + try(BufferedReader br = new BufferedReader(new FileReader(file))) { + String line; + while ((line = br.readLine()) != null) { + String[] data = line.split(" "); + Product product = new Product(); + product.setProductId(data[0]); + product.setProductDesc(data[1]); + products.add(product); + System.out.println("产品ID = " + product.getProductId() + "\n"); + System.out.println("产品描述 = " + product.getProductDesc() + "\n"); + } + } catch (IOException e) { + throw new IOException(e.getMessage()); + } + return products; + } +} diff --git a/students/494800949/src/main/java/ood/work/srp/PromotionMail.java b/students/494800949/src/main/java/ood/work/srp/PromotionMail.java new file mode 100644 index 0000000000..fb9e5e0774 --- /dev/null +++ b/students/494800949/src/main/java/ood/work/srp/PromotionMail.java @@ -0,0 +1,32 @@ +package ood.work.srp; + +import java.io.IOException; + +/** + * Created by Administrator on 2017/6/17 0017. + */ +public class PromotionMail { + + private String path; + + public PromotionMail(String path) { + this.path = path; + } + + public void execute() throws IOException { + SMTPClient client = new SMTPClient(); + client.sendEmails(false, Emails.createEmails(path)); + } + + + + public static void main(String[] args) { + String path = "I:\\sourceCode\\coding2017_2\\students\\494800949\\src\\main\\java\\ood\\assignment\\srp\\product_promotion.txt"; + PromotionMail mail = new PromotionMail(path); + try { + mail.execute(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } +} diff --git a/students/494800949/src/main/java/ood/work/srp/SMTPClient.java b/students/494800949/src/main/java/ood/work/srp/SMTPClient.java new file mode 100644 index 0000000000..dda491e029 --- /dev/null +++ b/students/494800949/src/main/java/ood/work/srp/SMTPClient.java @@ -0,0 +1,71 @@ +package ood.work.srp; + +import java.util.Iterator; +import java.util.List; + +/** + * Created by Administrator on 2017/6/17 0017. + * + */ +public class SMTPClient { + + private String smtpHost; + private String altSmtpHost; + private String emailAdmin; + + public void config() { + Configuration configuration = new Configuration(); + this.smtpHost = configuration.getProperty(ConfigurationKeys.SMTP_SERVER); + this.altSmtpHost = configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + this.emailAdmin = configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + public SMTPClient() { + config(); + } + + /** + * 发送一封邮件 + * @param debug + * @param mailInfo + */ + public void sendEmail(boolean debug, Email mailInfo) { + try + { + if (mailInfo.getToAddress().length() > 0) + MailUtil.sendEmail(mailInfo, emailAdmin, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(mailInfo, emailAdmin, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + + /** + * 发送多封邮件 + */ + + public void sendEmails(boolean debug, List mailingList){ + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + sendEmail(debug, iter.next()); + } + } + else { + System.out.println("没有邮件发送"); + } + } + + + +} diff --git a/students/494800949/src/main/java/ood/work/srp/User.java b/students/494800949/src/main/java/ood/work/srp/User.java new file mode 100644 index 0000000000..091f594199 --- /dev/null +++ b/students/494800949/src/main/java/ood/work/srp/User.java @@ -0,0 +1,25 @@ +package ood.work.srp; + +/** + * Created by Administrator on 2017/6/17 0017. + */ +public class User { + private String name; + private String email; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} From 5168c5602703b08a1dc7c4f7a8df4ec4d57feddd Mon Sep 17 00:00:00 2001 From: MIMIEYES Date: Sat, 17 Jun 2017 14:16:18 +0800 Subject: [PATCH 152/214] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/402246209/learning/pom.xml | 12 ++ .../mimieye/odd/srp/config/Configuration.java | 27 +++++ .../odd/srp/config/ConfigurationKeys.java | 11 ++ .../srp/controller/PromotionAbstractMail.java | 108 ++++++++++++++++++ .../odd/srp/controller/PromotionMail.java | 19 +++ .../com/mimieye/odd/srp/dao/UserInfoDAO.java | 13 +++ .../odd/srp/dao/impl/UserInfoDAOImpl.java | 20 ++++ .../odd/srp/main/PromotionEmailMain.java | 17 +++ .../odd/srp/service/PromotionInfoService.java | 13 +++ .../odd/srp/service/UserInfoService.java | 12 ++ .../impl/PromotionInfoServiceImpl.java | 41 +++++++ .../srp/service/impl/UserInfoServiceImpl.java | 24 ++++ .../java/com/mimieye/odd/srp/util/DBUtil.java | 25 ++++ .../mimieye/odd/srp/util/FileReadUtil.java | 42 +++++++ .../com/mimieye/odd/srp/util/MailUtil.java | 18 +++ .../mimieye/odd/srp/util/PropertiesUtil.java | 18 +++ .../src/main/resources/config.properties | 5 + .../src/main/resources/product_promotion.txt | 4 + 18 files changed, 429 insertions(+) create mode 100644 students/402246209/learning/pom.xml create mode 100644 students/402246209/learning/src/main/java/com/mimieye/odd/srp/config/Configuration.java create mode 100644 students/402246209/learning/src/main/java/com/mimieye/odd/srp/config/ConfigurationKeys.java create mode 100644 students/402246209/learning/src/main/java/com/mimieye/odd/srp/controller/PromotionAbstractMail.java create mode 100644 students/402246209/learning/src/main/java/com/mimieye/odd/srp/controller/PromotionMail.java create mode 100644 students/402246209/learning/src/main/java/com/mimieye/odd/srp/dao/UserInfoDAO.java create mode 100644 students/402246209/learning/src/main/java/com/mimieye/odd/srp/dao/impl/UserInfoDAOImpl.java create mode 100644 students/402246209/learning/src/main/java/com/mimieye/odd/srp/main/PromotionEmailMain.java create mode 100644 students/402246209/learning/src/main/java/com/mimieye/odd/srp/service/PromotionInfoService.java create mode 100644 students/402246209/learning/src/main/java/com/mimieye/odd/srp/service/UserInfoService.java create mode 100644 students/402246209/learning/src/main/java/com/mimieye/odd/srp/service/impl/PromotionInfoServiceImpl.java create mode 100644 students/402246209/learning/src/main/java/com/mimieye/odd/srp/service/impl/UserInfoServiceImpl.java create mode 100644 students/402246209/learning/src/main/java/com/mimieye/odd/srp/util/DBUtil.java create mode 100644 students/402246209/learning/src/main/java/com/mimieye/odd/srp/util/FileReadUtil.java create mode 100644 students/402246209/learning/src/main/java/com/mimieye/odd/srp/util/MailUtil.java create mode 100644 students/402246209/learning/src/main/java/com/mimieye/odd/srp/util/PropertiesUtil.java create mode 100644 students/402246209/learning/src/main/resources/config.properties create mode 100644 students/402246209/learning/src/main/resources/product_promotion.txt diff --git a/students/402246209/learning/pom.xml b/students/402246209/learning/pom.xml new file mode 100644 index 0000000000..23830a6621 --- /dev/null +++ b/students/402246209/learning/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + com.mimieye + learning + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/students/402246209/learning/src/main/java/com/mimieye/odd/srp/config/Configuration.java b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/config/Configuration.java new file mode 100644 index 0000000000..d4e3c6fe16 --- /dev/null +++ b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/config/Configuration.java @@ -0,0 +1,27 @@ +package com.mimieye.odd.srp.config; +import com.mimieye.odd.srp.util.PropertiesUtil; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +public class Configuration { + private static final String CONFIG_FILENAME = "config.properties"; + static Properties configurations = null; + static{ + try { + configurations = PropertiesUtil.getInstance(CONFIG_FILENAME); + } catch (Exception e) { + e.printStackTrace(); + } + } + /** + * @param key + * @return + */ + public String getProperty(String key) { + return configurations.getProperty(key); + } + +} diff --git a/students/402246209/learning/src/main/java/com/mimieye/odd/srp/config/ConfigurationKeys.java b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/config/ConfigurationKeys.java new file mode 100644 index 0000000000..7f1a264d0a --- /dev/null +++ b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/config/ConfigurationKeys.java @@ -0,0 +1,11 @@ +package com.mimieye.odd.srp.config; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + public static final String PROMOTION_FILEPATH = "promotion.filepath"; + public static final String EMAIL_DEBUG = "emailDebug"; + +} diff --git a/students/402246209/learning/src/main/java/com/mimieye/odd/srp/controller/PromotionAbstractMail.java b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/controller/PromotionAbstractMail.java new file mode 100644 index 0000000000..eb7544b4f1 --- /dev/null +++ b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/controller/PromotionAbstractMail.java @@ -0,0 +1,108 @@ +package com.mimieye.odd.srp.controller; + +import com.mimieye.odd.srp.config.Configuration; +import com.mimieye.odd.srp.config.ConfigurationKeys; +import com.mimieye.odd.srp.dao.impl.UserInfoDAOImpl; +import com.mimieye.odd.srp.service.PromotionInfoService; +import com.mimieye.odd.srp.service.UserInfoService; +import com.mimieye.odd.srp.service.impl.PromotionInfoServiceImpl; +import com.mimieye.odd.srp.service.impl.UserInfoServiceImpl; +import com.mimieye.odd.srp.util.MailUtil; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +public abstract class PromotionAbstractMail { + + protected static Configuration config; + protected static final String NAME_KEY = "NAME"; + protected static final String EMAIL_KEY = "EMAIL"; + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + protected String filePath; + protected boolean mailDebug; + protected List userInfos; + protected String productID; + protected String productDesc; + + public void init() throws Exception{ + config = new Configuration(); + setFilePath(config.getProperty(ConfigurationKeys.PROMOTION_FILEPATH)); + setMailDebug(Boolean.parseBoolean(ConfigurationKeys.EMAIL_DEBUG)); + setSMTPHost(); + setAltSMTPHost(); + setFromAddress(); + // 降价商品service + PromotionInfoService promotionInfoService = new PromotionInfoServiceImpl(); + // 邮件接收人service + UserInfoService userInfoService = new UserInfoServiceImpl(new UserInfoDAOImpl()); + // 获取第一条降价商品 + Map promotion = promotionInfoService.listPromotions(filePath).get(0); + setProductID(promotion.get("productID")); + setProductDesc(promotion.get("productDesc")); + // 获取邮件接收人 + setUserInfos(userInfoService.loadMailingList(productID)); + } + + + protected void configureEMail(HashMap userInfo) throws IOException { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected void sendEMails(boolean debug, List mailingList) throws IOException { + + System.out.println("开始发送邮件"); + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } catch (Exception e) { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + } else { + System.out.println("没有邮件发送"); + } + + } + + public void send() throws IOException { sendEMails(mailDebug, userInfos); } + + protected void setSMTPHost() { smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); } + + protected void setAltSMTPHost() { altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); } + + protected void setFromAddress() { fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); } + + protected void setFilePath(String filePath) { this.filePath = filePath; } + + protected void setMailDebug(boolean mailDebug) { this.mailDebug = mailDebug; } + + protected void setUserInfos(List userInfos) { this.userInfos = userInfos; } + + protected void setProductID(String productID) { this.productID = productID; } + + protected void setProductDesc(String productDesc) { this.productDesc = productDesc; } + + protected abstract void setMessage(HashMap userInfo) throws IOException ; +} diff --git a/students/402246209/learning/src/main/java/com/mimieye/odd/srp/controller/PromotionMail.java b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/controller/PromotionMail.java new file mode 100644 index 0000000000..983126e4db --- /dev/null +++ b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/controller/PromotionMail.java @@ -0,0 +1,19 @@ +package com.mimieye.odd.srp.controller; + +import java.io.IOException; +import java.util.HashMap; + +public class PromotionMail extends PromotionAbstractMail{ + + /** + * 自定义邮件内容 + * @param userInfo + * @throws IOException + */ + protected void setMessage(HashMap userInfo) throws IOException { + String name = (String) userInfo.get(NAME_KEY); + subject = "您关注的产品降价了"; + message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + } + +} diff --git a/students/402246209/learning/src/main/java/com/mimieye/odd/srp/dao/UserInfoDAO.java b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/dao/UserInfoDAO.java new file mode 100644 index 0000000000..00ef1860b2 --- /dev/null +++ b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/dao/UserInfoDAO.java @@ -0,0 +1,13 @@ +package com.mimieye.odd.srp.dao; + +import com.mimieye.odd.srp.util.DBUtil; + +import java.util.List; + +/** + * Created by Pierreluo on 2017/6/15. + */ +public interface UserInfoDAO { + + List loadMailingList(String productID) throws Exception; +} diff --git a/students/402246209/learning/src/main/java/com/mimieye/odd/srp/dao/impl/UserInfoDAOImpl.java b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/dao/impl/UserInfoDAOImpl.java new file mode 100644 index 0000000000..c59b4bde9e --- /dev/null +++ b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/dao/impl/UserInfoDAOImpl.java @@ -0,0 +1,20 @@ +package com.mimieye.odd.srp.dao.impl; + +import com.mimieye.odd.srp.dao.UserInfoDAO; +import com.mimieye.odd.srp.util.DBUtil; + +import java.util.List; + +/** + * Created by Pierreluo on 2017/6/15. + */ +public class UserInfoDAOImpl implements UserInfoDAO { + + public List loadMailingList(String productID) throws Exception { + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID + "' " + + "and send_mail=1 "; + System.out.println("loadQuery set"); + return DBUtil.query(sendMailQuery); + } +} diff --git a/students/402246209/learning/src/main/java/com/mimieye/odd/srp/main/PromotionEmailMain.java b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/main/PromotionEmailMain.java new file mode 100644 index 0000000000..4a6c4366b2 --- /dev/null +++ b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/main/PromotionEmailMain.java @@ -0,0 +1,17 @@ +package com.mimieye.odd.srp.main; + +import com.mimieye.odd.srp.controller.PromotionAbstractMail; +import com.mimieye.odd.srp.controller.PromotionMail; + +/** + * Created by Pierreluo on 2017/6/17. + */ +public class PromotionEmailMain { + public static void main(String[] args) throws Exception { + PromotionAbstractMail mail = new PromotionMail(); + // 初始化数据 + mail.init(); + // 发送邮件 + mail.send(); + } +} diff --git a/students/402246209/learning/src/main/java/com/mimieye/odd/srp/service/PromotionInfoService.java b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/service/PromotionInfoService.java new file mode 100644 index 0000000000..bfcb508100 --- /dev/null +++ b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/service/PromotionInfoService.java @@ -0,0 +1,13 @@ +package com.mimieye.odd.srp.service; + +import com.mimieye.odd.srp.util.FileReadUtil; + +import java.io.IOException; +import java.util.*; + +/** + * Created by Pierreluo on 2017/6/15. + */ +public interface PromotionInfoService { + List> listPromotions(String filePath) throws Exception; +} diff --git a/students/402246209/learning/src/main/java/com/mimieye/odd/srp/service/UserInfoService.java b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/service/UserInfoService.java new file mode 100644 index 0000000000..57747f298a --- /dev/null +++ b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/service/UserInfoService.java @@ -0,0 +1,12 @@ +package com.mimieye.odd.srp.service; + +import com.mimieye.odd.srp.util.DBUtil; + +import java.util.List; + +/** + * Created by Pierreluo on 2017/6/15. + */ +public interface UserInfoService { + List loadMailingList(String productID) throws Exception; +} diff --git a/students/402246209/learning/src/main/java/com/mimieye/odd/srp/service/impl/PromotionInfoServiceImpl.java b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/service/impl/PromotionInfoServiceImpl.java new file mode 100644 index 0000000000..20aa392ea2 --- /dev/null +++ b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/service/impl/PromotionInfoServiceImpl.java @@ -0,0 +1,41 @@ +package com.mimieye.odd.srp.service.impl; + +import com.mimieye.odd.srp.service.PromotionInfoService; +import com.mimieye.odd.srp.util.FileReadUtil; + +import java.io.IOException; +import java.util.*; + +/** + * Created by Pierreluo on 2017/6/15. + */ +public class PromotionInfoServiceImpl implements PromotionInfoService { + + @Override + public List> listPromotions(String filePath) throws Exception { + List> list = null; + List results = FileReadUtil.readFile(filePath); + if(results != null && results.size()>0){ + list = new ArrayList<>(); + String temp = null; + String[] data = null; + Map map = null; + Iterator iterator = results.iterator(); + int i=1; + while(iterator.hasNext()){ + temp = iterator.next(); + data = temp.split(" "); + map = new HashMap<>(); + map.put("productID",data[0]); + map.put("productDesc",data[1]); + list.add(map); + System.out.println("产品"+(i)+"ID = " + data[0] ); + System.out.println("产品"+(i++)+"描述 = " + data[1] + "\n"); + } + }else{ + throw new IOException("No Records."); + } + return list; + } + +} diff --git a/students/402246209/learning/src/main/java/com/mimieye/odd/srp/service/impl/UserInfoServiceImpl.java b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/service/impl/UserInfoServiceImpl.java new file mode 100644 index 0000000000..39d7613a61 --- /dev/null +++ b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/service/impl/UserInfoServiceImpl.java @@ -0,0 +1,24 @@ +package com.mimieye.odd.srp.service.impl; + +import com.mimieye.odd.srp.dao.UserInfoDAO; +import com.mimieye.odd.srp.service.UserInfoService; +import com.mimieye.odd.srp.util.DBUtil; + +import java.util.List; + +/** + * Created by Pierreluo on 2017/6/15. + */ +public class UserInfoServiceImpl implements UserInfoService { + + private UserInfoDAO dao; + + public UserInfoServiceImpl(){} + public UserInfoServiceImpl(UserInfoDAO dao){ + this.dao = dao; + } + + public List loadMailingList(String productID) throws Exception { + return dao.loadMailingList(productID); + } +} diff --git a/students/402246209/learning/src/main/java/com/mimieye/odd/srp/util/DBUtil.java b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/util/DBUtil.java new file mode 100644 index 0000000000..c3e16050f4 --- /dev/null +++ b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/util/DBUtil.java @@ -0,0 +1,25 @@ +package com.mimieye.odd.srp.util; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/402246209/learning/src/main/java/com/mimieye/odd/srp/util/FileReadUtil.java b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/util/FileReadUtil.java new file mode 100644 index 0000000000..0c0a8f58cf --- /dev/null +++ b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/util/FileReadUtil.java @@ -0,0 +1,42 @@ +package com.mimieye.odd.srp.util; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Pierreluo on 2017/6/13. + */ +public class FileReadUtil { + + public static List readFile(String fileName) throws IOException { + File file = new File(fileName); + BufferedReader reader = null; + List results = null; + try { + reader = new BufferedReader(new FileReader(file)); + String tempString = null; + while ((tempString = reader.readLine()) != null) { + if(results == null){ + results = new ArrayList<>(); + } + results.add(tempString); + } + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + throw e; + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e1) { + } + } + } + return results; + } +} diff --git a/students/402246209/learning/src/main/java/com/mimieye/odd/srp/util/MailUtil.java b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/util/MailUtil.java new file mode 100644 index 0000000000..b576aac1ed --- /dev/null +++ b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/util/MailUtil.java @@ -0,0 +1,18 @@ +package com.mimieye.odd.srp.util; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/402246209/learning/src/main/java/com/mimieye/odd/srp/util/PropertiesUtil.java b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/util/PropertiesUtil.java new file mode 100644 index 0000000000..b40c49447c --- /dev/null +++ b/students/402246209/learning/src/main/java/com/mimieye/odd/srp/util/PropertiesUtil.java @@ -0,0 +1,18 @@ +package com.mimieye.odd.srp.util; + +import java.io.File; +import java.io.InputStream; +import java.util.Properties; + +/** + * Created by Pierreluo on 2017/6/17. + */ +public class PropertiesUtil { + + public static Properties getInstance(String fileName) throws Exception { + InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName); + Properties properties = new Properties(); + properties.load(in); + return properties; + } +} diff --git a/students/402246209/learning/src/main/resources/config.properties b/students/402246209/learning/src/main/resources/config.properties new file mode 100644 index 0000000000..290d819f1e --- /dev/null +++ b/students/402246209/learning/src/main/resources/config.properties @@ -0,0 +1,5 @@ +smtp.server=smtp.163.com +alt.smtp.server=smtp1.163.com +email.admin=admin@company.com +promotion.filepath=F:/projectL/coding2017/students/402246209/learning/src/main/resources/product_promotion.txt +emailDebug=false diff --git a/students/402246209/learning/src/main/resources/product_promotion.txt b/students/402246209/learning/src/main/resources/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/402246209/learning/src/main/resources/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From fd7b984a703f6178dbae0b8ef8d9f35a3d019062 Mon Sep 17 00:00:00 2001 From: XiaodanL Date: Sat, 17 Jun 2017 14:18:08 +0800 Subject: [PATCH 153/214] =?UTF-8?q?[lixiaodan]=E7=AC=AC=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/861924479/src/com/learning/test/Test.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 students/861924479/src/com/learning/test/Test.java diff --git a/students/861924479/src/com/learning/test/Test.java b/students/861924479/src/com/learning/test/Test.java new file mode 100644 index 0000000000..df9a26ef20 --- /dev/null +++ b/students/861924479/src/com/learning/test/Test.java @@ -0,0 +1,5 @@ +package com.learning.test; + +public class Test { + +} From 21b17e0d76d3331f7b4e31bd6a00301bd77d6fef Mon Sep 17 00:00:00 2001 From: MIMIEYES Date: Sat, 17 Jun 2017 14:29:49 +0800 Subject: [PATCH 154/214] =?UTF-8?q?=E6=9B=B4=E6=94=B9pom.xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/402246209/learning/pom.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/students/402246209/learning/pom.xml b/students/402246209/learning/pom.xml index 23830a6621..129bccd496 100644 --- a/students/402246209/learning/pom.xml +++ b/students/402246209/learning/pom.xml @@ -6,7 +6,8 @@ com.mimieye learning - 1.0-SNAPSHOT + RELEASE + jar \ No newline at end of file From e5e176879e59389515697c4b2534ad41c022ee56 Mon Sep 17 00:00:00 2001 From: lorcx <740707954@qq.com> Date: Sat, 17 Jun 2017 15:40:34 +0800 Subject: [PATCH 155/214] =?UTF-8?q?2017=E5=B9=B46=E6=9C=8817=E6=97=A5=2015?= =?UTF-8?q?:40:28?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../740707954/src/ood/newSrp/FileUtil.java | 13 -- .../740707954/src/ood/newSrp/MailSender.java | 73 +++++++ .../740707954/src/ood/newSrp/MailUtil.java | 18 -- .../src/ood/newSrp/PromotionMail.java | 186 ++---------------- .../ood/newSrp/{ => conf}/Configuration.java | 9 +- .../src/ood/newSrp/entity/Email.java | 57 ++++++ .../src/ood/newSrp/entity/MainSmtpServer.java | 8 - .../src/ood/newSrp/entity/Notify.java | 14 -- .../src/ood/newSrp/entity/Product.java | 31 ++- .../src/ood/newSrp/entity/SmtpServer.java | 8 - .../src/ood/newSrp/entity/TempSmtpServer.java | 8 - .../ood/newSrp/server/MainSmtpFactory.java | 11 ++ .../src/ood/newSrp/server/MainSmtpServer.java | 30 +++ .../src/ood/newSrp/server/ProductServer.java | 55 ++++++ .../src/ood/newSrp/server/SmtpFactory.java | 8 + .../src/ood/newSrp/server/SmtpServer.java | 19 ++ .../ood/newSrp/server/TempSmtpFactory.java | 11 ++ .../src/ood/newSrp/server/TempSmtpServer.java | 29 +++ .../src/ood/newSrp/server/UserServer.java | 19 ++ .../src/ood/newSrp/{ => util}/DBUtil.java | 2 +- 20 files changed, 363 insertions(+), 246 deletions(-) delete mode 100644 students/740707954/src/ood/newSrp/FileUtil.java create mode 100644 students/740707954/src/ood/newSrp/MailSender.java delete mode 100644 students/740707954/src/ood/newSrp/MailUtil.java rename students/740707954/src/ood/newSrp/{ => conf}/Configuration.java (88%) create mode 100644 students/740707954/src/ood/newSrp/entity/Email.java delete mode 100644 students/740707954/src/ood/newSrp/entity/MainSmtpServer.java delete mode 100644 students/740707954/src/ood/newSrp/entity/Notify.java delete mode 100644 students/740707954/src/ood/newSrp/entity/SmtpServer.java delete mode 100644 students/740707954/src/ood/newSrp/entity/TempSmtpServer.java create mode 100644 students/740707954/src/ood/newSrp/server/MainSmtpFactory.java create mode 100644 students/740707954/src/ood/newSrp/server/MainSmtpServer.java create mode 100644 students/740707954/src/ood/newSrp/server/ProductServer.java create mode 100644 students/740707954/src/ood/newSrp/server/SmtpFactory.java create mode 100644 students/740707954/src/ood/newSrp/server/SmtpServer.java create mode 100644 students/740707954/src/ood/newSrp/server/TempSmtpFactory.java create mode 100644 students/740707954/src/ood/newSrp/server/TempSmtpServer.java create mode 100644 students/740707954/src/ood/newSrp/server/UserServer.java rename students/740707954/src/ood/newSrp/{ => util}/DBUtil.java (95%) diff --git a/students/740707954/src/ood/newSrp/FileUtil.java b/students/740707954/src/ood/newSrp/FileUtil.java deleted file mode 100644 index 57508724a6..0000000000 --- a/students/740707954/src/ood/newSrp/FileUtil.java +++ /dev/null @@ -1,13 +0,0 @@ -package ood.newSrp; - -import java.io.File; -import java.io.IOException; - -/** - * Created by Administrator on 2017/6/16 0016. - */ -public class FileUtil { - protected void readFile(File file) throws IOException { - - } -} diff --git a/students/740707954/src/ood/newSrp/MailSender.java b/students/740707954/src/ood/newSrp/MailSender.java new file mode 100644 index 0000000000..14604b2c14 --- /dev/null +++ b/students/740707954/src/ood/newSrp/MailSender.java @@ -0,0 +1,73 @@ +package ood.newSrp; + +import ood.newSrp.entity.Email; +import ood.newSrp.entity.Product; +import ood.newSrp.server.MainSmtpFactory; +import ood.newSrp.server.SmtpServer; +import ood.newSrp.server.TempSmtpFactory; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +/** + * 邮件发送器 + * Created by lx on 2017/6/17. + */ +public class MailSender { + + protected SmtpServer mainServer = new MainSmtpFactory().createSmtp(); + protected SmtpServer tempServer = new TempSmtpFactory().createSmtp(); + public static final String NAME_KEY = "NAME"; + + /** + * 批量发送邮件 + * @param p 产品信息 + * @param sendUserList 用户信息 + * @param d + * @throws IOException + */ + public void batchSendEMail(Product p, List sendUserList, boolean d) throws IOException { + System.out.println("--------开始发送邮件-------"); + if (null == sendUserList || sendUserList.size() == 0) { + System.out.println("没有邮件发送"); + return; + } + + for (Map userInfo : sendUserList) { + Email email = new Email(); + String toAddr = userInfo.get("EMAIL").toString(); + email.setToAddress(toAddr); + email.setFromAddress(mainServer.address); + email.setSubject("您关注的产品降价了"); + email.setMessage("尊敬的 " + userInfo.get(NAME_KEY).toString() + ", 您关注的产品 " + p.getProductDesc() + " 降价了,欢迎购买!"); + try { + sendEmail(email, d); + } catch (Exception e) { + try { + email.setFromAddress(tempServer.address); + sendEmail(email, d); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + } + + /** + * 发送邮件 + * @param n + * @param debug + */ + public static void sendEmail(Email n, boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(n.getFromAddress()).append("\n"); + buffer.append("To:").append(n.getToAddress()).append("\n"); + buffer.append("Subject:").append(n.getSubject()).append("\n"); + buffer.append("Content:").append(n.getMessage()).append("\n"); + System.out.println(buffer.toString()); + + } +} diff --git a/students/740707954/src/ood/newSrp/MailUtil.java b/students/740707954/src/ood/newSrp/MailUtil.java deleted file mode 100644 index a6ec476715..0000000000 --- a/students/740707954/src/ood/newSrp/MailUtil.java +++ /dev/null @@ -1,18 +0,0 @@ -package ood.newSrp; - -public class MailUtil { - - public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, - boolean debug) { - //假装发了一封邮件 - StringBuilder buffer = new StringBuilder(); - buffer.append("From:").append(fromAddress).append("\n"); - buffer.append("To:").append(toAddress).append("\n"); - buffer.append("Subject:").append(subject).append("\n"); - buffer.append("Content:").append(message).append("\n"); - System.out.println(buffer.toString()); - - } - - -} diff --git a/students/740707954/src/ood/newSrp/PromotionMail.java b/students/740707954/src/ood/newSrp/PromotionMail.java index 018f1aa8b9..74cb9741da 100644 --- a/students/740707954/src/ood/newSrp/PromotionMail.java +++ b/students/740707954/src/ood/newSrp/PromotionMail.java @@ -1,183 +1,31 @@ package ood.newSrp; -import ood.oldSrp.ConfigurationKeys; +import ood.newSrp.entity.Product; +import ood.newSrp.server.UserServer; +import ood.newSrp.server.ProductServer; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.util.HashMap; -import java.util.Iterator; import java.util.List; +import java.util.Map; /** * promotion 提升 */ public class PromotionMail { - - - protected String sendMailQuery = null; - - - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; - protected String toAddress = null; - protected String subject = null; - protected String message = null; - - protected String productID = null; - protected String productDesc = null; - - private static Configuration config; - - - private static final String NAME_KEY = "NAME"; - private static final String EMAIL_KEY = "EMAIL"; - + // 用户信息 + private static UserServer ms = new UserServer(); + // 邮件发送器 + private static MailSender mSend = new MailSender(); public static void main(String[] args) throws Exception { - - File f = new File(System.getProperty("user.dir") + "/src/ood/oldSrp/product_promotion.txt"); - boolean emailDebug = false; - - PromotionMail pe = new PromotionMail(f, emailDebug); - - } - - - public PromotionMail(File file, boolean mailDebug) throws Exception { - - //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - readFile(file); - - - config = new Configuration(); - - setSMTPHost(); - setAltSMTPHost(); - - - setFromAddress(); - - - setLoadQuery(); - - sendEMails(mailDebug, loadMailingList()); - - - } - - - protected void setProductID(String productID) { - this.productID = productID; - - } - - protected String getproductID() { - return productID; - } - - protected void setLoadQuery() throws Exception { - - sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + productID + "' " - + "and send_mail=1 "; - - - System.out.println("loadQuery set"); - } - - - protected void setSMTPHost() { - smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); - } - - - protected void setAltSMTPHost() { - altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); - - } - - - protected void setFromAddress() { - fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); - } - - protected void setMessage(HashMap userInfo) throws IOException { - - String name = (String) userInfo.get(NAME_KEY); - - subject = "您关注的产品降价了"; - message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; - - - } - - - protected void readFile(File file) throws IOException { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - setProductID(data[0]); - setProductDesc(data[1]); - - System.out.println("产品ID = " + productID + "\n"); - System.out.println("产品描述 = " + productDesc + "\n"); - - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); - } - } - - private void setProductDesc(String desc) { - this.productDesc = desc; - } - - - protected void configureEMail(HashMap userInfo) throws IOException { - toAddress = (String) userInfo.get(EMAIL_KEY); - if (toAddress.length() > 0) - setMessage(userInfo); - } - - protected List loadMailingList() throws Exception { - return DBUtil.query(this.sendMailQuery); - } - - - protected void sendEMails(boolean debug, List mailingList) throws IOException { - - System.out.println("开始发送邮件"); - - - if (mailingList != null) { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) { - configureEMail((HashMap) iter.next()); - try { - if (toAddress.length() > 0) - MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } catch (Exception e) { - - try { - MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } - } - - - } else { - System.out.println("没有邮件发送"); - + // 获取产品信息 + List pList = ProductServer.getUserProduct(); + + for (Product p : pList) { + System.out.println("产品ID: " + p.getProductId() + "\n" + "产品描述:" + p.getProductDesc()); + // 获取接收产品用户列表 + List sendUserList = ms.querySendUser(p.getProductId()); + // 发送邮件 + mSend.batchSendEMail(p, sendUserList, false); } } diff --git a/students/740707954/src/ood/newSrp/Configuration.java b/students/740707954/src/ood/newSrp/conf/Configuration.java similarity index 88% rename from students/740707954/src/ood/newSrp/Configuration.java rename to students/740707954/src/ood/newSrp/conf/Configuration.java index c2721b9a29..37bf3f3a58 100644 --- a/students/740707954/src/ood/newSrp/Configuration.java +++ b/students/740707954/src/ood/newSrp/conf/Configuration.java @@ -1,24 +1,25 @@ -package ood.newSrp; -import ood.oldSrp.ConfigurationKeys; +package ood.newSrp.conf; +import ood.oldSrp.ConfigurationKeys; import java.util.HashMap; import java.util.Map; public class Configuration { static Map configurations = new HashMap<>(); + static{ configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); } + /** * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 * @param key * @return */ - public String getProperty(String key) { - + public static String getProperty(String key) { return configurations.get(key); } diff --git a/students/740707954/src/ood/newSrp/entity/Email.java b/students/740707954/src/ood/newSrp/entity/Email.java new file mode 100644 index 0000000000..17159b916b --- /dev/null +++ b/students/740707954/src/ood/newSrp/entity/Email.java @@ -0,0 +1,57 @@ +package ood.newSrp.entity; + +/** + * 邮件 + * Created by Administrator on 2017/6/15 0015. + */ +public class Email { + private String subject; + private String message; + private String toAddress; + private String fromAddress; + private String smtpHost; + + public Email() { + + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } +} diff --git a/students/740707954/src/ood/newSrp/entity/MainSmtpServer.java b/students/740707954/src/ood/newSrp/entity/MainSmtpServer.java deleted file mode 100644 index 0e4cfa40bc..0000000000 --- a/students/740707954/src/ood/newSrp/entity/MainSmtpServer.java +++ /dev/null @@ -1,8 +0,0 @@ -package ood.newSrp.entity; - -/** - * 主要服务器 - * Created by Administrator on 2017/6/15 0015. - */ -public class MainSmtpServer implements SmtpServer { -} diff --git a/students/740707954/src/ood/newSrp/entity/Notify.java b/students/740707954/src/ood/newSrp/entity/Notify.java deleted file mode 100644 index aa2caa1049..0000000000 --- a/students/740707954/src/ood/newSrp/entity/Notify.java +++ /dev/null @@ -1,14 +0,0 @@ -package ood.newSrp.entity; - -import java.io.IOException; -import java.util.HashMap; - -/** - * 通知表 - * Created by Administrator on 2017/6/15 0015. - */ -public class Notify { - protected void setMessage(HashMap userInfo) throws IOException { - - } -} diff --git a/students/740707954/src/ood/newSrp/entity/Product.java b/students/740707954/src/ood/newSrp/entity/Product.java index e1851c1526..4fc38aacb4 100644 --- a/students/740707954/src/ood/newSrp/entity/Product.java +++ b/students/740707954/src/ood/newSrp/entity/Product.java @@ -1,10 +1,35 @@ package ood.newSrp.entity; /** - * 产品 + * 产品信息 * Created by Administrator on 2017/6/15 0015. */ public class Product { - protected String productID = null; - protected String productDesc = null; + private String productId = null; + private String productDesc = null; + + public Product() { + + } + + public Product(String productId, String productDesc) { + this.productId = productId; + this.productDesc = productDesc; + } + + public void setProductId(String productId) { + this.productId = productId; + } + + public String getProductId() { + return productId; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } } diff --git a/students/740707954/src/ood/newSrp/entity/SmtpServer.java b/students/740707954/src/ood/newSrp/entity/SmtpServer.java deleted file mode 100644 index 0e1ede3dda..0000000000 --- a/students/740707954/src/ood/newSrp/entity/SmtpServer.java +++ /dev/null @@ -1,8 +0,0 @@ -package ood.newSrp.entity; - -/** - * Created by Administrator on 2017/6/15 0015. - */ -public interface SmtpServer { - String address = ""; -} diff --git a/students/740707954/src/ood/newSrp/entity/TempSmtpServer.java b/students/740707954/src/ood/newSrp/entity/TempSmtpServer.java deleted file mode 100644 index 01321fbf7a..0000000000 --- a/students/740707954/src/ood/newSrp/entity/TempSmtpServer.java +++ /dev/null @@ -1,8 +0,0 @@ -package ood.newSrp.entity; - -/** - * 备用服务器 - * Created by Administrator on 2017/6/15 0015. - */ -public class TempSmtpServer implements SmtpServer { -} diff --git a/students/740707954/src/ood/newSrp/server/MainSmtpFactory.java b/students/740707954/src/ood/newSrp/server/MainSmtpFactory.java new file mode 100644 index 0000000000..5dcfdc5a5b --- /dev/null +++ b/students/740707954/src/ood/newSrp/server/MainSmtpFactory.java @@ -0,0 +1,11 @@ +package ood.newSrp.server; + +/** + * Created by lx on 2017/6/17. + */ +public class MainSmtpFactory implements SmtpFactory { + @Override + public SmtpServer createSmtp() { + return new MainSmtpServer(); + } +} diff --git a/students/740707954/src/ood/newSrp/server/MainSmtpServer.java b/students/740707954/src/ood/newSrp/server/MainSmtpServer.java new file mode 100644 index 0000000000..6e9be0a9ab --- /dev/null +++ b/students/740707954/src/ood/newSrp/server/MainSmtpServer.java @@ -0,0 +1,30 @@ +package ood.newSrp.server; + +import ood.newSrp.conf.Configuration; +import ood.oldSrp.ConfigurationKeys; + +/** + * 主要服务器 + * Created by Administrator on 2017/6/15 0015. + */ +public class MainSmtpServer extends SmtpServer { + + public MainSmtpServer() { + address = Configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN); + host = Configuration.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + /** + * 设置服务器地址 + */ + public void setServerAddr() { + address = Configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + /** + * 设置服务器host + */ + public void setServerHost() { + host = Configuration.getProperty(ConfigurationKeys.SMTP_SERVER); + } +} diff --git a/students/740707954/src/ood/newSrp/server/ProductServer.java b/students/740707954/src/ood/newSrp/server/ProductServer.java new file mode 100644 index 0000000000..db8efa8bc3 --- /dev/null +++ b/students/740707954/src/ood/newSrp/server/ProductServer.java @@ -0,0 +1,55 @@ +package ood.newSrp.server; + +import ood.newSrp.entity.Product; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + *产品服务 + * Created by lx on 2017/6/17. + */ +public class ProductServer { + private static List pList = new ArrayList<>(); + static { + try { + initSpecialProductList(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * 生成优惠产品信息 + * @return + * @throws IOException + */ + private static void initSpecialProductList() throws IOException { + String filePath = System.getProperty("user.dir") + "/src/ood/oldSrp/product_promotion.txt"; + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(filePath)); + String pInfo; + while ((pInfo = br.readLine()) != null) { + String[] data = pInfo.split(" "); + pList.add(new Product(data[0], data[1])); + } + } catch (IOException e) { + throw new IOException( "读取文件内容失败 " + e.getMessage()); + } finally { + if (br != null) { + br.close(); + } + } + } + + /** + * 获取用户的产品 + * @return + */ + public static List getUserProduct() { + return pList; + } +} diff --git a/students/740707954/src/ood/newSrp/server/SmtpFactory.java b/students/740707954/src/ood/newSrp/server/SmtpFactory.java new file mode 100644 index 0000000000..efdf3ec25b --- /dev/null +++ b/students/740707954/src/ood/newSrp/server/SmtpFactory.java @@ -0,0 +1,8 @@ +package ood.newSrp.server; + +/** + * Created by lx on 2017/6/17. + */ +public interface SmtpFactory { + public SmtpServer createSmtp(); +} \ No newline at end of file diff --git a/students/740707954/src/ood/newSrp/server/SmtpServer.java b/students/740707954/src/ood/newSrp/server/SmtpServer.java new file mode 100644 index 0000000000..ff35288d1f --- /dev/null +++ b/students/740707954/src/ood/newSrp/server/SmtpServer.java @@ -0,0 +1,19 @@ +package ood.newSrp.server; + +/** + * Created by Administrator on 2017/6/15 0015. + */ +public abstract class SmtpServer { + public String address = ""; + public String host = ""; + + /** + * 设置服务器地址 + */ + abstract void setServerAddr(); + + /** + * 设置服务器host + */ + abstract void setServerHost(); +} diff --git a/students/740707954/src/ood/newSrp/server/TempSmtpFactory.java b/students/740707954/src/ood/newSrp/server/TempSmtpFactory.java new file mode 100644 index 0000000000..ae16252fd0 --- /dev/null +++ b/students/740707954/src/ood/newSrp/server/TempSmtpFactory.java @@ -0,0 +1,11 @@ +package ood.newSrp.server; + +/** + * Created by lx on 2017/6/17. + */ +public class TempSmtpFactory implements SmtpFactory { + @Override + public SmtpServer createSmtp() { + return new TempSmtpServer(); + } +} diff --git a/students/740707954/src/ood/newSrp/server/TempSmtpServer.java b/students/740707954/src/ood/newSrp/server/TempSmtpServer.java new file mode 100644 index 0000000000..417cd8acd0 --- /dev/null +++ b/students/740707954/src/ood/newSrp/server/TempSmtpServer.java @@ -0,0 +1,29 @@ +package ood.newSrp.server; + +import ood.newSrp.conf.Configuration; +import ood.oldSrp.ConfigurationKeys; + +/** + * 备用服务器 + * Created by Administrator on 2017/6/15 0015. + */ +public class TempSmtpServer extends SmtpServer { + + public TempSmtpServer() { + address = Configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN); + host = Configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + } + /** + * 设置服务器地址 + */ + public void setServerAddr() { + address = Configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + /** + * 设置服务器host + */ + public void setServerHost() { + host = Configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + } +} diff --git a/students/740707954/src/ood/newSrp/server/UserServer.java b/students/740707954/src/ood/newSrp/server/UserServer.java new file mode 100644 index 0000000000..0c9864eab7 --- /dev/null +++ b/students/740707954/src/ood/newSrp/server/UserServer.java @@ -0,0 +1,19 @@ +package ood.newSrp.server; + +import ood.newSrp.util.DBUtil; +import java.util.List; +import java.util.Map; + +/** + * Created by lx on 2017/6/17. + */ +public class UserServer { + + /** + * 查询发送人 + * @return + */ + public List querySendUser(String productId){ + return DBUtil.query("Select name from subscriptions where product_id= '" + productId + "' and send_mail=1 "); + } +} diff --git a/students/740707954/src/ood/newSrp/DBUtil.java b/students/740707954/src/ood/newSrp/util/DBUtil.java similarity index 95% rename from students/740707954/src/ood/newSrp/DBUtil.java rename to students/740707954/src/ood/newSrp/util/DBUtil.java index 0022cb74b0..c1866fd925 100644 --- a/students/740707954/src/ood/newSrp/DBUtil.java +++ b/students/740707954/src/ood/newSrp/util/DBUtil.java @@ -1,4 +1,4 @@ -package ood.newSrp; +package ood.newSrp.util; import java.util.ArrayList; import java.util.HashMap; import java.util.List; From 1c703675344f03eb8ef548da444b7c8f5f2b2779 Mon Sep 17 00:00:00 2001 From: Arthur <465034663@qq.com> Date: Sat, 17 Jun 2017 16:52:21 +0800 Subject: [PATCH 156/214] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coderising/ood/mytest/AltSMTPHost.java | 13 ++ .../coderising/ood/mytest/Configuration.java | 24 +++ .../ood/mytest/ConfigurationKeys.java | 9 + .../com/coderising/ood/mytest/DBUtil.java | 36 ++++ .../java/com/coderising/ood/mytest/Email.java | 64 +++++++ .../java/com/coderising/ood/mytest/Host.java | 12 ++ .../com/coderising/ood/mytest/IOUtils.java | 30 +++ .../com/coderising/ood/mytest/MailUtil.java | 18 ++ .../coderising/ood/mytest/PromotionMail.java | 128 +++++++++++++ .../com/coderising/ood/mytest/SMTPHost.java | 14 ++ .../com/coderising/ood/srp/Configuration.java | 23 +++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 181 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 16 files changed, 608 insertions(+) create mode 100644 students/465034663/src/main/java/com/coderising/ood/mytest/AltSMTPHost.java create mode 100644 students/465034663/src/main/java/com/coderising/ood/mytest/Configuration.java create mode 100644 students/465034663/src/main/java/com/coderising/ood/mytest/ConfigurationKeys.java create mode 100644 students/465034663/src/main/java/com/coderising/ood/mytest/DBUtil.java create mode 100644 students/465034663/src/main/java/com/coderising/ood/mytest/Email.java create mode 100644 students/465034663/src/main/java/com/coderising/ood/mytest/Host.java create mode 100644 students/465034663/src/main/java/com/coderising/ood/mytest/IOUtils.java create mode 100644 students/465034663/src/main/java/com/coderising/ood/mytest/MailUtil.java create mode 100644 students/465034663/src/main/java/com/coderising/ood/mytest/PromotionMail.java create mode 100644 students/465034663/src/main/java/com/coderising/ood/mytest/SMTPHost.java create mode 100644 students/465034663/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/465034663/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/465034663/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/465034663/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/465034663/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/465034663/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/465034663/src/main/java/com/coderising/ood/mytest/AltSMTPHost.java b/students/465034663/src/main/java/com/coderising/ood/mytest/AltSMTPHost.java new file mode 100644 index 0000000000..cc492d23be --- /dev/null +++ b/students/465034663/src/main/java/com/coderising/ood/mytest/AltSMTPHost.java @@ -0,0 +1,13 @@ +package com.coderising.ood.mytest; + +/** + * Created by Arthur on 2017/6/17. + */ +public class AltSMTPHost implements Host { + + @Override + public String setHost() { + return this.configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + } + +} diff --git a/students/465034663/src/main/java/com/coderising/ood/mytest/Configuration.java b/students/465034663/src/main/java/com/coderising/ood/mytest/Configuration.java new file mode 100644 index 0000000000..ae74eda7be --- /dev/null +++ b/students/465034663/src/main/java/com/coderising/ood/mytest/Configuration.java @@ -0,0 +1,24 @@ +package com.coderising.ood.mytest; + +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/465034663/src/main/java/com/coderising/ood/mytest/ConfigurationKeys.java b/students/465034663/src/main/java/com/coderising/ood/mytest/ConfigurationKeys.java new file mode 100644 index 0000000000..ae9234e569 --- /dev/null +++ b/students/465034663/src/main/java/com/coderising/ood/mytest/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.mytest; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/465034663/src/main/java/com/coderising/ood/mytest/DBUtil.java b/students/465034663/src/main/java/com/coderising/ood/mytest/DBUtil.java new file mode 100644 index 0000000000..cb8b8d4927 --- /dev/null +++ b/students/465034663/src/main/java/com/coderising/ood/mytest/DBUtil.java @@ -0,0 +1,36 @@ +package com.coderising.ood.mytest; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } + + public static String loadQuery(String productID) throws Exception { + + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID + "' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + return sendMailQuery; + } +} diff --git a/students/465034663/src/main/java/com/coderising/ood/mytest/Email.java b/students/465034663/src/main/java/com/coderising/ood/mytest/Email.java new file mode 100644 index 0000000000..208fb59b21 --- /dev/null +++ b/students/465034663/src/main/java/com/coderising/ood/mytest/Email.java @@ -0,0 +1,64 @@ +package com.coderising.ood.mytest; + +/** + * Created by Arthur on 2017/6/17. + */ +public class Email { + + String toAddress; + String fromAddress; + String subject; + String message; + String smtpHost; + + public Email() {} + + public Email(String toAddress, String fromAddress, String subject, String message, String smtpHost) { + this.toAddress = toAddress; + this.fromAddress = fromAddress; + this.subject = subject; + this.message = message; + this.smtpHost = smtpHost; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + +} diff --git a/students/465034663/src/main/java/com/coderising/ood/mytest/Host.java b/students/465034663/src/main/java/com/coderising/ood/mytest/Host.java new file mode 100644 index 0000000000..3c87232aba --- /dev/null +++ b/students/465034663/src/main/java/com/coderising/ood/mytest/Host.java @@ -0,0 +1,12 @@ +package com.coderising.ood.mytest; + +/** + * Created by Arthur on 2017/6/17. + */ +public interface Host { + + Configuration configuration = new Configuration(); + + String setHost(); + +} diff --git a/students/465034663/src/main/java/com/coderising/ood/mytest/IOUtils.java b/students/465034663/src/main/java/com/coderising/ood/mytest/IOUtils.java new file mode 100644 index 0000000000..2d44ca482e --- /dev/null +++ b/students/465034663/src/main/java/com/coderising/ood/mytest/IOUtils.java @@ -0,0 +1,30 @@ +package com.coderising.ood.mytest; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +/** + * Created by Arthur on 2017/6/17. + */ +public class IOUtils { + + protected static String[] readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + + try { + + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + return data; + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + +} diff --git a/students/465034663/src/main/java/com/coderising/ood/mytest/MailUtil.java b/students/465034663/src/main/java/com/coderising/ood/mytest/MailUtil.java new file mode 100644 index 0000000000..b29261e059 --- /dev/null +++ b/students/465034663/src/main/java/com/coderising/ood/mytest/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.mytest; + +public class MailUtil { + + public static void sendEmail(Email email, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(email.getFromAddress()).append("\n"); + buffer.append("To:").append(email.getToAddress()).append("\n"); + buffer.append("Subject:").append(email.getSubject()).append("\n"); + buffer.append("Content:").append(email.getMessage()).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/465034663/src/main/java/com/coderising/ood/mytest/PromotionMail.java b/students/465034663/src/main/java/com/coderising/ood/mytest/PromotionMail.java new file mode 100644 index 0000000000..19f8e66222 --- /dev/null +++ b/students/465034663/src/main/java/com/coderising/ood/mytest/PromotionMail.java @@ -0,0 +1,128 @@ +package com.coderising.ood.mytest; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery; + + + protected String smtpHost; + protected String altSmtpHost; + protected String fromAddress; + protected String toAddress; + protected String subject; + protected String message; + + protected String productID; + protected String productDesc; + + private Email email; + + private static Configuration config; + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("D:\\IdeaWorspace\\works\\coding2017\\students\\465034663\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + IOUtils.readFile(file); + + + config = new Configuration(); + + /*setSMTPHost(); + setAltSMTPHost();*/ + this.smtpHost = new SMTPHost().setHost(); + this.altSmtpHost = new AltSMTPHost().setHost(); + + setFromAddress(); + + + //setLoadQuery(); + + DBUtil.loadQuery(this.productID); + sendEMails(mailDebug, loadMailingList()); + + + } + + protected void setFromAddress() { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + + + } + + protected void configureEMail(HashMap userInfo) throws IOException { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + setEmail(); + try { + if (toAddress.length() > 0) + MailUtil.sendEmail(this.email, debug); + } catch (Exception e) { + + try { + MailUtil.sendEmail(this.email, debug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } else { + System.out.println("没有邮件发送"); + + } + + } + + private void setEmail(){ + this.email = new Email(this.toAddress, this.fromAddress, this.subject, this.message, this.altSmtpHost); + } +} diff --git a/students/465034663/src/main/java/com/coderising/ood/mytest/SMTPHost.java b/students/465034663/src/main/java/com/coderising/ood/mytest/SMTPHost.java new file mode 100644 index 0000000000..3d2ab58ace --- /dev/null +++ b/students/465034663/src/main/java/com/coderising/ood/mytest/SMTPHost.java @@ -0,0 +1,14 @@ +package com.coderising.ood.mytest; + + +/** + * Created by Arthur on 2017/6/17. + */ +public class SMTPHost implements Host { + + @Override + public String setHost() { + return this.configuration.getProperty(ConfigurationKeys.SMTP_SERVER); + } + +} diff --git a/students/465034663/src/main/java/com/coderising/ood/srp/Configuration.java b/students/465034663/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/465034663/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/465034663/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/465034663/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/465034663/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/465034663/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/465034663/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/465034663/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/465034663/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/465034663/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/465034663/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/465034663/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/465034663/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..d29c2d3dc0 --- /dev/null +++ b/students/465034663/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,181 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("D:\\IdeaWorspace\\works\\coding2017\\students\\465034663\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + protected void setProductID(String productID) { + this.productID = productID; + + } + + protected String getproductID() { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID + "' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } catch (Exception e) { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/465034663/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/465034663/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/465034663/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 52c5d6ff591b359bd64808d82fc38c88480ee1ef Mon Sep 17 00:00:00 2001 From: cmhello88 Date: Sat, 17 Jun 2017 18:16:41 +0800 Subject: [PATCH 157/214] =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coderising/ood/srp/Configuration.java | 23 -- .../coderising/ood/srp/ConfigurationKeys.java | 9 - .../java/com/coderising/ood/srp/DBUtil.java | 25 --- .../java/com/coderising/ood/srp/MailUtil.java | 18 -- .../com/coderising/ood/srp/PromotionMail.java | 199 ------------------ .../coderising/ood/srp/product_promotion.txt | 4 - 6 files changed, 278 deletions(-) delete mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java delete mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java delete mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java delete mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java delete mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java delete mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java deleted file mode 100644 index f328c1816a..0000000000 --- a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.coderising.ood.srp; -import java.util.HashMap; -import java.util.Map; - -public class Configuration { - - static Map configurations = new HashMap<>(); - static{ - configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); - configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); - configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); - } - /** - * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 - * @param key - * @return - */ - public String getProperty(String key) { - - return configurations.get(key); - } - -} diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java deleted file mode 100644 index 8695aed644..0000000000 --- a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.coderising.ood.srp; - -public class ConfigurationKeys { - - public static final String SMTP_SERVER = "smtp.server"; - public static final String ALT_SMTP_SERVER = "alt.smtp.server"; - public static final String EMAIL_ADMIN = "email.admin"; - -} diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java deleted file mode 100644 index 82e9261d18..0000000000 --- a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.coderising.ood.srp; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - -public class DBUtil { - - /** - * 应该从数据库读, 但是简化为直接生成。 - * @param sql - * @return - */ - public static List query(String sql){ - - List userList = new ArrayList(); - for (int i = 1; i <= 3; i++) { - HashMap userInfo = new HashMap(); - userInfo.put("NAME", "User" + i); - userInfo.put("EMAIL", "aa@bb.com"); - userList.add(userInfo); - } - - return userList; - } -} diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java deleted file mode 100644 index 9f9e749af7..0000000000 --- a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.coderising.ood.srp; - -public class MailUtil { - - public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, - boolean debug) { - //假装发了一封邮件 - StringBuilder buffer = new StringBuilder(); - buffer.append("From:").append(fromAddress).append("\n"); - buffer.append("To:").append(toAddress).append("\n"); - buffer.append("Subject:").append(subject).append("\n"); - buffer.append("Content:").append(message).append("\n"); - System.out.println(buffer.toString()); - - } - - -} diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java deleted file mode 100644 index 781587a846..0000000000 --- a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ /dev/null @@ -1,199 +0,0 @@ -package com.coderising.ood.srp; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.Serializable; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; - -public class PromotionMail { - - - protected String sendMailQuery = null; - - - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; - protected String toAddress = null; - protected String subject = null; - protected String message = null; - - protected String productID = null; - protected String productDesc = null; - - private static Configuration config; - - - - private static final String NAME_KEY = "NAME"; - private static final String EMAIL_KEY = "EMAIL"; - - - public static void main(String[] args) throws Exception { - - File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); - boolean emailDebug = false; - - PromotionMail pe = new PromotionMail(f, emailDebug); - - } - - - public PromotionMail(File file, boolean mailDebug) throws Exception { - - //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - readFile(file); - - - config = new Configuration(); - - setSMTPHost(); - setAltSMTPHost(); - - - setFromAddress(); - - - setLoadQuery(); - - sendEMails(mailDebug, loadMailingList()); - - - } - - - - - protected void setProductID(String productID) - { - this.productID = productID; - - } - - protected String getproductID() - { - return productID; - } - - protected void setLoadQuery() throws Exception { - - sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + productID +"' " - + "and send_mail=1 "; - - - System.out.println("loadQuery set"); - } - - - protected void setSMTPHost() - { - smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); - } - - - protected void setAltSMTPHost() - { - altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); - - } - - - protected void setFromAddress() - { - fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); - } - - protected void setMessage(HashMap userInfo) throws IOException - { - - String name = (String) userInfo.get(NAME_KEY); - - subject = "您关注的产品降价了"; - message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; - - - - } - - - protected void readFile(File file) throws IOException // @02C - { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - setProductID(data[0]); - setProductDesc(data[1]); - - System.out.println("产品ID = " + productID + "\n"); - System.out.println("产品描述 = " + productDesc + "\n"); - - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); - } - } - - private void setProductDesc(String desc) { - this.productDesc = desc; - } - - - protected void configureEMail(HashMap userInfo) throws IOException - { - toAddress = (String) userInfo.get(EMAIL_KEY); - if (toAddress.length() > 0) - setMessage(userInfo); - } - - protected List loadMailingList() throws Exception { - return DBUtil.query(this.sendMailQuery); - } - - - protected void sendEMails(boolean debug, List mailingList) throws IOException - { - - System.out.println("开始发送邮件"); - - - if (mailingList != null) { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) { - configureEMail((HashMap) iter.next()); - try - { - if (toAddress.length() > 0) - MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } - catch (Exception e) - { - - try { - MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) - { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } - } - - - } - - else { - System.out.println("没有邮件发送"); - - } - - } -} diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt deleted file mode 100644 index b7a974adb3..0000000000 --- a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt +++ /dev/null @@ -1,4 +0,0 @@ -P8756 iPhone8 -P3946 XiaoMi10 -P8904 Oppo_R15 -P4955 Vivo_X20 \ No newline at end of file From 78497a0f05bcbe5a81460e43a8cd40694c70bc45 Mon Sep 17 00:00:00 2001 From: cmhello88 Date: Sat, 17 Jun 2017 18:20:48 +0800 Subject: [PATCH 158/214] =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../34594980/data-structure/answer/pom.xml | 32 --- .../coderising/download/DownloadThread.java | 20 -- .../coderising/download/FileDownloader.java | 73 ------- .../download/FileDownloaderTest.java | 59 ------ .../coderising/download/api/Connection.java | 23 -- .../download/api/ConnectionException.java | 5 - .../download/api/ConnectionManager.java | 10 - .../download/api/DownloadListener.java | 5 - .../download/impl/ConnectionImpl.java | 27 --- .../download/impl/ConnectionManagerImpl.java | 15 -- .../coderising/litestruts/LoginAction.java | 39 ---- .../com/coderising/litestruts/Struts.java | 34 --- .../com/coderising/litestruts/StrutsTest.java | 43 ---- .../java/com/coderising/litestruts/View.java | 23 -- .../java/com/coderising/litestruts/struts.xml | 11 - .../main/java/com/coding/basic/Iterator.java | 7 - .../src/main/java/com/coding/basic/List.java | 9 - .../com/coding/basic/array/ArrayList.java | 35 --- .../com/coding/basic/array/ArrayUtil.java | 96 --------- .../coding/basic/linklist/LRUPageFrame.java | 164 --------------- .../basic/linklist/LRUPageFrameTest.java | 34 --- .../com/coding/basic/linklist/LinkedList.java | 125 ----------- .../com/coding/basic/queue/CircleQueue.java | 47 ----- .../coding/basic/queue/CircleQueueTest.java | 44 ---- .../java/com/coding/basic/queue/Josephus.java | 39 ---- .../com/coding/basic/queue/JosephusTest.java | 27 --- .../java/com/coding/basic/queue/Queue.java | 61 ------ .../basic/queue/QueueWithTwoStacks.java | 55 ----- .../com/coding/basic/stack/QuickMinStack.java | 44 ---- .../coding/basic/stack/QuickMinStackTest.java | 39 ---- .../java/com/coding/basic/stack/Stack.java | 24 --- .../com/coding/basic/stack/StackUtil.java | 168 --------------- .../com/coding/basic/stack/StackUtilTest.java | 86 -------- .../basic/stack/StackWithTwoQueues.java | 53 ----- .../basic/stack/StackWithTwoQueuesTest.java | 36 ---- .../java/com/coding/basic/stack/Tail.java | 5 - .../basic/stack/TwoStackInOneArray.java | 117 ---------- .../basic/stack/TwoStackInOneArrayTest.java | 65 ------ .../coding/basic/stack/expr/InfixExpr.java | 72 ------- .../basic/stack/expr/InfixExprTest.java | 52 ----- .../basic/stack/expr/InfixToPostfix.java | 43 ---- .../basic/stack/expr/InfixToPostfixTest.java | 41 ---- .../coding/basic/stack/expr/PostfixExpr.java | 46 ---- .../basic/stack/expr/PostfixExprTest.java | 41 ---- .../coding/basic/stack/expr/PrefixExpr.java | 52 ----- .../basic/stack/expr/PrefixExprTest.java | 45 ---- .../com/coding/basic/stack/expr/Token.java | 50 ----- .../coding/basic/stack/expr/TokenParser.java | 57 ----- .../basic/stack/expr/TokenParserTest.java | 41 ---- .../coding/basic/tree/BinarySearchTree.java | 189 ----------------- .../basic/tree/BinarySearchTreeTest.java | 108 ---------- .../com/coding/basic/tree/BinaryTreeNode.java | 36 ---- .../com/coding/basic/tree/BinaryTreeUtil.java | 116 ---------- .../coding/basic/tree/BinaryTreeUtilTest.java | 75 ------- .../java/com/coding/basic/tree/FileList.java | 34 --- .../data-structure/assignment/pom.xml | 32 --- .../coderising/download/DownloadThread.java | 20 -- .../coderising/download/FileDownloader.java | 73 ------- .../download/FileDownloaderTest.java | 59 ------ .../coderising/download/api/Connection.java | 23 -- .../download/api/ConnectionException.java | 5 - .../download/api/ConnectionManager.java | 10 - .../download/api/DownloadListener.java | 5 - .../download/impl/ConnectionImpl.java | 27 --- .../download/impl/ConnectionManagerImpl.java | 15 -- .../coderising/litestruts/LoginAction.java | 39 ---- .../com/coderising/litestruts/Struts.java | 34 --- .../com/coderising/litestruts/StrutsTest.java | 43 ---- .../java/com/coderising/litestruts/View.java | 23 -- .../java/com/coderising/litestruts/struts.xml | 11 - .../com/coderising/ood/course/bad/Course.java | 24 --- .../ood/course/bad/CourseOffering.java | 26 --- .../ood/course/bad/CourseService.java | 16 -- .../coderising/ood/course/bad/Student.java | 14 -- .../coderising/ood/course/good/Course.java | 18 -- .../ood/course/good/CourseOffering.java | 34 --- .../ood/course/good/CourseService.java | 14 -- .../coderising/ood/course/good/Student.java | 21 -- .../java/com/coderising/ood/ocp/DateUtil.java | 10 - .../java/com/coderising/ood/ocp/Logger.java | 38 ---- .../java/com/coderising/ood/ocp/MailUtil.java | 10 - .../java/com/coderising/ood/ocp/SMSUtil.java | 10 - .../java/com/coderising/ood/srp/DBUtil.java | 25 --- .../java/com/coderising/ood/srp/MailUtil.java | 18 -- .../com/coderising/ood/srp/PromotionMail.java | 199 ------------------ .../main/java/com/coding/basic/Iterator.java | 7 - .../src/main/java/com/coding/basic/List.java | 9 - .../com/coding/basic/array/ArrayList.java | 35 --- .../com/coding/basic/array/ArrayUtil.java | 96 --------- .../coding/basic/linklist/LRUPageFrame.java | 57 ----- .../basic/linklist/LRUPageFrameTest.java | 34 --- .../com/coding/basic/linklist/LinkedList.java | 125 ----------- .../com/coding/basic/queue/CircleQueue.java | 39 ---- .../java/com/coding/basic/queue/Josephus.java | 18 -- .../com/coding/basic/queue/JosephusTest.java | 27 --- .../java/com/coding/basic/queue/Queue.java | 61 ------ .../basic/queue/QueueWithTwoStacks.java | 47 ----- .../com/coding/basic/stack/QuickMinStack.java | 19 -- .../java/com/coding/basic/stack/Stack.java | 24 --- .../com/coding/basic/stack/StackUtil.java | 48 ----- .../com/coding/basic/stack/StackUtilTest.java | 65 ------ .../basic/stack/StackWithTwoQueues.java | 16 -- .../basic/stack/TwoStackInOneArray.java | 57 ----- .../coding/basic/stack/expr/InfixExpr.java | 15 -- .../basic/stack/expr/InfixExprTest.java | 52 ----- .../basic/stack/expr/InfixToPostfix.java | 14 -- .../coding/basic/stack/expr/PostfixExpr.java | 18 -- .../basic/stack/expr/PostfixExprTest.java | 41 ---- .../coding/basic/stack/expr/PrefixExpr.java | 18 -- .../basic/stack/expr/PrefixExprTest.java | 45 ---- .../com/coding/basic/stack/expr/Token.java | 50 ----- .../coding/basic/stack/expr/TokenParser.java | 57 ----- .../basic/stack/expr/TokenParserTest.java | 41 ---- .../coding/basic/tree/BinarySearchTree.java | 55 ----- .../basic/tree/BinarySearchTreeTest.java | 109 ---------- .../com/coding/basic/tree/BinaryTreeNode.java | 35 --- .../com/coding/basic/tree/BinaryTreeUtil.java | 66 ------ .../coding/basic/tree/BinaryTreeUtilTest.java | 75 ------- .../java/com/coding/basic/tree/FileList.java | 10 - .../com/coderising/ood/srp/Configuration.java | 0 .../coderising/ood/srp/ConfigurationKeys.java | 0 .../java/com/coderising/ood/srp/DBUtil.java | 34 +++ .../java/com/coderising/ood/srp/Email.java | 49 +++++ .../java/com/coderising/ood/srp/FileUtil.java | 36 ++++ .../java/com/coderising/ood/srp/MailUtil.java | 77 +++++++ .../java/com/coderising/ood/srp/Product.java | 32 +++ .../com/coderising/ood/srp/PromotionMail.java | 16 ++ .../coderising/ood/srp/product_promotion.txt | 4 +- 128 files changed, 246 insertions(+), 5280 deletions(-) delete mode 100644 students/34594980/data-structure/answer/pom.xml delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/View.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/Iterator.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/List.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtilTest.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java delete mode 100644 students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java delete mode 100644 students/34594980/data-structure/assignment/pom.xml delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/DownloadThread.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/FileDownloader.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/FileDownloaderTest.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/Connection.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionException.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionManager.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/DownloadListener.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionImpl.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/LoginAction.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/Struts.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/StrutsTest.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/View.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/struts.xml delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Course.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseOffering.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseService.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Student.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Course.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseOffering.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseService.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Student.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/Logger.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/DBUtil.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/MailUtil.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/Iterator.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/List.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayList.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayUtil.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrame.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LinkedList.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/CircleQueue.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/Josephus.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/JosephusTest.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/Queue.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/QuickMinStack.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/Stack.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtil.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtilTest.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExpr.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/Token.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParser.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTree.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeNode.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java delete mode 100644 students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/FileList.java rename students/34594980/{data-structure/assignment => ood/ood-assignment}/src/main/java/com/coderising/ood/srp/Configuration.java (100%) rename students/34594980/{data-structure/assignment => ood/ood-assignment}/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java (100%) create mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Email.java create mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java create mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java rename students/34594980/{data-structure/assignment => ood/ood-assignment}/src/main/java/com/coderising/ood/srp/product_promotion.txt (50%) diff --git a/students/34594980/data-structure/answer/pom.xml b/students/34594980/data-structure/answer/pom.xml deleted file mode 100644 index ac6ba882df..0000000000 --- a/students/34594980/data-structure/answer/pom.xml +++ /dev/null @@ -1,32 +0,0 @@ - - 4.0.0 - - com.coderising - ds-answer - 0.0.1-SNAPSHOT - jar - - ds-answer - http://maven.apache.org - - - UTF-8 - - - - - - junit - junit - 4.12 - - - - - - aliyunmaven - http://maven.aliyun.com/nexus/content/groups/public/ - - - diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java deleted file mode 100644 index 900a3ad358..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/DownloadThread.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.coderising.download; - -import com.coderising.download.api.Connection; - -public class DownloadThread extends Thread{ - - Connection conn; - int startPos; - int endPos; - - public DownloadThread( Connection conn, int startPos, int endPos){ - - this.conn = conn; - this.startPos = startPos; - this.endPos = endPos; - } - public void run(){ - - } -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java deleted file mode 100644 index c3c8a3f27d..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/FileDownloader.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.coderising.download; - -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; - - -public class FileDownloader { - - String url; - - DownloadListener listener; - - ConnectionManager cm; - - - public FileDownloader(String _url) { - this.url = _url; - - } - - public void execute(){ - // 在这里实现你的代码, 注意: 需要用多线程实现下载 - // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 - // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) - // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 - // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 - // 具体的实现思路: - // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 - // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 - // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 - // 3. 把byte数组写入到文件中 - // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 - - // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 - Connection conn = null; - try { - - conn = cm.open(this.url); - - int length = conn.getContentLength(); - - new DownloadThread(conn,0,length-1).start(); - - } catch (ConnectionException e) { - e.printStackTrace(); - }finally{ - if(conn != null){ - conn.close(); - } - } - - - - - } - - public void setListener(DownloadListener listener) { - this.listener = listener; - } - - - - public void setConnectionManager(ConnectionManager ucm){ - this.cm = ucm; - } - - public DownloadListener getListener(){ - return this.listener; - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java deleted file mode 100644 index 4ff7f46ae0..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/FileDownloaderTest.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.coderising.download; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; -import com.coderising.download.impl.ConnectionManagerImpl; - -public class FileDownloaderTest { - boolean downloadFinished = false; - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testDownload() { - - String url = "http://localhost:8080/test.jpg"; - - FileDownloader downloader = new FileDownloader(url); - - - ConnectionManager cm = new ConnectionManagerImpl(); - downloader.setConnectionManager(cm); - - downloader.setListener(new DownloadListener() { - @Override - public void notifyFinished() { - downloadFinished = true; - } - - }); - - - downloader.execute(); - - // 等待多线程下载程序执行完毕 - while (!downloadFinished) { - try { - System.out.println("还没有下载完成,休眠五秒"); - //休眠5秒 - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - System.out.println("下载完成!"); - - - - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java deleted file mode 100644 index 0957eaf7f4..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/Connection.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.coderising.download.api; - -import java.io.IOException; - -public interface Connection { - /** - * 给定开始和结束位置, 读取数据, 返回值是字节数组 - * @param startPos 开始位置, 从0开始 - * @param endPos 结束位置 - * @return - */ - public byte[] read(int startPos,int endPos) throws IOException; - /** - * 得到数据内容的长度 - * @return - */ - public int getContentLength(); - - /** - * 关闭连接 - */ - public void close(); -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java deleted file mode 100644 index 1551a80b3d..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionException.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.coderising.download.api; - -public class ConnectionException extends Exception { - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java deleted file mode 100644 index ce045393b1..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/ConnectionManager.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.coderising.download.api; - -public interface ConnectionManager { - /** - * 给定一个url , 打开一个连接 - * @param url - * @return - */ - public Connection open(String url) throws ConnectionException; -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java deleted file mode 100644 index bf9807b307..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/api/DownloadListener.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.coderising.download.api; - -public interface DownloadListener { - public void notifyFinished(); -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java deleted file mode 100644 index 36a9d2ce15..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionImpl.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.coderising.download.impl; - -import java.io.IOException; - -import com.coderising.download.api.Connection; - -public class ConnectionImpl implements Connection{ - - @Override - public byte[] read(int startPos, int endPos) throws IOException { - - return null; - } - - @Override - public int getContentLength() { - - return 0; - } - - @Override - public void close() { - - - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java deleted file mode 100644 index 172371dd55..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.coderising.download.impl; - -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; - -public class ConnectionManagerImpl implements ConnectionManager { - - @Override - public Connection open(String url) throws ConnectionException { - - return null; - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java deleted file mode 100644 index dcdbe226ed..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/LoginAction.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.coderising.litestruts; - -/** - * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 - * @author liuxin - * - */ -public class LoginAction{ - private String name ; - private String password; - private String message; - - public String getName() { - return name; - } - - public String getPassword() { - return password; - } - - public String execute(){ - if("test".equals(name) && "1234".equals(password)){ - this.message = "login successful"; - return "success"; - } - this.message = "login failed,please check your user/pwd"; - return "fail"; - } - - public void setName(String name){ - this.name = name; - } - public void setPassword(String password){ - this.password = password; - } - public String getMessage(){ - return this.message; - } -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java deleted file mode 100644 index 85e2e22de3..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/Struts.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.coderising.litestruts; - -import java.util.Map; - - - -public class Struts { - - public static View runAction(String actionName, Map parameters) { - - /* - - 0. 读取配置文件struts.xml - - 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) - 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 - ("name"="test" , "password"="1234") , - 那就应该调用 setName和setPassword方法 - - 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" - - 3. 通过反射找到对象的所有getter方法(例如 getMessage), - 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , - 放到View对象的parameters - - 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, - 放到View对象的jsp字段中。 - - */ - - return null; - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java deleted file mode 100644 index b8c81faf3c..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/StrutsTest.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.coderising.litestruts; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.Assert; -import org.junit.Test; - - - - - -public class StrutsTest { - - @Test - public void testLoginActionSuccess() { - - String actionName = "login"; - - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); - Assert.assertEquals("login successful", view.getParameters().get("message")); - } - - @Test - public void testLoginActionFailed() { - String actionName = "login"; - Map params = new HashMap(); - params.put("name","test"); - params.put("password","123456"); //密码和预设的不一致 - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); - Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); - } -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/View.java b/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/View.java deleted file mode 100644 index 07df2a5dab..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/View.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.coderising.litestruts; - -import java.util.Map; - -public class View { - private String jsp; - private Map parameters; - - public String getJsp() { - return jsp; - } - public View setJsp(String jsp) { - this.jsp = jsp; - return this; - } - public Map getParameters() { - return parameters; - } - public View setParameters(Map parameters) { - this.parameters = parameters; - return this; - } -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml b/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml deleted file mode 100644 index e5d9aebba8..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coderising/litestruts/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - \ No newline at end of file diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/Iterator.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/Iterator.java deleted file mode 100644 index 06ef6311b2..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/List.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/List.java deleted file mode 100644 index 10d13b5832..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.coding.basic; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java deleted file mode 100644 index 4576c016af..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/array/ArrayList.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.coding.basic.array; - -import com.coding.basic.Iterator; -import com.coding.basic.List; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o){ - - } - public void add(int index, Object o){ - - } - - public Object get(int index){ - return null; - } - - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public Iterator iterator(){ - return null; - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java deleted file mode 100644 index 45740e6d57..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/array/ArrayUtil.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.coding.basic.array; - -public class ArrayUtil { - - /** - * 给定一个整形数组a , 对该数组的值进行置换 - 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] - 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * @param origin - * @return - */ - public void reverseArray(int[] origin){ - - } - - /** - * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: - * {1,3,4,5,6,6,5,4,7,6,7,5} - * @param oldArray - * @return - */ - - public int[] removeZero(int[] oldArray){ - return null; - } - - /** - * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 - * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 - * @param array1 - * @param array2 - * @return - */ - - public int[] merge(int[] array1, int[] array2){ - return null; - } - /** - * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 - * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 - * [2,3,6,0,0,0] - * @param oldArray - * @param size - * @return - */ - public int[] grow(int [] oldArray, int size){ - return null; - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 - * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] - * max = 1, 则返回空数组 [] - * @param max - * @return - */ - public int[] fibonacci(int max){ - return null; - } - - /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * @param max - * @return - */ - public int[] getPrimes(int max){ - return null; - } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * @param max - * @return - */ - public int[] getPerfectNumbers(int max){ - return null; - } - - /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" - * @param array - * @param s - * @return - */ - public String join(int[] array, String seperator){ - return null; - } - - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java deleted file mode 100644 index 24b9d8b155..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrame.java +++ /dev/null @@ -1,164 +0,0 @@ -package com.coding.basic.linklist; - - -public class LRUPageFrame { - - private static class Node { - - Node prev; - Node next; - int pageNum; - - Node() { - } - } - - private int capacity; - - private int currentSize; - private Node first;// 链表头 - private Node last;// 链表尾 - - - public LRUPageFrame(int capacity) { - this.currentSize = 0; - this.capacity = capacity; - - } - - /** - * 获取缓存中对象 - * - * @param key - * @return - */ - public void access(int pageNum) { - - Node node = find(pageNum); - //在该队列中存在, 则提到队列头 - if (node != null) { - - moveExistingNodeToHead(node); - - } else{ - - node = new Node(); - node.pageNum = pageNum; - - // 缓存容器是否已经超过大小. - if (currentSize >= capacity) { - removeLast(); - - } - - addNewNodetoHead(node); - - - - - } - } - - private void addNewNodetoHead(Node node) { - - if(isEmpty()){ - - node.prev = null; - node.next = null; - first = node; - last = node; - - } else{ - node.prev = null; - node.next = first; - first.prev = node; - first = node; - } - this.currentSize ++; - } - - private Node find(int data){ - - Node node = first; - while(node != null){ - if(node.pageNum == data){ - return node; - } - node = node.next; - } - return null; - - } - - - - - - - /** - * 删除链表尾部节点 表示 删除最少使用的缓存对象 - */ - private void removeLast() { - Node prev = last.prev; - prev.next = null; - last.prev = null; - last = prev; - this.currentSize --; - } - - /** - * 移动到链表头,表示这个节点是最新使用过的 - * - * @param node - */ - private void moveExistingNodeToHead(Node node) { - - if (node == first) { - - return; - } - else if(node == last){ - //当前节点是链表尾, 需要放到链表头 - Node prevNode = node.prev; - prevNode.next = null; - last.prev = null; - last = prevNode; - - } else{ - //node 在链表的中间, 把node 的前后节点连接起来 - Node prevNode = node.prev; - prevNode.next = node.next; - - Node nextNode = node.next; - nextNode.prev = prevNode; - - - } - - node.prev = null; - node.next = first; - first.prev = node; - first = node; - - } - private boolean isEmpty(){ - return (first == null) && (last == null); - } - - public String toString(){ - StringBuilder buffer = new StringBuilder(); - Node node = first; - while(node != null){ - buffer.append(node.pageNum); - - node = node.next; - if(node != null){ - buffer.append(","); - } - } - return buffer.toString(); - } - - - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java deleted file mode 100644 index 7fd72fc2b4..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.coding.basic.linklist; - -import org.junit.Assert; - -import org.junit.Test; - - -public class LRUPageFrameTest { - - @Test - public void testAccess() { - LRUPageFrame frame = new LRUPageFrame(3); - frame.access(7); - frame.access(0); - frame.access(1); - Assert.assertEquals("1,0,7", frame.toString()); - frame.access(2); - Assert.assertEquals("2,1,0", frame.toString()); - frame.access(0); - Assert.assertEquals("0,2,1", frame.toString()); - frame.access(0); - Assert.assertEquals("0,2,1", frame.toString()); - frame.access(3); - Assert.assertEquals("3,0,2", frame.toString()); - frame.access(0); - Assert.assertEquals("0,3,2", frame.toString()); - frame.access(4); - Assert.assertEquals("4,0,3", frame.toString()); - frame.access(5); - Assert.assertEquals("5,4,0", frame.toString()); - - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java deleted file mode 100644 index f4c7556a2e..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/linklist/LinkedList.java +++ /dev/null @@ -1,125 +0,0 @@ -package com.coding.basic.linklist; - -import com.coding.basic.Iterator; -import com.coding.basic.List; - -public class LinkedList implements List { - - private Node head; - - public void add(Object o){ - - } - public void add(int index , Object o){ - - } - public Object get(int index){ - return null; - } - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public void addFirst(Object o){ - - } - public void addLast(Object o){ - - } - public Object removeFirst(){ - return null; - } - public Object removeLast(){ - return null; - } - public Iterator iterator(){ - return null; - } - - - private static class Node{ - Object data; - Node next; - - } - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse(){ - - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - - */ - public void removeFirstHalf(){ - - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - */ - public void remove(int i, int length){ - - } - /** - * 假定当前链表和listB均包含已升序排列的整数 - * 从当前链表中取出那些listB所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - */ - public int[] getElements(LinkedList list){ - return null; - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在listB中出现的元素 - - * @param list - */ - - public void subtract(LinkedList list){ - - } - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues(){ - - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * @param min - * @param max - */ - public void removeRange(int min, int max){ - - } - - /** - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - * @param list - */ - public LinkedList intersection( LinkedList list){ - return null; - } -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java deleted file mode 100644 index f169d5f8e4..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueue.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.coding.basic.queue; - -public class CircleQueue { - - //用数组来保存循环队列的元素 - private Object[] elementData ; - int size = 0; - //队头 - private int front = 0; - //队尾 - private int rear = 0; - - public CircleQueue(int capacity){ - elementData = new Object[capacity]; - } - public boolean isEmpty() { - return (front == rear) && !isFull(); - - } - - public boolean isFull(){ - return size == elementData.length; - } - public int size() { - return size; - } - - public void enQueue(E data) { - if(isFull()){ - throw new RuntimeException("The queue is full"); - } - rear = (rear+1) % elementData.length; - elementData[rear++] = data; - size++; - } - - public E deQueue() { - if(isEmpty()){ - throw new RuntimeException("The queue is empty"); - } - E data = (E)elementData[front]; - elementData[front] = null; - front = (front+1) % elementData.length; - size --; - return data; - } -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java deleted file mode 100644 index 7307eb77d4..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/CircleQueueTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.coding.basic.queue; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class CircleQueueTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void test() { - CircleQueue queue = new CircleQueue(5); - Assert.assertTrue(queue.isEmpty()); - Assert.assertFalse(queue.isFull()); - - queue.enQueue("a"); - queue.enQueue("b"); - queue.enQueue("c"); - queue.enQueue("d"); - queue.enQueue("e"); - - Assert.assertTrue(queue.isFull()); - Assert.assertFalse(queue.isEmpty()); - Assert.assertEquals(5, queue.size()); - - Assert.assertEquals("a", queue.deQueue()); - Assert.assertEquals("b", queue.deQueue()); - Assert.assertEquals("c", queue.deQueue()); - Assert.assertEquals("d", queue.deQueue()); - Assert.assertEquals("e", queue.deQueue()); - - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java deleted file mode 100644 index 36ec615d36..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/Josephus.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.coding.basic.queue; - -import java.util.ArrayList; -import java.util.List; - -/** - * 用Queue来实现Josephus问题 - * 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来 - * @author liuxin - * - */ -public class Josephus { - - public static List execute(int n, int m){ - - Queue queue = new Queue(); - for (int i = 0; i < n; i++){ - queue.enQueue(i); - } - - List result = new ArrayList(); - int i = 0; - - while (!queue.isEmpty()) { - - int x = queue.deQueue(); - - if (++i % m == 0){ - result.add(x); - } else{ - queue.enQueue(x); - } - } - - - return result; - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java deleted file mode 100644 index 7d90318b51..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/JosephusTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.coding.basic.queue; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class JosephusTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testExecute() { - - Assert.assertEquals("[1, 3, 5, 0, 4, 2, 6]", Josephus.execute(7, 2).toString()); - - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java deleted file mode 100644 index c4c4b7325e..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/Queue.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.coding.basic.queue; - -import java.util.NoSuchElementException; - -public class Queue { - private Node first; - private Node last; - private int size; - - - private static class Node { - private E item; - private Node next; - } - - - public Queue() { - first = null; - last = null; - size = 0; - } - - - public boolean isEmpty() { - return first == null; - } - - public int size() { - return size; - } - - - - public void enQueue(E data) { - Node oldlast = last; - last = new Node(); - last.item = data; - last.next = null; - if (isEmpty()) { - first = last; - } - else{ - oldlast.next = last; - } - size++; - } - - public E deQueue() { - if (isEmpty()) { - throw new NoSuchElementException("Queue underflow"); - } - E item = first.item; - first = first.next; - size--; - if (isEmpty()) { - last = null; - } - return item; - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java deleted file mode 100644 index bc97df0800..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.coding.basic.queue; - -import java.util.NoSuchElementException; -import java.util.Stack; - -public class QueueWithTwoStacks { - private Stack stack1; - private Stack stack2; - - - public QueueWithTwoStacks() { - stack1 = new Stack(); - stack2 = new Stack(); - } - - - private void moveStack1ToStack2() { - while (!stack1.isEmpty()){ - stack2.push(stack1.pop()); - } - - } - - - public boolean isEmpty() { - return stack1.isEmpty() && stack2.isEmpty(); - } - - - - public int size() { - return stack1.size() + stack2.size(); - } - - - - public void enQueue(E item) { - stack1.push(item); - } - - public E deQueue() { - if (isEmpty()) { - throw new NoSuchElementException("Queue is empty"); - } - if (stack2.isEmpty()) { - moveStack1ToStack2(); - } - - return stack2.pop(); - } - - - - } - diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java deleted file mode 100644 index faf2644ab1..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStack.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.coding.basic.stack; - -import java.util.Stack; -/** - * 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素 - * finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值 - * @author liuxin - * - */ -public class QuickMinStack { - - private Stack normalStack = new Stack(); - private Stack minNumStack = new Stack(); - - public void push(int data){ - - normalStack.push(data); - - if(minNumStack.isEmpty()){ - minNumStack.push(data); - } else{ - if(minNumStack.peek() >= data) { - minNumStack.push(data); - } - } - - } - public int pop(){ - if(normalStack.isEmpty()){ - throw new RuntimeException("the stack is empty"); - } - int value = normalStack.pop(); - if(value == minNumStack.peek()){ - minNumStack.pop(); - } - return value; - } - public int findMin(){ - if(minNumStack.isEmpty()){ - throw new RuntimeException("the stack is empty"); - } - return minNumStack.peek(); - } -} \ No newline at end of file diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java deleted file mode 100644 index efe41a9f8f..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/QuickMinStackTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.coding.basic.stack; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - -public class QuickMinStackTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void test() { - QuickMinStack stack = new QuickMinStack(); - stack.push(5); - Assert.assertEquals(5, stack.findMin()); - stack.push(6); - Assert.assertEquals(5, stack.findMin()); - stack.push(4); - Assert.assertEquals(4, stack.findMin()); - stack.push(4); - Assert.assertEquals(4, stack.findMin()); - - stack.pop(); - Assert.assertEquals(4, stack.findMin()); - stack.pop(); - Assert.assertEquals(5, stack.findMin()); - stack.pop(); - Assert.assertEquals(5, stack.findMin()); - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java deleted file mode 100644 index fedb243604..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/Stack.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.coding.basic.stack; - -import com.coding.basic.array.ArrayList; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - } - - public Object pop(){ - return null; - } - - public Object peek(){ - return null; - } - public boolean isEmpty(){ - return false; - } - public int size(){ - return -1; - } -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java deleted file mode 100644 index 7c86d22fe7..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackUtil.java +++ /dev/null @@ -1,168 +0,0 @@ -package com.coding.basic.stack; -import java.util.Stack; -public class StackUtil { - - public static void bad_reverse(Stack s) { - if(s == null || s.isEmpty()){ - return; - } - Stack tmpStack = new Stack(); - while(!s.isEmpty()){ - tmpStack.push(s.pop()); - } - - s = tmpStack; - - } - - - - public static void reverse_247565311(Stack s){ - if(s == null || s.isEmpty()) { - return; - } - - int size = s.size(); - Stack tmpStack = new Stack(); - - for(int i=0;ii){ - tmpStack.push(s.pop()); - } - s.push(top); - while(tmpStack.size()>0){ - s.push(tmpStack.pop()); - } - } - } - - - - /** - * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 - * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 - */ - public static void reverse(Stack s) { - if(s == null || s.isEmpty()){ - return; - } - - Stack tmp = new Stack(); - while(!s.isEmpty()){ - tmp.push(s.pop()); - } - while(!tmp.isEmpty()){ - Integer top = tmp.pop(); - addToBottom(s,top); - } - - - } - public static void addToBottom(Stack s, Integer value){ - if(s.isEmpty()){ - s.push(value); - } else{ - Integer top = s.pop(); - addToBottom(s,value); - s.push(top); - } - - } - /** - * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 - * - * @param o - */ - public static void remove(Stack s,Object o) { - if(s == null || s.isEmpty()){ - return; - } - Stack tmpStack = new Stack(); - - while(!s.isEmpty()){ - Object value = s.pop(); - if(!value.equals(o)){ - tmpStack.push(value); - } - } - - while(!tmpStack.isEmpty()){ - s.push(tmpStack.pop()); - } - } - - /** - * 从栈顶取得len个元素, 原来的栈中元素保持不变 - * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 - * @param len - * @return - */ - public static Object[] getTop(Stack s,int len) { - - if(s == null || s.isEmpty() || s.size() stack = new Stack(); - for(int i=0;i s = new Stack(); - s.push(1); - s.push(2); - s.push(3); - - StackUtil.addToBottom(s, 0); - - Assert.assertEquals("[0, 1, 2, 3]", s.toString()); - - } - @Test - public void testReverse() { - Stack s = new Stack(); - s.push(1); - s.push(2); - s.push(3); - s.push(4); - s.push(5); - Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); - StackUtil.reverse(s); - Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString()); - } - @Test - public void testReverse_247565311() { - Stack s = new Stack(); - s.push(1); - s.push(2); - s.push(3); - - Assert.assertEquals("[1, 2, 3]", s.toString()); - StackUtil.reverse_247565311(s); - Assert.assertEquals("[3, 2, 1]", s.toString()); - } - @Test - public void testRemove() { - Stack s = new Stack(); - s.push(1); - s.push(2); - s.push(3); - StackUtil.remove(s, 2); - Assert.assertEquals("[1, 3]", s.toString()); - } - - @Test - public void testGetTop() { - Stack s = new Stack(); - s.push(1); - s.push(2); - s.push(3); - s.push(4); - s.push(5); - { - Object[] values = StackUtil.getTop(s, 3); - Assert.assertEquals(5, values[0]); - Assert.assertEquals(4, values[1]); - Assert.assertEquals(3, values[2]); - } - } - - @Test - public void testIsValidPairs() { - Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])")); - Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})")); - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java deleted file mode 100644 index 7a58fbff56..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.coding.basic.stack; - -import java.util.ArrayDeque; -import java.util.Queue; - -public class StackWithTwoQueues { - Queue queue1 = new ArrayDeque<>(); - Queue queue2 = new ArrayDeque<>(); - - public void push(int data) { - //两个栈都为空时,优先考虑queue1 - if (queue1.isEmpty()&&queue2.isEmpty()) { - queue1.add(data); - return; - } - - if (queue1.isEmpty()) { - queue2.add(data); - return; - } - - if (queue2.isEmpty()) { - queue1.add(data); - return; - } - - } - - public int pop() { - - if (queue1.isEmpty()&&queue2.isEmpty()) { - throw new RuntimeException("stack is empty"); - } - - if (queue1.isEmpty()) { - while (queue2.size()>1) { - queue1.add(queue2.poll()); - } - return queue2.poll(); - } - - if (queue2.isEmpty()) { - while (queue1.size()>1) { - queue2.add(queue1.poll()); - } - return queue1.poll(); - } - - throw new RuntimeException("no queue is empty, this is not allowed"); - - - } -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java deleted file mode 100644 index 4541b1f040..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/StackWithTwoQueuesTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.coding.basic.stack; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class StackWithTwoQueuesTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void test() { - StackWithTwoQueues stack = new StackWithTwoQueues(); - stack.push(1); - stack.push(2); - stack.push(3); - stack.push(4); - Assert.assertEquals(4, stack.pop()); - Assert.assertEquals(3, stack.pop()); - - stack.push(5); - Assert.assertEquals(5, stack.pop()); - Assert.assertEquals(2, stack.pop()); - Assert.assertEquals(1, stack.pop()); - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java deleted file mode 100644 index 7f30ce55c8..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/Tail.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.coding.basic.stack; - -public class Tail { - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java deleted file mode 100644 index a532fd6e6c..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.coding.basic.stack; - -import java.util.Arrays; - -/** - * 用一个数组实现两个栈 - * 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。 - * @author liuxin - * - */ -public class TwoStackInOneArray { - private Object[] data = new Object[10]; - private int size; - private int top1, top2; - - public TwoStackInOneArray(int n){ - data = new Object[n]; - size = n; - top1 = -1; - top2 = data.length; - } - /** - * 向第一个栈中压入元素 - * @param o - */ - public void push1(Object o){ - ensureCapacity(); - data[++top1] = o; - } - /* - * 向第二个栈压入元素 - */ - public void push2(Object o){ - ensureCapacity(); - data[--top2] = o; - } - public void ensureCapacity(){ - if(top2-top1>1){ - return; - } else{ - - Object[] newArray = new Object[data.length*2]; - System.arraycopy(data, 0, newArray, 0, top1+1); - - int stack2Size = data.length-top2; - int newTop2 = newArray.length-stack2Size; - System.arraycopy(data, top2, newArray, newTop2, stack2Size); - - top2 = newTop2; - data = newArray; - } - } - /** - * 从第一个栈中弹出元素 - * @return - */ - public Object pop1(){ - if(top1 == -1){ - throw new RuntimeException("Stack1 is empty"); - } - Object o = data[top1]; - data[top1] = null; - top1--; - return o; - - } - /** - * 从第二个栈弹出元素 - * @return - */ - public Object pop2(){ - if(top2 == data.length){ - throw new RuntimeException("Stack2 is empty"); - } - Object o = data[top2]; - data[top2] = null; - top2++; - return o; - } - /** - * 获取第一个栈的栈顶元素 - * @return - */ - - public Object peek1(){ - if(top1 == -1){ - throw new RuntimeException("Stack1 is empty"); - } - return data[top1]; - } - - - /** - * 获取第二个栈的栈顶元素 - * @return - */ - - public Object peek2(){ - if(top2 == data.length){ - throw new RuntimeException("Stack2 is empty"); - } - return data[top2]; - } - - public Object[] stack1ToArray(){ - return Arrays.copyOf(data, top1+1); - } - public Object[] stack2ToArray(){ - int size = data.length-top2; - Object [] stack2Data = new Object[size]; - int j=0; - for(int i=data.length-1; i>=top2 ;i--){ - stack2Data[j++] = data[i]; - } - return stack2Data; - } -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java deleted file mode 100644 index b743d422c6..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/TwoStackInOneArrayTest.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.coding.basic.stack; - -import java.util.Arrays; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class TwoStackInOneArrayTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void test1() { - TwoStackInOneArray stack = new TwoStackInOneArray(10); - stack.push1(1); - stack.push1(2); - stack.push1(3); - stack.push1(4); - stack.push1(5); - - stack.push2(1); - stack.push2(2); - stack.push2(3); - stack.push2(4); - stack.push2(5); - - for(int i=1;i<=5;i++){ - Assert.assertEquals(stack.peek1(), stack.peek2()); - Assert.assertEquals(stack.pop1(), stack.pop2()); - } - - - } - @Test - public void test2() { - TwoStackInOneArray stack = new TwoStackInOneArray(5); - stack.push1(1); - stack.push1(2); - stack.push1(3); - stack.push1(4); - stack.push1(5); - stack.push1(6); - stack.push1(7); - - stack.push2(1); - stack.push2(2); - stack.push2(3); - stack.push2(4); - - - Assert.assertEquals("[1, 2, 3, 4, 5, 6, 7]",Arrays.toString(stack.stack1ToArray())); - Assert.assertEquals("[1, 2, 3, 4]",Arrays.toString(stack.stack2ToArray())); - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java deleted file mode 100644 index cebef21fa3..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExpr.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.List; -import java.util.Stack; - - -public class InfixExpr { - String expr = null; - - public InfixExpr(String expr) { - this.expr = expr; - } - - public float evaluate() { - - - TokenParser parser = new TokenParser(); - List tokens = parser.parse(this.expr); - - - Stack opStack = new Stack<>(); - Stack numStack = new Stack<>(); - - for(Token token : tokens){ - - if (token.isOperator()){ - - while(!opStack.isEmpty() - && !token.hasHigherPriority(opStack.peek())){ - Token prevOperator = opStack.pop(); - Float f2 = numStack.pop(); - Float f1 = numStack.pop(); - Float result = calculate(prevOperator.toString(), f1,f2); - numStack.push(result); - - } - opStack.push(token); - } - if(token.isNumber()){ - numStack.push(new Float(token.getIntValue())); - } - } - - while(!opStack.isEmpty()){ - Token token = opStack.pop(); - Float f2 = numStack.pop(); - Float f1 = numStack.pop(); - numStack.push(calculate(token.toString(), f1,f2)); - } - - - return numStack.pop().floatValue(); - } - private Float calculate(String op, Float f1, Float f2){ - if(op.equals("+")){ - return f1+f2; - } - if(op.equals("-")){ - return f1-f2; - } - if(op.equals("*")){ - return f1*f2; - } - if(op.equals("/")){ - return f1/f2; - } - throw new RuntimeException(op + " is not supported"); - } - - - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java deleted file mode 100644 index 20e34e8852..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.coding.basic.stack.expr; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - -public class InfixExprTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEvaluate() { - //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); - { - InfixExpr expr = new InfixExpr("2+3*4+5"); - Assert.assertEquals(19.0, expr.evaluate(), 0.001f); - } - { - InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); - Assert.assertEquals(100.0, expr.evaluate(), 0.001f); - } - - { - InfixExpr expr = new InfixExpr("3*20/2"); - Assert.assertEquals(30, expr.evaluate(), 0.001f); - } - - { - InfixExpr expr = new InfixExpr("20/2*3"); - Assert.assertEquals(30, expr.evaluate(), 0.001f); - } - - { - InfixExpr expr = new InfixExpr("10-30+50"); - Assert.assertEquals(30, expr.evaluate(), 0.001f); - } - { - InfixExpr expr = new InfixExpr("10-2*3+50"); - Assert.assertEquals(54, expr.evaluate(), 0.001f); - } - - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java deleted file mode 100644 index 9e501eda20..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -public class InfixToPostfix { - - public static List convert(String expr) { - List inFixTokens = new TokenParser().parse(expr); - - List postFixTokens = new ArrayList<>(); - - Stack opStack = new Stack(); - for(Token token : inFixTokens){ - - if(token.isOperator()){ - - while(!opStack.isEmpty() - && !token.hasHigherPriority(opStack.peek())){ - postFixTokens.add(opStack.pop()); - - } - opStack.push(token); - - } - if(token.isNumber()){ - - postFixTokens.add(token); - - } - } - - while(!opStack.isEmpty()){ - postFixTokens.add(opStack.pop()); - } - - return postFixTokens; - } - - - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java deleted file mode 100644 index f879f55f14..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/InfixToPostfixTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.List; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class InfixToPostfixTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testConvert() { - { - List tokens = InfixToPostfix.convert("2+3"); - Assert.assertEquals("[2, 3, +]", tokens.toString()); - } - { - - List tokens = InfixToPostfix.convert("2+3*4"); - Assert.assertEquals("[2, 3, 4, *, +]", tokens.toString()); - } - - { - - List tokens = InfixToPostfix.convert("2-3*4+5"); - Assert.assertEquals("[2, 3, 4, *, -, 5, +]", tokens.toString()); - } - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java deleted file mode 100644 index c54eb69e2a..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.List; -import java.util.Stack; - -public class PostfixExpr { -String expr = null; - - public PostfixExpr(String expr) { - this.expr = expr; - } - - public float evaluate() { - TokenParser parser = new TokenParser(); - List tokens = parser.parse(this.expr); - - - Stack numStack = new Stack<>(); - for(Token token : tokens){ - if(token.isNumber()){ - numStack.push(new Float(token.getIntValue())); - } else{ - Float f2 = numStack.pop(); - Float f1 = numStack.pop(); - numStack.push(calculate(token.toString(),f1,f2)); - } - } - return numStack.pop().floatValue(); - } - - private Float calculate(String op, Float f1, Float f2){ - if(op.equals("+")){ - return f1+f2; - } - if(op.equals("-")){ - return f1-f2; - } - if(op.equals("*")){ - return f1*f2; - } - if(op.equals("/")){ - return f1/f2; - } - throw new RuntimeException(op + " is not supported"); - } -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java deleted file mode 100644 index c0435a2db5..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.coding.basic.stack.expr; - - - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class PostfixExprTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEvaluate() { - { - PostfixExpr expr = new PostfixExpr("6 5 2 3 + 8 * + 3 + *"); - Assert.assertEquals(288, expr.evaluate(),0.0f); - } - { - //9+(3-1)*3+10/2 - PostfixExpr expr = new PostfixExpr("9 3 1-3*+ 10 2/+"); - Assert.assertEquals(20, expr.evaluate(),0.0f); - } - - { - //10-2*3+50 - PostfixExpr expr = new PostfixExpr("10 2 3 * - 50 +"); - Assert.assertEquals(54, expr.evaluate(),0.0f); - } - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java deleted file mode 100644 index f811fd6d9a..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.List; -import java.util.Stack; - -public class PrefixExpr { - String expr = null; - - public PrefixExpr(String expr) { - this.expr = expr; - } - - public float evaluate() { - TokenParser parser = new TokenParser(); - List tokens = parser.parse(this.expr); - - Stack exprStack = new Stack<>(); - Stack numStack = new Stack<>(); - for(Token token : tokens){ - exprStack.push(token); - } - - while(!exprStack.isEmpty()){ - Token t = exprStack.pop(); - if(t.isNumber()){ - numStack.push(new Float(t.getIntValue())); - }else{ - Float f1 = numStack.pop(); - Float f2 = numStack.pop(); - numStack.push(calculate(t.toString(),f1,f2)); - - } - } - return numStack.pop().floatValue(); - } - - private Float calculate(String op, Float f1, Float f2){ - if(op.equals("+")){ - return f1+f2; - } - if(op.equals("-")){ - return f1-f2; - } - if(op.equals("*")){ - return f1*f2; - } - if(op.equals("/")){ - return f1/f2; - } - throw new RuntimeException(op + " is not supported"); - } -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java deleted file mode 100644 index 5cec210e75..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.coding.basic.stack.expr; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - -public class PrefixExprTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEvaluate() { - { - // 2*3+4*5 - PrefixExpr expr = new PrefixExpr("+ * 2 3* 4 5"); - Assert.assertEquals(26, expr.evaluate(),0.001f); - } - { - // 4*2 + 6+9*2/3 -8 - PrefixExpr expr = new PrefixExpr("-++6/*2 9 3 * 4 2 8"); - Assert.assertEquals(12, expr.evaluate(),0.001f); - } - { - //(3+4)*5-6 - PrefixExpr expr = new PrefixExpr("- * + 3 4 5 6"); - Assert.assertEquals(29, expr.evaluate(),0.001f); - } - { - //1+((2+3)*4)-5 - PrefixExpr expr = new PrefixExpr("- + 1 * + 2 3 4 5"); - Assert.assertEquals(16, expr.evaluate(),0.001f); - } - - - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java deleted file mode 100644 index 8579743fe9..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/Token.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -class Token { - public static final List OPERATORS = Arrays.asList("+", "-", "*", "/"); - private static final Map priorities = new HashMap<>(); - static { - priorities.put("+", 1); - priorities.put("-", 1); - priorities.put("*", 2); - priorities.put("/", 2); - } - static final int OPERATOR = 1; - static final int NUMBER = 2; - String value; - int type; - public Token(int type, String value){ - this.type = type; - this.value = value; - } - - public boolean isNumber() { - return type == NUMBER; - } - - public boolean isOperator() { - return type == OPERATOR; - } - - public int getIntValue() { - return Integer.valueOf(value).intValue(); - } - public String toString(){ - return value; - } - - public boolean hasHigherPriority(Token t){ - if(!this.isOperator() && !t.isOperator()){ - throw new RuntimeException("numbers can't compare priority"); - } - return priorities.get(this.value) - priorities.get(t.value) > 0; - } - - - -} \ No newline at end of file diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java deleted file mode 100644 index d3b0f167e1..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParser.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.ArrayList; -import java.util.List; - -public class TokenParser { - - - public List parse(String expr) { - List tokens = new ArrayList<>(); - - int i = 0; - - while (i < expr.length()) { - - char c = expr.charAt(i); - - if (isOperator(c)) { - - Token t = new Token(Token.OPERATOR, String.valueOf(c)); - tokens.add(t); - i++; - - } else if (Character.isDigit(c)) { - - int nextOperatorIndex = indexOfNextOperator(i, expr); - String value = expr.substring(i, nextOperatorIndex); - Token t = new Token(Token.NUMBER, value); - tokens.add(t); - i = nextOperatorIndex; - - } else{ - System.out.println("char :["+c+"] is not number or operator,ignore"); - i++; - } - - } - return tokens; - } - - private int indexOfNextOperator(int i, String expr) { - - while (Character.isDigit(expr.charAt(i))) { - i++; - if (i == expr.length()) { - break; - } - } - return i; - - } - - private boolean isOperator(char c) { - String sc = String.valueOf(c); - return Token.OPERATORS.contains(sc); - } -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java deleted file mode 100644 index 399d3e857e..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.coding.basic.stack.expr; - -import static org.junit.Assert.*; - -import java.util.List; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -public class TokenParserTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void test() { - - TokenParser parser = new TokenParser(); - List tokens = parser.parse("300*20+12*5-20/4"); - - Assert.assertEquals(300, tokens.get(0).getIntValue()); - Assert.assertEquals("*", tokens.get(1).toString()); - Assert.assertEquals(20, tokens.get(2).getIntValue()); - Assert.assertEquals("+", tokens.get(3).toString()); - Assert.assertEquals(12, tokens.get(4).getIntValue()); - Assert.assertEquals("*", tokens.get(5).toString()); - Assert.assertEquals(5, tokens.get(6).getIntValue()); - Assert.assertEquals("-", tokens.get(7).toString()); - Assert.assertEquals(20, tokens.get(8).getIntValue()); - Assert.assertEquals("/", tokens.get(9).toString()); - Assert.assertEquals(4, tokens.get(10).getIntValue()); - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java deleted file mode 100644 index 284e5b0011..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTree.java +++ /dev/null @@ -1,189 +0,0 @@ -package com.coding.basic.tree; - -import java.util.ArrayList; -import java.util.List; - -import com.coding.basic.queue.Queue; - - -public class BinarySearchTree { - - BinaryTreeNode root; - public BinarySearchTree(BinaryTreeNode root){ - this.root = root; - } - public BinaryTreeNode getRoot(){ - return root; - } - public T findMin(){ - if(root == null){ - return null; - } - return findMin(root).data; - } - public T findMax(){ - if(root == null){ - return null; - } - return findMax(root).data; - } - public int height() { - return height(root); - } - public int size() { - return size(root); - } - public void remove(T e){ - remove(e, root); - } - - private BinaryTreeNode remove(T x, BinaryTreeNode t){ - if(t == null){ - return t; - } - int compareResult = x.compareTo(t.data); - - if(compareResult< 0 ){ - t.left = remove(x,t.left); - - } else if(compareResult > 0){ - t.right = remove(x, t.right); - - } else { - if(t.left != null && t.right != null){ - - t.data = findMin(t.right).data; - t.right = remove(t.data,t.right); - - } else{ - t = (t.left != null) ? t.left : t.right; - } - } - return t; - } - - private BinaryTreeNode findMin(BinaryTreeNode p){ - if (p==null){ - return null; - } else if (p.left == null){ - return p; - } else{ - return findMin(p.left); - } - } - private BinaryTreeNode findMax(BinaryTreeNode p){ - if (p==null){ - return null; - }else if (p.right==null){ - return p; - } else{ - return findMax(p.right); - } - } - private int height(BinaryTreeNode t){ - if (t==null){ - return 0; - }else { - int leftChildHeight=height(t.left); - int rightChildHeight=height(t.right); - if(leftChildHeight > rightChildHeight){ - return leftChildHeight+1; - } else{ - return rightChildHeight+1; - } - } - } - private int size(BinaryTreeNode t){ - if (t == null){ - return 0; - } - return size(t.left) + 1 + size(t.right); - - } - - public List levelVisit(){ - List result = new ArrayList(); - if(root == null){ - return result; - } - Queue> queue = new Queue>(); - BinaryTreeNode node = root; - queue.enQueue(node); - while (!queue.isEmpty()) { - node = queue.deQueue(); - result.add(node.data); - if (node.left != null){ - queue.enQueue(node.left); - } - if (node.right != null){ - queue.enQueue(node.right); - } - } - return result; - } - public boolean isValid(){ - return isValid(root); - } - public T getLowestCommonAncestor(T n1, T n2){ - if (root == null){ - return null; - } - return lowestCommonAncestor(root,n1,n2); - - } - public List getNodesBetween(T n1, T n2){ - List elements = new ArrayList<>(); - getNodesBetween(elements,root,n1,n2); - return elements; - } - - public void getNodesBetween(List elements ,BinaryTreeNode node, T n1, T n2){ - - if (node == null) { - return; - } - - if (n1.compareTo(node.data) < 0) { - getNodesBetween(elements,node.left, n1, n2); - } - - if ((n1.compareTo(node.data) <= 0 ) - && (n2.compareTo(node.data) >= 0 )) { - elements.add(node.data); - } - if (n2.compareTo(node.data)>0) { - getNodesBetween(elements,node.right, n1, n2); - } - } - private T lowestCommonAncestor(BinaryTreeNode node,T n1, T n2){ - if(node == null){ - return null; - } - // 如果n1和n2都比 node的值小, LCA在左孩子 - if (node.data.compareTo(n1) > 0 && node.data.compareTo(n2) >0){ - return lowestCommonAncestor(node.left, n1, n2); - } - - // 如果n1和n2都比 node的值小, LCA在右孩子 - if (node.data.compareTo(n1) < 0 && node.data.compareTo(n2) <0) - return lowestCommonAncestor(node.right, n1, n2); - - return node.data; - } - private boolean isValid(BinaryTreeNode t){ - if(t == null){ - return true; - } - if(t.left != null && findMax(t.left).data.compareTo(t.data) >0){ - return false; - } - if(t.right !=null && findMin(t.right).data.compareTo(t.data) <0){ - return false; - } - if(!isValid(t.left) || !isValid(t.right)){ - return false; - } - return true; - } -} - diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java deleted file mode 100644 index 590e60306c..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java +++ /dev/null @@ -1,108 +0,0 @@ -package com.coding.basic.tree; - -import java.util.List; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class BinarySearchTreeTest { - - BinarySearchTree tree = null; - - @Before - public void setUp() throws Exception { - BinaryTreeNode root = new BinaryTreeNode(6); - root.left = new BinaryTreeNode(2); - root.right = new BinaryTreeNode(8); - root.left.left = new BinaryTreeNode(1); - root.left.right = new BinaryTreeNode(4); - root.left.right.left = new BinaryTreeNode(3); - root.left.right.right = new BinaryTreeNode(5); - tree = new BinarySearchTree(root); - } - - @After - public void tearDown() throws Exception { - tree = null; - } - - @Test - public void testFindMin() { - Assert.assertEquals(1, tree.findMin().intValue()); - - } - - @Test - public void testFindMax() { - Assert.assertEquals(8, tree.findMax().intValue()); - } - - @Test - public void testHeight() { - Assert.assertEquals(4, tree.height()); - } - - @Test - public void testSize() { - Assert.assertEquals(7, tree.size()); - } - - @Test - public void testRemoveLeaf() { - tree.remove(3); - BinaryTreeNode root= tree.getRoot(); - Assert.assertEquals(4, root.left.right.data.intValue()); - - } - @Test - public void testRemoveMiddleNode1() { - tree.remove(4); - BinaryTreeNode root= tree.getRoot(); - Assert.assertEquals(5, root.left.right.data.intValue()); - Assert.assertEquals(3, root.left.right.left.data.intValue()); - } - @Test - public void testRemoveMiddleNode2() { - tree.remove(2); - BinaryTreeNode root= tree.getRoot(); - Assert.assertEquals(3, root.left.data.intValue()); - Assert.assertEquals(4, root.left.right.data.intValue()); - } - - @Test - public void testLevelVisit() { - List values = tree.levelVisit(); - Assert.assertEquals("[6, 2, 8, 1, 4, 3, 5]", values.toString()); - - } - @Test - public void testLCA(){ - Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 5).intValue()); - Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 4).intValue()); - Assert.assertEquals(6,tree.getLowestCommonAncestor(3, 8).intValue()); - } - @Test - public void testIsValid() { - - Assert.assertTrue(tree.isValid()); - - BinaryTreeNode root = new BinaryTreeNode(6); - root.left = new BinaryTreeNode(2); - root.right = new BinaryTreeNode(8); - root.left.left = new BinaryTreeNode(4); - root.left.right = new BinaryTreeNode(1); - root.left.right.left = new BinaryTreeNode(3); - tree = new BinarySearchTree(root); - - Assert.assertFalse(tree.isValid()); - } - @Test - public void testGetNodesBetween(){ - List numbers = this.tree.getNodesBetween(3, 8); - Assert.assertEquals("[3, 4, 5, 6, 8]",numbers.toString()); - } -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java deleted file mode 100644 index 3f6f4d2b44..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeNode.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.coding.basic.tree; - -public class BinaryTreeNode { - - public T data; - public BinaryTreeNode left; - public BinaryTreeNode right; - - public BinaryTreeNode(T data){ - this.data=data; - } - public T getData() { - return data; - } - public void setData(T data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java deleted file mode 100644 index f2a6515fa6..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.coding.basic.tree; - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -public class BinaryTreeUtil { - /** - * 用递归的方式实现对二叉树的前序遍历, 需要通过BinaryTreeUtilTest测试 - * - * @param root - * @return - */ - public static List preOrderVisit(BinaryTreeNode root) { - List result = new ArrayList(); - preOrderVisit(root, result); - return result; - } - - /** - * 用递归的方式实现对二叉树的中遍历 - * - * @param root - * @return - */ - public static List inOrderVisit(BinaryTreeNode root) { - List result = new ArrayList(); - inOrderVisit(root, result); - return result; - } - - /** - * 用递归的方式实现对二叉树的后遍历 - * - * @param root - * @return - */ - public static List postOrderVisit(BinaryTreeNode root) { - List result = new ArrayList(); - postOrderVisit(root, result); - return result; - } - - public static List preOrderWithoutRecursion(BinaryTreeNode root) { - - List result = new ArrayList(); - Stack> stack = new Stack>(); - - BinaryTreeNode node = root; - - if(node != null){ - stack.push(node); - } - - while(!stack.isEmpty()){ - node = stack.pop(); - result.add(node.data); - - if(node.right != null){ - stack.push(node.right); - } - - if(node.left != null){ - stack.push(node.right); - } - } - return result; - } - - public static List inOrderWithoutRecursion(BinaryTreeNode root) { - - List result = new ArrayList(); - BinaryTreeNode node = root; - Stack> stack = new Stack>(); - - while (node != null || !stack.isEmpty()) { - - while (node != null) { - stack.push(node); - node = node.left; - } - BinaryTreeNode currentNode = stack.pop(); - result.add(currentNode.data); - node = currentNode.right; - } - return result; - } - - private static void preOrderVisit(BinaryTreeNode node, List result) { - if (node == null) { - return; - } - result.add(node.getData()); - preOrderVisit(node.getLeft(), result); - preOrderVisit(node.getRight(), result); - } - - private static void inOrderVisit(BinaryTreeNode node, List result) { - if (node == null) { - return; - } - inOrderVisit(node.getLeft(), result); - result.add(node.getData()); - inOrderVisit(node.getRight(), result); - } - - private static void postOrderVisit(BinaryTreeNode node, List result) { - if (node == null) { - return; - } - postOrderVisit(node.getLeft(), result); - postOrderVisit(node.getRight(), result); - result.add(node.getData()); - } - -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java deleted file mode 100644 index 41857e137d..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.coding.basic.tree; - -import java.util.List; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class BinaryTreeUtilTest { - - BinaryTreeNode root = null; - @Before - public void setUp() throws Exception { - root = new BinaryTreeNode(1); - root.setLeft(new BinaryTreeNode(2)); - root.setRight(new BinaryTreeNode(5)); - root.getLeft().setLeft(new BinaryTreeNode(3)); - root.getLeft().setRight(new BinaryTreeNode(4)); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testPreOrderVisit() { - - List result = BinaryTreeUtil.preOrderVisit(root); - Assert.assertEquals("[1, 2, 3, 4, 5]", result.toString()); - - - } - @Test - public void testInOrderVisit() { - - - List result = BinaryTreeUtil.inOrderVisit(root); - Assert.assertEquals("[3, 2, 4, 1, 5]", result.toString()); - - } - - @Test - public void testPostOrderVisit() { - - - List result = BinaryTreeUtil.postOrderVisit(root); - Assert.assertEquals("[3, 4, 2, 5, 1]", result.toString()); - - } - - - @Test - public void testInOrderVisitWithoutRecursion() { - BinaryTreeNode node = root.getLeft().getRight(); - node.setLeft(new BinaryTreeNode(6)); - node.setRight(new BinaryTreeNode(7)); - - List result = BinaryTreeUtil.inOrderWithoutRecursion(root); - Assert.assertEquals("[3, 2, 6, 4, 7, 1, 5]", result.toString()); - - } - @Test - public void testPreOrderVisitWithoutRecursion() { - BinaryTreeNode node = root.getLeft().getRight(); - node.setLeft(new BinaryTreeNode(6)); - node.setRight(new BinaryTreeNode(7)); - - List result = BinaryTreeUtil.preOrderWithoutRecursion(root); - Assert.assertEquals("[1, 2, 3, 4, 6, 7, 5]", result.toString()); - - } -} diff --git a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java b/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java deleted file mode 100644 index 85fb8ab2a4..0000000000 --- a/students/34594980/data-structure/answer/src/main/java/com/coding/basic/tree/FileList.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.coding.basic.tree; - -import java.io.File; - -public class FileList { - public void list(File f) { - list(f, 0); - } - - public void list(File f, int depth) { - printName(f, depth); - if (f.isDirectory()) { - File[] files = f.listFiles(); - for (File i : files) - list(i, depth + 1); - } - } - - void printName(File f, int depth) { - String name = f.getName(); - for (int i = 0; i < depth; i++) - System.out.print("+"); - if (f.isDirectory()) - System.out.println("Dir: " + name); - else - System.out.println(f.getName() + " " + f.length()); - } - - public static void main(String args[]) { - FileList L = new FileList(); - File f = new File("C:\\coderising\\tmp"); - L.list(f); - } -} diff --git a/students/34594980/data-structure/assignment/pom.xml b/students/34594980/data-structure/assignment/pom.xml deleted file mode 100644 index 5024466d17..0000000000 --- a/students/34594980/data-structure/assignment/pom.xml +++ /dev/null @@ -1,32 +0,0 @@ - - 4.0.0 - - com.coderising - ds-assignment - 0.0.1-SNAPSHOT - jar - - ds-assignment - http://maven.apache.org - - - UTF-8 - - - - - - junit - junit - 4.12 - - - - - - aliyunmaven - http://maven.aliyun.com/nexus/content/groups/public/ - - - diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/DownloadThread.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/DownloadThread.java deleted file mode 100644 index 900a3ad358..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/DownloadThread.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.coderising.download; - -import com.coderising.download.api.Connection; - -public class DownloadThread extends Thread{ - - Connection conn; - int startPos; - int endPos; - - public DownloadThread( Connection conn, int startPos, int endPos){ - - this.conn = conn; - this.startPos = startPos; - this.endPos = endPos; - } - public void run(){ - - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/FileDownloader.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/FileDownloader.java deleted file mode 100644 index c3c8a3f27d..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/FileDownloader.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.coderising.download; - -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; - - -public class FileDownloader { - - String url; - - DownloadListener listener; - - ConnectionManager cm; - - - public FileDownloader(String _url) { - this.url = _url; - - } - - public void execute(){ - // 在这里实现你的代码, 注意: 需要用多线程实现下载 - // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 - // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) - // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 - // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 - // 具体的实现思路: - // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 - // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 - // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 - // 3. 把byte数组写入到文件中 - // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 - - // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 - Connection conn = null; - try { - - conn = cm.open(this.url); - - int length = conn.getContentLength(); - - new DownloadThread(conn,0,length-1).start(); - - } catch (ConnectionException e) { - e.printStackTrace(); - }finally{ - if(conn != null){ - conn.close(); - } - } - - - - - } - - public void setListener(DownloadListener listener) { - this.listener = listener; - } - - - - public void setConnectionManager(ConnectionManager ucm){ - this.cm = ucm; - } - - public DownloadListener getListener(){ - return this.listener; - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/FileDownloaderTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/FileDownloaderTest.java deleted file mode 100644 index 4ff7f46ae0..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/FileDownloaderTest.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.coderising.download; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.coderising.download.api.ConnectionManager; -import com.coderising.download.api.DownloadListener; -import com.coderising.download.impl.ConnectionManagerImpl; - -public class FileDownloaderTest { - boolean downloadFinished = false; - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testDownload() { - - String url = "http://localhost:8080/test.jpg"; - - FileDownloader downloader = new FileDownloader(url); - - - ConnectionManager cm = new ConnectionManagerImpl(); - downloader.setConnectionManager(cm); - - downloader.setListener(new DownloadListener() { - @Override - public void notifyFinished() { - downloadFinished = true; - } - - }); - - - downloader.execute(); - - // 等待多线程下载程序执行完毕 - while (!downloadFinished) { - try { - System.out.println("还没有下载完成,休眠五秒"); - //休眠5秒 - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - System.out.println("下载完成!"); - - - - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/Connection.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/Connection.java deleted file mode 100644 index 0957eaf7f4..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/Connection.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.coderising.download.api; - -import java.io.IOException; - -public interface Connection { - /** - * 给定开始和结束位置, 读取数据, 返回值是字节数组 - * @param startPos 开始位置, 从0开始 - * @param endPos 结束位置 - * @return - */ - public byte[] read(int startPos,int endPos) throws IOException; - /** - * 得到数据内容的长度 - * @return - */ - public int getContentLength(); - - /** - * 关闭连接 - */ - public void close(); -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionException.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionException.java deleted file mode 100644 index 1551a80b3d..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionException.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.coderising.download.api; - -public class ConnectionException extends Exception { - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionManager.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionManager.java deleted file mode 100644 index ce045393b1..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/ConnectionManager.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.coderising.download.api; - -public interface ConnectionManager { - /** - * 给定一个url , 打开一个连接 - * @param url - * @return - */ - public Connection open(String url) throws ConnectionException; -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/DownloadListener.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/DownloadListener.java deleted file mode 100644 index bf9807b307..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/api/DownloadListener.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.coderising.download.api; - -public interface DownloadListener { - public void notifyFinished(); -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionImpl.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionImpl.java deleted file mode 100644 index 36a9d2ce15..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionImpl.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.coderising.download.impl; - -import java.io.IOException; - -import com.coderising.download.api.Connection; - -public class ConnectionImpl implements Connection{ - - @Override - public byte[] read(int startPos, int endPos) throws IOException { - - return null; - } - - @Override - public int getContentLength() { - - return 0; - } - - @Override - public void close() { - - - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java deleted file mode 100644 index 172371dd55..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/download/impl/ConnectionManagerImpl.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.coderising.download.impl; - -import com.coderising.download.api.Connection; -import com.coderising.download.api.ConnectionException; -import com.coderising.download.api.ConnectionManager; - -public class ConnectionManagerImpl implements ConnectionManager { - - @Override - public Connection open(String url) throws ConnectionException { - - return null; - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/LoginAction.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/LoginAction.java deleted file mode 100644 index dcdbe226ed..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/LoginAction.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.coderising.litestruts; - -/** - * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 - * @author liuxin - * - */ -public class LoginAction{ - private String name ; - private String password; - private String message; - - public String getName() { - return name; - } - - public String getPassword() { - return password; - } - - public String execute(){ - if("test".equals(name) && "1234".equals(password)){ - this.message = "login successful"; - return "success"; - } - this.message = "login failed,please check your user/pwd"; - return "fail"; - } - - public void setName(String name){ - this.name = name; - } - public void setPassword(String password){ - this.password = password; - } - public String getMessage(){ - return this.message; - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/Struts.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/Struts.java deleted file mode 100644 index 85e2e22de3..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/Struts.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.coderising.litestruts; - -import java.util.Map; - - - -public class Struts { - - public static View runAction(String actionName, Map parameters) { - - /* - - 0. 读取配置文件struts.xml - - 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) - 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 - ("name"="test" , "password"="1234") , - 那就应该调用 setName和setPassword方法 - - 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" - - 3. 通过反射找到对象的所有getter方法(例如 getMessage), - 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , - 放到View对象的parameters - - 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, - 放到View对象的jsp字段中。 - - */ - - return null; - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/StrutsTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/StrutsTest.java deleted file mode 100644 index b8c81faf3c..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/StrutsTest.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.coderising.litestruts; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.Assert; -import org.junit.Test; - - - - - -public class StrutsTest { - - @Test - public void testLoginActionSuccess() { - - String actionName = "login"; - - Map params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); - Assert.assertEquals("login successful", view.getParameters().get("message")); - } - - @Test - public void testLoginActionFailed() { - String actionName = "login"; - Map params = new HashMap(); - params.put("name","test"); - params.put("password","123456"); //密码和预设的不一致 - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); - Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/View.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/View.java deleted file mode 100644 index 07df2a5dab..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/View.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.coderising.litestruts; - -import java.util.Map; - -public class View { - private String jsp; - private Map parameters; - - public String getJsp() { - return jsp; - } - public View setJsp(String jsp) { - this.jsp = jsp; - return this; - } - public Map getParameters() { - return parameters; - } - public View setParameters(Map parameters) { - this.parameters = parameters; - return this; - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/struts.xml b/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/struts.xml deleted file mode 100644 index e5d9aebba8..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/litestruts/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - \ No newline at end of file diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Course.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Course.java deleted file mode 100644 index 436d092f58..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Course.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.coderising.ood.course.bad; - -import java.util.List; - -public class Course { - private String id; - private String desc; - private int duration ; - - List prerequisites; - - public List getPrerequisites() { - return prerequisites; - } - - - public boolean equals(Object o){ - if(o == null || !(o instanceof Course)){ - return false; - } - Course c = (Course)o; - return (c != null) && c.id.equals(id); - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseOffering.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseOffering.java deleted file mode 100644 index ab8c764584..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseOffering.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.coderising.ood.course.bad; - -import java.util.ArrayList; -import java.util.List; - - -public class CourseOffering { - private Course course; - private String location; - private String teacher; - private int maxStudents; - - List students = new ArrayList(); - - public int getMaxStudents() { - return maxStudents; - } - - public List getStudents() { - return students; - } - - public Course getCourse() { - return course; - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseService.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseService.java deleted file mode 100644 index 8c34bad0c3..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/CourseService.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.coderising.ood.course.bad; - - - -public class CourseService { - - public void chooseCourse(Student student, CourseOffering sc){ - //如果学生上过该科目的先修科目,并且该课程还未满, 则学生可以加入该课程 - if(student.getCoursesAlreadyTaken().containsAll( - sc.getCourse().getPrerequisites()) - && sc.getMaxStudents() > sc.getStudents().size()){ - sc.getStudents().add(student); - } - - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Student.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Student.java deleted file mode 100644 index a651923ef5..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/bad/Student.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.coderising.ood.course.bad; - -import java.util.ArrayList; -import java.util.List; - -public class Student { - private String id; - private String name; - private List coursesAlreadyTaken = new ArrayList(); - - public List getCoursesAlreadyTaken() { - return coursesAlreadyTaken; - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Course.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Course.java deleted file mode 100644 index aefc9692bb..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Course.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.coderising.ood.course.good; - -import java.util.List; - -public class Course { - private String id; - private String desc; - private int duration ; - - List prerequisites; - - public List getPrerequisites() { - return prerequisites; - } - -} - - diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseOffering.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseOffering.java deleted file mode 100644 index 8660ec8109..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseOffering.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.coderising.ood.course.good; - -import java.util.ArrayList; -import java.util.List; - -public class CourseOffering { - private Course course; - private String location; - private String teacher; - private int maxStudents; - - List students = new ArrayList(); - - public List getStudents() { - return students; - } - public int getMaxStudents() { - return maxStudents; - } - public Course getCourse() { - return course; - } - - - // 第二步: 把主要逻辑移动到CourseOffering 中 - public void addStudent(Student student){ - - if(student.canAttend(course) - && this.maxStudents > students.size()){ - students.add(student); - } - } - // 第三步: 重构CourseService -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseService.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseService.java deleted file mode 100644 index 22ba4a5450..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/CourseService.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.coderising.ood.course.good; - - - -public class CourseService { - - public void chooseCourse(Student student, CourseOffering sc){ - //第一步:重构: canAttend , 但是还有问题 - if(student.canAttend(sc.getCourse()) - && sc.getMaxStudents() > sc.getStudents().size()){ - sc.getStudents().add(student); - } - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Student.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Student.java deleted file mode 100644 index 2c7e128b2a..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/course/good/Student.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.coderising.ood.course.good; - -import java.util.ArrayList; -import java.util.List; - -public class Student { - private String id; - private String name; - private List coursesAlreadyTaken = new ArrayList(); - - public List getCoursesAlreadyTaken() { - return coursesAlreadyTaken; - } - - public boolean canAttend(Course course){ - return this.coursesAlreadyTaken.containsAll( - course.getPrerequisites()); - } -} - - diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java deleted file mode 100644 index b6cf28c096..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.coderising.ood.ocp; - -public class DateUtil { - - public static String getCurrentDateAsString() { - - return null; - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/Logger.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/Logger.java deleted file mode 100644 index 0357c4d912..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/Logger.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.coderising.ood.ocp; - -public class Logger { - - public final int RAW_LOG = 1; - public final int RAW_LOG_WITH_DATE = 2; - public final int EMAIL_LOG = 1; - public final int SMS_LOG = 2; - public final int PRINT_LOG = 3; - - int type = 0; - int method = 0; - - public Logger(int logType, int logMethod){ - this.type = logType; - this.method = logMethod; - } - public void log(String msg){ - - String logMsg = msg; - - if(this.type == RAW_LOG){ - logMsg = msg; - } else if(this.type == RAW_LOG_WITH_DATE){ - String txtDate = DateUtil.getCurrentDateAsString(); - logMsg = txtDate + ": " + msg; - } - - if(this.method == EMAIL_LOG){ - MailUtil.send(logMsg); - } else if(this.method == SMS_LOG){ - SMSUtil.send(logMsg); - } else if(this.method == PRINT_LOG){ - System.out.println(logMsg); - } - } -} - diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java deleted file mode 100644 index ec54b839c5..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.coderising.ood.ocp; - -public class MailUtil { - - public static void send(String logMsg) { - // TODO Auto-generated method stub - - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java deleted file mode 100644 index 13cf802418..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.coderising.ood.ocp; - -public class SMSUtil { - - public static void send(String logMsg) { - // TODO Auto-generated method stub - - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/DBUtil.java deleted file mode 100644 index 82e9261d18..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/DBUtil.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.coderising.ood.srp; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - -public class DBUtil { - - /** - * 应该从数据库读, 但是简化为直接生成。 - * @param sql - * @return - */ - public static List query(String sql){ - - List userList = new ArrayList(); - for (int i = 1; i <= 3; i++) { - HashMap userInfo = new HashMap(); - userInfo.put("NAME", "User" + i); - userInfo.put("EMAIL", "aa@bb.com"); - userList.add(userInfo); - } - - return userList; - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/MailUtil.java deleted file mode 100644 index 9f9e749af7..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/MailUtil.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.coderising.ood.srp; - -public class MailUtil { - - public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, - boolean debug) { - //假装发了一封邮件 - StringBuilder buffer = new StringBuilder(); - buffer.append("From:").append(fromAddress).append("\n"); - buffer.append("To:").append(toAddress).append("\n"); - buffer.append("Subject:").append(subject).append("\n"); - buffer.append("Content:").append(message).append("\n"); - System.out.println(buffer.toString()); - - } - - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java deleted file mode 100644 index 781587a846..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ /dev/null @@ -1,199 +0,0 @@ -package com.coderising.ood.srp; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.Serializable; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; - -public class PromotionMail { - - - protected String sendMailQuery = null; - - - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; - protected String toAddress = null; - protected String subject = null; - protected String message = null; - - protected String productID = null; - protected String productDesc = null; - - private static Configuration config; - - - - private static final String NAME_KEY = "NAME"; - private static final String EMAIL_KEY = "EMAIL"; - - - public static void main(String[] args) throws Exception { - - File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); - boolean emailDebug = false; - - PromotionMail pe = new PromotionMail(f, emailDebug); - - } - - - public PromotionMail(File file, boolean mailDebug) throws Exception { - - //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - readFile(file); - - - config = new Configuration(); - - setSMTPHost(); - setAltSMTPHost(); - - - setFromAddress(); - - - setLoadQuery(); - - sendEMails(mailDebug, loadMailingList()); - - - } - - - - - protected void setProductID(String productID) - { - this.productID = productID; - - } - - protected String getproductID() - { - return productID; - } - - protected void setLoadQuery() throws Exception { - - sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + productID +"' " - + "and send_mail=1 "; - - - System.out.println("loadQuery set"); - } - - - protected void setSMTPHost() - { - smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); - } - - - protected void setAltSMTPHost() - { - altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); - - } - - - protected void setFromAddress() - { - fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); - } - - protected void setMessage(HashMap userInfo) throws IOException - { - - String name = (String) userInfo.get(NAME_KEY); - - subject = "您关注的产品降价了"; - message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; - - - - } - - - protected void readFile(File file) throws IOException // @02C - { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - setProductID(data[0]); - setProductDesc(data[1]); - - System.out.println("产品ID = " + productID + "\n"); - System.out.println("产品描述 = " + productDesc + "\n"); - - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); - } - } - - private void setProductDesc(String desc) { - this.productDesc = desc; - } - - - protected void configureEMail(HashMap userInfo) throws IOException - { - toAddress = (String) userInfo.get(EMAIL_KEY); - if (toAddress.length() > 0) - setMessage(userInfo); - } - - protected List loadMailingList() throws Exception { - return DBUtil.query(this.sendMailQuery); - } - - - protected void sendEMails(boolean debug, List mailingList) throws IOException - { - - System.out.println("开始发送邮件"); - - - if (mailingList != null) { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) { - configureEMail((HashMap) iter.next()); - try - { - if (toAddress.length() > 0) - MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } - catch (Exception e) - { - - try { - MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) - { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } - } - - - } - - else { - System.out.println("没有邮件发送"); - - } - - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/Iterator.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/Iterator.java deleted file mode 100644 index 06ef6311b2..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/List.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/List.java deleted file mode 100644 index 10d13b5832..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.coding.basic; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayList.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayList.java deleted file mode 100644 index 4576c016af..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayList.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.coding.basic.array; - -import com.coding.basic.Iterator; -import com.coding.basic.List; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o){ - - } - public void add(int index, Object o){ - - } - - public Object get(int index){ - return null; - } - - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public Iterator iterator(){ - return null; - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayUtil.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayUtil.java deleted file mode 100644 index 45740e6d57..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/array/ArrayUtil.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.coding.basic.array; - -public class ArrayUtil { - - /** - * 给定一个整形数组a , 对该数组的值进行置换 - 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] - 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * @param origin - * @return - */ - public void reverseArray(int[] origin){ - - } - - /** - * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: - * {1,3,4,5,6,6,5,4,7,6,7,5} - * @param oldArray - * @return - */ - - public int[] removeZero(int[] oldArray){ - return null; - } - - /** - * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 - * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 - * @param array1 - * @param array2 - * @return - */ - - public int[] merge(int[] array1, int[] array2){ - return null; - } - /** - * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 - * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 - * [2,3,6,0,0,0] - * @param oldArray - * @param size - * @return - */ - public int[] grow(int [] oldArray, int size){ - return null; - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 - * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] - * max = 1, 则返回空数组 [] - * @param max - * @return - */ - public int[] fibonacci(int max){ - return null; - } - - /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * @param max - * @return - */ - public int[] getPrimes(int max){ - return null; - } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * @param max - * @return - */ - public int[] getPerfectNumbers(int max){ - return null; - } - - /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" - * @param array - * @param s - * @return - */ - public String join(int[] array, String seperator){ - return null; - } - - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrame.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrame.java deleted file mode 100644 index 994a241a3d..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrame.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.coding.basic.linklist; - - -public class LRUPageFrame { - - private static class Node { - - Node prev; - Node next; - int pageNum; - - Node() { - } - } - - private int capacity; - - private int currentSize; - private Node first;// 链表头 - private Node last;// 链表尾 - - - public LRUPageFrame(int capacity) { - this.currentSize = 0; - this.capacity = capacity; - - } - - /** - * 获取缓存中对象 - * - * @param key - * @return - */ - public void access(int pageNum) { - - - } - - - public String toString(){ - StringBuilder buffer = new StringBuilder(); - Node node = first; - while(node != null){ - buffer.append(node.pageNum); - - node = node.next; - if(node != null){ - buffer.append(","); - } - } - return buffer.toString(); - } - - - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java deleted file mode 100644 index 7fd72fc2b4..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LRUPageFrameTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.coding.basic.linklist; - -import org.junit.Assert; - -import org.junit.Test; - - -public class LRUPageFrameTest { - - @Test - public void testAccess() { - LRUPageFrame frame = new LRUPageFrame(3); - frame.access(7); - frame.access(0); - frame.access(1); - Assert.assertEquals("1,0,7", frame.toString()); - frame.access(2); - Assert.assertEquals("2,1,0", frame.toString()); - frame.access(0); - Assert.assertEquals("0,2,1", frame.toString()); - frame.access(0); - Assert.assertEquals("0,2,1", frame.toString()); - frame.access(3); - Assert.assertEquals("3,0,2", frame.toString()); - frame.access(0); - Assert.assertEquals("0,3,2", frame.toString()); - frame.access(4); - Assert.assertEquals("4,0,3", frame.toString()); - frame.access(5); - Assert.assertEquals("5,4,0", frame.toString()); - - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LinkedList.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LinkedList.java deleted file mode 100644 index f4c7556a2e..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/linklist/LinkedList.java +++ /dev/null @@ -1,125 +0,0 @@ -package com.coding.basic.linklist; - -import com.coding.basic.Iterator; -import com.coding.basic.List; - -public class LinkedList implements List { - - private Node head; - - public void add(Object o){ - - } - public void add(int index , Object o){ - - } - public Object get(int index){ - return null; - } - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public void addFirst(Object o){ - - } - public void addLast(Object o){ - - } - public Object removeFirst(){ - return null; - } - public Object removeLast(){ - return null; - } - public Iterator iterator(){ - return null; - } - - - private static class Node{ - Object data; - Node next; - - } - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse(){ - - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - - */ - public void removeFirstHalf(){ - - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - */ - public void remove(int i, int length){ - - } - /** - * 假定当前链表和listB均包含已升序排列的整数 - * 从当前链表中取出那些listB所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - */ - public int[] getElements(LinkedList list){ - return null; - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在listB中出现的元素 - - * @param list - */ - - public void subtract(LinkedList list){ - - } - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues(){ - - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * @param min - * @param max - */ - public void removeRange(int min, int max){ - - } - - /** - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - * @param list - */ - public LinkedList intersection( LinkedList list){ - return null; - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/CircleQueue.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/CircleQueue.java deleted file mode 100644 index 2e0550c67e..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/CircleQueue.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.coding.basic.queue; - -/** - * 用数组实现循环队列 - * @author liuxin - * - * @param - */ -public class CircleQueue { - - private final static int DEFAULT_SIZE = 10; - - //用数组来保存循环队列的元素 - private Object[] elementData = new Object[DEFAULT_SIZE] ; - - //队头 - private int front = 0; - //队尾 - private int rear = 0; - - public boolean isEmpty() { - return false; - - } - - public int size() { - return -1; - } - - - - public void enQueue(E data) { - - } - - public E deQueue() { - return null; - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/Josephus.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/Josephus.java deleted file mode 100644 index 6a3ea639b9..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/Josephus.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.coding.basic.queue; - -import java.util.List; - -/** - * 用Queue来实现Josephus问题 - * 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来 - * 该方法返回一个List, 包含了被杀死人的次序 - * @author liuxin - * - */ -public class Josephus { - - public static List execute(int n, int m){ - return null; - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/JosephusTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/JosephusTest.java deleted file mode 100644 index 7d90318b51..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/JosephusTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.coding.basic.queue; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class JosephusTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testExecute() { - - Assert.assertEquals("[1, 3, 5, 0, 4, 2, 6]", Josephus.execute(7, 2).toString()); - - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/Queue.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/Queue.java deleted file mode 100644 index c4c4b7325e..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/Queue.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.coding.basic.queue; - -import java.util.NoSuchElementException; - -public class Queue { - private Node first; - private Node last; - private int size; - - - private static class Node { - private E item; - private Node next; - } - - - public Queue() { - first = null; - last = null; - size = 0; - } - - - public boolean isEmpty() { - return first == null; - } - - public int size() { - return size; - } - - - - public void enQueue(E data) { - Node oldlast = last; - last = new Node(); - last.item = data; - last.next = null; - if (isEmpty()) { - first = last; - } - else{ - oldlast.next = last; - } - size++; - } - - public E deQueue() { - if (isEmpty()) { - throw new NoSuchElementException("Queue underflow"); - } - E item = first.item; - first = first.next; - size--; - if (isEmpty()) { - last = null; - } - return item; - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java deleted file mode 100644 index cef19a8b59..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/queue/QueueWithTwoStacks.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.coding.basic.queue; - -import java.util.Stack; - -/** - * 用两个栈来实现一个队列 - * @author liuxin - * - * @param - */ -public class QueueWithTwoStacks { - private Stack stack1; - private Stack stack2; - - - public QueueWithTwoStacks() { - stack1 = new Stack(); - stack2 = new Stack(); - } - - - - - public boolean isEmpty() { - return false; - } - - - - public int size() { - return -1; - } - - - - public void enQueue(E item) { - - } - - public E deQueue() { - return null; - } - - - - } - diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/QuickMinStack.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/QuickMinStack.java deleted file mode 100644 index f391d92b8f..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/QuickMinStack.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.coding.basic.stack; - -/** - * 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素 - * finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值 - * @author liuxin - * - */ -public class QuickMinStack { - public void push(int data){ - - } - public int pop(){ - return -1; - } - public int findMin(){ - return -1; - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/Stack.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/Stack.java deleted file mode 100644 index fedb243604..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/Stack.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.coding.basic.stack; - -import com.coding.basic.array.ArrayList; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - } - - public Object pop(){ - return null; - } - - public Object peek(){ - return null; - } - public boolean isEmpty(){ - return false; - } - public int size(){ - return -1; - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtil.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtil.java deleted file mode 100644 index b0ec38161d..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtil.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.coding.basic.stack; -import java.util.Stack; -public class StackUtil { - - - - /** - * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 - * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 - */ - public static void reverse(Stack s) { - - - - } - - /** - * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 - * - * @param o - */ - public static void remove(Stack s,Object o) { - - } - - /** - * 从栈顶取得len个元素, 原来的栈中元素保持不变 - * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 - * @param len - * @return - */ - public static Object[] getTop(Stack s,int len) { - return null; - } - /** - * 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz - * 使用堆栈检查字符串s中的括号是不是成对出现的。 - * 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true - * 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; - * @param s - * @return - */ - public static boolean isValidPairs(String s){ - return false; - } - - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtilTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtilTest.java deleted file mode 100644 index 76f2cb7668..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackUtilTest.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.coding.basic.stack; - -import java.util.Stack; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -public class StackUtilTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - - @Test - public void testReverse() { - Stack s = new Stack(); - s.push(1); - s.push(2); - s.push(3); - s.push(4); - s.push(5); - Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); - StackUtil.reverse(s); - Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString()); - } - - @Test - public void testRemove() { - Stack s = new Stack(); - s.push(1); - s.push(2); - s.push(3); - StackUtil.remove(s, 2); - Assert.assertEquals("[1, 3]", s.toString()); - } - - @Test - public void testGetTop() { - Stack s = new Stack(); - s.push(1); - s.push(2); - s.push(3); - s.push(4); - s.push(5); - { - Object[] values = StackUtil.getTop(s, 3); - Assert.assertEquals(5, values[0]); - Assert.assertEquals(4, values[1]); - Assert.assertEquals(3, values[2]); - } - } - - @Test - public void testIsValidPairs() { - Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])")); - Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})")); - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java deleted file mode 100644 index d0ab4387d2..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/StackWithTwoQueues.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.coding.basic.stack; - - -public class StackWithTwoQueues { - - - public void push(int data) { - - } - - public int pop() { - return -1; - } - - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java deleted file mode 100644 index e86d056a24..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/TwoStackInOneArray.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.coding.basic.stack; - -/** - * 用一个数组实现两个栈 - * 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。 - * @author liuxin - * - */ -public class TwoStackInOneArray { - Object[] data = new Object[10]; - - /** - * 向第一个栈中压入元素 - * @param o - */ - public void push1(Object o){ - - } - /** - * 从第一个栈中弹出元素 - * @return - */ - public Object pop1(){ - return null; - } - - /** - * 获取第一个栈的栈顶元素 - * @return - */ - - public Object peek1(){ - return null; - } - /* - * 向第二个栈压入元素 - */ - public void push2(Object o){ - - } - /** - * 从第二个栈弹出元素 - * @return - */ - public Object pop2(){ - return null; - } - /** - * 获取第二个栈的栈顶元素 - * @return - */ - - public Object peek2(){ - return null; - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExpr.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExpr.java deleted file mode 100644 index ef85ff007f..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExpr.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.coding.basic.stack.expr; - -public class InfixExpr { - String expr = null; - - public InfixExpr(String expr) { - this.expr = expr; - } - - public float evaluate() { - return 0.0f; - } - - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java deleted file mode 100644 index 20e34e8852..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixExprTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.coding.basic.stack.expr; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - -public class InfixExprTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEvaluate() { - //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); - { - InfixExpr expr = new InfixExpr("2+3*4+5"); - Assert.assertEquals(19.0, expr.evaluate(), 0.001f); - } - { - InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); - Assert.assertEquals(100.0, expr.evaluate(), 0.001f); - } - - { - InfixExpr expr = new InfixExpr("3*20/2"); - Assert.assertEquals(30, expr.evaluate(), 0.001f); - } - - { - InfixExpr expr = new InfixExpr("20/2*3"); - Assert.assertEquals(30, expr.evaluate(), 0.001f); - } - - { - InfixExpr expr = new InfixExpr("10-30+50"); - Assert.assertEquals(30, expr.evaluate(), 0.001f); - } - { - InfixExpr expr = new InfixExpr("10-2*3+50"); - Assert.assertEquals(54, expr.evaluate(), 0.001f); - } - - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java deleted file mode 100644 index 96a2194a67..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/InfixToPostfix.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.List; - -public class InfixToPostfix { - - public static List convert(String expr) { - - return null; - } - - - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java deleted file mode 100644 index dcbb18be4b..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExpr.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.List; -import java.util.Stack; - -public class PostfixExpr { -String expr = null; - - public PostfixExpr(String expr) { - this.expr = expr; - } - - public float evaluate() { - return 0.0f; - } - - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java deleted file mode 100644 index c0435a2db5..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PostfixExprTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.coding.basic.stack.expr; - - - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class PostfixExprTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEvaluate() { - { - PostfixExpr expr = new PostfixExpr("6 5 2 3 + 8 * + 3 + *"); - Assert.assertEquals(288, expr.evaluate(),0.0f); - } - { - //9+(3-1)*3+10/2 - PostfixExpr expr = new PostfixExpr("9 3 1-3*+ 10 2/+"); - Assert.assertEquals(20, expr.evaluate(),0.0f); - } - - { - //10-2*3+50 - PostfixExpr expr = new PostfixExpr("10 2 3 * - 50 +"); - Assert.assertEquals(54, expr.evaluate(),0.0f); - } - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java deleted file mode 100644 index 956927e2df..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExpr.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.List; -import java.util.Stack; - -public class PrefixExpr { - String expr = null; - - public PrefixExpr(String expr) { - this.expr = expr; - } - - public float evaluate() { - return 0.0f; - } - - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java deleted file mode 100644 index 5cec210e75..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/PrefixExprTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.coding.basic.stack.expr; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - -public class PrefixExprTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEvaluate() { - { - // 2*3+4*5 - PrefixExpr expr = new PrefixExpr("+ * 2 3* 4 5"); - Assert.assertEquals(26, expr.evaluate(),0.001f); - } - { - // 4*2 + 6+9*2/3 -8 - PrefixExpr expr = new PrefixExpr("-++6/*2 9 3 * 4 2 8"); - Assert.assertEquals(12, expr.evaluate(),0.001f); - } - { - //(3+4)*5-6 - PrefixExpr expr = new PrefixExpr("- * + 3 4 5 6"); - Assert.assertEquals(29, expr.evaluate(),0.001f); - } - { - //1+((2+3)*4)-5 - PrefixExpr expr = new PrefixExpr("- + 1 * + 2 3 4 5"); - Assert.assertEquals(16, expr.evaluate(),0.001f); - } - - - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/Token.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/Token.java deleted file mode 100644 index 8579743fe9..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/Token.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -class Token { - public static final List OPERATORS = Arrays.asList("+", "-", "*", "/"); - private static final Map priorities = new HashMap<>(); - static { - priorities.put("+", 1); - priorities.put("-", 1); - priorities.put("*", 2); - priorities.put("/", 2); - } - static final int OPERATOR = 1; - static final int NUMBER = 2; - String value; - int type; - public Token(int type, String value){ - this.type = type; - this.value = value; - } - - public boolean isNumber() { - return type == NUMBER; - } - - public boolean isOperator() { - return type == OPERATOR; - } - - public int getIntValue() { - return Integer.valueOf(value).intValue(); - } - public String toString(){ - return value; - } - - public boolean hasHigherPriority(Token t){ - if(!this.isOperator() && !t.isOperator()){ - throw new RuntimeException("numbers can't compare priority"); - } - return priorities.get(this.value) - priorities.get(t.value) > 0; - } - - - -} \ No newline at end of file diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParser.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParser.java deleted file mode 100644 index d3b0f167e1..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParser.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.coding.basic.stack.expr; - -import java.util.ArrayList; -import java.util.List; - -public class TokenParser { - - - public List parse(String expr) { - List tokens = new ArrayList<>(); - - int i = 0; - - while (i < expr.length()) { - - char c = expr.charAt(i); - - if (isOperator(c)) { - - Token t = new Token(Token.OPERATOR, String.valueOf(c)); - tokens.add(t); - i++; - - } else if (Character.isDigit(c)) { - - int nextOperatorIndex = indexOfNextOperator(i, expr); - String value = expr.substring(i, nextOperatorIndex); - Token t = new Token(Token.NUMBER, value); - tokens.add(t); - i = nextOperatorIndex; - - } else{ - System.out.println("char :["+c+"] is not number or operator,ignore"); - i++; - } - - } - return tokens; - } - - private int indexOfNextOperator(int i, String expr) { - - while (Character.isDigit(expr.charAt(i))) { - i++; - if (i == expr.length()) { - break; - } - } - return i; - - } - - private boolean isOperator(char c) { - String sc = String.valueOf(c); - return Token.OPERATORS.contains(sc); - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java deleted file mode 100644 index 399d3e857e..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/stack/expr/TokenParserTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.coding.basic.stack.expr; - -import static org.junit.Assert.*; - -import java.util.List; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -public class TokenParserTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void test() { - - TokenParser parser = new TokenParser(); - List tokens = parser.parse("300*20+12*5-20/4"); - - Assert.assertEquals(300, tokens.get(0).getIntValue()); - Assert.assertEquals("*", tokens.get(1).toString()); - Assert.assertEquals(20, tokens.get(2).getIntValue()); - Assert.assertEquals("+", tokens.get(3).toString()); - Assert.assertEquals(12, tokens.get(4).getIntValue()); - Assert.assertEquals("*", tokens.get(5).toString()); - Assert.assertEquals(5, tokens.get(6).getIntValue()); - Assert.assertEquals("-", tokens.get(7).toString()); - Assert.assertEquals(20, tokens.get(8).getIntValue()); - Assert.assertEquals("/", tokens.get(9).toString()); - Assert.assertEquals(4, tokens.get(10).getIntValue()); - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTree.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTree.java deleted file mode 100644 index 4536ee7a2b..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTree.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.coding.basic.tree; - -import java.util.ArrayList; -import java.util.List; - -import com.coding.basic.queue.Queue; - -public class BinarySearchTree { - - BinaryTreeNode root; - public BinarySearchTree(BinaryTreeNode root){ - this.root = root; - } - public BinaryTreeNode getRoot(){ - return root; - } - public T findMin(){ - return null; - } - public T findMax(){ - return null; - } - public int height() { - return -1; - } - public int size() { - return -1; - } - public void remove(T e){ - - } - public List levelVisit(){ - - return null; - } - public boolean isValid(){ - return false; - } - public T getLowestCommonAncestor(T n1, T n2){ - return null; - - } - /** - * 返回所有满足下列条件的节点的值: n1 <= n <= n2 , n 为 - * 该二叉查找树中的某一节点 - * @param n1 - * @param n2 - * @return - */ - public List getNodesBetween(T n1, T n2){ - return null; - } - -} - diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java deleted file mode 100644 index 4a53dbe2f1..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java +++ /dev/null @@ -1,109 +0,0 @@ -package com.coding.basic.tree; - -import java.util.List; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class BinarySearchTreeTest { - - BinarySearchTree tree = null; - - @Before - public void setUp() throws Exception { - BinaryTreeNode root = new BinaryTreeNode(6); - root.left = new BinaryTreeNode(2); - root.right = new BinaryTreeNode(8); - root.left.left = new BinaryTreeNode(1); - root.left.right = new BinaryTreeNode(4); - root.left.right.left = new BinaryTreeNode(3); - root.left.right.right = new BinaryTreeNode(5); - tree = new BinarySearchTree(root); - } - - @After - public void tearDown() throws Exception { - tree = null; - } - - @Test - public void testFindMin() { - Assert.assertEquals(1, tree.findMin().intValue()); - - } - - @Test - public void testFindMax() { - Assert.assertEquals(8, tree.findMax().intValue()); - } - - @Test - public void testHeight() { - Assert.assertEquals(4, tree.height()); - } - - @Test - public void testSize() { - Assert.assertEquals(7, tree.size()); - } - - @Test - public void testRemoveLeaf() { - tree.remove(3); - BinaryTreeNode root= tree.getRoot(); - Assert.assertEquals(4, root.left.right.data.intValue()); - - } - @Test - public void testRemoveMiddleNode1() { - tree.remove(4); - BinaryTreeNode root= tree.getRoot(); - Assert.assertEquals(5, root.left.right.data.intValue()); - Assert.assertEquals(3, root.left.right.left.data.intValue()); - } - @Test - public void testRemoveMiddleNode2() { - tree.remove(2); - BinaryTreeNode root= tree.getRoot(); - Assert.assertEquals(3, root.left.data.intValue()); - Assert.assertEquals(4, root.left.right.data.intValue()); - } - - @Test - public void testLevelVisit() { - List values = tree.levelVisit(); - Assert.assertEquals("[6, 2, 8, 1, 4, 3, 5]", values.toString()); - - } - @Test - public void testLCA(){ - Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 5).intValue()); - Assert.assertEquals(2,tree.getLowestCommonAncestor(1, 4).intValue()); - Assert.assertEquals(6,tree.getLowestCommonAncestor(3, 8).intValue()); - } - @Test - public void testIsValid() { - - Assert.assertTrue(tree.isValid()); - - BinaryTreeNode root = new BinaryTreeNode(6); - root.left = new BinaryTreeNode(2); - root.right = new BinaryTreeNode(8); - root.left.left = new BinaryTreeNode(4); - root.left.right = new BinaryTreeNode(1); - root.left.right.left = new BinaryTreeNode(3); - tree = new BinarySearchTree(root); - - Assert.assertFalse(tree.isValid()); - } - @Test - public void testGetNodesBetween(){ - List numbers = this.tree.getNodesBetween(3, 8); - System.out.println(numbers.toString()); - - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeNode.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeNode.java deleted file mode 100644 index c1421cd398..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeNode.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.coding.basic.tree; - -public class BinaryTreeNode { - - public T data; - public BinaryTreeNode left; - public BinaryTreeNode right; - - public BinaryTreeNode(T data){ - this.data=data; - } - public T getData() { - return data; - } - public void setData(T data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java deleted file mode 100644 index b033cbe1d5..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtil.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.coding.basic.tree; - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -public class BinaryTreeUtil { - /** - * 用递归的方式实现对二叉树的前序遍历, 需要通过BinaryTreeUtilTest测试 - * - * @param root - * @return - */ - public static List preOrderVisit(BinaryTreeNode root) { - List result = new ArrayList(); - - return result; - } - - /** - * 用递归的方式实现对二叉树的中遍历 - * - * @param root - * @return - */ - public static List inOrderVisit(BinaryTreeNode root) { - List result = new ArrayList(); - - return result; - } - - /** - * 用递归的方式实现对二叉树的后遍历 - * - * @param root - * @return - */ - public static List postOrderVisit(BinaryTreeNode root) { - List result = new ArrayList(); - - return result; - } - /** - * 用非递归的方式实现对二叉树的前序遍历 - * @param root - * @return - */ - public static List preOrderWithoutRecursion(BinaryTreeNode root) { - - List result = new ArrayList(); - - return result; - } - /** - * 用非递归的方式实现对二叉树的中序遍历 - * @param root - * @return - */ - public static List inOrderWithoutRecursion(BinaryTreeNode root) { - - List result = new ArrayList(); - - return result; - } - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java deleted file mode 100644 index 41857e137d..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/BinaryTreeUtilTest.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.coding.basic.tree; - -import java.util.List; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class BinaryTreeUtilTest { - - BinaryTreeNode root = null; - @Before - public void setUp() throws Exception { - root = new BinaryTreeNode(1); - root.setLeft(new BinaryTreeNode(2)); - root.setRight(new BinaryTreeNode(5)); - root.getLeft().setLeft(new BinaryTreeNode(3)); - root.getLeft().setRight(new BinaryTreeNode(4)); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testPreOrderVisit() { - - List result = BinaryTreeUtil.preOrderVisit(root); - Assert.assertEquals("[1, 2, 3, 4, 5]", result.toString()); - - - } - @Test - public void testInOrderVisit() { - - - List result = BinaryTreeUtil.inOrderVisit(root); - Assert.assertEquals("[3, 2, 4, 1, 5]", result.toString()); - - } - - @Test - public void testPostOrderVisit() { - - - List result = BinaryTreeUtil.postOrderVisit(root); - Assert.assertEquals("[3, 4, 2, 5, 1]", result.toString()); - - } - - - @Test - public void testInOrderVisitWithoutRecursion() { - BinaryTreeNode node = root.getLeft().getRight(); - node.setLeft(new BinaryTreeNode(6)); - node.setRight(new BinaryTreeNode(7)); - - List result = BinaryTreeUtil.inOrderWithoutRecursion(root); - Assert.assertEquals("[3, 2, 6, 4, 7, 1, 5]", result.toString()); - - } - @Test - public void testPreOrderVisitWithoutRecursion() { - BinaryTreeNode node = root.getLeft().getRight(); - node.setLeft(new BinaryTreeNode(6)); - node.setRight(new BinaryTreeNode(7)); - - List result = BinaryTreeUtil.preOrderWithoutRecursion(root); - Assert.assertEquals("[1, 2, 3, 4, 6, 7, 5]", result.toString()); - - } -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/FileList.java b/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/FileList.java deleted file mode 100644 index 6e65192e4a..0000000000 --- a/students/34594980/data-structure/assignment/src/main/java/com/coding/basic/tree/FileList.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.coding.basic.tree; - -import java.io.File; - -public class FileList { - public void list(File f) { - } - - -} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java similarity index 100% rename from students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/Configuration.java rename to students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java similarity index 100% rename from students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java rename to students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..b40eb53476 --- /dev/null +++ b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,34 @@ +package com.coderising.ood.srp; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * + * @param sql + * @return + */ + public static List> query(String sql) { + + List> userList = new ArrayList<>(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap<>(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + return userList; + } + + public static List> loadMailingList(Product product) { + + String sendMailQuery = "Select name from subscriptions " + "where product_id= '" + + product.getId() + "' " + "and send_mail=1 "; + System.out.println("loadQuery set"); + return DBUtil.query(sendMailQuery); + } +} diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Email.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Email.java new file mode 100644 index 0000000000..e5e6a29076 --- /dev/null +++ b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Email.java @@ -0,0 +1,49 @@ +package com.coderising.ood.srp; + +public class Email { + + private String smtpHost ; + private String altSmtpHost ; + private String fromAddress ; + private String toAddress ; + private String subject ; + private String message ; + + + public String getSmtpHost() { + return smtpHost; + } + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + public String getAltSmtpHost() { + return altSmtpHost; + } + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + public String getFromAddress() { + return fromAddress; + } + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + public String getToAddress() { + return toAddress; + } + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + public String getSubject() { + return subject; + } + public void setSubject(String subject) { + this.subject = subject; + } + public String getMessage() { + return message; + } + public void setMessage(String message) { + this.message = message; + } +} diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java new file mode 100644 index 0000000000..6abdc05ebd --- /dev/null +++ b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java @@ -0,0 +1,36 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class FileUtil { + + private static final String FILENAME="product_promotion.txt"; + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + public static Product readFile()// @02C + { + Product product = null; + BufferedReader br = null; + try { + String filePath = FileUtil.class.getResource(FILENAME).getPath(); + File file = new File(filePath); + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + product = new Product(data[0], data[1]); + + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + br.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return product; + } +} diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..ef1eb60c5b --- /dev/null +++ b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,77 @@ +package com.coderising.ood.srp; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class MailUtil { + + + private static Configuration config; + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void sendEMails(boolean debug) + { + + Product product = FileUtil.readFile(); + System.out.println(product.toString()); + + System.out.println("开始发送邮件"); + + List> mailingList = DBUtil.loadMailingList(product); + + if (mailingList == null){ + System.out.println("没有邮件发送"); + return ; + } + + Iterator> iter = mailingList.iterator(); + while (iter.hasNext()) { + HashMap userInfo = iter.next(); + Email email = MailUtil.configureEMail(userInfo,product); + try { + sendEmail(email.getToAddress(),email.getFromAddress(),email.getSubject(),email.getMessage(),email.getSmtpHost(),debug); + } catch (Exception e1) { + try { + sendEmail(email.getToAddress(),email.getFromAddress(),email.getSubject(),email.getMessage(),email.getAltSmtpHost(),debug); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + } + + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost,boolean debug) { + + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + public static Email configureEMail(HashMap userInfo,Product product){ + + Email email = new Email(); + config = new Configuration(); + email.setSmtpHost(config.getProperty(ConfigurationKeys.SMTP_SERVER)); + email.setAltSmtpHost(config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + email.setFromAddress(config.getProperty(ConfigurationKeys.EMAIL_ADMIN)); + email.setSubject("您关注的产品降价了"); + String toAddress = (String) userInfo.get(EMAIL_KEY); + email.setToAddress(toAddress); + String name = (String) userInfo.get(NAME_KEY); + email.setMessage("尊敬的 "+name+", 您关注的产品 " + product.getDesc() + " 降价了,欢迎购买!"); + + return email; + + } +} diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..e96809d498 --- /dev/null +++ b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,32 @@ +package com.coderising.ood.srp; + +public class Product { + + private String id; //产品ID + private String desc; //产品描述 + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } + public String getDesc() { + return desc; + } + public void setDesc(String desc) { + this.desc = desc; + } + + + public Product() { + } + public Product(String id, String desc) { + this.id = id; + this.desc = desc; + } + @Override + public String toString() { + return "产品ID=" + id + "\n产品描述 = " + desc; + } + +} diff --git a/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..cf5b0879a0 --- /dev/null +++ b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,16 @@ +package com.coderising.ood.srp; + + +public class PromotionMail { + + + public static void main(String[] args) { + + boolean emailDebug = false; + + MailUtil.sendEMails(emailDebug); + + } + + +} diff --git a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt similarity index 50% rename from students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt rename to students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt index a98917f829..b7a974adb3 100644 --- a/students/34594980/data-structure/assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt +++ b/students/34594980/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -1,4 +1,4 @@ P8756 iPhone8 P3946 XiaoMi10 -P8904 Oppo R15 -P4955 Vivo X20 \ No newline at end of file +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From a2fa5a922ec2759d59a29ae297c08b6d60df0522 Mon Sep 17 00:00:00 2001 From: vvivey Date: Sat, 17 Jun 2017 18:41:44 +0800 Subject: [PATCH 159/214] first --- students/1398524980/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/1398524980/README.md diff --git a/students/1398524980/README.md b/students/1398524980/README.md new file mode 100644 index 0000000000..6af04c97b1 --- /dev/null +++ b/students/1398524980/README.md @@ -0,0 +1 @@ +#第一次作业 \ No newline at end of file From 0d46d49344f03c453cc98148606cd1d490cfce34 Mon Sep 17 00:00:00 2001 From: vvivey Date: Sat, 17 Jun 2017 18:52:32 +0800 Subject: [PATCH 160/214] second --- students/1398524980/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/students/1398524980/README.md b/students/1398524980/README.md index 6af04c97b1..4e91a7ce31 100644 --- a/students/1398524980/README.md +++ b/students/1398524980/README.md @@ -1 +1 @@ -#第一次作业 \ No newline at end of file +第一次作业 \ No newline at end of file From ffc58bcdbbd4eaeee1a0c0d563810a9a12474cee Mon Sep 17 00:00:00 2001 From: vvivey Date: Sat, 17 Jun 2017 19:25:05 +0800 Subject: [PATCH 161/214] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/1398524980/README.md | 2 +- .../1398524980/second phase/once/README.md | 1 + .../com/coderising/ood/srp/Configuration.java | 34 ++++++++++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 ++++ .../java/com/coderising/ood/srp/DBUtil.java | 24 +++++++++ .../com/coderising/ood/srp/FromAddress.java | 15 ++++++ .../coderising/ood/srp/LoadInformation.java | 22 ++++++++ .../java/com/coderising/ood/srp/MailUtil.java | 16 ++++++ .../com/coderising/ood/srp/ProductInfo.java | 52 +++++++++++++++++++ .../com/coderising/ood/srp/PromotionMail.java | 36 +++++++++++++ .../com/coderising/ood/srp/SendEMails.java | 51 ++++++++++++++++++ .../com/coderising/ood/srp/SetProtocol.java | 32 ++++++++++++ .../ood/srp/ToAddressAndMessage.java | 52 +++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 ++ 14 files changed, 349 insertions(+), 1 deletion(-) create mode 100644 students/1398524980/second phase/once/README.md create mode 100644 students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/FromAddress.java create mode 100644 students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/LoadInformation.java create mode 100644 students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java create mode 100644 students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/SendEMails.java create mode 100644 students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/SetProtocol.java create mode 100644 students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/ToAddressAndMessage.java create mode 100644 students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/1398524980/README.md b/students/1398524980/README.md index 4e91a7ce31..a652ad5ecf 100644 --- a/students/1398524980/README.md +++ b/students/1398524980/README.md @@ -1 +1 @@ -第一次作业 \ No newline at end of file +作业 \ No newline at end of file diff --git a/students/1398524980/second phase/once/README.md b/students/1398524980/second phase/once/README.md new file mode 100644 index 0000000000..4a2bb84fe9 --- /dev/null +++ b/students/1398524980/second phase/once/README.md @@ -0,0 +1 @@ +面向对象设计 \ No newline at end of file diff --git a/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..0fb3dcfa77 --- /dev/null +++ b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,34 @@ +package main.java.com.coderising.ood.srp; + +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + private static Configuration config; + + static Map configurations = new HashMap<>(); + static { + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + + private Configuration() { + } + + protected static Configuration getConfig() { + return config; + } + + /** + * + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..e63eb922a4 --- /dev/null +++ b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package main.java.com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..1a88f401f1 --- /dev/null +++ b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,24 @@ +package main.java.com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * @param sql + * @return + */ + public static List> query(String sql){ + + List> userList = new ArrayList>(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/FromAddress.java b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/FromAddress.java new file mode 100644 index 0000000000..1a71da260d --- /dev/null +++ b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/FromAddress.java @@ -0,0 +1,15 @@ +package main.java.com.coderising.ood.srp; + +public class FromAddress { + + private String fromAddress = null; + + protected void setFromAddress() { + fromAddress = Configuration.getConfig().getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + public String getFromAddress() { + return fromAddress; + } + +} diff --git a/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/LoadInformation.java b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/LoadInformation.java new file mode 100644 index 0000000000..1eac48983f --- /dev/null +++ b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/LoadInformation.java @@ -0,0 +1,22 @@ +package main.java.com.coderising.ood.srp; + +import java.util.List; + +public class LoadInformation { + + private String productID = new ProductInfo().getproductID(); + + private String sendMailQuery = null; + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + "where product_id= '" + productID + "' " + "and send_mail=1 "; + + System.out.println("loadQuery set"); + } + +} diff --git a/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..84dbffa75d --- /dev/null +++ b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,16 @@ +package main.java.com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + +} diff --git a/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java new file mode 100644 index 0000000000..a678d2987e --- /dev/null +++ b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java @@ -0,0 +1,52 @@ +package main.java.com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class ProductInfo { + + private String productID = null; + private String productDesc = null; + + /** + * + * @throws IOException + */ + public void readProductInfo(String pomotionInfoAddress) throws IOException { + + File f = new File(pomotionInfoAddress); + + readFile(f); + } + + private void readFile(File file) throws IOException { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + productID = data[0]; + productDesc = data[1]; + + System.out.println("ƷID = " + productID + "\n"); + System.out.println("Ʒ = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + protected String getproductID() { + return productID; + } + + protected String getProductDesc() { + return productDesc; + } + +} diff --git a/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..c94ac5a088 --- /dev/null +++ b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,36 @@ +package main.java.com.coderising.ood.srp; + +public class PromotionMail { + + private static ProductInfo productInfo = new ProductInfo(); + private static SetProtocol setprotocol = new SetProtocol(); + private static LoadInformation loadInformation = new LoadInformation(); + private static FromAddress fromAddress = new FromAddress(); + private static SendEMails sendEmails = new SendEMails(); + + public static void main(String[] args) throws Exception { + + boolean emailDebug = false; + new PromotionMail(emailDebug, + "C:\\Users\\123\\Documents\\workspace\\ood_assignment\\src\\com\\coderising\\ood\\srp\\product_promotion.txt"); + } + + PromotionMail(boolean emailDebug, String pomotionInfoAddress) throws Exception { + + // ȡƷϢ + productInfo.readProductInfo(pomotionInfoAddress); + + // ÷ͷЭ + setprotocol.setProtocol(); + + // öȡϢ + loadInformation.setLoadQuery(); + + // ÷͵ַ + fromAddress.setFromAddress(); + + // ʼ ʼ + sendEmails.sendEMails(emailDebug); + } + +} \ No newline at end of file diff --git a/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/SendEMails.java b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/SendEMails.java new file mode 100644 index 0000000000..29bb0f1363 --- /dev/null +++ b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/SendEMails.java @@ -0,0 +1,51 @@ +package main.java.com.coderising.ood.srp; + +import java.util.HashMap; +import java.util.Iterator; + +public class SendEMails { + + private ToAddressAndMessage message = new ToAddressAndMessage(); + + private String smtpHost = new SetProtocol().getSMTPHost(); + private String altSmtpHost = new SetProtocol().getAltSMTPHost(); + + private LoadInformation load = new LoadInformation(); + + /** + * + * @param debug + * @param mailingList + * @throws Exception + */ + protected void sendEMails(boolean debug) throws Exception { + + System.out.println("开始发送邮件"); + + if (load.loadMailingList() != null) { + Iterator iter = load.loadMailingList().iterator(); + while (iter.hasNext()) { + message.configureEMail((HashMap) iter.next()); + try { + if (message.toAddress.length() > 0) { + message.sendSMTPHostWay(smtpHost, debug); + } + } catch (Exception e) { + try { + message.sendAltSMTPHostWay(altSmtpHost, debug); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + } + + else { + System.out.println("没有邮件发送"); + + } + + } + +} diff --git a/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/SetProtocol.java b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/SetProtocol.java new file mode 100644 index 0000000000..98e0151fb6 --- /dev/null +++ b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/SetProtocol.java @@ -0,0 +1,32 @@ +package main.java.com.coderising.ood.srp; + +public class SetProtocol { + + private String smtpHost = null; + private String altSmtpHost = null; + + private Configuration config; + + protected void setProtocol() { + config = Configuration.getConfig(); + setSMTPHost(); + setAltSMTPHost(); + } + + private void setSMTPHost() { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + private void setAltSMTPHost() { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + } + + protected String getSMTPHost() { + return smtpHost; + } + + protected String getAltSMTPHost() { + return altSmtpHost; + } + +} diff --git a/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/ToAddressAndMessage.java b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/ToAddressAndMessage.java new file mode 100644 index 0000000000..c99bbc683b --- /dev/null +++ b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/ToAddressAndMessage.java @@ -0,0 +1,52 @@ +package main.java.com.coderising.ood.srp; + +import java.io.IOException; +import java.util.HashMap; + +public class ToAddressAndMessage { + + private String productDesc = new ProductInfo().getProductDesc(); + + protected String toAddress = null; + private String fromAddress = new FromAddress().getFromAddress(); + + private String subject = null; + private String message = null; + + private static final String EMAIL_KEY = "EMAIL"; + private static final String NAME_KEY = "NAME"; + + /** + * + * @param userInfo + * @throws IOException + */ + protected void addressAndMessage(HashMap userInfo) throws IOException { + configureEMail(userInfo); + setMessage(userInfo); + } + + protected void configureEMail(HashMap userInfo) throws IOException { + + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected void setMessage(HashMap userInfo) throws IOException { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + } + + protected void sendSMTPHostWay(String smtpHost, boolean debug) { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + + protected void sendAltSMTPHostWay(String altSmtpHost, boolean debug) { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + } + +} diff --git a/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/1398524980/second phase/once/ood_assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 7d44bbb30bc627f96207c685eaaf42159bceff1c Mon Sep 17 00:00:00 2001 From: vvivey Date: Sat, 17 Jun 2017 19:35:11 +0800 Subject: [PATCH 162/214] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../second phase/once/ood_assignment/pom.xml | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 students/1398524980/second phase/once/ood_assignment/pom.xml diff --git a/students/1398524980/second phase/once/ood_assignment/pom.xml b/students/1398524980/second phase/once/ood_assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/1398524980/second phase/once/ood_assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + From 988a0c79020702af67cbec4f2179d5a5b35d8d7f Mon Sep 17 00:00:00 2001 From: gongxun Date: Sat, 17 Jun 2017 21:53:58 +0800 Subject: [PATCH 163/214] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=AC=A1=E5=B0=8F?= =?UTF-8?q?=E7=BB=86=E8=8A=82=E6=94=B9=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../785396327/first/ood/srp/BeanUtils.java | 12 -------- .../785396327/first/ood/srp/ConfigParser.java | 2 +- .../785396327/first/ood/srp/DBParser.java | 16 +++++----- students/785396327/first/ood/srp/Email.java | 2 +- .../785396327/first/ood/srp/EmailParser.java | 2 +- .../785396327/first/ood/srp/MailSender.java | 23 +++++++++++---- .../first/ood/srp/PromotionFileParser.java | 2 +- .../ood/srp/PromotionMailConfigParser.java | 2 +- .../first/ood/srp/PromotionMailDBParser.java | 29 ++++++++++++------- .../785396327/first/ood/srp/SendMailTest.java | 6 ++-- .../785396327/first/ood/srp/StringUtils.java | 10 +++---- 11 files changed, 58 insertions(+), 48 deletions(-) delete mode 100644 students/785396327/first/ood/srp/BeanUtils.java diff --git a/students/785396327/first/ood/srp/BeanUtils.java b/students/785396327/first/ood/srp/BeanUtils.java deleted file mode 100644 index 617c96666e..0000000000 --- a/students/785396327/first/ood/srp/BeanUtils.java +++ /dev/null @@ -1,12 +0,0 @@ -package first.ood.srp; - -/** - * Created by IBM on 2017/6/12. - */ -public class BeanUtils { - - public static void copyProperties(Object dst, Object src) { - //拷贝方法暂不实现 - } - -} diff --git a/students/785396327/first/ood/srp/ConfigParser.java b/students/785396327/first/ood/srp/ConfigParser.java index 21ab317bc9..87e00e707e 100644 --- a/students/785396327/first/ood/srp/ConfigParser.java +++ b/students/785396327/first/ood/srp/ConfigParser.java @@ -1,7 +1,7 @@ package first.ood.srp; /** - * Created by gongxun on 2017/6/14. + * Created by william on 2017/6/14. */ public abstract class ConfigParser { diff --git a/students/785396327/first/ood/srp/DBParser.java b/students/785396327/first/ood/srp/DBParser.java index d85f602528..3e49e6abd7 100644 --- a/students/785396327/first/ood/srp/DBParser.java +++ b/students/785396327/first/ood/srp/DBParser.java @@ -4,19 +4,21 @@ import java.util.List; /** - * Created by gongxun on 2017/6/14. + * Created by william on 2017/6/14. */ -public abstract class DBParser { - private String sql; +public abstract class DBParser { + protected String sql; + protected Object[] params; - protected DBParser(String sql) { + protected DBParser(String sql, Object[] params) { this.sql = sql; + this.params = params; } - protected List parseInfoFromDB(Email email) { - List> data = DBUtil.query(sql, null); + protected List parseInfoFromDB(T email) { + List> data = DBUtil.query(sql, params); return convertData(email, data); } - abstract List convertData(Email email, List> data); + abstract List convertData(T email, List> data); } diff --git a/students/785396327/first/ood/srp/Email.java b/students/785396327/first/ood/srp/Email.java index 11a2c408ae..ebca19e579 100644 --- a/students/785396327/first/ood/srp/Email.java +++ b/students/785396327/first/ood/srp/Email.java @@ -1,7 +1,7 @@ package first.ood.srp; /** - * Created by gongxun on 2017/6/12. + * Created by william on 2017/6/12. */ public class Email { protected String smtpHost; diff --git a/students/785396327/first/ood/srp/EmailParser.java b/students/785396327/first/ood/srp/EmailParser.java index 74341ad610..e6f422b52f 100644 --- a/students/785396327/first/ood/srp/EmailParser.java +++ b/students/785396327/first/ood/srp/EmailParser.java @@ -3,7 +3,7 @@ import java.util.List; /** - * Created by gongxun on 2017/6/12. + * Created by william on 2017/6/12. */ public class EmailParser { private ConfigParser configParser; diff --git a/students/785396327/first/ood/srp/MailSender.java b/students/785396327/first/ood/srp/MailSender.java index 90d7f42bdb..a2ed82db46 100644 --- a/students/785396327/first/ood/srp/MailSender.java +++ b/students/785396327/first/ood/srp/MailSender.java @@ -4,17 +4,28 @@ import java.util.List; /** - * Created by gongxun on 2017/6/12. + * Created by william on 2017/6/12. */ -public class MailSender { +public class MailSender { - private void sendMail(PromotionMail mail, boolean isDebug) { - MailUtil.sendEmail(mail.toAddress, mail.fromAddress, mail.subject, mail.message, StringUtils.isEmpty(mail.smtpHost) == true ? mail.smtpHost : mail.altSmtpHost, isDebug); + private void sendMail(Email mail, boolean isDebug) { + if (!StringUtils.isEmpty(mail.toAddress)) + try { + MailUtil.sendEmail( + mail.toAddress, + mail.fromAddress, + mail.subject, + mail.message, + StringUtils.isEmpty(mail.smtpHost) == true ? mail.smtpHost : mail.altSmtpHost, + isDebug); + } catch (Exception e) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e.getMessage()); + } } - public void sendMailList(List mailList, boolean isDebug) { + public void sendMailList(List mailList, boolean isDebug) { if (mailList != null) { - for (Iterator iterator = mailList.iterator(); iterator.hasNext(); ) { + for (Iterator iterator = mailList.iterator(); iterator.hasNext(); ) { sendMail(iterator.next(), isDebug); } } diff --git a/students/785396327/first/ood/srp/PromotionFileParser.java b/students/785396327/first/ood/srp/PromotionFileParser.java index 78a8dfaa66..1d57b8de2e 100644 --- a/students/785396327/first/ood/srp/PromotionFileParser.java +++ b/students/785396327/first/ood/srp/PromotionFileParser.java @@ -1,7 +1,7 @@ package first.ood.srp; /** - * Created by gongxun on 2017/6/14. + * Created by william on 2017/6/14. */ public class PromotionFileParser extends FileParser { diff --git a/students/785396327/first/ood/srp/PromotionMailConfigParser.java b/students/785396327/first/ood/srp/PromotionMailConfigParser.java index dd6ce2e89f..6fd8feb08c 100644 --- a/students/785396327/first/ood/srp/PromotionMailConfigParser.java +++ b/students/785396327/first/ood/srp/PromotionMailConfigParser.java @@ -1,7 +1,7 @@ package first.ood.srp; /** - * Created by gongxun on 2017/6/14. + * Created by william on 2017/6/14. */ public class PromotionMailConfigParser extends ConfigParser { diff --git a/students/785396327/first/ood/srp/PromotionMailDBParser.java b/students/785396327/first/ood/srp/PromotionMailDBParser.java index 68878c02d4..55e740474f 100644 --- a/students/785396327/first/ood/srp/PromotionMailDBParser.java +++ b/students/785396327/first/ood/srp/PromotionMailDBParser.java @@ -5,24 +5,33 @@ import java.util.List; /** - * Created by gongxun on 2017/6/14. + * Created by william on 2017/6/14. */ public class PromotionMailDBParser extends DBParser { - protected PromotionMailDBParser(String sql) { - super(sql); + protected PromotionMailDBParser(String sql, Object[] params) { + super(sql, params); } + /** + * 由于sql参数需要运行时提供所以重写parseInfoFromDB方法 + * @param email + * @return + */ @Override - List convertData(Email email, List> data) { + protected List parseInfoFromDB(PromotionMail email) { + List> data = DBUtil.query(super.sql, new Object[]{email.getproductID()}); + return convertData(email, data); + } + + @Override + List convertData(PromotionMail email, List> data) { List mailList = new ArrayList(); for (HashMap map : data) { - PromotionMail completeMail = new PromotionMail(); - BeanUtils.copyProperties(completeMail, email); - completeMail.setToAddress(parseToAddress(map)); - completeMail.setMessage(parseMessage(map, completeMail)); - completeMail.setSubject("您关注的产品降价了"); - mailList.add(completeMail); + email.setToAddress(parseToAddress(map)); + email.setMessage(parseMessage(map, email)); + email.setSubject("您关注的产品降价了"); + mailList.add(email); } return mailList; } diff --git a/students/785396327/first/ood/srp/SendMailTest.java b/students/785396327/first/ood/srp/SendMailTest.java index ef917aa593..2b54947c05 100644 --- a/students/785396327/first/ood/srp/SendMailTest.java +++ b/students/785396327/first/ood/srp/SendMailTest.java @@ -3,7 +3,7 @@ import java.util.List; /** - * Created by gongxun on 2017/6/12. + * Created by william on 2017/6/12. */ public class SendMailTest { @@ -14,11 +14,11 @@ public static void main(String[] args) { ConfigParser configParser = new PromotionMailConfigParser(); FileParser fileParser = new PromotionFileParser(filepath); - DBParser DBParser = new PromotionMailDBParser(sql); + DBParser DBParser = new PromotionMailDBParser(sql, null); EmailParser emailParser = new EmailParser(configParser, fileParser, DBParser); List promotionMails = emailParser.parseEmailList(); - MailSender mailSender = new MailSender(); + MailSender mailSender = new MailSender(); mailSender.sendMailList(promotionMails, isDebug); } } diff --git a/students/785396327/first/ood/srp/StringUtils.java b/students/785396327/first/ood/srp/StringUtils.java index f5321161bb..ec0483fa8b 100644 --- a/students/785396327/first/ood/srp/StringUtils.java +++ b/students/785396327/first/ood/srp/StringUtils.java @@ -1,17 +1,17 @@ package first.ood.srp; /** - * Created by gongxun on 2017/6/12. + * Created by william on 2017/6/12. */ public class StringUtils { /** - * 判断文件路径是否为空 + * 判断字符串是否为空 * - * @param filePath + * @param str * @return */ - public static boolean isEmpty(String filePath) { - return filePath == null || filePath.trim().isEmpty(); + public static boolean isEmpty(String str) { + return str == null || str.trim().isEmpty(); } } From 7c42e240352f367450c09b70e27fb874df163c36 Mon Sep 17 00:00:00 2001 From: renfuyi <605159467@qq.com> Date: Sun, 18 Jun 2017 10:41:20 +0800 Subject: [PATCH 164/214] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/605159467/ood-assignment/pom.xml | 32 ++++++ .../com/coderising/ood/srp/bean/Email.java | 98 +++++++++++++++++++ .../com/coderising/ood/srp/bean/Person.java | 43 ++++++++ .../com/coderising/ood/srp/bean/Product.java | 43 ++++++++ .../ood/srp/dao/PromotionMailDao.java | 22 +++++ .../ood/srp/dao/PromotionMailDaoImpl.java | 29 ++++++ .../ood/srp/main/PromotionMail.java | 55 +++++++++++ .../ood/srp/resource/ConfigurationKeys.java | 10 ++ .../ood/srp/resource/product_promotion.txt | 4 + .../ood/srp/service/PromotionMailService.java | 30 ++++++ .../srp/service/PromotionMailServiceImpl.java | 67 +++++++++++++ .../com/coderising/ood/srp/utils/DBUtil.java | 23 +++++ .../coderising/ood/srp/utils/FileUtil.java | 55 +++++++++++ .../coderising/ood/srp/utils/MailUtil.java | 18 ++++ .../ood/srp/utils/PropertiesUtil.java | 79 +++++++++++++++ 15 files changed, 608 insertions(+) create mode 100644 students/605159467/ood-assignment/pom.xml create mode 100644 students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Email.java create mode 100644 students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Person.java create mode 100644 students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Product.java create mode 100644 students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/dao/PromotionMailDao.java create mode 100644 students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/dao/PromotionMailDaoImpl.java create mode 100644 students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/main/PromotionMail.java create mode 100644 students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/resource/ConfigurationKeys.java create mode 100644 students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/resource/product_promotion.txt create mode 100644 students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/service/PromotionMailService.java create mode 100644 students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/service/PromotionMailServiceImpl.java create mode 100644 students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/utils/DBUtil.java create mode 100644 students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/utils/FileUtil.java create mode 100644 students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/utils/MailUtil.java create mode 100644 students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/utils/PropertiesUtil.java diff --git a/students/605159467/ood-assignment/pom.xml b/students/605159467/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/605159467/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Email.java b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Email.java new file mode 100644 index 0000000000..b8c16c5091 --- /dev/null +++ b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Email.java @@ -0,0 +1,98 @@ +package com.coderising.ood.srp.bean; + +import com.coderising.ood.srp.resource.ConfigurationKeys; +import com.coderising.ood.srp.utils.MailUtil; + +/** + * Created with IDEA + * Created by fuyi.ren on 2017/6/17 22:04 + * Description: + */ +public class Email +{ + + + + + + + private String toAddress; + private String fromAddress; + private String subject; + private String message; + private String smtpHost; + + public String getFromAddress() + { + return fromAddress; + } + + public void setFromAddress(String fromAddress) + { + this.fromAddress = fromAddress; + } + public String getToAddress() + { + return toAddress; + } + + public void setToAddress(String toAddress) + { + this.toAddress = toAddress; + } + + public String getSubject() + { + return subject; + } + + public void setSubject(String subject) + { + this.subject = subject; + } + + public String getMessage() + { + return message; + } + + public void setMessage(String message) + { + this.message = message; + } + + public String getSmtpHost() + { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) + { + this.smtpHost = smtpHost; + } + + /** + * 具有发送的行为 + */ + public void sendMessage(){ + MailUtil.sendEmail(this.toAddress, + ConfigurationKeys.EMAIL_ADMIN, + this.subject, + this.message, + ConfigurationKeys.ALT_SMTP_SERVER, + true); + } + + /** + * 备用发送 + */ + public void standbySendMessage(){ + MailUtil.sendEmail(this.toAddress, + ConfigurationKeys.EMAIL_ADMIN, + this.subject, + this.message, + ConfigurationKeys.SMTP_SERVER, + true); + } + +} diff --git a/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Person.java b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Person.java new file mode 100644 index 0000000000..942b246154 --- /dev/null +++ b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Person.java @@ -0,0 +1,43 @@ +package com.coderising.ood.srp.bean; + +/** + * Created with IDEA + * Created by fuyi.ren on 2017/6/17 15:21 + * Description: + */ +public class Person +{ + private Long id; + private String name; + private String email; + + public Long getId() + { + return id; + } + + public void setId(Long id) + { + this.id = id; + } + + public String getName() + { + return name; + } + + public void setName(String name) + { + this.name = name; + } + + public String getEmail() + { + return email; + } + + public void setEmail(String email) + { + this.email = email; + } +} diff --git a/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Product.java b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Product.java new file mode 100644 index 0000000000..1fe6c40ef7 --- /dev/null +++ b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/bean/Product.java @@ -0,0 +1,43 @@ +package com.coderising.ood.srp.bean; + +/** + * Created with IDEA + * Created by fuyi.ren on 2017/6/17 11:19 + * Description: 产品实体 + */ +public class Product +{ + private String productID ; + private String productDesc; + + public Product() + { + } + + public Product(String productID, String productDesc) + { + this.productID = productID; + this.productDesc = productDesc; + } + + public String getProductID() + { + return productID; + } + + public void setProductID(String productID) + { + this.productID = productID; + } + + public String getProductDesc() + { + return productDesc; + } + + public void setProductDesc(String productDesc) + { + this.productDesc = productDesc; + } + +} diff --git a/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/dao/PromotionMailDao.java b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/dao/PromotionMailDao.java new file mode 100644 index 0000000000..8b79dfac51 --- /dev/null +++ b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/dao/PromotionMailDao.java @@ -0,0 +1,22 @@ +package com.coderising.ood.srp.dao; + +import java.util.List; + +/** + * Created with IDEA + * Created by fuyi.ren on 2017/6/17 14:07 + * Description: + */ +public interface PromotionMailDao +{ + + /** + * 从数据库中读取信息,人员信息 + */ + public List loadMailingList() throws Exception; + + + + +} + diff --git a/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/dao/PromotionMailDaoImpl.java b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/dao/PromotionMailDaoImpl.java new file mode 100644 index 0000000000..a730c3c188 --- /dev/null +++ b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/dao/PromotionMailDaoImpl.java @@ -0,0 +1,29 @@ +package com.coderising.ood.srp.dao; + +import com.coderising.ood.srp.bean.Person; +import com.coderising.ood.srp.utils.DBUtil; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * Created with IDEA + * Created by fuyi.ren on 2017/6/17 14:48 + * Description: + */ +public class PromotionMailDaoImpl implements PromotionMailDao +{ + public List loadMailingList() throws Exception + { + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + Person person=new Person(); + person.setId(Long.valueOf(i)); + person.setName("User" + i); + person.setEmail("aa@bb.com"); + userList.add(person); + } + return userList; + } +} diff --git a/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/main/PromotionMail.java b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/main/PromotionMail.java new file mode 100644 index 0000000000..e946ece828 --- /dev/null +++ b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/main/PromotionMail.java @@ -0,0 +1,55 @@ +package com.coderising.ood.srp.main; + +import com.coderising.ood.srp.bean.Email; +import com.coderising.ood.srp.bean.Person; +import com.coderising.ood.srp.bean.Product; +import com.coderising.ood.srp.resource.ConfigurationKeys; +import com.coderising.ood.srp.service.PromotionMailService; +import com.coderising.ood.srp.service.PromotionMailServiceImpl; + +import javax.xml.ws.Service; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail +{ + + public static PromotionMailService mailService = new PromotionMailServiceImpl(); + public static void main(String[] args) throws Exception + { + + String src = mailService.getClass().getResource("../resource") + "/product_promotion.txt"; + List productList = mailService.readFile(src); + List personList = mailService.querySendPerons(); + + List emailList=getEmailList(productList,personList); + + mailService.sendMessage(emailList); + } + + private static List getEmailList( List productList, List personList) throws Exception + { + List emailList=new ArrayList(); + for (Person person : personList) + { + for (Product product : productList) + { + Email email = new Email(); + String message=mailService.jointMessage(person, product); + email.setToAddress(person.getEmail()); + email.setMessage(message); + emailList.add(email); + } + } + return emailList; + } + + +} \ No newline at end of file diff --git a/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/resource/ConfigurationKeys.java b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/resource/ConfigurationKeys.java new file mode 100644 index 0000000000..c6fac0201c --- /dev/null +++ b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/resource/ConfigurationKeys.java @@ -0,0 +1,10 @@ +package com.coderising.ood.srp.resource; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + + public static final String EMAIL_ADMIN = "email.admin"; +} diff --git a/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/resource/product_promotion.txt b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/resource/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/resource/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/service/PromotionMailService.java b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/service/PromotionMailService.java new file mode 100644 index 0000000000..f520e17439 --- /dev/null +++ b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/service/PromotionMailService.java @@ -0,0 +1,30 @@ +package com.coderising.ood.srp.service; + +import com.coderising.ood.srp.bean.Email; +import com.coderising.ood.srp.bean.Person; +import com.coderising.ood.srp.bean.Product; + +import java.io.IOException; +import java.util.List; + + +public interface PromotionMailService +{ + + + + + + + + public List readFile(String src) throws IOException; + + + + public List querySendPerons() throws Exception; + + + public String jointMessage(Person person, Product product)throws Exception; + + public void sendMessage(List emailList) throws IOException; +} diff --git a/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/service/PromotionMailServiceImpl.java b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/service/PromotionMailServiceImpl.java new file mode 100644 index 0000000000..ed57cb2e90 --- /dev/null +++ b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/service/PromotionMailServiceImpl.java @@ -0,0 +1,67 @@ +package com.coderising.ood.srp.service; + +import com.coderising.ood.srp.bean.Email; +import com.coderising.ood.srp.bean.Person; +import com.coderising.ood.srp.bean.Product; +import com.coderising.ood.srp.dao.PromotionMailDao; +import com.coderising.ood.srp.dao.PromotionMailDaoImpl; +import com.coderising.ood.srp.utils.FileUtil; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +/** + * Created with IDEA + * Created by fuyi.ren on 2017/6/17 15:04 + * Description: + */ +public class PromotionMailServiceImpl implements PromotionMailService +{ + private static PromotionMailDao promotionMailDao=new PromotionMailDaoImpl(); + private List persons; + private List products; + + + + public void sendMessage(List emailList) throws IOException + { + for (Email email:emailList){ + try + { + email.sendMessage(); + }catch (Exception e){ + + try { + email.standbySendMessage(); + } catch (Exception e2){ + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + + } + } + } + + + public List readFile(String src) throws IOException + { + File file=new File(src); + return FileUtil.readFile(file); // 获得 产品 + } + + public List querySendPerons() throws Exception + { + return promotionMailDao.loadMailingList(); //获得人员 + } + + + public String jointMessage(Person person, Product product) + { + StringBuffer message=new StringBuffer(); + String personName=person.getName(); + String productDesc=product.getProductDesc(); + message.append("您关注的产品降价了").append(" 尊敬的 ").append(personName) + .append(" 您关注的产品").append(productDesc).append("降价了,欢迎购买!"); + return message.toString(); + } +} diff --git a/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/utils/DBUtil.java b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/utils/DBUtil.java new file mode 100644 index 0000000000..2449bf2029 --- /dev/null +++ b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/utils/DBUtil.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp.utils; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + return userList; + } +} diff --git a/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/utils/FileUtil.java b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/utils/FileUtil.java new file mode 100644 index 0000000000..9ee29078b8 --- /dev/null +++ b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/utils/FileUtil.java @@ -0,0 +1,55 @@ +package com.coderising.ood.srp.utils; + +import com.coderising.ood.srp.bean.Product; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * Created with IDEA + * Created by fuyi.ren on 2017/6/17 11:08 + * Description: + */ +public class FileUtil +{ + + /** + * 读取文件内容,返回List + * @param file + * @return + * @throws IOException + */ + public static List readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + List list = null; + try + { + br = new BufferedReader(new FileReader(file)); + String line = null; + Product product = null; + list = new ArrayList(); + while ((line = br.readLine()) != null) + { + String[] data = line.split(" "); + product = new Product(); + product.setProductID(data[0]); + product.setProductDesc(data[1]); + + list.add(product); + } + return list; + } catch (IOException e) + { + throw new IOException(e.getMessage()); + } finally + { + br.close(); + } + } + +} diff --git a/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/utils/MailUtil.java b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/utils/MailUtil.java new file mode 100644 index 0000000000..557234d95c --- /dev/null +++ b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/utils/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp.utils; + +public class MailUtil { + + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + } + + + +} diff --git a/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/utils/PropertiesUtil.java b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/utils/PropertiesUtil.java new file mode 100644 index 0000000000..3c1226ffbb --- /dev/null +++ b/students/605159467/ood-assignment/src/main/java/com/coderising/ood/srp/utils/PropertiesUtil.java @@ -0,0 +1,79 @@ +package com.coderising.ood.srp.utils; + +import org.junit.Test; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URI; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +/** + * Created with IDEA + * Created by fuyi.ren on 2017/6/17 16:27 + * Description: + */ +public class PropertiesUtil +{ + private Properties props; + private URI uri; + + public PropertiesUtil(String fileName){ + readProperties(fileName); + } + private void readProperties(String fileName) { + try { + props = new Properties(); + InputStream fis =getClass().getResourceAsStream(fileName); + props.load(fis); + uri = this.getClass().getResource("/dbConfig.properties").toURI(); + } catch (Exception e) { + e.printStackTrace(); + } + } + /** + * 获取某个属性 + */ + public String getProperty(String key){ + return props.getProperty(key); + } + /** + * 获取所有属性,返回一个map,不常用 + * 可以试试props.putAll(t) + */ + public Map getAllProperty(){ + Map map=new HashMap(); + Enumeration enu = props.propertyNames(); + while (enu.hasMoreElements()) { + String key = (String) enu.nextElement(); + String value = props.getProperty(key); + map.put(key, value); + } + return map; + } + /** + * 在控制台上打印出所有属性,调试时用。 + */ + public void printProperties(){ + props.list(System.out); + } + /** + * 写入properties信息 + */ + public void writeProperties(String key, String value) { + try { + OutputStream fos = new FileOutputStream(new File(uri)); + props.setProperty(key, value); + // 将此 Properties 表中的属性列表(键和元素对)写入输出流 + props.store(fos, "『comments』Update key:" + key); + } catch (Exception e) { + e.printStackTrace(); + } + } + + +} From 8e78357f38c0593e6c6f9a64fc127e9ac0a432ca Mon Sep 17 00:00:00 2001 From: Yangming Xie Date: Sun, 18 Jun 2017 00:26:04 -0400 Subject: [PATCH 165/214] first assignment --- .../com/coderising/ood/srp/FileProdUtil.java | 36 +++++++ .../java/com/coderising/ood/srp/Mail.java | 77 +++++++++++---- .../java/com/coderising/ood/srp/Product.java | 24 +---- .../com/coderising/ood/srp/PromotionMail.java | 99 ++----------------- .../java/com/coderising/ood/srp/Theme.java | 31 ++++++ 5 files changed, 132 insertions(+), 135 deletions(-) create mode 100644 students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/FileProdUtil.java create mode 100644 students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Theme.java diff --git a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/FileProdUtil.java b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/FileProdUtil.java new file mode 100644 index 0000000000..4e3a7d2373 --- /dev/null +++ b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/FileProdUtil.java @@ -0,0 +1,36 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.LinkedList; + +public class FileProdUtil { + //if other files, need polymorphically present file reading + public static void readFile(File file, LinkedList themes) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String sCurrentLine = ""; + + while ((sCurrentLine = br.readLine()) != null) + { + Theme prod = new Product(); + String[] data = sCurrentLine.split(" "); + prod.setID(data[0]); + prod.setDesc(data[1]); + + System.out.println("产品ID = " + prod.getID() + "\n"); + System.out.println("产品描述 = " + prod.getDesc() + "\n"); + themes.add(prod); + } + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + +} diff --git a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java index c7c6bc3ba3..4820d10a12 100644 --- a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java +++ b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; public abstract class Mail { @@ -15,18 +16,17 @@ public abstract class Mail { protected String subject = null; protected String message = null; protected String sendMailQuery = null; - - - public Mail(File file) throws Exception + protected LinkedList theme; + public Mail(File file, boolean emailDebug) throws Exception { - readFile(file); + theme = new LinkedList<>(); + FileProdUtil.readFile(file, theme); setSMTPHost(); setAltSMTPHost(); setFromAddress(); - - + sendEMails(emailDebug, theme); } - protected abstract void readFile(File fie) throws IOException; + //protected abstract void readFile(File fie, LinkedList theme) throws IOException; protected void setSMTPHost() { @@ -45,8 +45,9 @@ protected void setFromAddress() fromAddress = Configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN); } - - + protected void setToAddress(HashMap userInfo){ + toAddress = (String) userInfo.get(Configuration.EMAIL_KEY); + } protected List loadMailingList() throws Exception { //user information, name and email address return DBUtil.query(this.sendMailQuery); @@ -55,23 +56,59 @@ protected List loadMailingList() throws Exception { //user information, name a //protected abstract void setMessage(String name) throws IOException; + abstract protected void setSendMailQuery(Theme theme) throws Exception; - protected void setToAddress(HashMap userInfo){ - toAddress = (String) userInfo.get(Configuration.EMAIL_KEY); + + abstract protected void setMessage(String name, Theme theme) throws IOException; + + + protected void emailProcessing(List mailingList, boolean debug, Theme theme) throws Exception + { + if (mailingList != null) + { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) + { + HashMap userInfo = (HashMap) iter.next(); + setToAddress(userInfo); + if (toAddress.length() > 0) + setMessage((String)userInfo.get(Configuration.NAME_KEY), theme); + + try + { + if (toAddress.length() > 0) + sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + } + else { + System.out.println("没有邮件发送"); + } } - protected abstract void emailProcessing(List mailingList, boolean debug) throws IOException, Exception; - - protected void sendEMails(boolean debug) throws Exception + protected void sendEMails(boolean debug, LinkedList theme) throws Exception { - - List mailingList = loadMailingList(); //persons - - System.out.println("开始发送邮件"); - emailProcessing(mailingList, debug); - + for(Theme topic : theme) + { + setSendMailQuery(topic); + List mailingList = loadMailingList(); //persons + + System.out.println("开始发送邮件"); + emailProcessing(mailingList, debug, topic); + } } public void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, diff --git a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java index d12c24ddba..1f84e2f087 100644 --- a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java +++ b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -1,30 +1,8 @@ package com.coderising.ood.srp; -public class Product { - - private String productID; - private String productDesc; - - - protected void setProductID(String ID) - { - productID = ID; - - } +public class Product extends Theme { - protected String getProductID() - { - return productID; - } - protected void setProductDesc(String desc) { - productDesc = desc; - } - - protected String getProductDesc(){ - return productDesc; - } - } diff --git a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index 22783ba987..d6ef4b8c26 100644 --- a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -1,17 +1,11 @@ package com.coderising.ood.srp; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; import java.io.IOException; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; public class PromotionMail extends Mail { //inheritance from mail - private LinkedList products; + public static void main(String[] args) throws Exception { @@ -23,100 +17,21 @@ public static void main(String[] args) throws Exception { public PromotionMail(File file, boolean mailDebug) throws Exception { - super(file); - products = new LinkedList<>(); - sendEMails(mailDebug); + super(file, mailDebug); } - - - protected void setSendMailQuery(Product pro) throws Exception { + protected void setSendMailQuery(Theme theme) throws Exception { sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + pro.getProductID() +"' " + + "where product_id= '" + theme.getID() +"' " + "and send_mail=1 "; System.out.println("loadQuery set"); } + - protected void setMessage(String name, Product pro) throws IOException + protected void setMessage(String name, Theme theme) throws IOException { subject = "您关注的产品降价了"; - message = "尊敬的 "+ name +", 您关注的产品 " + pro.getProductDesc() + " 降价了,欢迎购买!" ; - } - - protected void emailProcessing(List mailingList, boolean debug) throws Exception - { - - for(Product pro : products){ - - setSendMailQuery(pro); - - if (mailingList != null) - { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) - { - HashMap userInfo = (HashMap) iter.next(); - setToAddress(userInfo); - if (toAddress.length() > 0) - setMessage((String)userInfo.get(Configuration.NAME_KEY), pro); - - try - { - if (toAddress.length() > 0) - sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } - catch (Exception e) - { - - try { - sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) - { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } - } - - } - else { - System.out.println("没有邮件发送"); - } - - } - - + message = "尊敬的 "+ name +", 您关注的产品 " + theme.getDesc() + " 降价了,欢迎购买!" ; } - - - - - @Override - protected void readFile(File file) throws IOException // @02C - { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String sCurrentLine = ""; - - while ((sCurrentLine = br.readLine()) != null) - { - Product prod = new Product(); - String[] data = sCurrentLine.split(" "); - prod.setProductID(data[0]); - prod.setProductDesc(data[1]); - - System.out.println("产品ID = " + prod.getProductID() + "\n"); - System.out.println("产品描述 = " + prod.getProductDesc() + "\n"); - products.add(prod); - } - - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); - } - } - } diff --git a/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Theme.java b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Theme.java new file mode 100644 index 0000000000..e2bf2f5e00 --- /dev/null +++ b/students/702282822/ood-assignment/src/main/java/com/coderising/ood/srp/Theme.java @@ -0,0 +1,31 @@ +/** + * + */ +package com.coderising.ood.srp; + +/** + * @author funkyxym + * + */ +public abstract class Theme { + private String ID = ""; + private String Desc = ""; + + protected void setID(String ID) + { + this.ID = ID; + } + + protected String getID() + { + return ID; + } + + protected void setDesc(String desc) { + this.Desc = desc; + } + + protected String getDesc(){ + return Desc; + } +} From 2e84fcb4f0d9da2b2300c9359aefad1f2db6a9a6 Mon Sep 17 00:00:00 2001 From: 'mldn' <'yunshicheng@gmail.com'> Date: Sun, 18 Jun 2017 13:51:26 +0800 Subject: [PATCH 166/214] =?UTF-8?q?=E9=82=AE=E4=BB=B6=E5=8F=91=E9=80=81?= =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coderising/ood/bean/MailBean.java | 57 +++++++++ .../com/coderising/ood/bean/ProductBean.java | 56 +++++++++ .../src/com/coderising/ood/bean/UserBean.java | 85 ++++++++++++++ .../com/coderising/ood/srp/Configuration.java | 30 +++++ .../coderising/ood/srp/ConfigurationKeys.java | 16 +++ .../src/com/coderising/ood/srp/DBUtil.java | 60 ++++++++++ .../src/com/coderising/ood/srp/MailUtil.java | 108 ++++++++++++++++++ .../com/coderising/ood/srp/PromotionMail.java | 60 ++++++++++ students/254647832/src/pom.xml | 32 ++++++ 9 files changed, 504 insertions(+) create mode 100644 students/254647832/src/com/coderising/ood/bean/MailBean.java create mode 100644 students/254647832/src/com/coderising/ood/bean/ProductBean.java create mode 100644 students/254647832/src/com/coderising/ood/bean/UserBean.java create mode 100644 students/254647832/src/com/coderising/ood/srp/Configuration.java create mode 100644 students/254647832/src/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/254647832/src/com/coderising/ood/srp/DBUtil.java create mode 100644 students/254647832/src/com/coderising/ood/srp/MailUtil.java create mode 100644 students/254647832/src/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/254647832/src/pom.xml diff --git a/students/254647832/src/com/coderising/ood/bean/MailBean.java b/students/254647832/src/com/coderising/ood/bean/MailBean.java new file mode 100644 index 0000000000..2befb197fe --- /dev/null +++ b/students/254647832/src/com/coderising/ood/bean/MailBean.java @@ -0,0 +1,57 @@ +package com.coderising.ood.bean; + +/** + *

Title: MailBean

+ *

Description: 邮件信息

+ *

Company: smartisan

+ * @author Administrator + * @date 2017年6月18日 + */ +public class MailBean { + + /** + * 收件箱地址 + */ + private String toAddress; + + /** + * 邮件主题 + */ + private String subject; + + /** + * 邮件内容 + */ + private String message; + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + @Override + public String toString() { + return "MailBean [toAddress=" + toAddress + ", subject=" + subject + + ", message=" + message + "]"; + } + +} diff --git a/students/254647832/src/com/coderising/ood/bean/ProductBean.java b/students/254647832/src/com/coderising/ood/bean/ProductBean.java new file mode 100644 index 0000000000..f8ae0bfb00 --- /dev/null +++ b/students/254647832/src/com/coderising/ood/bean/ProductBean.java @@ -0,0 +1,56 @@ +package com.coderising.ood.bean; + +import java.util.List; + +/** + *

Title: ProductBean

+ *

Description: 产品信息

+ *

Company: smartisan

+ * @author Administrator + * @date 2017年6月18日 + */ +public class ProductBean { + + /** + * 产品编号 + */ + private String productID; + + /** + * 产品描述 + */ + private String productDesc; + + private List users; + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + + public List getUsers() { + return users; + } + + public void setUsers(List users) { + this.users = users; + } + + @Override + public String toString() { + return "ProductBean [productID=" + productID + ", productDesc=" + + productDesc + ", users=" + users + "]"; + } + +} diff --git a/students/254647832/src/com/coderising/ood/bean/UserBean.java b/students/254647832/src/com/coderising/ood/bean/UserBean.java new file mode 100644 index 0000000000..c1f36a3ef7 --- /dev/null +++ b/students/254647832/src/com/coderising/ood/bean/UserBean.java @@ -0,0 +1,85 @@ +package com.coderising.ood.bean; + +import java.util.List; + +/** + *

Title: UserBean

+ *

Description: 用户信息

+ *

Company: smartisan

+ * @author Administrator + * @date 2017年6月18日 + */ +public class UserBean { + + /** + * 用户ID + */ + private String userid; + + /** + * 用户姓名 + */ + private String name; + + /** + * 邮箱 + */ + private String email; + + /** + * 邮件推送标识 0-不推送;1-推送 + */ + private String sendFlag; + + /** + * 关注的产品 + */ + private List pros; + + public String getUserid() { + return userid; + } + + public void setUserid(String userid) { + this.userid = userid; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getSendFlag() { + return sendFlag; + } + + public void setSendFlag(String sendFlag) { + this.sendFlag = sendFlag; + } + + public List getPros() { + return pros; + } + + public void setPros(List pros) { + this.pros = pros; + } + + @Override + public String toString() { + return "UserBean [userid=" + userid + ", name=" + name + ", email=" + + email + ", sendFlag=" + sendFlag + ", pros=" + pros + "]"; + } + +} diff --git a/students/254647832/src/com/coderising/ood/srp/Configuration.java b/students/254647832/src/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..5e384821d0 --- /dev/null +++ b/students/254647832/src/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,30 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +/** + *

Title: Configuration

+ *

Description: 获取配置信息

+ *

Company: smartisan

+ * @author Administrator + * @date 2017年6月18日 + */ +public class Configuration { + //封装成私有变量 + private static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/254647832/src/com/coderising/ood/srp/ConfigurationKeys.java b/students/254647832/src/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..70606dda99 --- /dev/null +++ b/students/254647832/src/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,16 @@ +package com.coderising.ood.srp; + +/** + *

Title: ConfigurationKeys

+ *

Description: 邮件服务器配置参数

+ *

Company: smartisan

+ * @author Administrator + * @date 2017年6月18日 + */ +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/254647832/src/com/coderising/ood/srp/DBUtil.java b/students/254647832/src/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..4cb4d8ac30 --- /dev/null +++ b/students/254647832/src/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,60 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import com.coderising.ood.bean.ProductBean; +import com.coderising.ood.bean.UserBean; + +/** + *

Title: DBUtil

+ *

Description: 数据库操作

+ *

Company: smartisan

+ * @author Administrator + * @date 2017年6月18日 + */ +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * 用户和关注的产品,应该是多对多的关系,应该有张多对多的表[用户id + 产品id] + * 先根据产品id查询出用户id,再根据用户id查询出用户信息。此处省略这部分的处理 + * @param sql + * @return + */ + public static List query(Set proIds){ + + /** + * SELECT name, email FROM user WHERE userid IN( + * SELECT userid FROM user_pro WHERE proId in(proIds)) + */ + StringBuilder sendMailQuery = new StringBuilder(); + sendMailQuery.append("Select name from subscriptions where send_mail=1 and product_id in("); + + Iterator iter = proIds.iterator(); + while(iter.hasNext()){ + sendMailQuery.append("'" + iter.next() + "',"); + } + sendMailQuery.delete(sendMailQuery.length()-1, sendMailQuery.length()).append(")"); + + List proList = new ArrayList(); + ProductBean proBean = null; + List userList = new ArrayList(); + UserBean userInfo = null; + for (int i = 1; i <= 3; i++) { + userInfo = new UserBean(); + userInfo.setName("User_" + i); + userInfo.setEmail("aa@bb.com_" + i); + proBean = new ProductBean(); + proBean.setProductID("pro_" + i); + proBean.setProductDesc("proDesc_" + i); + proList.add(proBean); + userInfo.setPros(proList); + userList.add(userInfo); + } + + return userList; + } + +} diff --git a/students/254647832/src/com/coderising/ood/srp/MailUtil.java b/students/254647832/src/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9763abc625 --- /dev/null +++ b/students/254647832/src/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,108 @@ +package com.coderising.ood.srp; + +import java.util.ArrayList; +import java.util.List; + +import com.coderising.ood.bean.MailBean; +import com.coderising.ood.bean.ProductBean; +import com.coderising.ood.bean.UserBean; + +/** + *

Title: MailUtil

+ *

Description: 邮件工具类

+ *

Company: smartisan

+ * @author Administrator + * @date 2017年6月18日 + */ +public class MailUtil { + + /** + * 获取邮件正文信息 + * @param user 用户信息 + * @return 邮件正文 + */ + private static String getMassege(UserBean user){ + List list = user.getPros(); + StringBuilder s = new StringBuilder(); + s.append("尊敬的 "); + s.append(user.getName()); + s.append(", 您关注的产品 "); + for(ProductBean pro : list){ + s.append(pro.getProductDesc()).append("、"); + } + s.delete(s.length()-1, s.length()); + s.append(" 降价了,欢迎购买!"); + return s.toString(); + } + + /** + * 获取待发送的邮件信息列表 + * @param users 用户信息 + * @return 待发送邮件列表 + */ + private static List getMailInf(List users){ + List retList = new ArrayList(); + MailBean mailInf; + //获取邮件服务器配置信息 + if (!users.isEmpty()) { + for(UserBean bean : users){ + mailInf = new MailBean(); + mailInf.setToAddress(bean.getEmail()); + mailInf.setSubject("您关注的产品降价了"); + mailInf.setMessage(getMassege(bean)); + retList.add(mailInf); + } + } + return retList; + } + + /** + * 发送邮件 + * @param users 待发送的用户 + */ + public static void sendEmail(List users) { + List mailList = getMailInf(users); + if(!mailList.isEmpty()){ + //获取邮件服务器信息 + Configuration config = new Configuration(); + String server = config.getProperty(ConfigurationKeys.SMTP_SERVER); + String altServer = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + System.out.println("开始发送邮件..."); + System.out.println("发件箱:" + config.getProperty(ConfigurationKeys.EMAIL_ADMIN)); + + for(MailBean mailInf : mailList){ + try{ + send(server, mailInf); + }catch (Exception e){ + try { + send(altServer, mailInf); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + System.out.println("邮件发送结束..."); + }else{ + System.out.println("没有邮件发送"); + } + + } + + /** + * 邮件发送主方法 + * @param server 发送服务器 + */ + public static void send(String server, MailBean mailInf){ + + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + + buffer.append("To:").append(mailInf.getToAddress()).append("\n"); + buffer.append("Subject:").append(mailInf.getSubject()).append("\n"); + buffer.append("Content:").append(mailInf.getMessage()).append("\n"); + + System.out.println(buffer.toString()); + } + +} diff --git a/students/254647832/src/com/coderising/ood/srp/PromotionMail.java b/students/254647832/src/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..2e0cd8793f --- /dev/null +++ b/students/254647832/src/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,60 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import com.coderising.ood.bean.UserBean; + +/** + *

Title: PromotionMail

+ *

Description: 邮件发送处理

+ *

Company: smartisan

+ * @author Administrator + * @date 2017年6月18日 + */ +public class PromotionMail { + + public static void main(String[] args) throws Exception { + + //1、获取降价产品信息 + File f = new File("E:\\gitpro\\liuxin\\coding2017\\liuxin\\ood\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"); + Set proIds = readFile(f); + + //2、根据降价产品的ID获取关注该产品的用户信息 + List users = DBUtil.query(proIds); + + //3、向这些用户发送邮件 + MailUtil.sendEmail(users); + } + + /** + * 读取文件,获取降价产品信息 + * @param file + * @return 产品信息的ID + * @throws IOException + */ + private static Set readFile(File file) throws IOException{ + Set proIds = new HashSet(); + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + while(temp != null && "".equals(temp)){ + String[] data = temp.split(" "); + proIds.add(data[0]); + System.out.println("产品ID = " + data[0] + "\n"); + System.out.println("产品描述 = " + data[1] + "\n"); + } + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + return proIds; + } +} diff --git a/students/254647832/src/pom.xml b/students/254647832/src/pom.xml new file mode 100644 index 0000000000..1be81576cc --- /dev/null +++ b/students/254647832/src/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + From 91353f677343f5426dd7815412ae44b0d4ad40b1 Mon Sep 17 00:00:00 2001 From: 'mldn' <'yunshicheng@gmail.com'> Date: Sun, 18 Jun 2017 14:02:57 +0800 Subject: [PATCH 167/214] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E9=99=8D=E4=BB=B7=E5=95=86=E5=93=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coderising/ood/srp/PromotionMail.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/students/254647832/src/com/coderising/ood/srp/PromotionMail.java b/students/254647832/src/com/coderising/ood/srp/PromotionMail.java index 2e0cd8793f..9af969c773 100644 --- a/students/254647832/src/com/coderising/ood/srp/PromotionMail.java +++ b/students/254647832/src/com/coderising/ood/srp/PromotionMail.java @@ -41,14 +41,19 @@ public static void main(String[] args) throws Exception { private static Set readFile(File file) throws IOException{ Set proIds = new HashSet(); BufferedReader br = null; + boolean flag = true; try { br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - while(temp != null && "".equals(temp)){ - String[] data = temp.split(" "); - proIds.add(data[0]); - System.out.println("产品ID = " + data[0] + "\n"); - System.out.println("产品描述 = " + data[1] + "\n"); + while(flag){ + String temp = br.readLine(); + if(temp != null && !"".equals(temp)){ + String[] data = temp.split(" "); + proIds.add(data[0]); + System.out.println("产品ID = " + data[0]); + System.out.println("产品描述 = " + data[1] + "\n"); + }else{ + flag = false; + } } } catch (IOException e) { throw new IOException(e.getMessage()); From a8fb018a4793956319ce33ce33b57122effcd17a Mon Sep 17 00:00:00 2001 From: "Z_Z.W" Date: Sun, 18 Jun 2017 14:39:55 +0800 Subject: [PATCH 168/214] init homework of myself --- students/511134962/ood-assignment/pom.xml | 32 +++ .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 7 files changed, 310 insertions(+) create mode 100644 students/511134962/ood-assignment/pom.xml create mode 100644 students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/511134962/ood-assignment/pom.xml b/students/511134962/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/511134962/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..781587a846 --- /dev/null +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From c072a337ea5281ac3d22916b6dbd3942fc3689e0 Mon Sep 17 00:00:00 2001 From: "Z_Z.W" Date: Sun, 18 Jun 2017 14:49:51 +0800 Subject: [PATCH 169/214] Updated file path to myself's. --- .../java/com/coderising/ood/srp/PromotionMail.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index 781587a846..74adfbc63c 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -1,10 +1,17 @@ +/********************************************************************************************************************** + * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit. * + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. * + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. * + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. * + * Vestibulum commodo. Ut rhoncus gravida arcu. * + **********************************************************************************************************************/ + package com.coderising.ood.srp; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; -import java.io.Serializable; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -35,7 +42,7 @@ public class PromotionMail { public static void main(String[] args) throws Exception { - File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + File f = new File("D:\\02_workspace\\myproject\\coding2017\\students\\511134962\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"); boolean emailDebug = false; PromotionMail pe = new PromotionMail(f, emailDebug); From 5668592725a4678d1e0b0dbe4b34d07f3142d784 Mon Sep 17 00:00:00 2001 From: "Z_Z.W" Date: Sun, 18 Jun 2017 14:55:40 +0800 Subject: [PATCH 170/214] Added pom.xml signature. --- students/511134962/ood-assignment/pom.xml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/students/511134962/ood-assignment/pom.xml b/students/511134962/ood-assignment/pom.xml index cac49a5328..542d8e08ef 100644 --- a/students/511134962/ood-assignment/pom.xml +++ b/students/511134962/ood-assignment/pom.xml @@ -1,3 +1,11 @@ + + 4.0.0 From 3c6d5caf01bda49078d729ffa08eeef8cbde66ac Mon Sep 17 00:00:00 2001 From: "Z_Z.W" Date: Sun, 18 Jun 2017 14:56:13 +0800 Subject: [PATCH 171/214] Added *.java signature. --- .../java/com/coderising/ood/srp/ConfigurationKeys.java | 8 ++++++++ .../src/main/java/com/coderising/ood/srp/DBUtil.java | 8 ++++++++ .../src/main/java/com/coderising/ood/srp/MailUtil.java | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java index 8695aed644..20c6321972 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -1,3 +1,11 @@ +/********************************************************************************************************************** + * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit. * + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. * + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. * + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. * + * Vestibulum commodo. Ut rhoncus gravida arcu. * + **********************************************************************************************************************/ + package com.coderising.ood.srp; public class ConfigurationKeys { diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java index 82e9261d18..e1854a3bdc 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -1,3 +1,11 @@ +/********************************************************************************************************************** + * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit. * + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. * + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. * + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. * + * Vestibulum commodo. Ut rhoncus gravida arcu. * + **********************************************************************************************************************/ + package com.coderising.ood.srp; import java.util.ArrayList; import java.util.HashMap; diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java index 9f9e749af7..3d417b3c6a 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -1,3 +1,11 @@ +/********************************************************************************************************************** + * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit. * + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. * + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. * + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. * + * Vestibulum commodo. Ut rhoncus gravida arcu. * + **********************************************************************************************************************/ + package com.coderising.ood.srp; public class MailUtil { From 32da00666376844f97b818ab1a441f16dea11836 Mon Sep 17 00:00:00 2001 From: "Z_Z.W" Date: Sun, 18 Jun 2017 14:59:43 +0800 Subject: [PATCH 172/214] Formatted code --- .../com/coderising/ood/srp/PromotionMail.java | 346 ++++++++---------- 1 file changed, 159 insertions(+), 187 deletions(-) diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index 74adfbc63c..d4cf4203d2 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -16,191 +16,163 @@ import java.util.Iterator; import java.util.List; -public class PromotionMail { - - - protected String sendMailQuery = null; - - - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; - protected String toAddress = null; - protected String subject = null; - protected String message = null; - - protected String productID = null; - protected String productDesc = null; - - private static Configuration config; - - - - private static final String NAME_KEY = "NAME"; - private static final String EMAIL_KEY = "EMAIL"; - - - public static void main(String[] args) throws Exception { - - File f = new File("D:\\02_workspace\\myproject\\coding2017\\students\\511134962\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"); - boolean emailDebug = false; - - PromotionMail pe = new PromotionMail(f, emailDebug); - - } - - - public PromotionMail(File file, boolean mailDebug) throws Exception { - - //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - readFile(file); - - - config = new Configuration(); - - setSMTPHost(); - setAltSMTPHost(); - - - setFromAddress(); - - - setLoadQuery(); - - sendEMails(mailDebug, loadMailingList()); - - - } - - - - - protected void setProductID(String productID) - { - this.productID = productID; - - } - - protected String getproductID() - { - return productID; - } - - protected void setLoadQuery() throws Exception { - - sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + productID +"' " - + "and send_mail=1 "; - - - System.out.println("loadQuery set"); - } - - - protected void setSMTPHost() - { - smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); - } - - - protected void setAltSMTPHost() - { - altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); - - } - - - protected void setFromAddress() - { - fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); - } - - protected void setMessage(HashMap userInfo) throws IOException - { - - String name = (String) userInfo.get(NAME_KEY); - - subject = "您关注的产品降价了"; - message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; - - - - } - - - protected void readFile(File file) throws IOException // @02C - { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - setProductID(data[0]); - setProductDesc(data[1]); - - System.out.println("产品ID = " + productID + "\n"); - System.out.println("产品描述 = " + productDesc + "\n"); - - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); - } - } - - private void setProductDesc(String desc) { - this.productDesc = desc; - } - - - protected void configureEMail(HashMap userInfo) throws IOException - { - toAddress = (String) userInfo.get(EMAIL_KEY); - if (toAddress.length() > 0) - setMessage(userInfo); - } - - protected List loadMailingList() throws Exception { - return DBUtil.query(this.sendMailQuery); - } - - - protected void sendEMails(boolean debug, List mailingList) throws IOException - { - - System.out.println("开始发送邮件"); - - - if (mailingList != null) { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) { - configureEMail((HashMap) iter.next()); - try - { - if (toAddress.length() > 0) - MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } - catch (Exception e) - { - - try { - MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) - { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } - } - - - } - - else { - System.out.println("没有邮件发送"); - - } - - } +public class PromotionMail +{ + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + private static Configuration config; + protected String sendMailQuery = null; + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + protected String productID = null; + protected String productDesc = null; + + public PromotionMail( File file, boolean mailDebug ) throws Exception + { + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile( file ); + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + setFromAddress(); + setLoadQuery(); + sendEMails( mailDebug, loadMailingList() ); + } + + protected void readFile( File file ) throws IOException // @02C + { + BufferedReader br = null; + try + { + br = new BufferedReader( new FileReader( file ) ); + String temp = br.readLine(); + String[] data = temp.split( " " ); + + setProductID( data[ 0 ] ); + setProductDesc( data[ 1 ] ); + + System.out.println( "产品ID = " + productID + "\n" ); + System.out.println( "产品描述 = " + productDesc + "\n" ); + + } + catch ( IOException e ) + { + throw new IOException( e.getMessage() ); + } + finally + { + br.close(); + } + } + + protected void setSMTPHost() + { + smtpHost = config.getProperty( ConfigurationKeys.SMTP_SERVER ); + } + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty( ConfigurationKeys.ALT_SMTP_SERVER ); + } + + protected void setFromAddress() + { + fromAddress = config.getProperty( ConfigurationKeys.EMAIL_ADMIN ); + } + + protected void setLoadQuery() throws Exception + { + + sendMailQuery + = "Select name from subscriptions " + "where product_id= '" + productID + "' " + "and send_mail=1 "; + System.out.println( "loadQuery set" ); + } + + protected void sendEMails( boolean debug, List mailingList ) throws IOException + { + System.out.println( "开始发送邮件" ); + if ( mailingList != null ) + { + Iterator iter = mailingList.iterator(); + while ( iter.hasNext() ) + { + configureEMail( ( HashMap ) iter.next() ); + try + { + if ( toAddress.length() > 0 ) + { + MailUtil.sendEmail( toAddress, fromAddress, subject, message, smtpHost, debug ); + } + } + catch ( Exception e ) + { + try + { + MailUtil.sendEmail( toAddress, fromAddress, subject, message, altSmtpHost, debug ); + + } + catch ( Exception e2 ) + { + System.out.println( "通过备用 SMTP服务器发送邮件失败: " + e2.getMessage() ); + } + } + } + } + + else + { + System.out.println( "没有邮件发送" ); + } + + } + + protected List loadMailingList() throws Exception + { + return DBUtil.query( this.sendMailQuery ); + } + + protected void setProductID( String productID ) + { + this.productID = productID; + + } + + private void setProductDesc( String desc ) + { + this.productDesc = desc; + } + + protected void configureEMail( HashMap userInfo ) throws IOException + { + toAddress = ( String ) userInfo.get( EMAIL_KEY ); + if ( toAddress.length() > 0 ) + { + setMessage( userInfo ); + } + } + + protected void setMessage( HashMap userInfo ) throws IOException + { + String name = ( String ) userInfo.get( NAME_KEY ); + subject = "您关注的产品降价了"; + message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + } + + public static void main( String[] args ) throws Exception + { + File f = new File( + "D:\\02_workspace\\myproject\\coding2017\\students\\511134962\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt" ); + boolean emailDebug = false; + PromotionMail pe = new PromotionMail( f, emailDebug ); + } + + protected String getproductID() + { + return productID; + } } From e1f3128bd455e5432bbc3bb4ce7696167830f154 Mon Sep 17 00:00:00 2001 From: "Z_Z.W" Date: Sun, 18 Jun 2017 15:01:13 +0800 Subject: [PATCH 173/214] Formatted code --- .../src/main/java/com/coderising/ood/srp/Configuration.java | 1 - .../main/java/com/coderising/ood/srp/ConfigurationKeys.java | 2 -- .../src/main/java/com/coderising/ood/srp/DBUtil.java | 2 -- .../src/main/java/com/coderising/ood/srp/MailUtil.java | 3 --- 4 files changed, 8 deletions(-) diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java index f328c1816a..3985daf8b5 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -16,7 +16,6 @@ public class Configuration { * @return */ public String getProperty(String key) { - return configurations.get(key); } diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java index 20c6321972..16eda31c95 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -9,9 +9,7 @@ package com.coderising.ood.srp; public class ConfigurationKeys { - public static final String SMTP_SERVER = "smtp.server"; public static final String ALT_SMTP_SERVER = "alt.smtp.server"; public static final String EMAIL_ADMIN = "email.admin"; - } diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java index e1854a3bdc..c145b7440b 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -19,7 +19,6 @@ public class DBUtil { * @return */ public static List query(String sql){ - List userList = new ArrayList(); for (int i = 1; i <= 3; i++) { HashMap userInfo = new HashMap(); @@ -27,7 +26,6 @@ public static List query(String sql){ userInfo.put("EMAIL", "aa@bb.com"); userList.add(userInfo); } - return userList; } } diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java index 3d417b3c6a..a8939d2f01 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -19,8 +19,5 @@ public static void sendEmail(String toAddress, String fromAddress, String subjec buffer.append("Subject:").append(subject).append("\n"); buffer.append("Content:").append(message).append("\n"); System.out.println(buffer.toString()); - } - - } From 65063507f17eddbc5ccf725f984fa59ed5830de4 Mon Sep 17 00:00:00 2001 From: "Z_Z.W" Date: Sun, 18 Jun 2017 15:03:22 +0800 Subject: [PATCH 174/214] Formatted code --- .../com/coderising/ood/srp/Configuration.java | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java index 3985daf8b5..af99642b9d 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -1,22 +1,28 @@ package com.coderising.ood.srp; + import java.util.HashMap; import java.util.Map; -public class Configuration { +public class Configuration +{ + static Map< String, String > configurations = new HashMap<>(); + static + { + configurations.put( ConfigurationKeys.SMTP_SERVER, "smtp.163.com" ); + configurations.put( ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com" ); + configurations.put( ConfigurationKeys.EMAIL_ADMIN, "admin@company.com" ); + } - static Map configurations = new HashMap<>(); - static{ - configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); - configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); - configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); - } - /** - * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 - * @param key - * @return - */ - public String getProperty(String key) { - return configurations.get(key); - } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * + * @param key + * + * @return + */ + public String getProperty( String key ) + { + return configurations.get( key ); + } } From 2460a89b10b2310a0ab1132344c0a2aedd4b4935 Mon Sep 17 00:00:00 2001 From: "Z_Z.W" Date: Sun, 18 Jun 2017 15:10:10 +0800 Subject: [PATCH 175/214] Formatted codes --- .../src/main/java/com/coderising/ood/srp/PromotionMail.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index d4cf4203d2..0ea4c534b7 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -139,7 +139,6 @@ protected List loadMailingList() throws Exception protected void setProductID( String productID ) { this.productID = productID; - } private void setProductDesc( String desc ) @@ -165,10 +164,10 @@ protected void setMessage( HashMap userInfo ) throws IOException public static void main( String[] args ) throws Exception { - File f = new File( + File productPromotionFile = new File( "D:\\02_workspace\\myproject\\coding2017\\students\\511134962\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt" ); boolean emailDebug = false; - PromotionMail pe = new PromotionMail( f, emailDebug ); + PromotionMail pe = new PromotionMail( productPromotionFile, emailDebug ); } protected String getproductID() From 9e2474a6598baad18e6f12246adac15cd4341339 Mon Sep 17 00:00:00 2001 From: "Z_Z.W" Date: Sun, 18 Jun 2017 15:21:38 +0800 Subject: [PATCH 176/214] Delegated read file code to FileUtil.java --- .../java/com/coderising/ood/srp/FileUtil.java | 43 +++++++++++++ .../com/coderising/ood/srp/PromotionMail.java | 62 +++++++------------ 2 files changed, 64 insertions(+), 41 deletions(-) create mode 100644 students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java new file mode 100644 index 0000000000..7e6d1ba355 --- /dev/null +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java @@ -0,0 +1,43 @@ +/********************************************************************************************************************** + * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit. * + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. * + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. * + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. * + * Vestibulum commodo. Ut rhoncus gravida arcu. * + **********************************************************************************************************************/ + +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class FileUtil +{ + public FileUtil() { } + + public String[] readFile( File file ) throws IOException // @02C + { + BufferedReader br = null; + try + { + br = new BufferedReader( new FileReader( file ) ); + String temp = br.readLine(); + String[] data = temp.split( " " ); + br.close(); + return data; + } + catch ( IOException e ) + { + throw new IOException( e.getMessage() ); + } + finally + { + if ( null != br ) + { + br.close(); + } + } + } +} \ No newline at end of file diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index 0ea4c534b7..705258e063 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -8,9 +8,7 @@ package com.coderising.ood.srp; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; @@ -21,22 +19,21 @@ public class PromotionMail private static final String NAME_KEY = "NAME"; private static final String EMAIL_KEY = "EMAIL"; private static Configuration config; - protected String sendMailQuery = null; - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; - protected String toAddress = null; - protected String subject = null; - protected String message = null; - protected String productID = null; - protected String productDesc = null; + private final FileUtil fileUtil = new FileUtil(); + protected String sendMailQuery = null; + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + protected String productID = null; + protected String productDesc = null; public PromotionMail( File file, boolean mailDebug ) throws Exception { - //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - readFile( file ); + readProductInfos( file ); config = new Configuration(); - setSMTPHost(); setAltSMTPHost(); setFromAddress(); @@ -44,30 +41,13 @@ public PromotionMail( File file, boolean mailDebug ) throws Exception sendEMails( mailDebug, loadMailingList() ); } - protected void readFile( File file ) throws IOException // @02C - { - BufferedReader br = null; - try - { - br = new BufferedReader( new FileReader( file ) ); - String temp = br.readLine(); - String[] data = temp.split( " " ); - - setProductID( data[ 0 ] ); - setProductDesc( data[ 1 ] ); - - System.out.println( "产品ID = " + productID + "\n" ); - System.out.println( "产品描述 = " + productDesc + "\n" ); - - } - catch ( IOException e ) - { - throw new IOException( e.getMessage() ); - } - finally - { - br.close(); - } + private void readProductInfos( File file ) throws IOException + {//读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + String[] productInfos = fileUtil.readFile( file ); + setProductID( productInfos[ 0 ] ); + setProductDesc( productInfos[ 1 ] ); + System.out.println( "产品ID = " + productID + "\n" ); + System.out.println( "产品描述 = " + productDesc + "\n" ); } protected void setSMTPHost() @@ -136,14 +116,14 @@ protected List loadMailingList() throws Exception return DBUtil.query( this.sendMailQuery ); } - protected void setProductID( String productID ) + private void setProductID( String productID ) { this.productID = productID; } - private void setProductDesc( String desc ) + private void setProductDesc( String productDesc ) { - this.productDesc = desc; + this.productDesc = productDesc; } protected void configureEMail( HashMap userInfo ) throws IOException From 2f4d56ec40a062fade6a843ecd1acd9d88515712 Mon Sep 17 00:00:00 2001 From: yangdd1205 Date: Sun, 18 Jun 2017 15:26:31 +0800 Subject: [PATCH 177/214] ood srp --- .../java/com/coderising/ood/srp/DBUtil.java | 57 ++++-- .../java/com/coderising/ood/srp/MailData.java | 73 +++++++ .../java/com/coderising/ood/srp/MailUtil.java | 53 +++-- .../com/coderising/ood/srp/ProductInfo.java | 33 ++++ .../ood/srp/ProductInfoService.java | 38 ++++ .../com/coderising/ood/srp/PromotionMail.java | 182 +++--------------- .../java/com/coderising/ood/srp/UserInfo.java | 29 +++ .../coderising/ood/srp/UserInfoService.java | 13 ++ 8 files changed, 295 insertions(+), 183 deletions(-) create mode 100644 students/1049843090/ood/src/main/java/com/coderising/ood/srp/MailData.java create mode 100644 students/1049843090/ood/src/main/java/com/coderising/ood/srp/ProductInfo.java create mode 100644 students/1049843090/ood/src/main/java/com/coderising/ood/srp/ProductInfoService.java create mode 100644 students/1049843090/ood/src/main/java/com/coderising/ood/srp/UserInfo.java create mode 100644 students/1049843090/ood/src/main/java/com/coderising/ood/srp/UserInfoService.java diff --git a/students/1049843090/ood/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/DBUtil.java index 4b8452ded2..27b3f180a7 100644 --- a/students/1049843090/ood/src/main/java/com/coderising/ood/srp/DBUtil.java +++ b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -1,25 +1,46 @@ package com.coderising.ood.srp; + +import java.net.URI; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Random; public class DBUtil { - - /** - * 应该从数据库读, 但是简化为直接生成。 - * @param sql - * @return - */ - public static List query(String sql){ - - List userList = new ArrayList(); - for (int i = 1; i <= 3; i++) { - HashMap userInfo = new HashMap(); - userInfo.put("NAME", "User" + i); - userInfo.put("EMAIL", "aa@bb.com"); - userList.add(userInfo); - } - - return userList; - } + + /** + * 应该从数据库读, 但是简化为直接生成。 + * + * @param sql + * @return + */ + public static List query(String sql) { + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } + + + public static List querySubscriber(String productId) { + + List list = new ArrayList<>(); + + Random random = new Random(); + int number = random.nextInt(3); + + for (int i = 1; i <= number; i++) { + UserInfo userInfo = new UserInfo(); + userInfo.setName("User" + productId + i); + userInfo.setMail(userInfo.getName() + "@" + productId + ".com"); + list.add(userInfo); + } + return list; + } } \ No newline at end of file diff --git a/students/1049843090/ood/src/main/java/com/coderising/ood/srp/MailData.java b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/MailData.java new file mode 100644 index 0000000000..6bcf25cc99 --- /dev/null +++ b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/MailData.java @@ -0,0 +1,73 @@ +package com.coderising.ood.srp; + +/** + * 邮件数据 + * + * @author yangdd + */ +public class MailData { + + protected String smtpHost; + protected String altSmtpHost; + protected String fromAddress; + protected String toAddress; + protected String subject; + protected String message; + private boolean debug; + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isDebug() { + return debug; + } + + public void setDebug(boolean debug) { + this.debug = debug; + } +} diff --git a/students/1049843090/ood/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/MailUtil.java index 9e77ef1968..9948734ff5 100644 --- a/students/1049843090/ood/src/main/java/com/coderising/ood/srp/MailUtil.java +++ b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -2,17 +2,44 @@ public class MailUtil { - public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, - boolean debug) { - //假装发了一封邮件 - StringBuilder buffer = new StringBuilder(); - buffer.append("From:").append(fromAddress).append("\n"); - buffer.append("To:").append(toAddress).append("\n"); - buffer.append("Subject:").append(subject).append("\n"); - buffer.append("Content:").append(message).append("\n"); - System.out.println(buffer.toString()); - - } - - + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + + public static void sendEmail(MailData mailData) { + //假装发了一封邮件 + try { + StringBuilder buffer = new StringBuilder(); + buffer.append("Smtp:").append(mailData.getSmtpHost()).append("\n"); + buffer.append("From:").append(mailData.getFromAddress()).append("\n"); + buffer.append("To:").append(mailData.getToAddress()).append("\n"); + buffer.append("Subject:").append(mailData.getSubject()).append("\n"); + buffer.append("Content:").append(mailData.getMessage()).append("\n"); + System.out.println(buffer.toString()); + } catch (Exception e) { + try { + StringBuilder buffer = new StringBuilder(); + buffer.append("Smtp:").append(mailData.getAltSmtpHost()).append("\n"); + buffer.append("From:").append(mailData.getFromAddress()).append("\n"); + buffer.append("To:").append(mailData.getToAddress()).append("\n"); + buffer.append("Subject:").append(mailData.getSubject()).append("\n"); + buffer.append("Content:").append(mailData.getMessage()).append("\n"); + System.out.println(buffer.toString()); + } catch (Exception e1) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e1.getMessage()); + } + } + + + } + } \ No newline at end of file diff --git a/students/1049843090/ood/src/main/java/com/coderising/ood/srp/ProductInfo.java b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/ProductInfo.java new file mode 100644 index 0000000000..c699da54a1 --- /dev/null +++ b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/ProductInfo.java @@ -0,0 +1,33 @@ +package com.coderising.ood.srp; + +import java.util.List; + +/** + * 产品信息 + * + * @author yangdd + */ +public class ProductInfo { + + private String id; + + private String description; + + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + +} diff --git a/students/1049843090/ood/src/main/java/com/coderising/ood/srp/ProductInfoService.java b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/ProductInfoService.java new file mode 100644 index 0000000000..5f91b05c36 --- /dev/null +++ b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/ProductInfoService.java @@ -0,0 +1,38 @@ +package com.coderising.ood.srp; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; + +/** + * @author yangdd + */ +public class ProductInfoService { + + public List getPromotionProducts() { + List list = new ArrayList<>(); + //可以抽取出一个读取文件的Util + BufferedReader br = null; + try { + String path = "/Users/yangdd/Documents/code/GitHub/coding2017-Q2/students/1049843090/ood/src/main/java/com/coderising/ood/srp/product_promotion.txt"; + File file = new File(path); + br = new BufferedReader(new FileReader(file)); + String temp = null; + String[] data = null; + while ((temp = br.readLine()) != null) { + data = temp.split(" "); + ProductInfo productInfo = new ProductInfo(); + productInfo.setId(data[0]); + productInfo.setDescription(data[1]); + list.add(productInfo); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return list; + } + +} diff --git a/students/1049843090/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java index 55c1f2558c..d8accbd797 100644 --- a/students/1049843090/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -1,180 +1,58 @@ package com.coderising.ood.srp; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.util.HashMap; -import java.util.Iterator; import java.util.List; public class PromotionMail { - protected String sendMailQuery = null; - - - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; - protected String toAddress = null; - protected String subject = null; - protected String message = null; - - protected String productID = null; - protected String productDesc = null; - - private static Configuration config; - - - private static final String NAME_KEY = "NAME"; - private static final String EMAIL_KEY = "EMAIL"; - - public static void main(String[] args) throws Exception { - - File f = new File("E:\\git\\coding2017-Q2\\students\\1049843090\\ood\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"); - boolean emailDebug = false; - - PromotionMail pe = new PromotionMail(f, emailDebug); - - } - - - public PromotionMail(File file, boolean mailDebug) throws Exception { - - //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - readFile(file); - - - config = new Configuration(); - - setSMTPHost(); - setAltSMTPHost(); - - - setFromAddress(); - - - setLoadQuery(); - - sendEMails(mailDebug, loadMailingList()); - + new PromotionMail().sendEMails(); } - protected void setProductID(String productID) { - this.productID = productID; + private void setMessage(ProductInfo productInfo, UserInfo userInfo, MailData mailData) { - } - - protected String getproductID() { - return productID; - } - - protected void setLoadQuery() throws Exception { - - sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + productID + "' " - + "and send_mail=1 "; - - - System.out.println("loadQuery set"); - } + mailData.setSubject("您关注的产品降价了"); + String message = "尊敬的 " + userInfo.getName() + ", 您关注的产品 " + productInfo.getDescription() + " 降价了,欢迎购买!"; + mailData.setMessage(message); - protected void setSMTPHost() { - smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); } - protected void setAltSMTPHost() { - altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + private void sendEMails() throws Exception { + ProductInfoService productInfoService = new ProductInfoService(); + UserInfoService userInfoService = new UserInfoService(); + List productInfos = productInfoService.getPromotionProducts(); - } - - - protected void setFromAddress() { - fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); - } - - protected void setMessage(HashMap userInfo) throws IOException { - - String name = (String) userInfo.get(NAME_KEY); - - subject = "您关注的产品降价了"; - message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; - - - } - - - protected void readFile(File file) throws IOException // @02C - { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - setProductID(data[0]); - setProductDesc(data[1]); + System.out.println("开始发送邮件"); + MailData mailData = getMailData(); + productInfos.forEach(e -> { + List userInfos = userInfoService.getuserInfoByProduceId(e.getId()); + userInfos.forEach(userInfo -> { + if (userInfo.getMail().length() > 0) { + mailData.setToAddress(userInfo.getMail()); + setMessage(e, userInfo, mailData); + MailUtil.sendEmail(mailData); + } else { + System.out.println(userInfo.getName() + "邮件格式不正确"); + } - System.out.println("产品ID = " + productID + "\n"); - System.out.println("产品描述 = " + productDesc + "\n"); + }); + }); - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); - } - } - private void setProductDesc(String desc) { - this.productDesc = desc; } - protected void configureEMail(HashMap userInfo) throws IOException { - toAddress = (String) userInfo.get(EMAIL_KEY); - if (toAddress.length() > 0) - setMessage(userInfo); + private MailData getMailData() { + Configuration config = new Configuration(); + MailData mailData = new MailData(); + mailData.setSmtpHost(config.getProperty(ConfigurationKeys.SMTP_SERVER)); + mailData.setAltSmtpHost(config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + mailData.setFromAddress(config.getProperty(ConfigurationKeys.EMAIL_ADMIN)); + return mailData; } - protected List loadMailingList() throws Exception { - return DBUtil.query(this.sendMailQuery); - } - - - protected void sendEMails(boolean debug, List mailingList) throws IOException { - - System.out.println("开始发送邮件"); - - - if (mailingList != null) { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) { - configureEMail((HashMap) iter.next()); - try { - if (toAddress.length() > 0) - MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } catch (Exception e) { - - try { - MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } - } - - - } else { - System.out.println("没有邮件发送"); - - } - - } } \ No newline at end of file diff --git a/students/1049843090/ood/src/main/java/com/coderising/ood/srp/UserInfo.java b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/UserInfo.java new file mode 100644 index 0000000000..98b04b338e --- /dev/null +++ b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/UserInfo.java @@ -0,0 +1,29 @@ +package com.coderising.ood.srp; + +/** + * 用户信息 + * + * @author yangdd + */ +public class UserInfo { + + private String name; + + private String mail; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getMail() { + return mail; + } + + public void setMail(String mail) { + this.mail = mail; + } +} diff --git a/students/1049843090/ood/src/main/java/com/coderising/ood/srp/UserInfoService.java b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/UserInfoService.java new file mode 100644 index 0000000000..d320d41491 --- /dev/null +++ b/students/1049843090/ood/src/main/java/com/coderising/ood/srp/UserInfoService.java @@ -0,0 +1,13 @@ +package com.coderising.ood.srp; + +import java.util.List; + +/** + * @author yangdd + */ +public class UserInfoService { + + public List getuserInfoByProduceId(String productId) { + return DBUtil.querySubscriber(productId); + } +} From 7462969744039e1b3ca55666477cc8fd5526fca3 Mon Sep 17 00:00:00 2001 From: "Z_Z.W" Date: Sun, 18 Jun 2017 15:31:25 +0800 Subject: [PATCH 178/214] Refactored configuring email setting --- .../java/com/coderising/ood/srp/PromotionMail.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index 705258e063..406c7e164e 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -33,12 +33,18 @@ public class PromotionMail public PromotionMail( File file, boolean mailDebug ) throws Exception { readProductInfos( file ); + configuringEMAILSetting(); + setLoadQuery(); + List mailingList = loadMailingList(); + sendEMails( mailDebug, mailingList ); + } + + private void configuringEMAILSetting() + { config = new Configuration(); setSMTPHost(); setAltSMTPHost(); setFromAddress(); - setLoadQuery(); - sendEMails( mailDebug, loadMailingList() ); } private void readProductInfos( File file ) throws IOException @@ -67,7 +73,6 @@ protected void setFromAddress() protected void setLoadQuery() throws Exception { - sendMailQuery = "Select name from subscriptions " + "where product_id= '" + productID + "' " + "and send_mail=1 "; System.out.println( "loadQuery set" ); @@ -94,7 +99,6 @@ protected void sendEMails( boolean debug, List mailingList ) throws IOException try { MailUtil.sendEmail( toAddress, fromAddress, subject, message, altSmtpHost, debug ); - } catch ( Exception e2 ) { @@ -103,7 +107,6 @@ protected void sendEMails( boolean debug, List mailingList ) throws IOException } } } - else { System.out.println( "没有邮件发送" ); From 3b54f4fde6b88d1e79a90ee46989330933448148 Mon Sep 17 00:00:00 2001 From: "Z_Z.W" Date: Sun, 18 Jun 2017 15:47:31 +0800 Subject: [PATCH 179/214] Extracted emailDebug property --- .../com/coderising/ood/srp/PromotionMail.java | 52 ++++++++++--------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index 406c7e164e..4f3211d113 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -29,9 +29,11 @@ public class PromotionMail protected String message = null; protected String productID = null; protected String productDesc = null; + private boolean emailDebug; public PromotionMail( File file, boolean mailDebug ) throws Exception { + this.emailDebug = mailDebug; readProductInfos( file ); configuringEMAILSetting(); setLoadQuery(); @@ -39,14 +41,6 @@ public PromotionMail( File file, boolean mailDebug ) throws Exception sendEMails( mailDebug, mailingList ); } - private void configuringEMAILSetting() - { - config = new Configuration(); - setSMTPHost(); - setAltSMTPHost(); - setFromAddress(); - } - private void readProductInfos( File file ) throws IOException {//读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 String[] productInfos = fileUtil.readFile( file ); @@ -56,19 +50,12 @@ private void readProductInfos( File file ) throws IOException System.out.println( "产品描述 = " + productDesc + "\n" ); } - protected void setSMTPHost() - { - smtpHost = config.getProperty( ConfigurationKeys.SMTP_SERVER ); - } - - protected void setAltSMTPHost() - { - altSmtpHost = config.getProperty( ConfigurationKeys.ALT_SMTP_SERVER ); - } - - protected void setFromAddress() + private void configuringEMAILSetting() { - fromAddress = config.getProperty( ConfigurationKeys.EMAIL_ADMIN ); + config = new Configuration(); + setSMTPHost(); + setAltSMTPHost(); + setFromAddress(); } protected void setLoadQuery() throws Exception @@ -78,6 +65,11 @@ protected void setLoadQuery() throws Exception System.out.println( "loadQuery set" ); } + protected List loadMailingList() throws Exception + { + return DBUtil.query( this.sendMailQuery ); + } + protected void sendEMails( boolean debug, List mailingList ) throws IOException { System.out.println( "开始发送邮件" ); @@ -114,11 +106,6 @@ protected void sendEMails( boolean debug, List mailingList ) throws IOException } - protected List loadMailingList() throws Exception - { - return DBUtil.query( this.sendMailQuery ); - } - private void setProductID( String productID ) { this.productID = productID; @@ -129,6 +116,21 @@ private void setProductDesc( String productDesc ) this.productDesc = productDesc; } + protected void setSMTPHost() + { + smtpHost = config.getProperty( ConfigurationKeys.SMTP_SERVER ); + } + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty( ConfigurationKeys.ALT_SMTP_SERVER ); + } + + protected void setFromAddress() + { + fromAddress = config.getProperty( ConfigurationKeys.EMAIL_ADMIN ); + } + protected void configureEMail( HashMap userInfo ) throws IOException { toAddress = ( String ) userInfo.get( EMAIL_KEY ); From 73889792294814f08733a54b4be31b0873c5ac29 Mon Sep 17 00:00:00 2001 From: Heveinyu Date: Sun, 18 Jun 2017 15:50:21 +0800 Subject: [PATCH 180/214] first --- students/1418243288/readme.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 students/1418243288/readme.md diff --git a/students/1418243288/readme.md b/students/1418243288/readme.md new file mode 100644 index 0000000000..6f447ec300 --- /dev/null +++ b/students/1418243288/readme.md @@ -0,0 +1,2 @@ +测试下心上传的值 + From b6dbae57e3f846f5609e16ceabd9be8a3d33bb36 Mon Sep 17 00:00:00 2001 From: "Z_Z.W" Date: Sun, 18 Jun 2017 15:51:46 +0800 Subject: [PATCH 181/214] Extracted ProductInfo.java --- .../com/coderising/ood/srp/ProductInfo.java | 72 +++++++++++++++++++ .../com/coderising/ood/srp/PromotionMail.java | 55 +++++++------- 2 files changed, 99 insertions(+), 28 deletions(-) create mode 100644 students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java new file mode 100644 index 0000000000..9e3caa1f57 --- /dev/null +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java @@ -0,0 +1,72 @@ +/********************************************************************************************************************** + * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit. * + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. * + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. * + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. * + * Vestibulum commodo. Ut rhoncus gravida arcu. * + **********************************************************************************************************************/ + +package com.coderising.ood.srp; + +import java.util.Objects; + +public class ProductInfo +{ + public String productID = null; + public String productDesc = null; + + public ProductInfo() { } + + @Override + public int hashCode() + { + return Objects.hash( getProductID(), getProductDesc() ); + } + + @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( !( o instanceof ProductInfo ) ) + { + return false; + } + ProductInfo that = ( ProductInfo ) o; + return Objects.equals( getProductID(), that.getProductID() ) && Objects.equals( getProductDesc(), + that.getProductDesc() ); + } + + public String getProductID() + { + return productID; + } + + public String getProductDesc() + { + + return productDesc; + } + + public void setProductDesc( String productDesc ) + { + this.productDesc = productDesc; + } + + public void setProductID( String productID ) + { + this.productID = productID; + } + + @Override + public String toString() + { + final StringBuilder sb = new StringBuilder( "ProductInfo{" ); + sb.append( " productDesc='" ).append( productDesc ).append( '\'' ); + sb.append( ", productID='" ).append( productID ).append( '\'' ); + sb.append( '}' ); + return sb.toString(); + } +} \ No newline at end of file diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index 4f3211d113..065c6d0980 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -18,17 +18,16 @@ public class PromotionMail { private static final String NAME_KEY = "NAME"; private static final String EMAIL_KEY = "EMAIL"; - private static Configuration config; - private final FileUtil fileUtil = new FileUtil(); - protected String sendMailQuery = null; - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; - protected String toAddress = null; - protected String subject = null; - protected String message = null; - protected String productID = null; - protected String productDesc = null; + private Configuration config; + private FileUtil fileUtil = new FileUtil(); + private String sendMailQuery = null; + private String smtpHost = null; + private String altSmtpHost = null; + private String fromAddress = null; + private String toAddress = null; + private String subject = null; + private String message = null; + private ProductInfo productInfo = new ProductInfo(); private boolean emailDebug; public PromotionMail( File file, boolean mailDebug ) throws Exception @@ -44,10 +43,10 @@ public PromotionMail( File file, boolean mailDebug ) throws Exception private void readProductInfos( File file ) throws IOException {//读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 String[] productInfos = fileUtil.readFile( file ); - setProductID( productInfos[ 0 ] ); - setProductDesc( productInfos[ 1 ] ); - System.out.println( "产品ID = " + productID + "\n" ); - System.out.println( "产品描述 = " + productDesc + "\n" ); + productInfo.setProductID( productInfos[ 0 ] ); + productInfo.setProductDesc( productInfos[ 1 ] ); + System.out.println( "产品ID = " + productInfo.productID + "\n" ); + System.out.println( "产品描述 = " + productInfo.productDesc + "\n" ); } private void configuringEMAILSetting() @@ -61,7 +60,7 @@ private void configuringEMAILSetting() protected void setLoadQuery() throws Exception { sendMailQuery - = "Select name from subscriptions " + "where product_id= '" + productID + "' " + "and send_mail=1 "; + = "Select name from subscriptions " + "where product_id= '" + productInfo.productID + "' " + "and send_mail=1 "; System.out.println( "loadQuery set" ); } @@ -106,16 +105,6 @@ protected void sendEMails( boolean debug, List mailingList ) throws IOException } - private void setProductID( String productID ) - { - this.productID = productID; - } - - private void setProductDesc( String productDesc ) - { - this.productDesc = productDesc; - } - protected void setSMTPHost() { smtpHost = config.getProperty( ConfigurationKeys.SMTP_SERVER ); @@ -144,7 +133,7 @@ protected void setMessage( HashMap userInfo ) throws IOException { String name = ( String ) userInfo.get( NAME_KEY ); subject = "您关注的产品降价了"; - message = "尊敬的 " + name + ", 您关注的产品 " + productDesc + " 降价了,欢迎购买!"; + message = "尊敬的 " + name + ", 您关注的产品 " + productInfo.productDesc + " 降价了,欢迎购买!"; } public static void main( String[] args ) throws Exception @@ -155,8 +144,18 @@ public static void main( String[] args ) throws Exception PromotionMail pe = new PromotionMail( productPromotionFile, emailDebug ); } + private void setProductID( String productID ) + { + productInfo.setProductID( productID ); + } + + private void setProductDesc( String productDesc ) + { + productInfo.setProductDesc( productDesc ); + } + protected String getproductID() { - return productID; + return productInfo.productID; } } From bef7598104abd76a4042da05714fe7e19bf903c0 Mon Sep 17 00:00:00 2001 From: oceanbest <13844090289@163.com> Date: Sun, 18 Jun 2017 16:03:27 +0800 Subject: [PATCH 182/214] first --- students/1418243288/readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/students/1418243288/readme.md b/students/1418243288/readme.md index 6f447ec300..51e5d8cd57 100644 --- a/students/1418243288/readme.md +++ b/students/1418243288/readme.md @@ -1,2 +1,2 @@ -测试下心上传的值 - +测试下新上传的值 +#test From 9c4a3ca9b1b2e66856cc1814f15de2275bf6526b Mon Sep 17 00:00:00 2001 From: "Z_Z.W" Date: Sun, 18 Jun 2017 16:04:03 +0800 Subject: [PATCH 183/214] Extracted ProductInfo.java --- .../com/coderising/ood/srp/ProductInfo.java | 4 +- .../com/coderising/ood/srp/PromotionMail.java | 73 ++++++++----------- 2 files changed, 34 insertions(+), 43 deletions(-) diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java index 9e3caa1f57..c210afb014 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java @@ -12,8 +12,8 @@ public class ProductInfo { - public String productID = null; - public String productDesc = null; + private String productID = null; + private String productDesc = null; public ProductInfo() { } diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index 065c6d0980..60f0daec06 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -16,27 +16,26 @@ public class PromotionMail { - private static final String NAME_KEY = "NAME"; - private static final String EMAIL_KEY = "EMAIL"; - private Configuration config; - private FileUtil fileUtil = new FileUtil(); - private String sendMailQuery = null; - private String smtpHost = null; - private String altSmtpHost = null; - private String fromAddress = null; - private String toAddress = null; - private String subject = null; - private String message = null; - private ProductInfo productInfo = new ProductInfo(); - private boolean emailDebug; + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + private Configuration config = new Configuration(); + private ProductInfo productInfo = new ProductInfo(); + private FileUtil fileUtil = new FileUtil(); + private boolean emailDebug = false; + private String sendMailQuery = null; + private String smtpHost = null; + private String altSmtpHost = null; + private String fromAddress = null; + private String toAddress = null; + private String subject = null; + private String message = null; public PromotionMail( File file, boolean mailDebug ) throws Exception { this.emailDebug = mailDebug; readProductInfos( file ); configuringEMAILSetting(); - setLoadQuery(); - List mailingList = loadMailingList(); + List mailingList = queryMailingList(); sendEMails( mailDebug, mailingList ); } @@ -45,28 +44,21 @@ private void readProductInfos( File file ) throws IOException String[] productInfos = fileUtil.readFile( file ); productInfo.setProductID( productInfos[ 0 ] ); productInfo.setProductDesc( productInfos[ 1 ] ); - System.out.println( "产品ID = " + productInfo.productID + "\n" ); - System.out.println( "产品描述 = " + productInfo.productDesc + "\n" ); + System.out.println( "产品ID = " + productInfo.getProductID() + "\n" ); + System.out.println( "产品描述 = " + productInfo.getProductDesc() + "\n" ); } private void configuringEMAILSetting() { - config = new Configuration(); setSMTPHost(); setAltSMTPHost(); setFromAddress(); } - protected void setLoadQuery() throws Exception - { - sendMailQuery - = "Select name from subscriptions " + "where product_id= '" + productInfo.productID + "' " + "and send_mail=1 "; - System.out.println( "loadQuery set" ); - } - - protected List loadMailingList() throws Exception + private List queryMailingList() throws Exception { - return DBUtil.query( this.sendMailQuery ); + setLoadQuery(); + return loadMailingList(); } protected void sendEMails( boolean debug, List mailingList ) throws IOException @@ -120,6 +112,18 @@ protected void setFromAddress() fromAddress = config.getProperty( ConfigurationKeys.EMAIL_ADMIN ); } + protected void setLoadQuery() throws Exception + { + sendMailQuery = "Select name from subscriptions " + "where product_id= '" + productInfo + .getProductID() + "' " + "and send_mail=1 "; + System.out.println( "loadQuery set" ); + } + + protected List loadMailingList() throws Exception + { + return DBUtil.query( this.sendMailQuery ); + } + protected void configureEMail( HashMap userInfo ) throws IOException { toAddress = ( String ) userInfo.get( EMAIL_KEY ); @@ -133,7 +137,7 @@ protected void setMessage( HashMap userInfo ) throws IOException { String name = ( String ) userInfo.get( NAME_KEY ); subject = "您关注的产品降价了"; - message = "尊敬的 " + name + ", 您关注的产品 " + productInfo.productDesc + " 降价了,欢迎购买!"; + message = "尊敬的 " + name + ", 您关注的产品 " + productInfo.getProductDesc() + " 降价了,欢迎购买!"; } public static void main( String[] args ) throws Exception @@ -144,18 +148,5 @@ public static void main( String[] args ) throws Exception PromotionMail pe = new PromotionMail( productPromotionFile, emailDebug ); } - private void setProductID( String productID ) - { - productInfo.setProductID( productID ); - } - - private void setProductDesc( String productDesc ) - { - productInfo.setProductDesc( productDesc ); - } - protected String getproductID() - { - return productInfo.productID; - } } From 9bd07941e7a4380e9c402e5ccaa3efb385b803c9 Mon Sep 17 00:00:00 2001 From: "Z_Z.W" Date: Sun, 18 Jun 2017 16:23:47 +0800 Subject: [PATCH 184/214] Extracted ProductPromotionDAO.java --- .../java/com/coderising/ood/srp/DBUtil.java | 40 ++++--- .../ood/srp/ProductPromotionDAO.java | 31 ++++++ .../com/coderising/ood/srp/PromotionMail.java | 103 ++++++++---------- 3 files changed, 100 insertions(+), 74 deletions(-) create mode 100644 students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductPromotionDAO.java diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java index c145b7440b..9d2ae5d967 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -7,25 +7,31 @@ **********************************************************************************************************************/ package com.coderising.ood.srp; + import java.util.ArrayList; import java.util.HashMap; import java.util.List; -public class DBUtil { - - /** - * 应该从数据库读, 但是简化为直接生成。 - * @param sql - * @return - */ - public static List query(String sql){ - List userList = new ArrayList(); - for (int i = 1; i <= 3; i++) { - HashMap userInfo = new HashMap(); - userInfo.put("NAME", "User" + i); - userInfo.put("EMAIL", "aa@bb.com"); - userList.add(userInfo); - } - return userList; - } +public class DBUtil +{ + + /** + * 应该从数据库读, 但是简化为直接生成。 + * + * @param sql + * + * @return + */ + public static List< HashMap > query( String sql ) + { + List< HashMap > userList = new ArrayList(); + for ( int i = 1; i <= 3; i++ ) + { + HashMap< String, String > userInfo = new HashMap(); + userInfo.put( "NAME", "User" + i ); + userInfo.put( "EMAIL", "aa@bb.com" ); + userList.add( userInfo ); + } + return userList; + } } diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductPromotionDAO.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductPromotionDAO.java new file mode 100644 index 0000000000..eb7e6c8719 --- /dev/null +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductPromotionDAO.java @@ -0,0 +1,31 @@ +/********************************************************************************************************************** + * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit. * + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. * + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. * + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. * + * Vestibulum commodo. Ut rhoncus gravida arcu. * + **********************************************************************************************************************/ + +package com.coderising.ood.srp; + +import java.util.HashMap; +import java.util.List; + +public class ProductPromotionDAO +{ + private String sendMailQuery = null; + + public ProductPromotionDAO() { } + + public void setLoadQuery( String productID ) throws Exception + { + sendMailQuery + = "Select name from subscriptions " + "where product_id= '" + productID + "' " + "and send_mail=1 "; + System.out.println( "loadQuery set" ); + } + + public List loadMailingList() throws Exception + { + return DBUtil.query( this.sendMailQuery ); + } +} \ No newline at end of file diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index 60f0daec06..5a86b7fedf 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -16,27 +16,28 @@ public class PromotionMail { - private static final String NAME_KEY = "NAME"; - private static final String EMAIL_KEY = "EMAIL"; - private Configuration config = new Configuration(); - private ProductInfo productInfo = new ProductInfo(); - private FileUtil fileUtil = new FileUtil(); - private boolean emailDebug = false; - private String sendMailQuery = null; - private String smtpHost = null; - private String altSmtpHost = null; - private String fromAddress = null; - private String toAddress = null; - private String subject = null; - private String message = null; + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + private ProductPromotionDAO productPromotionDAO = new ProductPromotionDAO(); + private Configuration config = new Configuration(); + private ProductInfo productInfo = new ProductInfo(); + private FileUtil fileUtil = new FileUtil(); + private boolean emailDebug = false; + private String smtpHost = null; + private String altSmtpHost = null; + private String fromAddress = null; + private String toAddress = null; + private String subject = null; + private String message = null; + private List< HashMap > mailingList = null; + public PromotionMail( File file, boolean mailDebug ) throws Exception { this.emailDebug = mailDebug; readProductInfos( file ); configuringEMAILSetting(); - List mailingList = queryMailingList(); - sendEMails( mailDebug, mailingList ); + mailingList = queryMailingList(); } private void readProductInfos( File file ) throws IOException @@ -55,13 +56,37 @@ private void configuringEMAILSetting() setFromAddress(); } - private List queryMailingList() throws Exception + private List< HashMap > queryMailingList() throws Exception + { + productPromotionDAO.setLoadQuery( productInfo.getProductID() ); + return productPromotionDAO.loadMailingList(); + } + + protected void setSMTPHost() + { + smtpHost = config.getProperty( ConfigurationKeys.SMTP_SERVER ); + } + + protected void setAltSMTPHost() { - setLoadQuery(); - return loadMailingList(); + altSmtpHost = config.getProperty( ConfigurationKeys.ALT_SMTP_SERVER ); } - protected void sendEMails( boolean debug, List mailingList ) throws IOException + protected void setFromAddress() + { + fromAddress = config.getProperty( ConfigurationKeys.EMAIL_ADMIN ); + } + + public static void main( String[] args ) throws Exception + { + File productPromotionFile = new File( + "D:\\02_workspace\\myproject\\coding2017\\students\\511134962\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt" ); + boolean emailDebug = false; + PromotionMail pe = new PromotionMail( productPromotionFile, emailDebug ); + pe.sendEMails(); + } + + protected void sendEMails() throws IOException { System.out.println( "开始发送邮件" ); if ( mailingList != null ) @@ -74,14 +99,14 @@ protected void sendEMails( boolean debug, List mailingList ) throws IOException { if ( toAddress.length() > 0 ) { - MailUtil.sendEmail( toAddress, fromAddress, subject, message, smtpHost, debug ); + MailUtil.sendEmail( toAddress, fromAddress, subject, message, smtpHost, emailDebug ); } } catch ( Exception e ) { try { - MailUtil.sendEmail( toAddress, fromAddress, subject, message, altSmtpHost, debug ); + MailUtil.sendEmail( toAddress, fromAddress, subject, message, altSmtpHost, emailDebug ); } catch ( Exception e2 ) { @@ -97,33 +122,6 @@ protected void sendEMails( boolean debug, List mailingList ) throws IOException } - protected void setSMTPHost() - { - smtpHost = config.getProperty( ConfigurationKeys.SMTP_SERVER ); - } - - protected void setAltSMTPHost() - { - altSmtpHost = config.getProperty( ConfigurationKeys.ALT_SMTP_SERVER ); - } - - protected void setFromAddress() - { - fromAddress = config.getProperty( ConfigurationKeys.EMAIL_ADMIN ); - } - - protected void setLoadQuery() throws Exception - { - sendMailQuery = "Select name from subscriptions " + "where product_id= '" + productInfo - .getProductID() + "' " + "and send_mail=1 "; - System.out.println( "loadQuery set" ); - } - - protected List loadMailingList() throws Exception - { - return DBUtil.query( this.sendMailQuery ); - } - protected void configureEMail( HashMap userInfo ) throws IOException { toAddress = ( String ) userInfo.get( EMAIL_KEY ); @@ -140,13 +138,4 @@ protected void setMessage( HashMap userInfo ) throws IOException message = "尊敬的 " + name + ", 您关注的产品 " + productInfo.getProductDesc() + " 降价了,欢迎购买!"; } - public static void main( String[] args ) throws Exception - { - File productPromotionFile = new File( - "D:\\02_workspace\\myproject\\coding2017\\students\\511134962\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt" ); - boolean emailDebug = false; - PromotionMail pe = new PromotionMail( productPromotionFile, emailDebug ); - } - - } From c3de70dd0d47d2e93077a47b023815443d2d5c41 Mon Sep 17 00:00:00 2001 From: "Z_Z.W" Date: Sun, 18 Jun 2017 16:28:31 +0800 Subject: [PATCH 185/214] Refactored code structure --- .../com/coderising/ood/srp/Configuration.java | 28 --------------- .../com/coderising/ood/srp/PromotionMail.java | 10 ++++-- .../ood/srp/common/Configuration.java | 36 +++++++++++++++++++ .../srp/{ => common}/ConfigurationKeys.java | 2 +- .../srp/{ => dao}/ProductPromotionDAO.java | 4 ++- .../coderising/ood/srp/{ => util}/DBUtil.java | 2 +- .../ood/srp/{ => util}/FileUtil.java | 2 +- .../ood/srp/{ => util}/MailUtil.java | 2 +- .../ood/srp/{ => vo}/ProductInfo.java | 2 +- .../srp => resources}/product_promotion.txt | 0 10 files changed, 52 insertions(+), 36 deletions(-) delete mode 100644 students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/common/Configuration.java rename students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/{ => common}/ConfigurationKeys.java (96%) rename students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/{ => dao}/ProductPromotionDAO.java (94%) rename students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/{ => util}/DBUtil.java (97%) rename students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/{ => util}/FileUtil.java (97%) rename students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/{ => util}/MailUtil.java (97%) rename students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/{ => vo}/ProductInfo.java (98%) rename students/511134962/ood-assignment/src/main/{java/com/coderising/ood/srp => resources}/product_promotion.txt (100%) diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java deleted file mode 100644 index af99642b9d..0000000000 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.coderising.ood.srp; - -import java.util.HashMap; -import java.util.Map; - -public class Configuration -{ - static Map< String, String > configurations = new HashMap<>(); - static - { - configurations.put( ConfigurationKeys.SMTP_SERVER, "smtp.163.com" ); - configurations.put( ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com" ); - configurations.put( ConfigurationKeys.EMAIL_ADMIN, "admin@company.com" ); - } - - /** - * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 - * - * @param key - * - * @return - */ - public String getProperty( String key ) - { - return configurations.get( key ); - } - -} diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index 5a86b7fedf..aae91db287 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -8,6 +8,13 @@ package com.coderising.ood.srp; +import com.coderising.ood.srp.common.Configuration; +import com.coderising.ood.srp.common.ConfigurationKeys; +import com.coderising.ood.srp.dao.ProductPromotionDAO; +import com.coderising.ood.srp.util.FileUtil; +import com.coderising.ood.srp.util.MailUtil; +import com.coderising.ood.srp.vo.ProductInfo; + import java.io.File; import java.io.IOException; import java.util.HashMap; @@ -79,8 +86,7 @@ protected void setFromAddress() public static void main( String[] args ) throws Exception { - File productPromotionFile = new File( - "D:\\02_workspace\\myproject\\coding2017\\students\\511134962\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt" ); + File productPromotionFile = new File( "D:\\02_workspace\\myproject\\coding2017\\students\\511134962\\ood-assignment\\src\\main\\resources\\product_promotion.txt" ); boolean emailDebug = false; PromotionMail pe = new PromotionMail( productPromotionFile, emailDebug ); pe.sendEMails(); diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/common/Configuration.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/common/Configuration.java new file mode 100644 index 0000000000..331f204e58 --- /dev/null +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/common/Configuration.java @@ -0,0 +1,36 @@ +/********************************************************************************************************************** + * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit. * + * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. * + * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna. * + * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus. * + * Vestibulum commodo. Ut rhoncus gravida arcu. * + **********************************************************************************************************************/ + +package com.coderising.ood.srp.common; + +import java.util.HashMap; +import java.util.Map; + +public class Configuration +{ + static Map< String, String > configurations = new HashMap<>(); + static + { + configurations.put( ConfigurationKeys.SMTP_SERVER, "smtp.163.com" ); + configurations.put( ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com" ); + configurations.put( ConfigurationKeys.EMAIL_ADMIN, "admin@company.com" ); + } + + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * + * @param key + * + * @return + */ + public String getProperty( String key ) + { + return configurations.get( key ); + } + +} diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/common/ConfigurationKeys.java similarity index 96% rename from students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java rename to students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/common/ConfigurationKeys.java index 16eda31c95..fa6b1ec04a 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/common/ConfigurationKeys.java @@ -6,7 +6,7 @@ * Vestibulum commodo. Ut rhoncus gravida arcu. * **********************************************************************************************************************/ -package com.coderising.ood.srp; +package com.coderising.ood.srp.common; public class ConfigurationKeys { public static final String SMTP_SERVER = "smtp.server"; diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductPromotionDAO.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/dao/ProductPromotionDAO.java similarity index 94% rename from students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductPromotionDAO.java rename to students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/dao/ProductPromotionDAO.java index eb7e6c8719..62b8c05710 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductPromotionDAO.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/dao/ProductPromotionDAO.java @@ -6,7 +6,9 @@ * Vestibulum commodo. Ut rhoncus gravida arcu. * **********************************************************************************************************************/ -package com.coderising.ood.srp; +package com.coderising.ood.srp.dao; + +import com.coderising.ood.srp.util.DBUtil; import java.util.HashMap; import java.util.List; diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/util/DBUtil.java similarity index 97% rename from students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java rename to students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/util/DBUtil.java index 9d2ae5d967..832ba0408a 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/util/DBUtil.java @@ -6,7 +6,7 @@ * Vestibulum commodo. Ut rhoncus gravida arcu. * **********************************************************************************************************************/ -package com.coderising.ood.srp; +package com.coderising.ood.srp.util; import java.util.ArrayList; import java.util.HashMap; diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/util/FileUtil.java similarity index 97% rename from students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java rename to students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/util/FileUtil.java index 7e6d1ba355..963eb72001 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/util/FileUtil.java @@ -6,7 +6,7 @@ * Vestibulum commodo. Ut rhoncus gravida arcu. * **********************************************************************************************************************/ -package com.coderising.ood.srp; +package com.coderising.ood.srp.util; import java.io.BufferedReader; import java.io.File; diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/util/MailUtil.java similarity index 97% rename from students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java rename to students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/util/MailUtil.java index a8939d2f01..cac1a23f8d 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/util/MailUtil.java @@ -6,7 +6,7 @@ * Vestibulum commodo. Ut rhoncus gravida arcu. * **********************************************************************************************************************/ -package com.coderising.ood.srp; +package com.coderising.ood.srp.util; public class MailUtil { diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/vo/ProductInfo.java similarity index 98% rename from students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java rename to students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/vo/ProductInfo.java index c210afb014..3bd52e942d 100644 --- a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/ProductInfo.java +++ b/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/vo/ProductInfo.java @@ -6,7 +6,7 @@ * Vestibulum commodo. Ut rhoncus gravida arcu. * **********************************************************************************************************************/ -package com.coderising.ood.srp; +package com.coderising.ood.srp.vo; import java.util.Objects; diff --git a/students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/511134962/ood-assignment/src/main/resources/product_promotion.txt similarity index 100% rename from students/511134962/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt rename to students/511134962/ood-assignment/src/main/resources/product_promotion.txt From a4dcbcaa1877fedf354d93ee0b4278a33ce0c84d Mon Sep 17 00:00:00 2001 From: lqhtmc <252705978@qq.com> Date: Sun, 18 Jun 2017 16:30:37 +0800 Subject: [PATCH 186/214] ood srp homework --- students/252705978/ood/srp/pom.xml | 32 ++++++++++++ .../com/coderising/ood/srp/Configuration.java | 23 ++++++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 ++++ .../coderising/ood/srp/ConfigurationUtil.java | 31 +++++++++++ .../java/com/coderising/ood/srp/DBUtil.java | 25 +++++++++ .../java/com/coderising/ood/srp/FileUtil.java | 28 ++++++++++ .../java/com/coderising/ood/srp/MailUtil.java | 52 +++++++++++++++++++ .../java/com/coderising/ood/srp/Product.java | 41 +++++++++++++++ .../com/coderising/ood/srp/PromotionMail.java | 27 ++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 ++ 10 files changed, 272 insertions(+) create mode 100644 students/252705978/ood/srp/pom.xml create mode 100644 students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/ConfigurationUtil.java create mode 100644 students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/FileUtil.java create mode 100644 students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/252705978/ood/srp/pom.xml b/students/252705978/ood/srp/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/252705978/ood/srp/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/Configuration.java b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/ConfigurationUtil.java b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/ConfigurationUtil.java new file mode 100644 index 0000000000..3f5d95cc50 --- /dev/null +++ b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/ConfigurationUtil.java @@ -0,0 +1,31 @@ +package com.coderising.ood.srp; + +import java.util.HashMap; + +/** + * 邮件配置工具类 + * + * @author lin + * @since + */ +public class ConfigurationUtil { + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + public static void configure(PromotionMail mail, Configuration config) { + mail.smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + mail.altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + mail.fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + + } + + public static void configure2(PromotionMail mail, HashMap userInfo, Product product) { + mail.toAddress = (String) userInfo.get(EMAIL_KEY); + if (mail.toAddress.length() > 0) { + String name = (String) userInfo.get(NAME_KEY); + + mail.subject = "您关注的产品降价了"; + mail.message = "尊敬的 " + name + ", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!"; + } + } +} diff --git a/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/FileUtil.java b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/FileUtil.java new file mode 100644 index 0000000000..f9c67fea16 --- /dev/null +++ b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/FileUtil.java @@ -0,0 +1,28 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +/** + * 文件工具类 + * + * @author lin + * @since + */ +public class FileUtil { + public static String[] readFile(File file) throws IOException { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + return data; + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } +} diff --git a/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..04bab2b8a9 --- /dev/null +++ b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,52 @@ +package com.coderising.ood.srp; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, boolean debug) { + // 假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + @SuppressWarnings("rawtypes") + public static void sendEMails(PromotionMail mail, Configuration config, boolean debug, Product product, List mailingList) throws IOException { + + System.out.println("开始发送邮件"); + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + ConfigurationUtil.configure2(mail, (HashMap) iter.next(), product); + try { + if (mail.toAddress.length() > 0) + MailUtil.sendEmail(mail.toAddress, mail.fromAddress, mail.subject, mail.message, mail.smtpHost, debug); + } catch (Exception e) { + + try { + MailUtil.sendEmail(mail.toAddress, mail.fromAddress, mail.subject, mail.message, mail.altSmtpHost, debug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/Product.java b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..833b2a98f5 --- /dev/null +++ b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,41 @@ +package com.coderising.ood.srp; + +/** + * 产品实体类 + * + * @author lin + * @since + */ +public class Product { + private String productID; + private String productDesc; + + public Product() { + } + + public Product(String productID, String productDesc) { + this.productID = productID; + this.productDesc = productDesc; + } + + public Product(String[] strs) { + setProductID(strs[0]); + setProductDesc(strs[1]); + } + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } +} diff --git a/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..83af138d3b --- /dev/null +++ b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,27 @@ +package com.coderising.ood.srp; + +import java.io.File; + +public class PromotionMail { + + protected String sendMailQuery = null; + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + public PromotionMail() { + }; + + public static void main(String[] args) throws Exception { + PromotionMail mail = new PromotionMail(); + Product product = new Product(FileUtil.readFile(new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"))); + Configuration config = new Configuration(); + ConfigurationUtil.configure(mail, config); + MailUtil.sendEMails(mail, config, false, product, DBUtil.query(mail.sendMailQuery)); + } + +} diff --git a/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/252705978/ood/srp/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 3c7f44b792cb9a5e494673229b6c0b7238a8257c Mon Sep 17 00:00:00 2001 From: oceanbest <13844090289@163.com> Date: Sun, 18 Jun 2017 16:41:03 +0800 Subject: [PATCH 187/214] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/1418243288/readme.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/students/1418243288/readme.md b/students/1418243288/readme.md index 51e5d8cd57..fcb796bdef 100644 --- a/students/1418243288/readme.md +++ b/students/1418243288/readme.md @@ -1,2 +1,5 @@ 测试下新上传的值 -#test +#test + +#试试对不对# +public void main From 03e2ed4a68fa5932dcf9443a84c1a4139a098eb4 Mon Sep 17 00:00:00 2001 From: easonzhang1992 Date: Sun, 18 Jun 2017 16:52:07 +0800 Subject: [PATCH 188/214] =?UTF-8?q?=E9=87=8D=E6=9E=84sendMail=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coderising/ood/srp/Configuration.java | 10 ++++ .../src/com/coderising/ood/srp/DBUtil.java | 25 ++++++++++ .../src/com/coderising/ood/srp/MailUtil.java | 46 +++++++++++++++++++ .../src/com/coderising/ood/srp/Product.java | 25 ++++++++++ .../com/coderising/ood/srp/ProductUtil.java | 34 ++++++++++++++ .../com/coderising/ood/srp/PromotionMail.java | 32 +++++++++++++ .../src/com/coderising/ood/srp/User.java | 27 +++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 ++ 8 files changed, 203 insertions(+) create mode 100644 students/1058267830/newMail/src/com/coderising/ood/srp/Configuration.java create mode 100644 students/1058267830/newMail/src/com/coderising/ood/srp/DBUtil.java create mode 100644 students/1058267830/newMail/src/com/coderising/ood/srp/MailUtil.java create mode 100644 students/1058267830/newMail/src/com/coderising/ood/srp/Product.java create mode 100644 students/1058267830/newMail/src/com/coderising/ood/srp/ProductUtil.java create mode 100644 students/1058267830/newMail/src/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/1058267830/newMail/src/com/coderising/ood/srp/User.java create mode 100644 students/1058267830/newMail/src/com/coderising/ood/srp/product_promotion.txt diff --git a/students/1058267830/newMail/src/com/coderising/ood/srp/Configuration.java b/students/1058267830/newMail/src/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f1fe384d94 --- /dev/null +++ b/students/1058267830/newMail/src/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,10 @@ +package com.coderising.ood.srp; + +public class Configuration { + + public static final String SMTP_SERVER = "smtp.163.com"; + public static final String ALT_SMTP_SERVER = "smtp1.163.com"; + public static final String EMAIL_ADMIN = "admin@company.com"; + public static final String PRODUCTS_FILE_PATH = "D:\\workspace\\design-pattern\\newMail\\src\\com\\coderising\\ood\\srp\\product_promotion.txt"; + +} diff --git a/students/1058267830/newMail/src/com/coderising/ood/srp/DBUtil.java b/students/1058267830/newMail/src/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..ec4dd0fddb --- /dev/null +++ b/students/1058267830/newMail/src/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +@SuppressWarnings("all") +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List queryUserList(String sql){ + + List userList = new ArrayList(); + + for (int i = 1; i <= 3; i++) { + User user = new User("User" + i, "aa@bb.com"); + userList.add(user); + } + + return userList; + } +} diff --git a/students/1058267830/newMail/src/com/coderising/ood/srp/MailUtil.java b/students/1058267830/newMail/src/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..77f7699825 --- /dev/null +++ b/students/1058267830/newMail/src/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,46 @@ +package com.coderising.ood.srp; + +import java.util.List; + +public class MailUtil { + + public static void sendEmail(List products, List users){ + + for(Product p : products){ + for(User u : users){ + sendEmailToUser(p, u); + } + } + + } + + private static void sendEmailToUser(Product product, User user) { + + String message = "尊敬的 "+user.getName()+", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!" ; + + String fromAddress = Configuration.EMAIL_ADMIN; + String subject = "您关注的产品降价了"; + String smtpHost = Configuration.SMTP_SERVER; + String altSmtpHost = Configuration.ALT_SMTP_SERVER; + + try{ + _sendEmailReal(user.getEmail(), fromAddress, subject, message, smtpHost, false); + }catch(Exception e){ + _sendEmailReal(user.getEmail(), fromAddress, subject, message, altSmtpHost, false); + } + + } + + private static void _sendEmailReal(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + +} diff --git a/students/1058267830/newMail/src/com/coderising/ood/srp/Product.java b/students/1058267830/newMail/src/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..4712c8f0ff --- /dev/null +++ b/students/1058267830/newMail/src/com/coderising/ood/srp/Product.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; + +public class Product { + + private String productId; + private String productDesc; + public Product(String productId, String productDesc) { + super(); + this.productId = productId; + this.productDesc = productDesc; + } + public String getProductId() { + return productId; + } + public void setProductId(String productId) { + this.productId = productId; + } + public String getProductDesc() { + return productDesc; + } + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + +} diff --git a/students/1058267830/newMail/src/com/coderising/ood/srp/ProductUtil.java b/students/1058267830/newMail/src/com/coderising/ood/srp/ProductUtil.java new file mode 100644 index 0000000000..d49f51dc51 --- /dev/null +++ b/students/1058267830/newMail/src/com/coderising/ood/srp/ProductUtil.java @@ -0,0 +1,34 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; + +@SuppressWarnings("all") +public class ProductUtil { + /** + * 得到促销产品列表,至于从哪里读取,由该方法内部决定,外部不需要知道 + * @throws IOException + */ + public static List getProducts() throws IOException{ + return readFile(new File(Configuration.PRODUCTS_FILE_PATH)); + } + + private static List readFile(File file) throws IOException { + List products = new ArrayList(); + FileInputStream fis = new FileInputStream(file); + BufferedReader br = new BufferedReader(new InputStreamReader(fis, "UTF-8")); + for( String line = br.readLine(); line != null; line = br.readLine() ){ + Product product = new Product(line.split(" ")[0], line.split(" ")[1]); + products.add(product); + } + + return products; + + } + +} diff --git a/students/1058267830/newMail/src/com/coderising/ood/srp/PromotionMail.java b/students/1058267830/newMail/src/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..0f6baa324e --- /dev/null +++ b/students/1058267830/newMail/src/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,32 @@ +package com.coderising.ood.srp; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class PromotionMail { + + public static void main(String[] args) { + + /** + * 整体步骤大概有3步: + * 1、读取配置文件,得到促销的商品列表 + * 2、调用DBUtil读取DB,得到订阅的用户列表 + * 3、调用MailUtil发送邮件 + */ + List products = new ArrayList(); + List users = new ArrayList(); + try { + + products = ProductUtil.getProducts(); + users = DBUtil.queryUserList("select *** from t_user"); + MailUtil.sendEmail(products, users); + + } catch (IOException e) { + e.printStackTrace(); + } + + + } + +} diff --git a/students/1058267830/newMail/src/com/coderising/ood/srp/User.java b/students/1058267830/newMail/src/com/coderising/ood/srp/User.java new file mode 100644 index 0000000000..6a7bc15952 --- /dev/null +++ b/students/1058267830/newMail/src/com/coderising/ood/srp/User.java @@ -0,0 +1,27 @@ +package com.coderising.ood.srp; + +public class User { + + private String name; + private String email; + + public User(String name, String email) { + super(); + this.name = name; + this.email = email; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getEmail() { + return email; + } + public void setEmail(String email) { + this.email = email; + } + + +} diff --git a/students/1058267830/newMail/src/com/coderising/ood/srp/product_promotion.txt b/students/1058267830/newMail/src/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/1058267830/newMail/src/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 57f042ea8e22b017a19b8fcdabbeaa64eee67d37 Mon Sep 17 00:00:00 2001 From: onlyliuxin <14703250@qq.com> Date: Sun, 18 Jun 2017 17:24:22 +0800 Subject: [PATCH 189/214] refector --- .../main/java/com/coding/basic/tree/BinarySearchTreeTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/liuxin/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java b/liuxin/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java index 590e60306c..3da4625faf 100644 --- a/liuxin/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java +++ b/liuxin/data-structure/answer/src/main/java/com/coding/basic/tree/BinarySearchTreeTest.java @@ -104,5 +104,7 @@ public void testIsValid() { public void testGetNodesBetween(){ List numbers = this.tree.getNodesBetween(3, 8); Assert.assertEquals("[3, 4, 5, 6, 8]",numbers.toString()); + numbers = this.tree.getNodesBetween(1, 8); + Assert.assertEquals("[1, 2, 3, 4, 5, 6, 8]",numbers.toString()); } } From 8071d7dd4a7eefc09f2e585be7cf0e9a8bc0ec5d Mon Sep 17 00:00:00 2001 From: XMT-CN <542194147@qq.com> Date: Sun, 18 Jun 2017 17:36:33 +0800 Subject: [PATCH 190/214] =?UTF-8?q?ood=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/542194147/pom.xml | 6 ++ .../ood/srp/config/Configuration.java | 23 +++++ .../ood/srp/config/ConfigurationKeys.java | 9 ++ .../com/coderising/ood/srp/domain/Email.java | 55 ++++++++++++ .../coderising/ood/srp/domain/Product.java | 28 ++++++ .../coderising/ood/srp/product_promotion.txt | 4 + .../ood/srp/service/NoticeService.java | 89 +++++++++++++++++++ .../com/coderising/ood/srp/util/DBUtil.java | 25 ++++++ .../com/coderising/ood/srp/util/FileUtil.java | 26 ++++++ .../com/coderising/ood/srp/util/MailUtil.java | 19 ++++ 10 files changed, 284 insertions(+) create mode 100644 students/542194147/pom.xml create mode 100644 students/542194147/src/main/java/com/coderising/ood/srp/config/Configuration.java create mode 100644 students/542194147/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java create mode 100644 students/542194147/src/main/java/com/coderising/ood/srp/domain/Email.java create mode 100644 students/542194147/src/main/java/com/coderising/ood/srp/domain/Product.java create mode 100644 students/542194147/src/main/java/com/coderising/ood/srp/product_promotion.txt create mode 100644 students/542194147/src/main/java/com/coderising/ood/srp/service/NoticeService.java create mode 100644 students/542194147/src/main/java/com/coderising/ood/srp/util/DBUtil.java create mode 100644 students/542194147/src/main/java/com/coderising/ood/srp/util/FileUtil.java create mode 100644 students/542194147/src/main/java/com/coderising/ood/srp/util/MailUtil.java diff --git a/students/542194147/pom.xml b/students/542194147/pom.xml new file mode 100644 index 0000000000..dfd96c3362 --- /dev/null +++ b/students/542194147/pom.xml @@ -0,0 +1,6 @@ + + 4.0.0 + com.coding2017 + season2 + 0.0.1-SNAPSHOT + \ No newline at end of file diff --git a/students/542194147/src/main/java/com/coderising/ood/srp/config/Configuration.java b/students/542194147/src/main/java/com/coderising/ood/srp/config/Configuration.java new file mode 100644 index 0000000000..8474097a7e --- /dev/null +++ b/students/542194147/src/main/java/com/coderising/ood/srp/config/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp.config; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/542194147/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java b/students/542194147/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java new file mode 100644 index 0000000000..7fe226d1bd --- /dev/null +++ b/students/542194147/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp.config; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/542194147/src/main/java/com/coderising/ood/srp/domain/Email.java b/students/542194147/src/main/java/com/coderising/ood/srp/domain/Email.java new file mode 100644 index 0000000000..85b73ef7a4 --- /dev/null +++ b/students/542194147/src/main/java/com/coderising/ood/srp/domain/Email.java @@ -0,0 +1,55 @@ +package com.coderising.ood.srp.domain; + +import java.io.Serializable; +/** + * 邮件实体类 + * @author 小摩托 + * + */ +public class Email implements Serializable { + + private String smtpHost = null; + private String altSmtpHost = null; + private String fromAddress = null; + private String toAddress = null; + private String subject = null; + private String message = null; + public String getSmtpHost() { + return smtpHost; + } + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + public String getAltSmtpHost() { + return altSmtpHost; + } + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + public String getFromAddress() { + return fromAddress; + } + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + public String getToAddress() { + return toAddress; + } + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + public String getSubject() { + return subject; + } + public void setSubject(String subject) { + this.subject = subject; + } + public String getMessage() { + return message; + } + public void setMessage(String message) { + this.message = message; + } + + +} diff --git a/students/542194147/src/main/java/com/coderising/ood/srp/domain/Product.java b/students/542194147/src/main/java/com/coderising/ood/srp/domain/Product.java new file mode 100644 index 0000000000..9976f9f09c --- /dev/null +++ b/students/542194147/src/main/java/com/coderising/ood/srp/domain/Product.java @@ -0,0 +1,28 @@ +package com.coderising.ood.srp.domain; + +import java.io.Serializable; + +/** + * 产品实体类 + * @author 小摩托 + * + */ +public class Product implements Serializable { + + private String productID = null; + private String productDesc = null; + public String getProductID() { + return productID; + } + public void setProductID(String productID) { + this.productID = productID; + } + public String getProductDesc() { + return productDesc; + } + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + + +} diff --git a/students/542194147/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/542194147/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/542194147/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/542194147/src/main/java/com/coderising/ood/srp/service/NoticeService.java b/students/542194147/src/main/java/com/coderising/ood/srp/service/NoticeService.java new file mode 100644 index 0000000000..53aa4b77d5 --- /dev/null +++ b/students/542194147/src/main/java/com/coderising/ood/srp/service/NoticeService.java @@ -0,0 +1,89 @@ +package com.coderising.ood.srp.service; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import com.coderising.ood.srp.config.Configuration; +import com.coderising.ood.srp.config.ConfigurationKeys; +import com.coderising.ood.srp.domain.Email; +import com.coderising.ood.srp.domain.Product; +import com.coderising.ood.srp.util.DBUtil; +import com.coderising.ood.srp.util.FileUtil; +import com.coderising.ood.srp.util.MailUtil; + +/** + * 消息发布接口 + * @author 小摩托 + * + */ +public class NoticeService { + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + private static Configuration config = new Configuration();; + + public void sendEMails(File file, boolean mailDebug) throws IOException + { + + String[] data=FileUtil.readFile(file); + Product product=new Product(); + product.setProductID(data[0]); + product.setProductDesc(data[1]); + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + product.getProductID() +"' " + + "and send_mail=1 "; + List list=DBUtil.query(sendMailQuery); + System.out.println("开始发送邮件"); + Email email=new Email(); + if (list != null) { + Iterator iter = list.iterator(); + while (iter.hasNext()) { + Map userInfo=(HashMap) iter.next(); + String toAddress = (String)userInfo.get(EMAIL_KEY); + email.setToAddress(toAddress); + email.setFromAddress(config.getProperty(ConfigurationKeys.EMAIL_ADMIN)); + email.setSmtpHost(config.getProperty(ConfigurationKeys.SMTP_SERVER)); + email.setAltSmtpHost(config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + if (toAddress.length() > 0){ + String name = (String)userInfo.get(NAME_KEY); + email.setSubject("您关注的产品降价了"); + email.setMessage("尊敬的 "+name+", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!"); + } + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(email); + } + catch (Exception e) + { + try { + MailUtil.sendEmail(email); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + else { + System.out.println("没有邮件发送"); + + } + + } + public static void main(String[] args) throws Exception { + + File f = new File("C:\\Users\\john\\Documents\\GitHub\\coding2017-2ndSeason\\students\\542194147\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"); + boolean emailDebug = false; + NoticeService ns = new NoticeService(); + ns.sendEMails(f, emailDebug); + + } +} diff --git a/students/542194147/src/main/java/com/coderising/ood/srp/util/DBUtil.java b/students/542194147/src/main/java/com/coderising/ood/srp/util/DBUtil.java new file mode 100644 index 0000000000..886477c678 --- /dev/null +++ b/students/542194147/src/main/java/com/coderising/ood/srp/util/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp.util; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa"+i+"@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/542194147/src/main/java/com/coderising/ood/srp/util/FileUtil.java b/students/542194147/src/main/java/com/coderising/ood/srp/util/FileUtil.java new file mode 100644 index 0000000000..035ec8749f --- /dev/null +++ b/students/542194147/src/main/java/com/coderising/ood/srp/util/FileUtil.java @@ -0,0 +1,26 @@ +package com.coderising.ood.srp.util; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class FileUtil { + + public static String[] readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + return data; + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } +} diff --git a/students/542194147/src/main/java/com/coderising/ood/srp/util/MailUtil.java b/students/542194147/src/main/java/com/coderising/ood/srp/util/MailUtil.java new file mode 100644 index 0000000000..817ca96466 --- /dev/null +++ b/students/542194147/src/main/java/com/coderising/ood/srp/util/MailUtil.java @@ -0,0 +1,19 @@ +package com.coderising.ood.srp.util; + +import com.coderising.ood.srp.domain.Email; + +public class MailUtil { + + public static void sendEmail(Email email) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(email.getFromAddress()).append("\n"); + buffer.append("To:").append(email.getToAddress()).append("\n"); + buffer.append("Subject:").append(email.getSubject()).append("\n"); + buffer.append("Content:").append(email.getMessage()).append("\n"); + System.out.println(buffer.toString()); + + } + + +} From d3a9325b6c8293e87b91d10da755909605fd096d Mon Sep 17 00:00:00 2001 From: '1299310140' <'13437282785@163.com'> Date: Sun, 18 Jun 2017 17:57:44 +0800 Subject: [PATCH 191/214] =?UTF-8?q?SRP=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coderising/ood/srp/CheckUtil.java | 11 + .../src/com/coderising/ood/srp/Product.java | 24 +++ .../com/coderising/ood/srp/PromotionMail.java | 197 +++++------------- .../src/com/coderising/ood/srp/ReadFile.java | 34 +++ 4 files changed, 116 insertions(+), 150 deletions(-) create mode 100644 students/1299310140/src/com/coderising/ood/srp/CheckUtil.java create mode 100644 students/1299310140/src/com/coderising/ood/srp/Product.java create mode 100644 students/1299310140/src/com/coderising/ood/srp/ReadFile.java diff --git a/students/1299310140/src/com/coderising/ood/srp/CheckUtil.java b/students/1299310140/src/com/coderising/ood/srp/CheckUtil.java new file mode 100644 index 0000000000..657c4c8102 --- /dev/null +++ b/students/1299310140/src/com/coderising/ood/srp/CheckUtil.java @@ -0,0 +1,11 @@ +package com.coderising.ood.srp; + +public class CheckUtil { + protected static boolean checkEmail(String emailAddress){ + if(emailAddress.length() > 0){ + return true; + }else{ + return false; + } + } +} diff --git a/students/1299310140/src/com/coderising/ood/srp/Product.java b/students/1299310140/src/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..4d1706b42b --- /dev/null +++ b/students/1299310140/src/com/coderising/ood/srp/Product.java @@ -0,0 +1,24 @@ +package com.coderising.ood.srp; + +public class Product { + + private String id; + + private String desc; + + public Product(String id, String desc) { + super(); + this.id = id; + this.desc = desc; + } + + public String getId() { + return id; + } + + public String getDesc() { + return desc; + } + + +} diff --git a/students/1299310140/src/com/coderising/ood/srp/PromotionMail.java b/students/1299310140/src/com/coderising/ood/srp/PromotionMail.java index 94bfcbaf54..aa40bc6251 100644 --- a/students/1299310140/src/com/coderising/ood/srp/PromotionMail.java +++ b/students/1299310140/src/com/coderising/ood/srp/PromotionMail.java @@ -1,199 +1,96 @@ package com.coderising.ood.srp; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; import java.io.IOException; -import java.io.Serializable; import java.util.HashMap; import java.util.Iterator; import java.util.List; public class PromotionMail { - - - protected String sendMailQuery = null; - - + protected String smtpHost = null; protected String altSmtpHost = null; protected String fromAddress = null; + protected String toAddress = null; protected String subject = null; protected String message = null; - protected String productID = null; - protected String productDesc = null; - - private static Configuration config; - - - private static final String NAME_KEY = "NAME"; private static final String EMAIL_KEY = "EMAIL"; public static void main(String[] args) throws Exception { - File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); - boolean emailDebug = false; - - PromotionMail pe = new PromotionMail(f, emailDebug); - - } - - - public PromotionMail(File file, boolean mailDebug) throws Exception { - - //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - readFile(file); - + //C:\\my Program Files\\mygit\\second\\coding2017\\students\\1299310140\\src\\com\\coderising\\ood\\srp + String productFilePath = "C:\\my Program Files\\mygit\\second\\coding2017\\students\\1299310140\\src\\com\\coderising\\ood\\srp\\product_promotion.txt"; + Product product = ReadFile.readProductFile(productFilePath); - config = new Configuration(); + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + product.getId() +"' " + + "and send_mail=1 "; + List list = DBUtil.query(sendMailQuery); - setSMTPHost(); - setAltSMTPHost(); - - - setFromAddress(); - + if(list == null){ + System.out.println("没有邮件发送"); + return; + } - setLoadQuery(); + Configuration config = new Configuration(); + PromotionMail pe = new PromotionMail(config); + boolean emailDebug = false; - sendEMails(mailDebug, loadMailingList()); - + Iterator iter = list.iterator(); + while (iter.hasNext()) { + HashMap userInfo = (HashMap) iter.next(); + if(CheckUtil.checkEmail((String) userInfo.get(EMAIL_KEY))){ + pe.setMessageAndToAddress(userInfo,product); + pe.sendEMails(emailDebug); + } + } } - - - - protected void setProductID(String productID) - { - this.productID = productID; - - } - - protected String getproductID() - { - return productID; - } - - protected void setLoadQuery() throws Exception { - - sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + productID +"' " - + "and send_mail=1 "; + + public PromotionMail(Configuration config){ + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); - System.out.println("loadQuery set"); - } - + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); - protected void setSMTPHost() - { - smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); - } - - - protected void setAltSMTPHost() - { - altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); - - } - - - protected void setFromAddress() - { - fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } - protected void setMessage(HashMap userInfo) throws IOException + protected void setMessageAndToAddress(HashMap userInfo,Product product) { + toAddress = (String) userInfo.get(EMAIL_KEY); String name = (String) userInfo.get(NAME_KEY); subject = "您关注的产品降价了"; - message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; - + message = "尊敬的 "+name+", 您关注的产品 " + product.getDesc() + " 降价了,欢迎购买!" ; - - } - - - protected void readFile(File file) throws IOException // @02C - { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - setProductID(data[0]); - setProductDesc(data[1]); - - System.out.println("产品ID = " + productID + "\n"); - System.out.println("产品描述 = " + productDesc + "\n"); - - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); - } - } - - private void setProductDesc(String desc) { - this.productDesc = desc; - } - - - protected void configureEMail(HashMap userInfo) throws IOException - { - toAddress = (String) userInfo.get(EMAIL_KEY); - if (toAddress.length() > 0) - setMessage(userInfo); } - - protected List loadMailingList() throws Exception { - return DBUtil.query(this.sendMailQuery); - } - - protected void sendEMails(boolean debug, List mailingList) throws IOException + protected void sendEMails(boolean debug) throws IOException { System.out.println("开始发送邮件"); - - - if (mailingList != null) { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) { - configureEMail((HashMap) iter.next()); - try - { - if (toAddress.length() > 0) - MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } - catch (Exception e) - { - - try { - MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) - { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } - } + try + { - - } - - else { - System.out.println("没有邮件发送"); + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } } - } } diff --git a/students/1299310140/src/com/coderising/ood/srp/ReadFile.java b/students/1299310140/src/com/coderising/ood/srp/ReadFile.java new file mode 100644 index 0000000000..15aadf91b8 --- /dev/null +++ b/students/1299310140/src/com/coderising/ood/srp/ReadFile.java @@ -0,0 +1,34 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class ReadFile { + + protected static Product readProductFile(String productFilePath) throws IOException + { + File file = new File(productFilePath); + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + Product product = new Product(data[0],data[1]); + + System.out.println("产品ID = " + product.getId() + "\n"); + System.out.println("产品描述 = " + product.getDesc() + "\n"); + + return product; + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } +} From ae73d968d2700ffb68cbfcc552918b757661b52e Mon Sep 17 00:00:00 2001 From: SYCHS <429301805@qq.com> Date: Sun, 18 Jun 2017 18:34:02 +0800 Subject: [PATCH 192/214] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E6=8F=90=E4=BA=A4=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/429301805/ood-assignment/pom.xml | 32 ++++ .../com/coderising/ood/srp/Configuration.java | 23 +++ .../coderising/ood/srp/ConfigurationKeys.java | 13 ++ .../java/com/coderising/ood/srp/DBUtil.java | 25 +++ .../com/coderising/ood/srp/HostService.java | 33 ++++ .../java/com/coderising/ood/srp/MailUtil.java | 40 +++++ .../java/com/coderising/ood/srp/Product.java | 23 +++ .../com/coderising/ood/srp/PromotionMail.java | 144 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 9 files changed, 337 insertions(+) create mode 100644 students/429301805/ood-assignment/pom.xml create mode 100644 students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/HostService.java create mode 100644 students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/429301805/ood-assignment/pom.xml b/students/429301805/ood-assignment/pom.xml new file mode 100644 index 0000000000..1be81576cc --- /dev/null +++ b/students/429301805/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..927c7155cc --- /dev/null +++ b/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..33e1d29f7d --- /dev/null +++ b/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,13 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + + public static final String NAME_KEY = "NAME"; + public static final String EMAIL_KEY = "EMAIL"; + + +} diff --git a/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..605d196312 --- /dev/null +++ b/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "user"+i+"@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/HostService.java b/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/HostService.java new file mode 100644 index 0000000000..ccb5041cad --- /dev/null +++ b/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/HostService.java @@ -0,0 +1,33 @@ +package com.coderising.ood.srp; + +public class HostService { + + private static String smtpHost; + + private static String altSmtpHost; + + public static void setSMTPHost(Configuration config) + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + public static void setAltSMTPHost(Configuration config) + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + public static String getSmtpHost() { + return smtpHost; + } + + + public static String getAltSmtpHost() { + return altSmtpHost; + } + + + +} diff --git a/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..f437e0e651 --- /dev/null +++ b/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,40 @@ +package com.coderising.ood.srp; + +import java.io.IOException; +import java.util.HashMap; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + public static boolean configureEMail(HashMap userInfo,String toAddress) //throws IOException + { + if (toAddress.length() > 0){ + return true; + }else{ + return false; + } + } + + public static String setFromAddress(Configuration config) + { + String fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + return fromAddress; + } + + public static String setToAddress(HashMap userInfo){ + String toAddress = (String) userInfo.get(ConfigurationKeys.EMAIL_KEY); + return toAddress; + } + +} diff --git a/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..5881e903ba --- /dev/null +++ b/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; + +public class Product { + + private String productID; + private String productDesc; + + public Product(String productID,String productDesc){ + this.productID = productID; + this.productDesc = productDesc; + } + + public String getProductID() { + return productID; + } + + public String getProductDesc() { + return productDesc; + } + + + +} diff --git a/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..54e494a37b --- /dev/null +++ b/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,144 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + protected String sendMailQuery = null; + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\Users\\CHS\\Desktop\\ood-assignment1\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + config = new Configuration(); + + initService(config); + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + protected void initService(Configuration config) { + HostService.setSMTPHost(config); + HostService.setAltSMTPHost(config); + smtpHost = HostService.getSmtpHost(); + altSmtpHost = HostService.getAltSmtpHost(); + fromAddress = MailUtil.setFromAddress(config); + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + System.out.println("loadQuery set"); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(ConfigurationKeys.NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + Product p = new Product(data[0], data[1]); + productID = p.getProductID(); + productDesc = p.getProductDesc(); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + System.out.println("开始发送邮件"); + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + HashMap userInfo = (HashMap) iter.next(); + toAddress = MailUtil.setToAddress(userInfo); + if(MailUtil.configureEMail(userInfo,toAddress)) + setMessage(userInfo); + else + System.out.println("用户信息不正确!"); + try + { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/429301805/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 86c532793e1ff379a7ea8f2c7541ca8acffad241 Mon Sep 17 00:00:00 2001 From: gordon <18852861966@163.com> Date: Sun, 18 Jun 2017 18:51:39 +0800 Subject: [PATCH 193/214] ood_test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 第一次ood邮件发送重构 --- .../com/coderising/ood/srp/Configuration.java | 26 +++ .../coderising/ood/srp/ConfigurationKeys.java | 10 + .../java/com/coderising/ood/srp/DBUtil.java | 28 +++ .../java/com/coderising/ood/srp/MailUtil.java | 19 ++ .../com/coderising/ood/srp/PromotionMail.java | 202 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + .../org/coderising/liteaop/Configuration.java | 55 +++++ .../coderising/liteaop/ConfigurationKeys.java | 12 ++ .../java/org/coderising/liteaop/DBUtil.java | 29 +++ .../org/coderising/liteaop/EmailUtil.java | 96 +++++++++ .../java/org/coderising/liteaop/Mail.java | 80 +++++++ .../org/coderising/liteaop/ProductUtil.java | 33 +++ .../org/coderising/liteaop/Production.java | 20 ++ .../java/org/coderising/liteaop/User.java | 21 ++ .../java/org/coderising/liteaop/UserUtil.java | 24 +++ .../java/org/coderising/liteaop/fileUtil.java | 39 ++++ .../org/coderising/liteaop/promotionMail.java | 26 +++ 17 files changed, 724 insertions(+) create mode 100644 students/505217361/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/505217361/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/505217361/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/505217361/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/505217361/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/505217361/src/main/java/com/coderising/ood/srp/product_promotion.txt create mode 100644 students/505217361/src/test/java/org/coderising/liteaop/Configuration.java create mode 100644 students/505217361/src/test/java/org/coderising/liteaop/ConfigurationKeys.java create mode 100644 students/505217361/src/test/java/org/coderising/liteaop/DBUtil.java create mode 100644 students/505217361/src/test/java/org/coderising/liteaop/EmailUtil.java create mode 100644 students/505217361/src/test/java/org/coderising/liteaop/Mail.java create mode 100644 students/505217361/src/test/java/org/coderising/liteaop/ProductUtil.java create mode 100644 students/505217361/src/test/java/org/coderising/liteaop/Production.java create mode 100644 students/505217361/src/test/java/org/coderising/liteaop/User.java create mode 100644 students/505217361/src/test/java/org/coderising/liteaop/UserUtil.java create mode 100644 students/505217361/src/test/java/org/coderising/liteaop/fileUtil.java create mode 100644 students/505217361/src/test/java/org/coderising/liteaop/promotionMail.java diff --git a/students/505217361/src/main/java/com/coderising/ood/srp/Configuration.java b/students/505217361/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..efd3c58820 --- /dev/null +++ b/students/505217361/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,26 @@ +package main.java.com.coderising.ood.srp; + + +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} + \ No newline at end of file diff --git a/students/505217361/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/505217361/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..b927dbac2f --- /dev/null +++ b/students/505217361/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,10 @@ +package main.java.com.coderising.ood.srp; + + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/505217361/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/505217361/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..3f7760a6e7 --- /dev/null +++ b/students/505217361/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,28 @@ +package main.java.com.coderising.ood.srp; + + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + + } + + return userList; + } +} diff --git a/students/505217361/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/505217361/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..f422fd9088 --- /dev/null +++ b/students/505217361/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,19 @@ +package main.java.com.coderising.ood.srp; + + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/505217361/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/505217361/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..6796f35858 --- /dev/null +++ b/students/505217361/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,202 @@ +package main.java.com.coderising.ood.srp; + + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("E:/product_promotion.txt"); + + + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + setFromAddress(); + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + System.out.println("进入"); + br = new BufferedReader(new FileReader(file)); + System.out.println("br"); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + + if (toAddress.length() > 0) + // 首选服务器 + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + // 备选服务器 + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/505217361/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/505217361/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/505217361/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/505217361/src/test/java/org/coderising/liteaop/Configuration.java b/students/505217361/src/test/java/org/coderising/liteaop/Configuration.java new file mode 100644 index 0000000000..06dde10147 --- /dev/null +++ b/students/505217361/src/test/java/org/coderising/liteaop/Configuration.java @@ -0,0 +1,55 @@ +package test.java.org.coderising.liteaop; + + +import java.util.HashMap; +import java.util.Map; + + +public class Configuration { + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + static Map configurations = new HashMap(); + + + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + + protected String setSMTPHost() + { + smtpHost = getProperty(ConfigurationKeys.SMTP_SERVER); + return smtpHost; + } + + + protected String setAltSMTPHost() + { + altSmtpHost = getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + return altSmtpHost; + } + + + protected String setFromAddress() + { + fromAddress = getProperty(ConfigurationKeys.EMAIL_ADMIN); + return fromAddress; + } + + + +} + \ No newline at end of file diff --git a/students/505217361/src/test/java/org/coderising/liteaop/ConfigurationKeys.java b/students/505217361/src/test/java/org/coderising/liteaop/ConfigurationKeys.java new file mode 100644 index 0000000000..8471dcf4a0 --- /dev/null +++ b/students/505217361/src/test/java/org/coderising/liteaop/ConfigurationKeys.java @@ -0,0 +1,12 @@ +package test.java.org.coderising.liteaop; + + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + + + +} diff --git a/students/505217361/src/test/java/org/coderising/liteaop/DBUtil.java b/students/505217361/src/test/java/org/coderising/liteaop/DBUtil.java new file mode 100644 index 0000000000..23ec85320d --- /dev/null +++ b/students/505217361/src/test/java/org/coderising/liteaop/DBUtil.java @@ -0,0 +1,29 @@ +package test.java.org.coderising.liteaop; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + /* + * 数据库交互类 + * */ + + // 假设这个是获取人员的数据库交互 + public List executeQuery(String sql){ + System.out.println("execute sql "); + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + + User user = new User(); + user.setUsername("User"+i); + user.setEmailadd("aa@bb.com"); + userList.add(user); + + } + + return userList; + + } + +} diff --git a/students/505217361/src/test/java/org/coderising/liteaop/EmailUtil.java b/students/505217361/src/test/java/org/coderising/liteaop/EmailUtil.java new file mode 100644 index 0000000000..0e1d84fa64 --- /dev/null +++ b/students/505217361/src/test/java/org/coderising/liteaop/EmailUtil.java @@ -0,0 +1,96 @@ +package test.java.org.coderising.liteaop; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + + + +public class EmailUtil { + + private static final String EMAIL_KEY = "EMAIL"; + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String subject = null; + protected String message = null; + + + + public void sendEmail(List users,Production product){ + boolean debugs = false; + // 获取配置信息 + Configuration config = new Configuration(); + smtpHost = config.setSMTPHost(); + altSmtpHost = config.setAltSMTPHost(); + fromAddress = config.setFromAddress(); + System.out.println("开始发送邮件"); + if(users != null){ + Iterator iter = users.iterator(); + while(iter.hasNext()){ + User user = (User) iter.next(); + String userEmail = user.getEmailadd(); + String userName = user.getUsername(); + + // 获取输入值 + setMessage(userName,product); + + try{ + if(userEmail.length()>0){ + Mail mail = new Mail(); + mail.setFromAddress(fromAddress); + mail.setUserEmail(userEmail); + mail.setSmtpHost(altSmtpHost); + mail.setSubject(subject); + mail.setMessage(message); + + mail.sendEmail(debugs); + + } + }catch(Exception e ){ + try{ + + Mail mail = new Mail(); + mail.setFromAddress(fromAddress); + mail.setUserEmail(userEmail); + mail.setSmtpHost(altSmtpHost); + mail.setSubject(subject); + mail.setMessage(message); + + System.out.println("使用备用服务器地址发送邮件!"); + mail.sendEmail(debugs); + + + }catch(Exception e2){ + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + + + } + }else { + System.out.println("没有邮件发送"); + + } + + + } + + + + protected void setMessage(String username,Production product) + { + + String name = username; + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!" ; + + } + + + + + + +} diff --git a/students/505217361/src/test/java/org/coderising/liteaop/Mail.java b/students/505217361/src/test/java/org/coderising/liteaop/Mail.java new file mode 100644 index 0000000000..ddb5fa8bc1 --- /dev/null +++ b/students/505217361/src/test/java/org/coderising/liteaop/Mail.java @@ -0,0 +1,80 @@ +package test.java.org.coderising.liteaop; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; + + + +public class Mail { + String fromAddress; + String userEmail; + String smtpHost; + String subject; + String message; + + + public String getFromAddress() { + return fromAddress; + } + + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + + public String getUserEmail() { + return userEmail; + } + + + public void setUserEmail(String userEmail) { + this.userEmail = userEmail; + } + + + public String getSmtpHost() { + return smtpHost; + } + + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + + public String getSubject() { + return subject; + } + + + public void setSubject(String subject) { + this.subject = subject; + } + + + public String getMessage() { + return message; + } + + + public void setMessage(String message) { + this.message = message; + } + + + public void sendEmail(boolean debug) { + + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(userEmail).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/505217361/src/test/java/org/coderising/liteaop/ProductUtil.java b/students/505217361/src/test/java/org/coderising/liteaop/ProductUtil.java new file mode 100644 index 0000000000..40434158f4 --- /dev/null +++ b/students/505217361/src/test/java/org/coderising/liteaop/ProductUtil.java @@ -0,0 +1,33 @@ +package test.java.org.coderising.liteaop; + +import java.io.File; + +public class ProductUtil { + /* + 获取促销产品 + */ + + // 存放产品信息文本 + + Production product ; + + public Production getPromotionalProduct(){ + + // filepath 产品信息路径 + String filepath = "E:/product_promotion.txt"; + + try{ + fileUtil fu = new fileUtil(); + + product = fu.readFile(filepath); + + }catch(Exception e){ + + e.printStackTrace(); + } + + return product; + + } + +} diff --git a/students/505217361/src/test/java/org/coderising/liteaop/Production.java b/students/505217361/src/test/java/org/coderising/liteaop/Production.java new file mode 100644 index 0000000000..c148a98fab --- /dev/null +++ b/students/505217361/src/test/java/org/coderising/liteaop/Production.java @@ -0,0 +1,20 @@ +package test.java.org.coderising.liteaop; + +public class Production { + String productID; + String ProductDesc; + public String getProductID() { + return productID; + } + public void setProductID(String productID) { + this.productID = productID; + } + public String getProductDesc() { + return ProductDesc; + } + public void setProductDesc(String productDesc) { + ProductDesc = productDesc; + } + + +} diff --git a/students/505217361/src/test/java/org/coderising/liteaop/User.java b/students/505217361/src/test/java/org/coderising/liteaop/User.java new file mode 100644 index 0000000000..683733054b --- /dev/null +++ b/students/505217361/src/test/java/org/coderising/liteaop/User.java @@ -0,0 +1,21 @@ +package test.java.org.coderising.liteaop; + +public class User { + public String username ; + public String emailadd; + public String getUsername() { + return username; + } + public void setUsername(String username) { + this.username = username; + } + public String getEmailadd() { + return emailadd; + } + public void setEmailadd(String emailadd) { + this.emailadd = emailadd; + } + + + +} diff --git a/students/505217361/src/test/java/org/coderising/liteaop/UserUtil.java b/students/505217361/src/test/java/org/coderising/liteaop/UserUtil.java new file mode 100644 index 0000000000..66e4f63e29 --- /dev/null +++ b/students/505217361/src/test/java/org/coderising/liteaop/UserUtil.java @@ -0,0 +1,24 @@ +package test.java.org.coderising.liteaop; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class UserUtil { + + // 获取有关注相关产品信息的用户 + public List getSubscriptionUser(String productID){ + String sql = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + DBUtil dbu = new DBUtil(); + List users = dbu.executeQuery(sql); + + System.out.println("loadQuery set"); + return users; + + } + + +} diff --git a/students/505217361/src/test/java/org/coderising/liteaop/fileUtil.java b/students/505217361/src/test/java/org/coderising/liteaop/fileUtil.java new file mode 100644 index 0000000000..2bc83a0213 --- /dev/null +++ b/students/505217361/src/test/java/org/coderising/liteaop/fileUtil.java @@ -0,0 +1,39 @@ +package test.java.org.coderising.liteaop; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class fileUtil { + + Production product; + + public Production readFile (String filepath) throws IOException // @02C + { + File file_product = new File("E:/product_promotion.txt"); + + BufferedReader br = null; + try { + + br = new BufferedReader(new FileReader(file_product)); + + String temp = br.readLine(); + String[] data = temp.split(" "); + product = new Production(); + product.setProductID(data[0]); + product.setProductDesc(data[1]); + + System.out.println("产品ID = " + data[0] + "\n"); + System.out.println("产品描述 = " + data[1] + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + + return product; + + } +} diff --git a/students/505217361/src/test/java/org/coderising/liteaop/promotionMail.java b/students/505217361/src/test/java/org/coderising/liteaop/promotionMail.java new file mode 100644 index 0000000000..87e1438634 --- /dev/null +++ b/students/505217361/src/test/java/org/coderising/liteaop/promotionMail.java @@ -0,0 +1,26 @@ +package test.java.org.coderising.liteaop; + +import java.util.List; + +public class promotionMail { + + public static void main(String[] args) { + // 促销邮件 + + // 促销产品 + ProductUtil pp = new ProductUtil(); + Production product = pp.getPromotionalProduct(); + + // 获得订阅人员 + UserUtil uu = new UserUtil(); + List userlist = uu.getSubscriptionUser(product.getProductID()); + + // 发送邮件 + EmailUtil eu = new EmailUtil(); + eu.sendEmail(userlist,product); + + } + + + +} From e55c2b9e8d209531ec41b0d6547d88c5a2b11a1a Mon Sep 17 00:00:00 2001 From: Kimi Date: Sun, 18 Jun 2017 20:31:47 +0800 Subject: [PATCH 194/214] [MI] refactor email feature --- students/349166103/ood/ood-assignment/pom.xml | 32 +++++++ .../com/coderising/ood/srp/Configuration.java | 67 +++++++++++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 ++ .../java/com/coderising/ood/srp/DBUtil.java | 93 +++++++++++++++++++ .../com/coderising/ood/srp/MailContent.java | 54 +++++++++++ .../java/com/coderising/ood/srp/MailUtil.java | 52 +++++++++++ .../com/coderising/ood/srp/PromotionMail.java | 33 +++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 8 files changed, 344 insertions(+) create mode 100644 students/349166103/ood/ood-assignment/pom.xml create mode 100644 students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailContent.java create mode 100644 students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/349166103/ood/ood-assignment/pom.xml b/students/349166103/ood/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/349166103/ood/ood-assignment/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..80d83d3a81 --- /dev/null +++ b/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,67 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + private String smtpHost = null; + private String altSmtpHost = null; + private String fromAddress = null; + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + + public Configuration(){ + setSMTPHost(); + setAltSMTPHost(); + setFromAddress(); + } + + + private void setSMTPHost() + { + smtpHost = ConfigurationKeys.SMTP_SERVER; + } + + private void setAltSMTPHost() + { + altSmtpHost = ConfigurationKeys.ALT_SMTP_SERVER; + + } + + private void setFromAddress() + { + fromAddress = ConfigurationKeys.EMAIL_ADMIN; + } + + public String getFromAddress() + { + return fromAddress; + } + + public String getSMTPHost() + { + return smtpHost; + } + + public String getAltSMTPHost() + { + return altSmtpHost; + } + + + +} diff --git a/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..1c9afcdfb2 --- /dev/null +++ b/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,93 @@ +package com.coderising.ood.srp; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + private String sendMailQuery = null; + private String productID = null; + private String productDesc = null; + private File f = null; + + DBUtil(File f){ + try { + readFile(f); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } + + public List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + private void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + getProductID() +"' " + + "and send_mail=1 "; + System.out.println("loadQuery set"); + } + + + private void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + protected void setProductID(String productID) + { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public String getProductID() { + return productID; + } +} diff --git a/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailContent.java b/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailContent.java new file mode 100644 index 0000000000..e1abf927f4 --- /dev/null +++ b/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailContent.java @@ -0,0 +1,54 @@ +package com.coderising.ood.srp; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; + +public class MailContent { + + private String subject = null; + private String message = null; + private String toAddress = null; + private final String NAME_KEY = "NAME"; + private final String EMAIL_KEY = "EMAIL"; + + public MailContent(File f, HashMap userInfo, String prodInfo){ + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + try { + configureEMail(userInfo, prodInfo); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + + private void setMessage(HashMap userInfo, String prodInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + prodInfo + " 降价了,欢迎购买!" ; + + } + + protected void configureEMail(HashMap userInfo, String prodInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo, prodInfo); + } + + public String getToAddress() { + return toAddress; + } + + public String getSubject() { + return subject; + } + + public String getMessage() { + return message; + } +} diff --git a/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..f676fd5fdb --- /dev/null +++ b/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,52 @@ +package com.coderising.ood.srp; + +import java.io.File; +import java.util.HashMap; + +public class MailUtil { + + private MailContent mc = null; + private String toAddress = null; + private String fromAddress = null; + private String subject = null; + private String message = null; + private String smtpHost = null; + private String altSmtpHost = null; + + public MailUtil(Configuration config, File f, HashMap userInfo, String prodInfo){ + mc = new MailContent(f, userInfo, prodInfo); + toAddress = mc.getToAddress(); + subject = mc.getSubject(); + message = mc.getMessage(); + fromAddress = config.getFromAddress(); + smtpHost = config.getSMTPHost(); + altSmtpHost = config.getAltSMTPHost(); + } + public void sendEmail(boolean debug) { + try + { + if (toAddress.length() > 0) + sendEmail(smtpHost); + } + catch (Exception e) + { + try { + sendEmail(altSmtpHost); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + private void sendEmail(String server){ + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + } +} diff --git a/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..86e5abaa0d --- /dev/null +++ b/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,33 @@ +package com.coderising.ood.srp; + +import java.io.File; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + public static void main(String[] args) throws Exception { + File f = new File(System.getProperty("user.dir")+"/src/main/java/com/coderising/ood/srp/product_promotion.txt"); + boolean emailDebug = false; + PromotionMail pe = new PromotionMail(emailDebug, f); + } + + public PromotionMail(boolean debug, File f) throws Exception + { + Configuration config = new Configuration(); + DBUtil dbu = new DBUtil(f); + List mailingList = dbu.loadMailingList(); + System.out.println("开始发送邮件"); + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + MailUtil mu = new MailUtil(config, f, (HashMap)iter.next(), dbu.getProductDesc()); + mu.sendEmail(debug); + } + } + else { + System.out.println("没有邮件发送"); + } + } +} diff --git a/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..0c0124cc61 --- /dev/null +++ b/students/349166103/ood/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From a890db40df1182422a0bff56123da7d6501b866c Mon Sep 17 00:00:00 2001 From: Enan Date: Sun, 18 Jun 2017 21:00:23 +0800 Subject: [PATCH 195/214] 1241588932 --- students/1241588932/pom.xml | 54 +++++++++++++++ .../ood/srp/config/Configuration.java | 27 ++++++++ .../ood/srp/config/ConfigurationKeys.java | 9 +++ .../com/coderising/ood/srp/dao/UserDao.java | 32 +++++++++ .../coderising/ood/srp/entity/Product.java | 15 +++++ .../com/coderising/ood/srp/entity/User.java | 13 ++++ .../java/com/coderising/ood/srp/main.java | 21 ++++++ .../ood/srp/service/IPromotionMail.java | 11 ++++ .../ood/srp/service/IReadProductConfig.java | 16 +++++ .../ood/srp/service/IUserService.java | 13 ++++ .../srp/service/impl/PromotionMailImpl.java | 66 +++++++++++++++++++ .../service/impl/ReadProductConfigImpl.java | 40 +++++++++++ .../ood/srp/service/impl/UserServiceImpl.java | 20 ++++++ .../com/coderising/ood/srp/util/DBUtil.java | 26 ++++++++ .../com/coderising/ood/srp/util/MailUtil.java | 17 +++++ .../main/resources/email_config.properties | 3 + .../src/main/resources/product_promotion.txt | 4 ++ 17 files changed, 387 insertions(+) create mode 100644 students/1241588932/pom.xml create mode 100644 students/1241588932/src/main/java/com/coderising/ood/srp/config/Configuration.java create mode 100644 students/1241588932/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java create mode 100644 students/1241588932/src/main/java/com/coderising/ood/srp/dao/UserDao.java create mode 100644 students/1241588932/src/main/java/com/coderising/ood/srp/entity/Product.java create mode 100644 students/1241588932/src/main/java/com/coderising/ood/srp/entity/User.java create mode 100644 students/1241588932/src/main/java/com/coderising/ood/srp/main.java create mode 100644 students/1241588932/src/main/java/com/coderising/ood/srp/service/IPromotionMail.java create mode 100644 students/1241588932/src/main/java/com/coderising/ood/srp/service/IReadProductConfig.java create mode 100644 students/1241588932/src/main/java/com/coderising/ood/srp/service/IUserService.java create mode 100644 students/1241588932/src/main/java/com/coderising/ood/srp/service/impl/PromotionMailImpl.java create mode 100644 students/1241588932/src/main/java/com/coderising/ood/srp/service/impl/ReadProductConfigImpl.java create mode 100644 students/1241588932/src/main/java/com/coderising/ood/srp/service/impl/UserServiceImpl.java create mode 100644 students/1241588932/src/main/java/com/coderising/ood/srp/util/DBUtil.java create mode 100644 students/1241588932/src/main/java/com/coderising/ood/srp/util/MailUtil.java create mode 100644 students/1241588932/src/main/resources/email_config.properties create mode 100644 students/1241588932/src/main/resources/product_promotion.txt diff --git a/students/1241588932/pom.xml b/students/1241588932/pom.xml new file mode 100644 index 0000000000..ad245eee9f --- /dev/null +++ b/students/1241588932/pom.xml @@ -0,0 +1,54 @@ + + 4.0.0 + + com.coderising + ood-assignment-enan + 0.0.1-SNAPSHOT + jar + + ood-assignment-enan + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + org.projectlombok + lombok + 1.16.16 + true + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + ${project.build.sourceEncoding} + + + + + diff --git a/students/1241588932/src/main/java/com/coderising/ood/srp/config/Configuration.java b/students/1241588932/src/main/java/com/coderising/ood/srp/config/Configuration.java new file mode 100644 index 0000000000..e468795316 --- /dev/null +++ b/students/1241588932/src/main/java/com/coderising/ood/srp/config/Configuration.java @@ -0,0 +1,27 @@ +package com.coderising.ood.srp.config; + +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + private static final String CONFIG_FILE_NAME = "email_config.properties"; + + static Map configurations = new HashMap<>(); + static{ + // TODO 从配置文件中加载配置到 map 中 + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public static String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/1241588932/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java b/students/1241588932/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java new file mode 100644 index 0000000000..7fe226d1bd --- /dev/null +++ b/students/1241588932/src/main/java/com/coderising/ood/srp/config/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp.config; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/1241588932/src/main/java/com/coderising/ood/srp/dao/UserDao.java b/students/1241588932/src/main/java/com/coderising/ood/srp/dao/UserDao.java new file mode 100644 index 0000000000..fb6970cee5 --- /dev/null +++ b/students/1241588932/src/main/java/com/coderising/ood/srp/dao/UserDao.java @@ -0,0 +1,32 @@ +package com.coderising.ood.srp.dao; + +import com.coderising.ood.srp.entity.User; +import com.coderising.ood.srp.util.DBUtil; + +import java.util.List; + +/** + * Created by Enan on 17/6/14. + */ +public class UserDao { + + private static UserDao INSTANCE; + + public static UserDao getInstance() { + if (INSTANCE == null) { + synchronized (UserDao.class) { + INSTANCE = new UserDao(); + } + } + return INSTANCE; + } + + private UserDao() {} + + public List querySubscriptedUsersByProductID(String productID) { + String sql = "Select name, email from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + return DBUtil.query(sql); + } +} diff --git a/students/1241588932/src/main/java/com/coderising/ood/srp/entity/Product.java b/students/1241588932/src/main/java/com/coderising/ood/srp/entity/Product.java new file mode 100644 index 0000000000..70b2c7870c --- /dev/null +++ b/students/1241588932/src/main/java/com/coderising/ood/srp/entity/Product.java @@ -0,0 +1,15 @@ +package com.coderising.ood.srp.entity; + +import lombok.Builder; +import lombok.Data; + +/** + * Created by Enan on 17/6/14. + */ +@Data +@Builder +public class Product { + + private String productID; + private String productDesc; +} diff --git a/students/1241588932/src/main/java/com/coderising/ood/srp/entity/User.java b/students/1241588932/src/main/java/com/coderising/ood/srp/entity/User.java new file mode 100644 index 0000000000..0f79352cf4 --- /dev/null +++ b/students/1241588932/src/main/java/com/coderising/ood/srp/entity/User.java @@ -0,0 +1,13 @@ +package com.coderising.ood.srp.entity; + +import lombok.Data; + +/** + * Created by Enan on 17/6/14. + */ +@Data +public class User { + + private String name; + private String email; +} diff --git a/students/1241588932/src/main/java/com/coderising/ood/srp/main.java b/students/1241588932/src/main/java/com/coderising/ood/srp/main.java new file mode 100644 index 0000000000..af2b5ce8f1 --- /dev/null +++ b/students/1241588932/src/main/java/com/coderising/ood/srp/main.java @@ -0,0 +1,21 @@ +package com.coderising.ood.srp; + +import com.coderising.ood.srp.service.IPromotionMail; +import com.coderising.ood.srp.service.impl.PromotionMailImpl; +import com.coderising.ood.srp.service.impl.ReadProductConfigImpl; +import com.coderising.ood.srp.service.impl.UserServiceImpl; + +import java.io.File; +import java.net.URL; + +/** + * Created by Enan on 17/6/18. + */ +public class main { + + public static void main(String[] args) { + IPromotionMail promotionMail = new PromotionMailImpl(new UserServiceImpl(), new ReadProductConfigImpl()); + URL base = Thread.currentThread().getContextClassLoader().getResource(""); + promotionMail.send(new File(base.getFile(), "product_promotion.txt")); + } +} diff --git a/students/1241588932/src/main/java/com/coderising/ood/srp/service/IPromotionMail.java b/students/1241588932/src/main/java/com/coderising/ood/srp/service/IPromotionMail.java new file mode 100644 index 0000000000..ba940b09d8 --- /dev/null +++ b/students/1241588932/src/main/java/com/coderising/ood/srp/service/IPromotionMail.java @@ -0,0 +1,11 @@ +package com.coderising.ood.srp.service; + +import java.io.File; + +/** + * Created by Enan on 17/6/18. + */ +public interface IPromotionMail { + + void send(File file); +} diff --git a/students/1241588932/src/main/java/com/coderising/ood/srp/service/IReadProductConfig.java b/students/1241588932/src/main/java/com/coderising/ood/srp/service/IReadProductConfig.java new file mode 100644 index 0000000000..1b2c45d1ee --- /dev/null +++ b/students/1241588932/src/main/java/com/coderising/ood/srp/service/IReadProductConfig.java @@ -0,0 +1,16 @@ +package com.coderising.ood.srp.service; + +import com.coderising.ood.srp.entity.Product; + +import java.io.File; +import java.io.IOException; +import java.util.Collection; + +/** + * Created by Enan on 17/6/18. + */ +public interface IReadProductConfig { + + Collection read(File file) throws IOException; + +} diff --git a/students/1241588932/src/main/java/com/coderising/ood/srp/service/IUserService.java b/students/1241588932/src/main/java/com/coderising/ood/srp/service/IUserService.java new file mode 100644 index 0000000000..652ba08fbb --- /dev/null +++ b/students/1241588932/src/main/java/com/coderising/ood/srp/service/IUserService.java @@ -0,0 +1,13 @@ +package com.coderising.ood.srp.service; + +import com.coderising.ood.srp.entity.User; + +import java.util.Collection; + +/** + * Created by Enan on 17/6/18. + */ +public interface IUserService { + + Collection querySubscriptedUsersByProductID(String productID); +} diff --git a/students/1241588932/src/main/java/com/coderising/ood/srp/service/impl/PromotionMailImpl.java b/students/1241588932/src/main/java/com/coderising/ood/srp/service/impl/PromotionMailImpl.java new file mode 100644 index 0000000000..497f16ccfd --- /dev/null +++ b/students/1241588932/src/main/java/com/coderising/ood/srp/service/impl/PromotionMailImpl.java @@ -0,0 +1,66 @@ +package com.coderising.ood.srp.service.impl; + +import com.coderising.ood.srp.config.Configuration; +import com.coderising.ood.srp.config.ConfigurationKeys; +import com.coderising.ood.srp.entity.Product; +import com.coderising.ood.srp.entity.User; +import com.coderising.ood.srp.service.IPromotionMail; +import com.coderising.ood.srp.service.IReadProductConfig; +import com.coderising.ood.srp.service.IUserService; +import com.coderising.ood.srp.util.MailUtil; + +import java.io.File; +import java.io.IOException; +import java.util.Collection; +import java.util.List; + +/** + * Created by Enan on 17/6/18. + */ +public class PromotionMailImpl implements IPromotionMail { + + private IUserService userService; + private IReadProductConfig readProductConfig; + + private static final String SUBJECT = "您关注的产品降价了"; + private static final String MESSAGE = "尊敬的 %s, 您关注的产品 %s 降价了,欢迎购买!"; + + public PromotionMailImpl (IUserService iUserService, IReadProductConfig iReadProductConfig) { + this.userService = iUserService; + this.readProductConfig = iReadProductConfig; + } + + @Override + public void send(File file) { + Collection products; + try { + products = readProductConfig.read(file); + } catch (IOException e) { + throw new RuntimeException("读取降价商品失败,终止发送降价邮件提醒"); + } + for (Product product : products) { + List users = (List) userService.querySubscriptedUsersByProductID(product.getProductID()); + + for (User user : users) { + try { + if (user.getEmail().length() > 0) + MailUtil.sendEmail(user.getEmail(), + Configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN), + SUBJECT, + String.format(MESSAGE, user.getName(), product.getProductDesc()), + Configuration.getProperty(ConfigurationKeys.SMTP_SERVER)); + } catch (Exception e) { + try { + MailUtil.sendEmail(user.getEmail(), + Configuration.getProperty(ConfigurationKeys.EMAIL_ADMIN), + SUBJECT, + String.format(MESSAGE, user.getName(), product.getProductDesc()), + Configuration.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + } + } +} diff --git a/students/1241588932/src/main/java/com/coderising/ood/srp/service/impl/ReadProductConfigImpl.java b/students/1241588932/src/main/java/com/coderising/ood/srp/service/impl/ReadProductConfigImpl.java new file mode 100644 index 0000000000..9360be24ad --- /dev/null +++ b/students/1241588932/src/main/java/com/coderising/ood/srp/service/impl/ReadProductConfigImpl.java @@ -0,0 +1,40 @@ +package com.coderising.ood.srp.service.impl; + +import com.coderising.ood.srp.entity.Product; +import com.coderising.ood.srp.service.IReadProductConfig; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * Created by Enan on 17/6/18. + */ +public class ReadProductConfigImpl implements IReadProductConfig { + @Override + public Collection read(File file) throws IOException { + List products = new ArrayList<>(); + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp; + while ((temp = br.readLine()) != null) { + String[] data = temp.split(" "); + + System.out.println("产品ID = " + data[0] + "\n"); + System.out.println("产品描述 = " + data[1] + "\n"); + + products.add(Product.builder().productID(data[0]).productDesc(data[1]).build()); + } + return products; + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } +} diff --git a/students/1241588932/src/main/java/com/coderising/ood/srp/service/impl/UserServiceImpl.java b/students/1241588932/src/main/java/com/coderising/ood/srp/service/impl/UserServiceImpl.java new file mode 100644 index 0000000000..f38ce0f773 --- /dev/null +++ b/students/1241588932/src/main/java/com/coderising/ood/srp/service/impl/UserServiceImpl.java @@ -0,0 +1,20 @@ +package com.coderising.ood.srp.service.impl; + +import com.coderising.ood.srp.dao.UserDao; +import com.coderising.ood.srp.entity.User; +import com.coderising.ood.srp.service.IUserService; + +import java.util.List; + +/** + * Created by Enan on 17/6/18. + */ +public class UserServiceImpl implements IUserService { + + private UserDao userDao = UserDao.getInstance(); + + @Override + public List querySubscriptedUsersByProductID(String productID) { + return userDao.querySubscriptedUsersByProductID(productID); + } +} diff --git a/students/1241588932/src/main/java/com/coderising/ood/srp/util/DBUtil.java b/students/1241588932/src/main/java/com/coderising/ood/srp/util/DBUtil.java new file mode 100644 index 0000000000..a6e495ab07 --- /dev/null +++ b/students/1241588932/src/main/java/com/coderising/ood/srp/util/DBUtil.java @@ -0,0 +1,26 @@ +package com.coderising.ood.srp.util; +import com.coderising.ood.srp.entity.User; + +import java.util.ArrayList; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + User user = new User(); + user.setName("User" + i); + user.setEmail("aa@bb.com"); + userList.add(user); + } + + return userList; + } +} diff --git a/students/1241588932/src/main/java/com/coderising/ood/srp/util/MailUtil.java b/students/1241588932/src/main/java/com/coderising/ood/srp/util/MailUtil.java new file mode 100644 index 0000000000..dd640423c9 --- /dev/null +++ b/students/1241588932/src/main/java/com/coderising/ood/srp/util/MailUtil.java @@ -0,0 +1,17 @@ +package com.coderising.ood.srp.util; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/1241588932/src/main/resources/email_config.properties b/students/1241588932/src/main/resources/email_config.properties new file mode 100644 index 0000000000..6f5615d18b --- /dev/null +++ b/students/1241588932/src/main/resources/email_config.properties @@ -0,0 +1,3 @@ +smtp.server=smtp.163.com +alt.smtp.server=smtp1.163.com +email.admin=admin@company.com \ No newline at end of file diff --git a/students/1241588932/src/main/resources/product_promotion.txt b/students/1241588932/src/main/resources/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/1241588932/src/main/resources/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From efd431d82bce6ba23c12cf00d496ea84ba042832 Mon Sep 17 00:00:00 2001 From: onlyliuxin <14703250@qq.com> Date: Sun, 18 Jun 2017 22:10:32 +0800 Subject: [PATCH 196/214] =?UTF-8?q?ood=20=E7=AC=AC=E4=BA=8C=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/coderising/ood/ocp/DateUtil.java | 10 +++++ .../java/com/coderising/ood/ocp/Logger.java | 38 +++++++++++++++++++ .../java/com/coderising/ood/ocp/MailUtil.java | 10 +++++ .../java/com/coderising/ood/ocp/SMSUtil.java | 10 +++++ 4 files changed, 68 insertions(+) create mode 100644 liuxin/ood/ood-assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java create mode 100644 liuxin/ood/ood-assignment/src/main/java/com/coderising/ood/ocp/Logger.java create mode 100644 liuxin/ood/ood-assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java create mode 100644 liuxin/ood/ood-assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java diff --git a/liuxin/ood/ood-assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java b/liuxin/ood/ood-assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java new file mode 100644 index 0000000000..0d0d01098f --- /dev/null +++ b/liuxin/ood/ood-assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class DateUtil { + + public static String getCurrentDateAsString() { + + return null; + } + +} diff --git a/liuxin/ood/ood-assignment/src/main/java/com/coderising/ood/ocp/Logger.java b/liuxin/ood/ood-assignment/src/main/java/com/coderising/ood/ocp/Logger.java new file mode 100644 index 0000000000..aca173e665 --- /dev/null +++ b/liuxin/ood/ood-assignment/src/main/java/com/coderising/ood/ocp/Logger.java @@ -0,0 +1,38 @@ +package com.coderising.ood.ocp; + +public class Logger { + + public final int RAW_LOG = 1; + public final int RAW_LOG_WITH_DATE = 2; + public final int EMAIL_LOG = 1; + public final int SMS_LOG = 2; + public final int PRINT_LOG = 3; + + int type = 0; + int method = 0; + + public Logger(int logType, int logMethod){ + this.type = logType; + this.method = logMethod; + } + public void log(String msg){ + + String logMsg = msg; + + if(this.type == RAW_LOG){ + logMsg = msg; + } else if(this.type == RAW_LOG_WITH_DATE){ + String txtDate = DateUtil.getCurrentDateAsString(); + logMsg = txtDate + ": " + msg; + } + + if(this.method == EMAIL_LOG){ + MailUtil.send(logMsg); + } else if(this.method == SMS_LOG){ + SMSUtil.send(logMsg); + } else if(this.method == PRINT_LOG){ + System.out.println(logMsg); + } + } +} + diff --git a/liuxin/ood/ood-assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java b/liuxin/ood/ood-assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java new file mode 100644 index 0000000000..59d77649a2 --- /dev/null +++ b/liuxin/ood/ood-assignment/src/main/java/com/coderising/ood/ocp/MailUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class MailUtil { + + public static void send(String logMsg) { + // TODO Auto-generated method stub + + } + +} diff --git a/liuxin/ood/ood-assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java b/liuxin/ood/ood-assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java new file mode 100644 index 0000000000..fab4cd01b7 --- /dev/null +++ b/liuxin/ood/ood-assignment/src/main/java/com/coderising/ood/ocp/SMSUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class SMSUtil { + + public static void send(String logMsg) { + // TODO Auto-generated method stub + + } + +} From fb35e9f8ec1fb619d50f650198c888511c67cbb8 Mon Sep 17 00:00:00 2001 From: YouHmilyForProgramming <706097141@qq.com> Date: Sun, 18 Jun 2017 22:12:10 +0800 Subject: [PATCH 197/214] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Configuration.java" | 23 ++++++ .../ConfigurationKeys.java" | 9 +++ .../DBUtil.java" | 41 ++++++++++ .../EMail.java" | 48 +++++++++++ .../MailUtil.java" | 20 +++++ .../Product.java" | 60 ++++++++++++++ .../PromotionMail.java" | 80 +++++++++++++++++++ .../Server.java" | 20 +++++ .../User.java" | 25 ++++++ .../product_promotion.txt" | 4 + 10 files changed, 330 insertions(+) create mode 100644 "students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/Configuration.java" create mode 100644 "students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/ConfigurationKeys.java" create mode 100644 "students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/DBUtil.java" create mode 100644 "students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/EMail.java" create mode 100644 "students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MailUtil.java" create mode 100644 "students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/Product.java" create mode 100644 "students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/PromotionMail.java" create mode 100644 "students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/Server.java" create mode 100644 "students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/User.java" create mode 100644 "students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/product_promotion.txt" diff --git "a/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/Configuration.java" "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/Configuration.java" new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/Configuration.java" @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git "a/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/ConfigurationKeys.java" "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/ConfigurationKeys.java" new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/ConfigurationKeys.java" @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git "a/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/DBUtil.java" "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/DBUtil.java" new file mode 100644 index 0000000000..c3107056d5 --- /dev/null +++ "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/DBUtil.java" @@ -0,0 +1,41 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + private String sql; + + + public void setLoadQuery(String productID) throws Exception { + + String sql = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + this.setSql(sql); + System.out.println("loadQuery set"); + } + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public List queryForList(){ + String query=this.getSql(); + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + User userInfo = new User(); + userInfo.setNAME("User" + i); + userInfo.setEMAIL("aa@bb.com"); + userList.add(userInfo); + } + return userList; + } + public String getSql() { + return sql; + } + public void setSql(String sql) { + this.sql = sql; + } +} diff --git "a/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/EMail.java" "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/EMail.java" new file mode 100644 index 0000000000..53834eba04 --- /dev/null +++ "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/EMail.java" @@ -0,0 +1,48 @@ +package com.coderising.ood.srp; + +import java.util.HashMap; + +public class EMail { + + private String fromAddress; + private String toAddress; + private String subject; + private String message; + + + + public EMail(String fromAddress, String toAddress, String subject, String message) { + this.fromAddress = fromAddress; + this.toAddress = toAddress; + this.subject = subject; + this.message = message; + } + + + public String getFromAddress() { + return fromAddress; + } + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + public String getToAddress() { + return toAddress; + } + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + public String getSubject() { + return subject; + } + public void setSubject(String subject) { + this.subject = subject; + } + public String getMessage() { + return message; + } + public void setMessage(String message) { + this.message=message; + } + + +} diff --git "a/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MailUtil.java" "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MailUtil.java" new file mode 100644 index 0000000000..86def76810 --- /dev/null +++ "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/MailUtil.java" @@ -0,0 +1,20 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git "a/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/Product.java" "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/Product.java" new file mode 100644 index 0000000000..93ec68aae0 --- /dev/null +++ "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/Product.java" @@ -0,0 +1,60 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +/** + * 产品类 + * @author Administrator + * + */ +public class Product { + + /** + * 产品ID + */ + private String productID; + + /** + * 产品描述 + */ + private String productDesc; + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + + public static Product readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + Product product = new Product(); + product.setProductID(data[0]); + product.setProductDesc(data[1]); + System.out.println("产品ID = " + product.getProductID() + "\n"); + System.out.println("产品描述 = " + product.getProductDesc() + "\n"); + return product; + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } +} diff --git "a/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/PromotionMail.java" "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/PromotionMail.java" new file mode 100644 index 0000000000..1c6225abdb --- /dev/null +++ "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/PromotionMail.java" @@ -0,0 +1,80 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + private static Configuration config; + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + Product product=Product.readFile(file); + config = new Configuration(); + Server server =new Server(); + server.setSmtpHost(config.getProperty(ConfigurationKeys.SMTP_SERVER)); + server.setAltSmtpHost(config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + DBUtil dbUtil = new DBUtil(); + dbUtil.setLoadQuery(product.getProductID()); + List mailingList= dbUtil.queryForList(); + if (mailingList != null) { + System.out.println("开始发送邮件"); + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + HashMap userInfo = (HashMap) iter.next(); + String toAddress = (String) userInfo.get(EMAIL_KEY); + String name = (String) userInfo.get(NAME_KEY); + String subject = "您关注的产品降价了"; + String message = "尊敬的 "+name+", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!" ; + String fromAddress=config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + EMail eMail = new EMail(toAddress,fromAddress,subject,message); + sendEMails(eMail,server,mailDebug); + } + }else{ + System.out.println("没有邮件发送"); + } + } + + protected void sendEMails(EMail eMail,Server server,boolean debug) throws IOException { + try + { + if (eMail.getToAddress().length() > 0) + MailUtil.sendEmail(eMail.getToAddress(), eMail.getFromAddress(), eMail.getSubject(), eMail.getMessage(), server.getSmtpHost(), debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(eMail.getToAddress(), eMail.getFromAddress(), eMail.getSubject(), eMail.getMessage(), server.getAltSmtpHost(), debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } +} + diff --git "a/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/Server.java" "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/Server.java" new file mode 100644 index 0000000000..37776d9aa6 --- /dev/null +++ "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/Server.java" @@ -0,0 +1,20 @@ +package com.coderising.ood.srp; + + +public class Server { + + private String smtpHost; + private String altSmtpHost; + public String getSmtpHost() { + return smtpHost; + } + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + public String getAltSmtpHost() { + return altSmtpHost; + } + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } +} diff --git "a/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/User.java" "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/User.java" new file mode 100644 index 0000000000..69d9010247 --- /dev/null +++ "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/User.java" @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; + +public class User { + + private String NAME; + + private String EMAIL; + + public String getNAME() { + return NAME; + } + + public void setNAME(String nAME) { + NAME = nAME; + } + + public String getEMAIL() { + return EMAIL; + } + + public void setEMAIL(String eMAIL) { + EMAIL = eMAIL; + } + +} diff --git "a/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/product_promotion.txt" "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/product_promotion.txt" new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ "b/students/706097141/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/product_promotion.txt" @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 7c7c5b191f7befbd2760229cc03c80bda2ce3b60 Mon Sep 17 00:00:00 2001 From: jyp Date: Sun, 18 Jun 2017 22:35:51 +0800 Subject: [PATCH 198/214] commit --- .../data-structure/build.gradle | 22 +-- .../com/coding/basic/array/ArrayUtil.java | 149 +++++++++++++----- .../com/coding/basic/array/ArrayUtilTest.java | 52 ++++++ 3 files changed, 160 insertions(+), 63 deletions(-) create mode 100644 students/992331664/data-structure/data-structure/src/test/java/com/coding/basic/array/ArrayUtilTest.java diff --git a/students/992331664/data-structure/data-structure/build.gradle b/students/992331664/data-structure/data-structure/build.gradle index 588e5e86aa..e8037fb1c4 100644 --- a/students/992331664/data-structure/data-structure/build.gradle +++ b/students/992331664/data-structure/data-structure/build.gradle @@ -1,30 +1,12 @@ -/* - * This build file was auto generated by running the Gradle 'init' task - * by 'gant' at '17-6-13 上午11:30' with Gradle 3.0 - * - * This generated file contains a sample Java project to get you started. - * For more details take a look at the Java Quickstart chapter in the Gradle - * user guide available at https://docs.gradle.org/3.0/userguide/tutorial_java_projects.html - */ - -// Apply the java plugin to add support for Java apply plugin: 'java' -// In this section you declare where to find the dependencies of your project repositories { - // Use 'jcenter' for resolving your dependencies. - // You can declare any Maven/Ivy/file repository here. jcenter() } -// In this section you declare the dependencies for your production and test code dependencies { - // The production code uses the SLF4J logging API at compile time compile 'org.slf4j:slf4j-api:1.7.21' - - // Declare the dependency for your favourite test framework you want to use in your tests. - // TestNG is also supported by the Gradle Test task. Just change the - // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add - // 'test.useTestNG()' to your build script. + compile 'org.apache.poi:poi:3.16' + compile 'org.apache.poi:poi-ooxml:3.16' testCompile 'junit:junit:4.12' } diff --git a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/array/ArrayUtil.java b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/array/ArrayUtil.java index 45740e6d57..c17e6def49 100644 --- a/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/array/ArrayUtil.java +++ b/students/992331664/data-structure/data-structure/src/main/java/com/coding/basic/array/ArrayUtil.java @@ -1,96 +1,159 @@ package com.coding.basic.array; +import java.util.Arrays; + +import javax.management.RuntimeErrorException; + public class ArrayUtil { - + /** - * 给定一个整形数组a , 对该数组的值进行置换 - 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] - 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = + * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * * @param origin * @return */ - public void reverseArray(int[] origin){ - + public void reverseArray(int[] origin) { + if (origin != null && origin.length > 1) { + for (int i = 0; i < origin.length / 2; i++) { + int temp = origin[i]; + origin[i] = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = temp; + } + } } - + /** - * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: - * {1,3,4,5,6,6,5,4,7,6,7,5} + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5} + * * @param oldArray * @return */ - - public int[] removeZero(int[] oldArray){ - return null; + + public int[] removeZero(int[] oldArray) { + // JDK 1.8 + // int[] newArray = Arrays.stream(oldArray).filter(item->item + // !=0).toArray(); + + int[] newArray = new int[oldArray.length]; + + int zeroCount = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) { + zeroCount++; + } else { + newArray[i - zeroCount] = oldArray[i]; + } + } + if (zeroCount == 0) { + return Arrays.copyOf(oldArray, oldArray.length); + } else { + return Arrays.copyOf(newArray, oldArray.length - zeroCount); + } } - + /** - * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 - * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * * @param array1 * @param array2 * @return */ - - public int[] merge(int[] array1, int[] array2){ - return null; + + public int[] merge(int[] array1, int[] array2) { + return null; } + /** * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 - * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 * [2,3,6,0,0,0] + * * @param oldArray * @param size * @return */ - public int[] grow(int [] oldArray, int size){ - return null; + public int[] grow(int[] oldArray, int size) { + if (oldArray.length + size < 0) { + throw new RuntimeErrorException(null, "size + oldArray.length 不能小于0"); + } + int[] newArray = new int[oldArray.length + size]; + + if (size < 0) { + for (int i = 0; i < newArray.length; i++) { + newArray[i] = oldArray[i]; + } + } else { + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + } + return newArray; } - + /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 - * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] - * max = 1, 则返回空数组 [] + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * * @param max * @return */ - public int[] fibonacci(int max){ - return null; + public int[] fibonacci(int max) { + if (max <= 1) { + return new int[0]; + } + int a = 1; + int count = 1; + for (int i = 1; i <= max; i += a) { + a += i; + count+=2; + } + + int[] result = new int[count]; + + a = 1; + count = 0; + result[count++] = 1; + + for (int i = 1; i <= max; i += a) { + a += i; + result[count++] = i; + result[count++] = a; + } + return result; } - + /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * * @param max * @return */ - public int[] getPrimes(int max){ + public int[] getPrimes(int max) { return null; } - + /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * * @param max * @return */ - public int[] getPerfectNumbers(int max){ + public int[] getPerfectNumbers(int max) { return null; } - + /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * * @param array * @param s * @return */ - public String join(int[] array, String seperator){ + public String join(int[] array, String seperator) { return null; } - } diff --git a/students/992331664/data-structure/data-structure/src/test/java/com/coding/basic/array/ArrayUtilTest.java b/students/992331664/data-structure/data-structure/src/test/java/com/coding/basic/array/ArrayUtilTest.java new file mode 100644 index 0000000000..255267ce2c --- /dev/null +++ b/students/992331664/data-structure/data-structure/src/test/java/com/coding/basic/array/ArrayUtilTest.java @@ -0,0 +1,52 @@ +package com.coding.basic.array; + +import java.util.Arrays; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ArrayUtilTest { + + ArrayUtil arrayUtil; + + int[] resultArray ; + + @Before + public void before(){ + arrayUtil = new ArrayUtil(); + } + + @After + public void printArray(){ + System.out.println(Arrays.toString(resultArray)); + } + + @Test + public void testReverseArray(){ + int[] arr = {12,344,5,6,0,4,65,4,}; + arrayUtil.reverseArray(arr); + resultArray = arr; + } + + @Test + public void testRemoveZero(){ + int[] arr = {}; + resultArray = arrayUtil.removeZero(arr); + } + + @Test + public void testMerge(){ + } + + @Test + public void testGrow(){ + int[] arr = {1,6,4,2,0}; + resultArray = arrayUtil.grow(arr, 2); + } + + @Test + public void testFibonacci(){ + resultArray = arrayUtil.fibonacci(15); + } +} From 9e507e2606d1a702422099d107c7f7b9a7b6dae2 Mon Sep 17 00:00:00 2001 From: Enan Date: Sun, 18 Jun 2017 23:38:46 +0800 Subject: [PATCH 199/214] 1241588932 --- .../main/java/com/coderising/ood/srp/config/Configuration.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/students/1241588932/src/main/java/com/coderising/ood/srp/config/Configuration.java b/students/1241588932/src/main/java/com/coderising/ood/srp/config/Configuration.java index e468795316..65cfff3ae9 100644 --- a/students/1241588932/src/main/java/com/coderising/ood/srp/config/Configuration.java +++ b/students/1241588932/src/main/java/com/coderising/ood/srp/config/Configuration.java @@ -9,7 +9,7 @@ public class Configuration { static Map configurations = new HashMap<>(); static{ - // TODO 从配置文件中加载配置到 map 中 + // TODO 从配置文件中加载配置到 map 中 configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); From 6bb773edf80545c6503949b3f294f74c820d2ab3 Mon Sep 17 00:00:00 2001 From: doublesouth <1241588932@qq.com> Date: Sun, 18 Jun 2017 23:54:22 +0800 Subject: [PATCH 200/214] 1241588932 --- .../main/java/com/coderising/ood/srp/config/Configuration.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/students/1241588932/src/main/java/com/coderising/ood/srp/config/Configuration.java b/students/1241588932/src/main/java/com/coderising/ood/srp/config/Configuration.java index 65cfff3ae9..e468795316 100644 --- a/students/1241588932/src/main/java/com/coderising/ood/srp/config/Configuration.java +++ b/students/1241588932/src/main/java/com/coderising/ood/srp/config/Configuration.java @@ -9,7 +9,7 @@ public class Configuration { static Map configurations = new HashMap<>(); static{ - // TODO 从配置文件中加载配置到 map 中 + // TODO 从配置文件中加载配置到 map 中 configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); From f497794da0db18c9546ba9ceeae91c5c8618157c Mon Sep 17 00:00:00 2001 From: tianxianhu <329866097@qq.com> Date: Mon, 19 Jun 2017 01:46:42 +0800 Subject: [PATCH 201/214] refactor the homework --- .../java/com/coderising/ood/srp/DBUtil.java | 11 +- .../java/com/coderising/ood/srp/Email.java | 49 +++++ .../java/com/coderising/ood/srp/FileUtil.java | 36 ++++ .../java/com/coderising/ood/srp/MailUtil.java | 18 -- .../com/coderising/ood/srp/PromotionMail.java | 204 +++--------------- .../java/com/coderising/ood/srp/User.java | 31 +++ 6 files changed, 145 insertions(+), 204 deletions(-) create mode 100644 students/329866097/src/main/java/com/coderising/ood/srp/Email.java create mode 100644 students/329866097/src/main/java/com/coderising/ood/srp/FileUtil.java delete mode 100644 students/329866097/src/main/java/com/coderising/ood/srp/MailUtil.java create mode 100644 students/329866097/src/main/java/com/coderising/ood/srp/User.java diff --git a/students/329866097/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/329866097/src/main/java/com/coderising/ood/srp/DBUtil.java index 82e9261d18..2c5d9dd968 100644 --- a/students/329866097/src/main/java/com/coderising/ood/srp/DBUtil.java +++ b/students/329866097/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -10,16 +10,13 @@ public class DBUtil { * @param sql * @return */ - public static List query(String sql){ + public static List query(String sql){ - List userList = new ArrayList(); + List userList = new ArrayList<>(); for (int i = 1; i <= 3; i++) { - HashMap userInfo = new HashMap(); - userInfo.put("NAME", "User" + i); - userInfo.put("EMAIL", "aa@bb.com"); - userList.add(userInfo); + User user = new User("User" + i, "aa@bb.com"); + userList.add(user); } - return userList; } } diff --git a/students/329866097/src/main/java/com/coderising/ood/srp/Email.java b/students/329866097/src/main/java/com/coderising/ood/srp/Email.java new file mode 100644 index 0000000000..ced66562d2 --- /dev/null +++ b/students/329866097/src/main/java/com/coderising/ood/srp/Email.java @@ -0,0 +1,49 @@ +package com.coderising.ood.srp; + +/** + * Created by tianxianhu on 2017/6/18. + */ +public class Email { + + private static String smtpHost; + private static String altSmtpHost; + private static String fromAddress; + + private static Configuration config; + + static { + config = new Configuration(); + setDefaultConfig(config); + } + + private static void setDefaultConfig(Configuration config) { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + public void send(User user, String product, String host) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + + if(user.getEmail().length() > 0) { + String name = user.getName(); + String subject = "您关注的产品降价了"; + String message = "尊敬的 " + name + ", 您关注的产品 " + product + " 降价了,欢迎购买!"; + + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(user.getEmail()).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + } + } + + public String getSmtpHost() { + return smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } +} diff --git a/students/329866097/src/main/java/com/coderising/ood/srp/FileUtil.java b/students/329866097/src/main/java/com/coderising/ood/srp/FileUtil.java new file mode 100644 index 0000000000..e8cdb97e9a --- /dev/null +++ b/students/329866097/src/main/java/com/coderising/ood/srp/FileUtil.java @@ -0,0 +1,36 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class FileUtil { + + public static Map readFile(String filePath) throws IOException { + File file = new File(filePath); + BufferedReader br = null; + Map productMap = new HashMap<>(); + try { + br = new BufferedReader(new FileReader(file)); + String content; + while((content = br.readLine()) != null) { + String[] data = content.split(" "); + String productId = data[0]; + String productDesc = data[1]; + productMap.put(productId, productDesc); + System.out.println("产品ID = " + productId + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + } + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + + return productMap; + } +} diff --git a/students/329866097/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/329866097/src/main/java/com/coderising/ood/srp/MailUtil.java deleted file mode 100644 index 9f9e749af7..0000000000 --- a/students/329866097/src/main/java/com/coderising/ood/srp/MailUtil.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.coderising.ood.srp; - -public class MailUtil { - - public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, - boolean debug) { - //假装发了一封邮件 - StringBuilder buffer = new StringBuilder(); - buffer.append("From:").append(fromAddress).append("\n"); - buffer.append("To:").append(toAddress).append("\n"); - buffer.append("Subject:").append(subject).append("\n"); - buffer.append("Content:").append(message).append("\n"); - System.out.println(buffer.toString()); - - } - - -} diff --git a/students/329866097/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/329866097/src/main/java/com/coderising/ood/srp/PromotionMail.java index d32df49c79..1273603cb8 100644 --- a/students/329866097/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/329866097/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -4,195 +4,41 @@ import java.io.File; import java.io.FileReader; import java.io.IOException; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; +import java.util.*; +import java.util.stream.Collectors; public class PromotionMail { - - protected String sendMailQuery = null; - - - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; - protected String toAddress = null; - protected String subject = null; - protected String message = null; - - protected String productID = null; - protected String productDesc = null; - - private static Configuration config; - - - - private static final String NAME_KEY = "NAME"; - private static final String EMAIL_KEY = "EMAIL"; - - public static void main(String[] args) throws Exception { - - File f = new File("src/main/resources/product_promotion.txt"); - boolean emailDebug = false; - - PromotionMail pe = new PromotionMail(f, emailDebug); - - } - - - public PromotionMail(File file, boolean mailDebug) throws Exception { - - //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - readFile(file); - - - config = new Configuration(); - - setSMTPHost(); - setAltSMTPHost(); - - - setFromAddress(); - - - setLoadQuery(); - - sendEMails(mailDebug, loadMailingList()); - - - } - - - - - protected void setProductID(String productID) - { - this.productID = productID; - - } - - protected String getproductID() - { - return productID; - } - - protected void setLoadQuery() throws Exception { - - sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + productID +"' " - + "and send_mail=1 "; - - - System.out.println("loadQuery set"); - } - - - protected void setSMTPHost() - { - smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); - } - - - protected void setAltSMTPHost() - { - altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); - - } - - - protected void setFromAddress() - { - fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); - } - - protected void setMessage(HashMap userInfo) throws IOException - { - - String name = (String) userInfo.get(NAME_KEY); - - subject = "您关注的产品降价了"; - message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; - - - - } - - - protected void readFile(File file) throws IOException // @02C - { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - setProductID(data[0]); - setProductDesc(data[1]); - - System.out.println("产品ID = " + productID + "\n"); - System.out.println("产品描述 = " + productDesc + "\n"); - - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); - } - } - - private void setProductDesc(String desc) { - this.productDesc = desc; - } - - - protected void configureEMail(HashMap userInfo) throws IOException - { - toAddress = (String) userInfo.get(EMAIL_KEY); - if (toAddress.length() > 0) - setMessage(userInfo); - } - - protected List loadMailingList() throws Exception { - return DBUtil.query(this.sendMailQuery); - } - - - protected void sendEMails(boolean debug, List mailingList) throws IOException - { - - System.out.println("开始发送邮件"); - - - if (mailingList != null) { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) { - configureEMail((HashMap) iter.next()); - try - { - if (toAddress.length() > 0) - MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } - catch (Exception e) - { - + Email email = new Email(); + + Map productMap = FileUtil.readFile( "src/main/resources/product_promotion.txt"); + for (Map.Entry entry : productMap.entrySet()) { + String productId = entry.getKey(); + String productDesc = entry.getValue(); + List userList = DBUtil.query(setLoadQuery(productId)); + for(User user : userList) { + try { + email.send(user, productDesc, email.getSmtpHost()); + } catch (Exception e) { try { - MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) - { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + email.send(user, productDesc, email.getAltSmtpHost()); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); } } } - - } + } - else { - System.out.println("没有邮件发送"); - - } + private static String setLoadQuery(String productID) throws Exception { + + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + System.out.println("loadQuery set"); + return sendMailQuery; } } diff --git a/students/329866097/src/main/java/com/coderising/ood/srp/User.java b/students/329866097/src/main/java/com/coderising/ood/srp/User.java new file mode 100644 index 0000000000..c1df43bc45 --- /dev/null +++ b/students/329866097/src/main/java/com/coderising/ood/srp/User.java @@ -0,0 +1,31 @@ +package com.coderising.ood.srp; + +/** + * Created by tianxianhu on 2017/6/18. + */ +public class User { + + private String name; + private String email; + + public User(String name, String email) { + this.name = name; + this.email = email; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} From 1f4be1b7d8d426e68700b4e6d74e4b4dd673810d Mon Sep 17 00:00:00 2001 From: zoakerc Date: Sun, 18 Jun 2017 10:47:24 +0800 Subject: [PATCH 202/214] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E7=9B=AE?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/303252800/.gitignore | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 students/303252800/.gitignore diff --git a/students/303252800/.gitignore b/students/303252800/.gitignore new file mode 100644 index 0000000000..41e8fd6dd8 --- /dev/null +++ b/students/303252800/.gitignore @@ -0,0 +1,22 @@ +target/ +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.idea +*.iws +*.iml +*.ipr +*.zip +*.class +*.jar +*.war +rebel.xml +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file From 881e737b43b182e3412397e6797ac7a3c2e11624 Mon Sep 17 00:00:00 2001 From: zoakerc Date: Sun, 18 Jun 2017 10:48:25 +0800 Subject: [PATCH 203/214] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E4=BD=BF=E4=B9=8B=E7=AC=A6=E5=90=88=E5=8D=95?= =?UTF-8?q?=E4=B8=80=E8=81=8C=E8=B4=A3=E5=8E=9F=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/303252800/practice13-ood-srp/pom.xml | 29 +++ .../303252800/practice13-ood-srp/readme.md | 8 + .../practice13/EmailConfiguration.java | 47 +++++ .../practice13/MainApplication.java | 17 ++ .../practice13/PromotionNotifier.java | 54 +++++ .../practice13/PromotionProduct.java | 52 +++++ .../practice13/PromotionSubscriber.java | 35 +++ .../com/coderising/ood/srp/Configuration.java | 23 ++ .../coderising/ood/srp/ConfigurationKeys.java | 9 + .../com/coderising/ood/srp/DBUtil.java | 25 +++ .../com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 199 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 13 files changed, 520 insertions(+) create mode 100644 students/303252800/practice13-ood-srp/pom.xml create mode 100644 students/303252800/practice13-ood-srp/readme.md create mode 100644 students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/EmailConfiguration.java create mode 100644 students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/MainApplication.java create mode 100644 students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/PromotionNotifier.java create mode 100644 students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/PromotionProduct.java create mode 100644 students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/PromotionSubscriber.java create mode 100644 students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/Configuration.java create mode 100644 students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/DBUtil.java create mode 100644 students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/MailUtil.java create mode 100644 students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/product_promotion.txt diff --git a/students/303252800/practice13-ood-srp/pom.xml b/students/303252800/practice13-ood-srp/pom.xml new file mode 100644 index 0000000000..7c3a372045 --- /dev/null +++ b/students/303252800/practice13-ood-srp/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + com.coding2017 + practice13-ood-srp + 1.0-SNAPSHOT + + + 1.6 + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${java.version} + ${java.version} + ${java.encoding} + + + + + \ No newline at end of file diff --git a/students/303252800/practice13-ood-srp/readme.md b/students/303252800/practice13-ood-srp/readme.md new file mode 100644 index 0000000000..e5f4455a16 --- /dev/null +++ b/students/303252800/practice13-ood-srp/readme.md @@ -0,0 +1,8 @@ +# practice13-ood-srp + +  重构一个项目使之符合 SRP (单一职责原则) + +- `EmailConfiguration` 邮件配置职责类 +- `PromotionProduct` 促销产品职责类 +- `PromotionSubscriber` 促销订阅职责类 +- `PromotionNotifier` 促销通知职责类 diff --git a/students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/EmailConfiguration.java b/students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/EmailConfiguration.java new file mode 100644 index 0000000000..3dde0cb7bc --- /dev/null +++ b/students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/EmailConfiguration.java @@ -0,0 +1,47 @@ +package com.coding2017.practice13; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +// 邮件配置职责类 +public final class EmailConfiguration { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + + private static final Map configurations = new HashMap(); + + static { + configurations.put(SMTP_SERVER, "smtp.163.com"); + configurations.put(ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(EMAIL_ADMIN, "admin@company.com"); + } + + // 外部不能创建实例 + private EmailConfiguration() { + } + + // 外部不能更改配置 + public static Map getInstance() { + return Collections.unmodifiableMap(configurations); + } + + + public static String getProperty(String key) { + return configurations.get(key); + } + + public static String getFromAddress() { + return configurations.get(EmailConfiguration.EMAIL_ADMIN); + } + + public static String getSmtpServer() { + return configurations.get(EmailConfiguration.SMTP_SERVER); + } + + public static String getAltSmtpServer() { + return configurations.get(EmailConfiguration.ALT_SMTP_SERVER); + } +} diff --git a/students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/MainApplication.java b/students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/MainApplication.java new file mode 100644 index 0000000000..b04ed0f0a1 --- /dev/null +++ b/students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/MainApplication.java @@ -0,0 +1,17 @@ +package com.coding2017.practice13; + +import java.util.List; + +public class MainApplication { + + public static void main(String[] args) throws Exception { + List products = PromotionProduct.readFromFile("com/coderising/ood/srp/product_promotion.txt"); + + for (PromotionProduct product : products) { + List subscribers = PromotionSubscriber.querySubscribers(product.getProductId()); + for (PromotionSubscriber subscriber : subscribers) { + PromotionNotifier.sendEmail(product, subscriber); + } + } + } +} diff --git a/students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/PromotionNotifier.java b/students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/PromotionNotifier.java new file mode 100644 index 0000000000..30c8a44f00 --- /dev/null +++ b/students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/PromotionNotifier.java @@ -0,0 +1,54 @@ +package com.coding2017.practice13; + +import java.text.MessageFormat; + +// 促销通知职责类 +public class PromotionNotifier { + + private static String subject = "您关注的产品降价了"; + private static String message = "尊敬的 {1} , 您关注的产品 {2} 降价了,欢迎购买!"; + + /** + * 发送邮件通知 + * + * @param product 促销产品 + * @param subscriber 订阅人 + */ + public static void sendEmail(PromotionProduct product, PromotionSubscriber subscriber) { + System.out.println("开始发送邮件"); + String content = MessageFormat.format(message, subscriber.getSubscriber(), product.getProductDesc()); + if (subscriber.getToAddress().length() > 0) { + try { + sendEmail(EmailConfiguration.getFromAddress(), subscriber.getToAddress(), subject, content, EmailConfiguration.getSmtpServer()); + } catch (Exception e1) { + try { + sendEmail(EmailConfiguration.getFromAddress(), subscriber.getToAddress(), subject, content, EmailConfiguration.getAltSmtpServer()); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } else { + System.out.println("没有邮件发送"); + } + } + + /** + * 执行发送邮件 + * + * @param fromAddress 发件人地址 + * @param toAddress 收件人地址 + * @param subject 邮件标题 + * @param content 邮件内容 + * @param smtpHost smtp服务器地址 + */ + private static void sendEmail(String fromAddress, String toAddress, String subject, String content, String smtpHost) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(content).append("\n"); + System.out.println(buffer.toString()); + } + +} diff --git a/students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/PromotionProduct.java b/students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/PromotionProduct.java new file mode 100644 index 0000000000..59200d05bf --- /dev/null +++ b/students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/PromotionProduct.java @@ -0,0 +1,52 @@ +package com.coding2017.practice13; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +// 促销产品职责类 +public class PromotionProduct { + + /** 产品ID */ + private String productId; + /** 产品描述 */ + private String productDesc; + + public String getProductId() { + return productId; + } + + public String getProductDesc() { + return productDesc; + } + + public static List readFromFile(String filepath) throws IOException { + List products = new ArrayList(); + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(new File(filepath))); + String line = null; + while ((line = br.readLine()) != null) { + String[] data = line.split(" "); + PromotionProduct product = new PromotionProduct(); + product.productId = data[0]; + product.productDesc = data[1]; + products.add(product); + System.out.println("产品ID = " + product.getProductId() + "\n"); + System.out.println("产品描述 = " + product.getProductDesc() + "\n"); + } + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + if (br != null) { + br.close(); + } + } + return products; + } + + +} diff --git a/students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/PromotionSubscriber.java b/students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/PromotionSubscriber.java new file mode 100644 index 0000000000..e58760e6ca --- /dev/null +++ b/students/303252800/practice13-ood-srp/src/main/java/com/coding2017/practice13/PromotionSubscriber.java @@ -0,0 +1,35 @@ +package com.coding2017.practice13; + +import java.util.ArrayList; +import java.util.List; + +// 促销订阅人职责类 +public class PromotionSubscriber { + + /** 订阅人邮件地址 */ + private String toAddress; + /** 订阅人姓名 */ + private String subscriber; + + public String getToAddress() { + return toAddress; + } + + public String getSubscriber() { + return subscriber; + } + + /** + * 查询促销产品订阅人列表 + * + * @param productId 产品ID + * @return 订阅人列表 + */ + public static List querySubscribers(String productId) { + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productId + "' " + + "and send_mail=1 "; + System.out.println("loadQuery set"); + return new ArrayList(); + } +} diff --git a/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/Configuration.java b/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/ConfigurationKeys.java b/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.srp; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/DBUtil.java b/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/MailUtil.java b/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,18 @@ +package com.coderising.ood.srp; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/PromotionMail.java b/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..781587a846 --- /dev/null +++ b/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,199 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + + public PromotionMail(File file, boolean mailDebug) throws Exception { + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(file); + + + config = new Configuration(); + + setSMTPHost(); + setAltSMTPHost(); + + + setFromAddress(); + + + setLoadQuery(); + + sendEMails(mailDebug, loadMailingList()); + + + } + + + + + protected void setProductID(String productID) + { + this.productID = productID; + + } + + protected String getproductID() + { + return productID; + } + + protected void setLoadQuery() throws Exception { + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } + + + protected void setSMTPHost() + { + smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + protected void setAltSMTPHost() + { + altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + + + protected void setFromAddress() + { + fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + protected void setMessage(HashMap userInfo) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + subject = "您关注的产品降价了"; + message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; + + + + } + + + protected void readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + setProductID(data[0]); + setProductDesc(data[1]); + + System.out.println("产品ID = " + productID + "\n"); + System.out.println("产品描述 = " + productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + private void setProductDesc(String desc) { + this.productDesc = desc; + } + + + protected void configureEMail(HashMap userInfo) throws IOException + { + toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo); + } + + protected List loadMailingList() throws Exception { + return DBUtil.query(this.sendMailQuery); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/product_promotion.txt b/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/303252800/practice13-ood-srp/src/main/resources/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From 3a0199d01cd6fe18969f3ca1c91320351d14b406 Mon Sep 17 00:00:00 2001 From: doudou Date: Mon, 19 Jun 2017 09:53:42 +0800 Subject: [PATCH 204/214] homework --- students/759412759/{ => ood-assignment}/pom.xml | 0 .../src/main/java/com/coderising/ood/srp/Configuration.java | 0 .../src/main/java/com/coderising/ood/srp/ConfigurationKeys.java | 0 .../src/main/java/com/coderising/ood/srp/DBUtil.java | 0 .../src/main/java/com/coderising/ood/srp/Mail.java | 0 .../src/main/java/com/coderising/ood/srp/MailUtil.java | 0 .../src/main/java/com/coderising/ood/srp/Product.java | 0 .../src/main/java/com/coderising/ood/srp/ProductService.java} | 2 +- .../src/main/java/com/coderising/ood/srp/PromotionMail.java | 2 +- .../src/main/java/com/coderising/ood/srp/UserService.java | 0 .../src/main/java/com/coderising/ood/srp/product_promotion.txt | 0 11 files changed, 2 insertions(+), 2 deletions(-) rename students/759412759/{ => ood-assignment}/pom.xml (100%) rename students/759412759/{ => ood-assignment}/src/main/java/com/coderising/ood/srp/Configuration.java (100%) rename students/759412759/{ => ood-assignment}/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java (100%) rename students/759412759/{ => ood-assignment}/src/main/java/com/coderising/ood/srp/DBUtil.java (100%) rename students/759412759/{ => ood-assignment}/src/main/java/com/coderising/ood/srp/Mail.java (100%) rename students/759412759/{ => ood-assignment}/src/main/java/com/coderising/ood/srp/MailUtil.java (100%) rename students/759412759/{ => ood-assignment}/src/main/java/com/coderising/ood/srp/Product.java (100%) rename students/759412759/{src/main/java/com/coderising/ood/srp/FileUtil.java => ood-assignment/src/main/java/com/coderising/ood/srp/ProductService.java} (96%) rename students/759412759/{ => ood-assignment}/src/main/java/com/coderising/ood/srp/PromotionMail.java (97%) rename students/759412759/{ => ood-assignment}/src/main/java/com/coderising/ood/srp/UserService.java (100%) rename students/759412759/{ => ood-assignment}/src/main/java/com/coderising/ood/srp/product_promotion.txt (100%) diff --git a/students/759412759/pom.xml b/students/759412759/ood-assignment/pom.xml similarity index 100% rename from students/759412759/pom.xml rename to students/759412759/ood-assignment/pom.xml diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/Configuration.java b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java similarity index 100% rename from students/759412759/src/main/java/com/coderising/ood/srp/Configuration.java rename to students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java similarity index 100% rename from students/759412759/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java rename to students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java similarity index 100% rename from students/759412759/src/main/java/com/coderising/ood/srp/DBUtil.java rename to students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/Mail.java b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java similarity index 100% rename from students/759412759/src/main/java/com/coderising/ood/srp/Mail.java rename to students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java similarity index 100% rename from students/759412759/src/main/java/com/coderising/ood/srp/MailUtil.java rename to students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/Product.java b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java similarity index 100% rename from students/759412759/src/main/java/com/coderising/ood/srp/Product.java rename to students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/FileUtil.java b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/ProductService.java similarity index 96% rename from students/759412759/src/main/java/com/coderising/ood/srp/FileUtil.java rename to students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/ProductService.java index 6a7eeba44c..18674e0101 100644 --- a/students/759412759/src/main/java/com/coderising/ood/srp/FileUtil.java +++ b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/ProductService.java @@ -5,7 +5,7 @@ /** * Created by Tudou on 2017/6/16. */ -public class FileUtil { +public class ProductService { public static Product loadProductFromFile(String filePath) throws IOException { Product product = new Product(); diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java similarity index 97% rename from students/759412759/src/main/java/com/coderising/ood/srp/PromotionMail.java rename to students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index 5dfa3853c8..015b68ff10 100644 --- a/students/759412759/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -15,7 +15,7 @@ public static void main(String[] args) throws Exception { PromotionMail pe = new PromotionMail(); String path = "F:\\IDEA_PRO_01\\coderrising\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"; - Product product = FileUtil.loadProductFromFile(path); + Product product = ProductService.loadProductFromFile(path); List> list = userService.loadMailingList(product.getProductID()); pe.sendEMails(list,product,Boolean.FALSE); diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/UserService.java b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/UserService.java similarity index 100% rename from students/759412759/src/main/java/com/coderising/ood/srp/UserService.java rename to students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/UserService.java diff --git a/students/759412759/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt similarity index 100% rename from students/759412759/src/main/java/com/coderising/ood/srp/product_promotion.txt rename to students/759412759/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt From a1cb65dd1367f8cb6e3e38da1156ad10473efa2d Mon Sep 17 00:00:00 2001 From: yuyingzhi Date: Mon, 19 Jun 2017 10:09:41 +0800 Subject: [PATCH 205/214] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../81681981/first_OOP_homework/readme.txt | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/students/81681981/first_OOP_homework/readme.txt b/students/81681981/first_OOP_homework/readme.txt index 0ddf999fd4..0330532c81 100644 --- a/students/81681981/first_OOP_homework/readme.txt +++ b/students/81681981/first_OOP_homework/readme.txt @@ -1,11 +1,11 @@ -ҵ˵ +作业说明 -༰ -1.conifgurationKeys.java ʼͷϢ -2.User.java ӦûϢû䣩 -3.޸DBUtil.java ݿ࣬Ѽֵ ޸ΪUser -4.Email.java άʼ͵ǰϢʼ⣬ʼݣsmtphost,altSmtpHost,fromAddress,subject,message -5.Product.java άƷϢû -6.Message.javaάҪ͵ -7.޸PromotionMail.java ܣȡƷϢ֯Ҫ͵ݷװΪMessage󣬻ȡòƷӦĶûMailUtil.java ʼ -8.MailUtil.java \ No newline at end of file +类及功能 +1.conifgurationKeys.java 邮件发送服务器配置信息 +2.新增User.java 对应订阅用户信息(用户名,邮箱) +3.修改DBUtil.java 数据库操作类,把键值 修改为User对象 +4.新增Email.java 维护邮件发送的前置信息和邮件标题,邮件内容,smtphost,altSmtpHost,fromAddress,subject,message +5.新增Product.java 维护产品信息及订阅用户 +6.新增Message.java 维护要发送的内容 +7.修改PromotionMail.java 功能:读取产品信息,组织要发送的内容封装为Message对象,获取该产品对应的订阅用户,调用MailUtil.java 发送邮件 +8.MailUtil.java 负责发送 \ No newline at end of file From 6679cda086e49e2add617c334aed407eb079a89c Mon Sep 17 00:00:00 2001 From: onlyliuxin <14703250@qq.com> Date: Mon, 19 Jun 2017 11:18:30 +0800 Subject: [PATCH 206/214] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coding2017 | 1 - 1 file changed, 1 deletion(-) delete mode 160000 coding2017 diff --git a/coding2017 b/coding2017 deleted file mode 160000 index bdbfcff8c9..0000000000 --- a/coding2017 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit bdbfcff8c9b106c07b6cbd6702fe97accc1dc90b From 4beaab48aefd1cfe68b994aa33b1fcd189e1335d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=98=B3=E9=98=B3?= <1425809544@qq.com> Date: Mon, 19 Jun 2017 11:28:27 +0800 Subject: [PATCH 207/214] =?UTF-8?q?=E9=87=8D=E6=9E=84=E9=82=AE=E4=BB=B6?= =?UTF-8?q?=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coderising/ood/config/Configuration.java | 23 +++++++ .../ood/config/ConfigurationKeys.java | 9 +++ .../coderising/ood/file/product_promotion.txt | 4 ++ .../java/com/coderising/ood/pojo/Email.java | 49 +++++++++++++++ .../ood/pojo/EmailServiceConfig.java | 45 +++++++++++++ .../java/com/coderising/ood/pojo/Product.java | 37 +++++++++++ .../java/com/coderising/ood/pojo/User.java | 29 +++++++++ .../coderising/ood/service/EmailService.java | 55 ++++++++++++++++ .../ood/service/ProductService.java | 52 +++++++++++++++ .../coderising/ood/service/PromotionMail.java | 63 +++++++++++++++++++ .../coderising/ood/service/UserService.java | 44 +++++++++++++ 11 files changed, 410 insertions(+) create mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/config/Configuration.java create mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/config/ConfigurationKeys.java create mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/file/product_promotion.txt create mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/Email.java create mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/EmailServiceConfig.java create mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/Product.java create mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/User.java create mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/EmailService.java create mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/ProductService.java create mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/PromotionMail.java create mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/UserService.java diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/config/Configuration.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/config/Configuration.java new file mode 100644 index 0000000000..a3f51236bc --- /dev/null +++ b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/config/Configuration.java @@ -0,0 +1,23 @@ +package com.coderising.ood.config; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/config/ConfigurationKeys.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/config/ConfigurationKeys.java new file mode 100644 index 0000000000..e7c449f10c --- /dev/null +++ b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/config/ConfigurationKeys.java @@ -0,0 +1,9 @@ +package com.coderising.ood.config; + +public class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/file/product_promotion.txt b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/file/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/file/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/Email.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/Email.java new file mode 100644 index 0000000000..ed92dfec0b --- /dev/null +++ b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/Email.java @@ -0,0 +1,49 @@ +package com.coderising.ood.pojo; + +/** + * @author xyy + * @create 2017-06-19 9:44 + **/ +public class Email { + + + private String toAddress; + private String subject; + private String message; + + public Email() { + } + + public Email(String toAddress, String subject, String message) { + this.toAddress = toAddress; + this.subject = subject; + this.message = message; + } + + public String getToAddress() { + return toAddress; + } + + public Email setToAddress(String toAddress) { + this.toAddress = toAddress; + return this; + } + + public String getSubject() { + return subject; + } + + public Email setSubject(String subject) { + this.subject = subject; + return this; + } + + public String getMessage() { + return message; + } + + public Email setMessage(String message) { + this.message = message; + return this; + } +} diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/EmailServiceConfig.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/EmailServiceConfig.java new file mode 100644 index 0000000000..091583b3ed --- /dev/null +++ b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/EmailServiceConfig.java @@ -0,0 +1,45 @@ +package com.coderising.ood.pojo; + +/** + * @author xyy + * @create 2017-06-19 10:00 + **/ +public class EmailServiceConfig { + + private String smtpHost; + private String altSmtpHost; + private String fromAddress; + + public EmailServiceConfig(String smtpHost, String altSmtpHost, String fromAddress) { + this.smtpHost = smtpHost; + this.altSmtpHost = altSmtpHost; + this.fromAddress = fromAddress; + } + + public String getSmtpHost() { + return smtpHost; + } + + public EmailServiceConfig setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + return this; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public EmailServiceConfig setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + return this; + } + + public String getFromAddress() { + return fromAddress; + } + + public EmailServiceConfig setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + return this; + } +} diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/Product.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/Product.java new file mode 100644 index 0000000000..f869bf1f12 --- /dev/null +++ b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/Product.java @@ -0,0 +1,37 @@ +package com.coderising.ood.pojo; + +/** + * 产品类 + * + * @author xyy + * @create 2017-06-19 9:30 + **/ +public class Product { + + + private String productID; + private String productDesc; + + + + + + + public String getProductID() { + return productID; + } + + public Product setProductID(String productID) { + this.productID = productID; + return this; + } + + public String getProductDesc() { + return productDesc; + } + + public Product setProductDesc(String productDesc) { + this.productDesc = productDesc; + return this; + } +} diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/User.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/User.java new file mode 100644 index 0000000000..69975f6cbd --- /dev/null +++ b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/User.java @@ -0,0 +1,29 @@ +package com.coderising.ood.pojo; + +/** + * @author xyy + * @create 2017-06-19 9:48 + **/ +public class User { + + private String name; + private String email; + + public String getName() { + return name; + } + + public User setName(String name) { + this.name = name; + return this; + } + + public String getEmail() { + return email; + } + + public User setEmail(String email) { + this.email = email; + return this; + } +} diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/EmailService.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/EmailService.java new file mode 100644 index 0000000000..2fece21ec2 --- /dev/null +++ b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/EmailService.java @@ -0,0 +1,55 @@ +package com.coderising.ood.service; + +import com.coderising.ood.pojo.Email; +import com.coderising.ood.pojo.EmailServiceConfig; +import com.coderising.ood.pojo.Product; +import com.coderising.ood.pojo.User; + +import java.io.IOException; + +/** + * @author xyy + * @create 2017-06-19 9:44 + **/ +public class EmailService { + + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + + public static Email configureEMail(User user, Product product) throws IOException { + String toAddress = user.getEmail(); + String name = ""; + if (toAddress.length() > 0) { + name = user.getName(); + } + String subject = "您关注的产品降价了"; + String message = "尊敬的 " + name + ", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!"; + Email email = new Email(toAddress, subject, message); + return email; + } + + public static void sendEmail(EmailServiceConfig emailServiceConfig, Email email, boolean debug) { + try { + if (email.getToAddress().length() > 0) { + sendEmail(email.getToAddress(), emailServiceConfig.getFromAddress(), email.getSubject(), email.getMessage(), emailServiceConfig.getSmtpHost(), debug); + } + } catch (Exception e) { + try { + sendEmail(email.getToAddress(), emailServiceConfig.getFromAddress(), email.getSubject(), email.getMessage(), emailServiceConfig.getAltSmtpHost(), debug); + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } +} diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/ProductService.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/ProductService.java new file mode 100644 index 0000000000..ccacef7bce --- /dev/null +++ b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/ProductService.java @@ -0,0 +1,52 @@ +package com.coderising.ood.service; + +import com.coderising.ood.pojo.Product; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * @author xyy + * @create 2017-06-19 9:34 + **/ +public class ProductService { + + + public static List getAllProductFromFile(File file) throws IOException { + List productList = new ArrayList(); + BufferedReader br = null; + + try { + + br = new BufferedReader(new FileReader(file)); + String temp = null; + while ((temp = br.readLine()) != null) { + String[] data = temp.split(" "); + Product product = new Product(); + product.setProductID(data[0]); + product.setProductDesc(data[1]); + System.out.println("产品ID = " + product.getProductID() + "\n"); + System.out.println("产品描述 = " + product.getProductDesc() + "\n"); + productList.add(product); + } + + return productList; + + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + br.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + return null; + } + +} diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/PromotionMail.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/PromotionMail.java new file mode 100644 index 0000000000..f89aff500a --- /dev/null +++ b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/PromotionMail.java @@ -0,0 +1,63 @@ +package com.coderising.ood.service; + +import com.coderising.ood.config.Configuration; +import com.coderising.ood.config.ConfigurationKeys; +import com.coderising.ood.pojo.Email; +import com.coderising.ood.pojo.EmailServiceConfig; +import com.coderising.ood.pojo.Product; +import com.coderising.ood.pojo.User; + +import java.io.File; +import java.util.List; + +public class PromotionMail { + + public static void main(String[] args) throws Exception { + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + File file = new File("D:\\product_promotion.txt"); + //1.获得产品信息 + ProductService productService = new ProductService(); + List productList = productService.getAllProductFromFile(file); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(productList, emailDebug); + } + + + + public PromotionMail(List productList, boolean mailDebug) throws Exception { + //2.邮件服务器配置 + Configuration config = new Configuration(); + EmailServiceConfig emailServiceConfig = new EmailServiceConfig(config.getProperty(ConfigurationKeys.SMTP_SERVER), config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER), config.getProperty(ConfigurationKeys.EMAIL_ADMIN)); + //3.发送邮件 + sendEMails(mailDebug, productList, emailServiceConfig); + + } + + + public void sendEMails(boolean debug, List productList, EmailServiceConfig emailServiceConfig) throws Exception { + System.out.println("开始发送邮件"); + if (productList != null) { + for (Product product : productList) { + List userList = UserService.getSendEmailUser(product); + if (null != userList && userList.size() > 0) { + for (User user : userList) { + Email email = EmailService.configureEMail((user), product); + if (email.getToAddress().length() > 0) { + EmailService.sendEmail(emailServiceConfig,email,debug); + } + } + } + } + + } else { + System.out.println("没有邮件发送"); + + } + + } + + +} + + diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/UserService.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/UserService.java new file mode 100644 index 0000000000..a7c6d8fe38 --- /dev/null +++ b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/UserService.java @@ -0,0 +1,44 @@ +package com.coderising.ood.service; + +import com.coderising.ood.pojo.Product; +import com.coderising.ood.pojo.User; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author xyy + * @create 2017-06-19 9:48 + **/ +public class UserService { + + + public static List getSendEmailUser(Product product) throws Exception { + + + setLoadQuery(product); + + List userList = new ArrayList(); + + for (int i = 0; i < 3; i++) { + User user = new User(); + user.setName("user" + i); + user.setEmail(user.getName() + "@qq.com"); + userList.add(user); + } + return userList; + } + + + //通过产品id获取关注了产品的用户 + public static void setLoadQuery(Product product) throws Exception { + + String sql = "Select name from subscriptions " + + "where product_id= '" + product.getProductID() + "' " + + "and send_mail=1 "; + + System.out.println("loadQuery set"); + } + + +} From ae97f0ebc6037fa194b03177fed0cf480e58b02c Mon Sep 17 00:00:00 2001 From: sheng <1158154002@qq.com> Date: Mon, 19 Jun 2017 13:02:45 +0800 Subject: [PATCH 208/214] 02 ocp --- .../java/com/coderising/ood/ocp/DateUtil.java | 11 ++++++++++ .../java/com/coderising/ood/ocp/EmailLog.java | 10 +++++++++ .../com/coderising/ood/ocp/LogMethod.java | 5 +++++ .../java/com/coderising/ood/ocp/LogType.java | 5 +++++ .../java/com/coderising/ood/ocp/Logger.java | 21 +++++++++++++++++++ .../java/com/coderising/ood/ocp/PrintLog.java | 10 +++++++++ .../java/com/coderising/ood/ocp/RawLog.java | 10 +++++++++ .../coderising/ood/ocp/RawLogWithDate.java | 12 +++++++++++ .../java/com/coderising/ood/ocp/SmsLog.java | 10 +++++++++ 9 files changed, 94 insertions(+) create mode 100644 students/1158154002/src/main/java/com/coderising/ood/ocp/DateUtil.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/ocp/EmailLog.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/ocp/LogMethod.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/ocp/LogType.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/ocp/Logger.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/ocp/PrintLog.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/ocp/RawLog.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/ocp/RawLogWithDate.java create mode 100644 students/1158154002/src/main/java/com/coderising/ood/ocp/SmsLog.java diff --git a/students/1158154002/src/main/java/com/coderising/ood/ocp/DateUtil.java b/students/1158154002/src/main/java/com/coderising/ood/ocp/DateUtil.java new file mode 100644 index 0000000000..9471308b5b --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/ocp/DateUtil.java @@ -0,0 +1,11 @@ +package com.coderising.ood.ocp; + +import java.util.Date; + +public class DateUtil { + + public static String getCurrentDateAsString() { + + return new Date().toString(); + } +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/ocp/EmailLog.java b/students/1158154002/src/main/java/com/coderising/ood/ocp/EmailLog.java new file mode 100644 index 0000000000..197f08d2db --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/ocp/EmailLog.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class EmailLog implements LogMethod{ + + @Override + public void send(String msg) { + System.out.println("Email send "+msg); + } + +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/ocp/LogMethod.java b/students/1158154002/src/main/java/com/coderising/ood/ocp/LogMethod.java new file mode 100644 index 0000000000..f813444cd8 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/ocp/LogMethod.java @@ -0,0 +1,5 @@ +package com.coderising.ood.ocp; + +public interface LogMethod { + void send(String msg); +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/ocp/LogType.java b/students/1158154002/src/main/java/com/coderising/ood/ocp/LogType.java new file mode 100644 index 0000000000..ed2b6fc7e6 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/ocp/LogType.java @@ -0,0 +1,5 @@ +package com.coderising.ood.ocp; + +public interface LogType { + String getMsg(String msg); +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/ocp/Logger.java b/students/1158154002/src/main/java/com/coderising/ood/ocp/Logger.java new file mode 100644 index 0000000000..09fae40095 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/ocp/Logger.java @@ -0,0 +1,21 @@ +package com.coderising.ood.ocp; + +public class Logger { + + LogType type; + LogMethod method; + + public Logger(LogType logType, LogMethod logMethod) { + this.type = logType; + this.method = logMethod; + } + + public void log(String msg) { + method.send(type.getMsg(msg)); + } + + public static void main(String[] args) { + Logger logger=new Logger(new RawLog(), new EmailLog()); + logger.log("hello world !"); + } +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/ocp/PrintLog.java b/students/1158154002/src/main/java/com/coderising/ood/ocp/PrintLog.java new file mode 100644 index 0000000000..6f1b379707 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/ocp/PrintLog.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class PrintLog implements LogMethod{ + + @Override + public void send(String msg) { + System.out.println("Print Log "+msg); + } + +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/ocp/RawLog.java b/students/1158154002/src/main/java/com/coderising/ood/ocp/RawLog.java new file mode 100644 index 0000000000..88f1811f2e --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/ocp/RawLog.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class RawLog implements LogType { + + @Override + public String getMsg(String msg) { + return msg; + } + +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/ocp/RawLogWithDate.java b/students/1158154002/src/main/java/com/coderising/ood/ocp/RawLogWithDate.java new file mode 100644 index 0000000000..c791fe142d --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/ocp/RawLogWithDate.java @@ -0,0 +1,12 @@ +package com.coderising.ood.ocp; + +public class RawLogWithDate implements LogType { + + @Override + public String getMsg(String msg) { + String txtDate = DateUtil.getCurrentDateAsString(); + msg = txtDate + ": " + msg; + return msg; + } + +} diff --git a/students/1158154002/src/main/java/com/coderising/ood/ocp/SmsLog.java b/students/1158154002/src/main/java/com/coderising/ood/ocp/SmsLog.java new file mode 100644 index 0000000000..8bca6372ba --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/ood/ocp/SmsLog.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class SmsLog implements LogMethod{ + + @Override + public void send(String msg) { + System.out.println("SMS send "+msg); + } + +} From c6853836ca933730234b79272014392b3e1de68b Mon Sep 17 00:00:00 2001 From: penglei Date: Mon, 19 Jun 2017 14:17:05 +0800 Subject: [PATCH 209/214] =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=90=8E=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/leipengzj/Configuration.java | 31 ++++ .../799298900/src/com/leipengzj/DBUtil.java | 25 ++++ .../799298900/src/com/leipengzj/MailInfo.java | 92 ++++++++++++ .../799298900/src/com/leipengzj/MailUtil.java | 18 +++ .../src/com/leipengzj/PromotionMail.java | 139 ++++++++++++++++++ .../src/com/leipengzj/product_promotion.txt | 4 + 6 files changed, 309 insertions(+) create mode 100644 students/799298900/src/com/leipengzj/Configuration.java create mode 100644 students/799298900/src/com/leipengzj/DBUtil.java create mode 100644 students/799298900/src/com/leipengzj/MailInfo.java create mode 100644 students/799298900/src/com/leipengzj/MailUtil.java create mode 100644 students/799298900/src/com/leipengzj/PromotionMail.java create mode 100644 students/799298900/src/com/leipengzj/product_promotion.txt diff --git a/students/799298900/src/com/leipengzj/Configuration.java b/students/799298900/src/com/leipengzj/Configuration.java new file mode 100644 index 0000000000..26665ad1a9 --- /dev/null +++ b/students/799298900/src/com/leipengzj/Configuration.java @@ -0,0 +1,31 @@ +package com.leipengzj; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + + static Map configurations = new HashMap<>(); + static{ + configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); + configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); + } + /** + * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } + +} + +class ConfigurationKeys { + + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + +} diff --git a/students/799298900/src/com/leipengzj/DBUtil.java b/students/799298900/src/com/leipengzj/DBUtil.java new file mode 100644 index 0000000000..ec8ae4aba9 --- /dev/null +++ b/students/799298900/src/com/leipengzj/DBUtil.java @@ -0,0 +1,25 @@ +package com.leipengzj; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * 应该从数据库读, 但是简化为直接生成。 + * @param sql + * @return + */ + public static List query(String sql){ + + List userList = new ArrayList(); + for (int i = 1; i <= 3; i++) { + HashMap userInfo = new HashMap(); + userInfo.put("NAME", "User" + i); + userInfo.put("EMAIL", "aa@bb.com"); + userList.add(userInfo); + } + + return userList; + } +} diff --git a/students/799298900/src/com/leipengzj/MailInfo.java b/students/799298900/src/com/leipengzj/MailInfo.java new file mode 100644 index 0000000000..ecaa0dac9b --- /dev/null +++ b/students/799298900/src/com/leipengzj/MailInfo.java @@ -0,0 +1,92 @@ +package com.leipengzj; + +/** + * Created by pl on 2017/6/19. + */ +public class MailInfo { + + protected String sendMailQuery = null; + + + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + protected String toAddress = null; + protected String subject = null; + protected String message = null; + + protected String productID = null; + protected String productDesc = null; + + public String getSendMailQuery() { + return sendMailQuery; + } + + public void setSendMailQuery(String sendMailQuery) { + this.sendMailQuery = sendMailQuery; + } + + public String getSmtpHost() { + return smtpHost; + } + + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + + public String getAltSmtpHost() { + return altSmtpHost; + } + + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } +} diff --git a/students/799298900/src/com/leipengzj/MailUtil.java b/students/799298900/src/com/leipengzj/MailUtil.java new file mode 100644 index 0000000000..42d4329f96 --- /dev/null +++ b/students/799298900/src/com/leipengzj/MailUtil.java @@ -0,0 +1,18 @@ +package com.leipengzj; + +public class MailUtil { + + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, + boolean debug) { + //假装发了一封邮件 + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(fromAddress).append("\n"); + buffer.append("To:").append(toAddress).append("\n"); + buffer.append("Subject:").append(subject).append("\n"); + buffer.append("Content:").append(message).append("\n"); + System.out.println(buffer.toString()); + + } + + +} diff --git a/students/799298900/src/com/leipengzj/PromotionMail.java b/students/799298900/src/com/leipengzj/PromotionMail.java new file mode 100644 index 0000000000..5d152ac3cb --- /dev/null +++ b/students/799298900/src/com/leipengzj/PromotionMail.java @@ -0,0 +1,139 @@ +package com.leipengzj; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionMail { + + +// protected String sendMailQuery = null; +// +// +// protected String smtpHost = null; +// protected String altSmtpHost = null; +// protected String fromAddress = null; +// protected String toAddress = null; +// protected String subject = null; +// protected String message = null; +// +// protected String productID = null; +// protected String productDesc = null; + + private static Configuration config; + + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + + public static void main(String[] args) throws Exception { + + File f = new File("C:\\coderising\\workspace_ds\\ood-example\\src\\product_promotion.txt"); + boolean emailDebug = false; + + PromotionMail pe = new PromotionMail(f, emailDebug); + + } + + //发送邮件 + public PromotionMail(File file, boolean mailDebug) throws Exception { + MailInfo mi = new MailInfo(); + + //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 + readFile(mi,file); + + + config = new Configuration(); + + mi.setSmtpHost(config.getProperty(ConfigurationKeys.SMTP_SERVER)); + mi.setAltSmtpHost(config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER)); + mi.setFromAddress(config.getProperty(ConfigurationKeys.EMAIL_ADMIN)); + + String sql = "Select name from subscriptions " + + "where product_id= '" + mi.getProductID() +"' " + + "and send_mail=1 "; + //查询出邮件列表 + List query = DBUtil.query(sql); + + sendEMails(mi,mailDebug, query); + + + } + + + //读取产品信息 + protected void readFile(MailInfo mi,File file) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + mi.setProductID(data[0]); + mi.setProductDesc(data[1]); + + System.out.println("产品ID = " + data[0] + "\n"); + System.out.println("产品描述 = " + data[1] + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + + //配置邮件并设置发送的邮件内容 + protected void configureEMail(HashMap userInfo,MailInfo mi) throws IOException + { + String toAddress = (String) userInfo.get(EMAIL_KEY); + String name = (String) userInfo.get(NAME_KEY); + if (toAddress.length() > 0){ + mi.setSubject("您关注的产品降价了"); + mi.setMessage("尊敬的 "+name+", 您关注的产品 " + mi.getProductDesc() + " 降价了,欢迎购买!"); + } + + } + + + protected void sendEMails(MailInfo mi,boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + configureEMail((HashMap) iter.next(),mi); + try + { + if (mi.getToAddress().length() > 0) + MailUtil.sendEmail(mi.getToAddress(), mi.getToAddress(), mi.getSubject(), mi.getMessage(), mi.getSmtpHost(), debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(mi.getToAddress(), mi.getToAddress(), mi.getSubject(), mi.getMessage(), mi.getAltSmtpHost(), debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + } + else { + System.out.println("没有邮件发送"); + } + + } +} diff --git a/students/799298900/src/com/leipengzj/product_promotion.txt b/students/799298900/src/com/leipengzj/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/799298900/src/com/leipengzj/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file From a2afd81213946e181da99d4f93dfa9fc21b51644 Mon Sep 17 00:00:00 2001 From: doudou Date: Mon, 19 Jun 2017 14:42:24 +0800 Subject: [PATCH 210/214] homework --- .gitignore | 2 -- .../com/coderising/ood/ocp/DateFormater.java | 14 +++++++++++++ .../java/com/coderising/ood/ocp/DateUtil.java | 10 ++++++++++ .../java/com/coderising/ood/ocp/Formater.java | 12 +++++++++++ .../java/com/coderising/ood/ocp/Logger.java | 20 +++++++++++++++++++ .../com/coderising/ood/ocp/MailPrintUtil.java | 10 ++++++++++ .../java/com/coderising/ood/ocp/Printer.java | 11 ++++++++++ .../com/coderising/ood/ocp/SMSPrintUtil.java | 9 +++++++++ 8 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/DateFormater.java create mode 100644 students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java create mode 100644 students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/Formater.java create mode 100644 students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/Logger.java create mode 100644 students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/MailPrintUtil.java create mode 100644 students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/Printer.java create mode 100644 students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/SMSPrintUtil.java diff --git a/.gitignore b/.gitignore index 4b65de1971..ccf385a085 100644 --- a/.gitignore +++ b/.gitignore @@ -280,8 +280,6 @@ target liuxin/.DS_Store liuxin/src/.DS_Store -students/* -!students/785396327 diff --git a/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/DateFormater.java b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/DateFormater.java new file mode 100644 index 0000000000..18b61a77b3 --- /dev/null +++ b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/DateFormater.java @@ -0,0 +1,14 @@ +package com.coderising.ood.ocp; + +/** + * 日期类型格式化模板 + * Created by Tudou on 2017/6/19. + */ +public class DateFormater extends Formater { + + @Override + public String formatMessage(String message) { + String txtDate = DateUtil.getCurrentDateAsString(); + return txtDate + " : " + message; + } +} diff --git a/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java new file mode 100644 index 0000000000..b6cf28c096 --- /dev/null +++ b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/DateUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class DateUtil { + + public static String getCurrentDateAsString() { + + return null; + } + +} diff --git a/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/Formater.java b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/Formater.java new file mode 100644 index 0000000000..7a94fea749 --- /dev/null +++ b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/Formater.java @@ -0,0 +1,12 @@ +package com.coderising.ood.ocp; + +/** + * 格式化打印参数基类 + * Created by Tudou on 2017/6/19. + */ +public class Formater { + + public String formatMessage(String message){ + return message; + } +} diff --git a/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/Logger.java b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/Logger.java new file mode 100644 index 0000000000..a9f62d6a66 --- /dev/null +++ b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/Logger.java @@ -0,0 +1,20 @@ +package com.coderising.ood.ocp; + +public class Logger { + + private Printer printer; + private Formater formater; + + + public Logger(Printer printer, Formater formater) { + this.printer = printer; + this.formater = formater; + } + + + public void log(String msg) { + String logMsg = formater.formatMessage(msg); + printer.print(logMsg); + } +} + diff --git a/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/MailPrintUtil.java b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/MailPrintUtil.java new file mode 100644 index 0000000000..d522e5e7d2 --- /dev/null +++ b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/MailPrintUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + + +public class MailPrintUtil extends Printer { + + @Override + public void print(String msg) { + + } +} diff --git a/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/Printer.java b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/Printer.java new file mode 100644 index 0000000000..caa28fbf0c --- /dev/null +++ b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/Printer.java @@ -0,0 +1,11 @@ +package com.coderising.ood.ocp; + +/** + * Created by Tudou on 2017/6/19. + */ +public class Printer { + + public void print(String msg){ + System.out.println(msg); + } +} diff --git a/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/SMSPrintUtil.java b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/SMSPrintUtil.java new file mode 100644 index 0000000000..204256f44b --- /dev/null +++ b/students/759412759/ood-assignment/src/main/java/com/coderising/ood/ocp/SMSPrintUtil.java @@ -0,0 +1,9 @@ +package com.coderising.ood.ocp; + +public class SMSPrintUtil extends Printer { + + @Override + public void print(String msg) { + + } +} From cf8de7eb7d6d595fe5cf1ad0a89353af914ca54d Mon Sep 17 00:00:00 2001 From: orajavac Date: Mon, 19 Jun 2017 15:09:41 +0800 Subject: [PATCH 211/214] 20170619_1509 --- .../orajavac/coding2017/ood/ocp/DateUtil.java | 8 + .../orajavac/coding2017/ood/ocp/Logger.java | 6 + .../coding2017/ood/ocp/LoggerManagement.java | 20 +++ .../orajavac/coding2017/ood/ocp/MailUtil.java | 8 + .../coding2017/ood/ocp/PrintUtil.java | 8 + .../orajavac/coding2017/ood/ocp/RawLog.java | 7 + .../coding2017/ood/ocp/RawLogWithDate.java | 8 + .../coding2017/ood/ocp/RawLogger.java | 5 + .../orajavac/coding2017/ood/ocp/SMSUtil.java | 8 + .../orajavac/coding2017/ood/srp/DBUtil.java | 10 ++ .../orajavac/coding2017/ood/srp/FileUtil.java | 31 ++++ .../orajavac/coding2017/ood/srp/Mail.java | 49 ++++++ .../orajavac/coding2017/ood/srp/MailUtil.java | 81 +++++++++- .../orajavac/coding2017/ood/srp/Product.java | 18 +++ .../coding2017/ood/srp/PromotionMail.java | 150 ++---------------- 15 files changed, 275 insertions(+), 142 deletions(-) create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/ocp/DateUtil.java create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/ocp/Logger.java create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/ocp/LoggerManagement.java create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/ocp/MailUtil.java create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/ocp/PrintUtil.java create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/ocp/RawLog.java create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/ocp/RawLogWithDate.java create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/ocp/RawLogger.java create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/ocp/SMSUtil.java create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/srp/FileUtil.java create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/srp/Mail.java create mode 100644 students/562768642/src/com/github/orajavac/coding2017/ood/srp/Product.java diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/DateUtil.java b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/DateUtil.java new file mode 100644 index 0000000000..d8bbcd124c --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/DateUtil.java @@ -0,0 +1,8 @@ +package com.github.orajavac.coding2017.ood.ocp; + +public class DateUtil { +public static String getCurrentDateAsString() { + + return null; + } +} diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/Logger.java b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/Logger.java new file mode 100644 index 0000000000..be360ccdd4 --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/Logger.java @@ -0,0 +1,6 @@ +package com.github.orajavac.coding2017.ood.ocp; + +public interface Logger { + + public void send(String msg); +} diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/LoggerManagement.java b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/LoggerManagement.java new file mode 100644 index 0000000000..c658bd33ea --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/LoggerManagement.java @@ -0,0 +1,20 @@ +package com.github.orajavac.coding2017.ood.ocp; + +import java.util.ArrayList; +import java.util.List; + +public class LoggerManagement { + + public void log(String msg){ + + List rawlog = new ArrayList(); + for (RawLogger r : rawlog){ + r.log(msg); + } + + List logger = new ArrayList(); + for (Logger l : logger){ + l.send(msg); + } + } +} diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/MailUtil.java b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/MailUtil.java new file mode 100644 index 0000000000..8cfd91064d --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/MailUtil.java @@ -0,0 +1,8 @@ +package com.github.orajavac.coding2017.ood.ocp; + +public class MailUtil implements Logger{ + public void send(String logMsg) { + // TODO Auto-generated method stub + + } +} diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/PrintUtil.java b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/PrintUtil.java new file mode 100644 index 0000000000..d83397c1d6 --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/PrintUtil.java @@ -0,0 +1,8 @@ +package com.github.orajavac.coding2017.ood.ocp; + +public class PrintUtil implements Logger{ + public void send(String logMsg) { + // TODO Auto-generated method stub + + } +} diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/RawLog.java b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/RawLog.java new file mode 100644 index 0000000000..ad6cbb7510 --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/RawLog.java @@ -0,0 +1,7 @@ +package com.github.orajavac.coding2017.ood.ocp; + +public class RawLog implements RawLogger{ + public void log(String msg){ + String logMsg = msg; + } +} diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/RawLogWithDate.java b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/RawLogWithDate.java new file mode 100644 index 0000000000..f9640769ee --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/RawLogWithDate.java @@ -0,0 +1,8 @@ +package com.github.orajavac.coding2017.ood.ocp; + +public class RawLogWithDate implements RawLogger{ + public void log(String msg){ + String txtDate = DateUtil.getCurrentDateAsString(); + String logMsg = txtDate + ": " + msg; + } +} diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/RawLogger.java b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/RawLogger.java new file mode 100644 index 0000000000..d4ad9eacd3 --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/RawLogger.java @@ -0,0 +1,5 @@ +package com.github.orajavac.coding2017.ood.ocp; + +public interface RawLogger { + public void log(String msg); +} diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/SMSUtil.java b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/SMSUtil.java new file mode 100644 index 0000000000..82eb15bed9 --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/ocp/SMSUtil.java @@ -0,0 +1,8 @@ +package com.github.orajavac.coding2017.ood.ocp; + +public class SMSUtil implements Logger{ + public void send(String logMsg) { + // TODO Auto-generated method stub + + } +} diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/srp/DBUtil.java b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/DBUtil.java index c4d5f8a65d..528ff9321d 100644 --- a/students/562768642/src/com/github/orajavac/coding2017/ood/srp/DBUtil.java +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/DBUtil.java @@ -22,4 +22,14 @@ public static List query(String sql){ return userList; } + + public static void setLoadQuery(String productID) throws Exception { + + String sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + } } diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/srp/FileUtil.java b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/FileUtil.java new file mode 100644 index 0000000000..14f6443bbf --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/FileUtil.java @@ -0,0 +1,31 @@ +package com.github.orajavac.coding2017.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +public class FileUtil { + public static Product readFile(File file) throws IOException // @02C + { + BufferedReader br = null; + Product p = new Product(); + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + p.setProductID(data[0]); + p.setProductDesc(data[1]); + + System.out.println("产品ID = " + p.getProductID() + "\n"); + System.out.println("产品描述 = " + p.getProductDesc() + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + return p; + } +} diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/srp/Mail.java b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/Mail.java new file mode 100644 index 0000000000..46f5a92db1 --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/Mail.java @@ -0,0 +1,49 @@ +package com.github.orajavac.coding2017.ood.srp; + +import java.io.IOException; +import java.util.HashMap; + +public class Mail { + private String smtpHost = null; + private String altSmtpHost = null; + private String fromAddress = null; + private String toAddress = null; + private String subject = null; + private String message = null; + public String getSmtpHost() { + return smtpHost; + } + public void setSmtpHost(String smtpHost) { + this.smtpHost = smtpHost; + } + public String getAltSmtpHost() { + return altSmtpHost; + } + public void setAltSmtpHost(String altSmtpHost) { + this.altSmtpHost = altSmtpHost; + } + public String getFromAddress() { + return fromAddress; + } + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + public String getToAddress() { + return toAddress; + } + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + public String getSubject() { + return subject; + } + public void setSubject(String subject) { + this.subject = subject; + } + public String getMessage() { + return message; + } + public void setMessage(String message) { + this.message = message; + } +} diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/srp/MailUtil.java b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/MailUtil.java index c9614ebfc5..aa16293062 100644 --- a/students/562768642/src/com/github/orajavac/coding2017/ood/srp/MailUtil.java +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/MailUtil.java @@ -1,7 +1,15 @@ package com.github.orajavac.coding2017.ood.srp; -public class MailUtil { +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +public class MailUtil { + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, boolean debug) { //假装发了一封邮件 @@ -13,6 +21,77 @@ public static void sendEmail(String toAddress, String fromAddress, String subjec System.out.println(buffer.toString()); } + + public static String setSMTPHost(Configuration config) + { + return config.getProperty(ConfigurationKeys.SMTP_SERVER); + } + + + public static String setAltSMTPHost(Configuration config) + { + return config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + + } + public static String setFromAddress(Configuration config) + { + return config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } + + public static void setMessage(HashMap userInfo,Product p,Mail mail) throws IOException + { + + String name = (String) userInfo.get(NAME_KEY); + + mail.setSubject("您关注的产品降价了"); + + String message = "尊敬的 "+name+", 您关注的产品 " + p.getProductDesc() + " 降价了,欢迎购买!" ; + mail.setMessage(message); + + + } + + public static void sendEMails(boolean debug, List mailingList,Product p,Mail mail) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + HashMap userInfo = (HashMap) iter.next(); + String toAddress = (String) userInfo.get(EMAIL_KEY); + if (toAddress.length() > 0) + setMessage(userInfo,p,mail); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(toAddress, mail.getFromAddress(), mail.getSubject(), mail.getMessage(), mail.getSmtpHost(), debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, mail.getFromAddress(), mail.getSubject(), mail.getMessage(), mail.getAltSmtpHost(), debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + else { + System.out.println("没有邮件发送"); + + } + + } + } diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/srp/Product.java b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/Product.java new file mode 100644 index 0000000000..02345d790c --- /dev/null +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/Product.java @@ -0,0 +1,18 @@ +package com.github.orajavac.coding2017.ood.srp; + +public class Product { + private String productID = null; + private String productDesc = null; + public String getProductID() { + return productID; + } + public void setProductID(String productID) { + this.productID = productID; + } + public String getProductDesc() { + return productDesc; + } + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } +} diff --git a/students/562768642/src/com/github/orajavac/coding2017/ood/srp/PromotionMail.java b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/PromotionMail.java index db982d9f34..53e7e27471 100644 --- a/students/562768642/src/com/github/orajavac/coding2017/ood/srp/PromotionMail.java +++ b/students/562768642/src/com/github/orajavac/coding2017/ood/srp/PromotionMail.java @@ -15,22 +15,15 @@ public class PromotionMail { protected String sendMailQuery = null; - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; - protected String toAddress = null; - protected String subject = null; - protected String message = null; + protected Mail mail = new Mail(); - protected String productID = null; - protected String productDesc = null; + private static Configuration config; - private static final String NAME_KEY = "NAME"; - private static final String EMAIL_KEY = "EMAIL"; + public static void main(String[] args) throws Exception { @@ -46,154 +39,29 @@ public static void main(String[] args) throws Exception { public PromotionMail(File file, boolean mailDebug) throws Exception { //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - readFile(file); + Product p = FileUtil.readFile(file); config = new Configuration(); - setSMTPHost(); - setAltSMTPHost(); + mail.setSmtpHost(MailUtil.setSMTPHost(config)); + mail.setAltSmtpHost(MailUtil.setAltSMTPHost(config)); - setFromAddress(); + mail.setFromAddress(MailUtil.setFromAddress(config)); - setLoadQuery(); + DBUtil.setLoadQuery(p.getProductID()); - sendEMails(mailDebug, loadMailingList()); + MailUtil.sendEMails(mailDebug, loadMailingList(),p,mail); } - - - - protected void setProductID(String productID) - { - this.productID = productID; - - } - - protected String getproductID() - { - return productID; - } - - protected void setLoadQuery() throws Exception { - - sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + productID +"' " - + "and send_mail=1 "; - - - System.out.println("loadQuery set"); - } - - - protected void setSMTPHost() - { - smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); - } - - protected void setAltSMTPHost() - { - altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); - } - - - protected void setFromAddress() - { - fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); - } - - protected void setMessage(HashMap userInfo) throws IOException - { - - String name = (String) userInfo.get(NAME_KEY); - - subject = "您关注的产品降价了"; - message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; - - - - } - - - protected void readFile(File file) throws IOException // @02C - { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - setProductID(data[0]); - setProductDesc(data[1]); - - System.out.println("产品ID = " + productID + "\n"); - System.out.println("产品描述 = " + productDesc + "\n"); - - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); - } - } - - private void setProductDesc(String desc) { - this.productDesc = desc; - } - - - protected void configureEMail(HashMap userInfo) throws IOException - { - toAddress = (String) userInfo.get(EMAIL_KEY); - if (toAddress.length() > 0) - setMessage(userInfo); - } protected List loadMailingList() throws Exception { return DBUtil.query(this.sendMailQuery); } - - - protected void sendEMails(boolean debug, List mailingList) throws IOException - { - - System.out.println("开始发送邮件"); - - - if (mailingList != null) { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) { - configureEMail((HashMap) iter.next()); - try - { - if (toAddress.length() > 0) - MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } - catch (Exception e) - { - - try { - MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) - { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } - } - - - } - - else { - System.out.println("没有邮件发送"); - - } - - } } From b468fdf8fe49b5280f24fe067d3f67f81b1e8b6c Mon Sep 17 00:00:00 2001 From: yyglider Date: Mon, 19 Jun 2017 15:13:17 +0800 Subject: [PATCH 212/214] delete season one delete season one --- .../main/java/season_1/code01/ArrayList.java | 138 -------- .../main/java/season_1/code01/BinaryTree.java | 97 ----- .../main/java/season_1/code01/Iterator.java | 7 - .../main/java/season_1/code01/LinkedList.java | 327 ----------------- .../main/java/season_1/code01/List.java | 9 - .../main/java/season_1/code01/Queue.java | 24 -- .../main/java/season_1/code01/Stack.java | 31 -- .../main/java/season_1/code02/ArrayUtil.java | 257 -------------- .../code02/litestruts/ActionConfig.java | 28 -- .../code02/litestruts/Configuration.java | 64 ---- .../code02/litestruts/LoginAction.java | 39 -- .../code02/litestruts/ReflectionUtil.java | 120 ------- .../season_1/code02/litestruts/Struts.java | 76 ---- .../java/season_1/code02/litestruts/View.java | 23 -- .../season_1/code03/v1/DownloadThread.java | 51 --- .../season_1/code03/v1/FileDownloader.java | 115 ------ .../season_1/code03/v1/api/Connection.java | 23 -- .../code03/v1/api/ConnectionException.java | 9 - .../code03/v1/api/ConnectionManager.java | 10 - .../code03/v1/api/DownloadListener.java | 5 - .../code03/v1/impl/ConnectionImpl.java | 95 ----- .../code03/v1/impl/ConnectionManagerImpl.java | 34 -- .../season_1/code03/v2/DownloadThread.java | 52 --- .../season_1/code03/v2/FileDownloader.java | 133 ------- .../season_1/code03/v2/api/Connection.java | 23 -- .../code03/v2/api/ConnectionException.java | 8 - .../code03/v2/api/ConnectionManager.java | 10 - .../code03/v2/api/DownloadListener.java | 5 - .../code03/v2/impl/ConnectionImpl.java | 92 ----- .../code03/v2/impl/ConnectionManagerImpl.java | 16 - .../java/season_1/code04/LRUPageFrame.java | 162 --------- .../main/java/season_1/code05/Stack.java | 54 --- .../main/java/season_1/code05/StackUtil.java | 148 -------- .../main/java/season_1/code06/InfixExpr.java | 126 ------- .../java/season_1/code07/InfixToPostfix.java | 44 --- .../java/season_1/code07/PostfixExpr.java | 47 --- .../main/java/season_1/code07/PrefixExpr.java | 55 --- .../main/java/season_1/code07/Token.java | 63 ---- .../java/season_1/code07/TokenParser.java | 89 ----- .../java/season_1/code08/CircleQueue.java | 93 ----- .../main/java/season_1/code08/Josephus.java | 55 --- .../main/java/season_1/code08/Queue.java | 61 ---- .../season_1/code08/QueueWithTwoStacks.java | 66 ---- .../java/season_1/code09/QuickMinStack.java | 40 --- .../season_1/code09/StackWithTwoQueues.java | 42 --- .../season_1/code09/TwoStackInOneArray.java | 152 -------- .../java/season_1/code10/BinaryTreeNode.java | 39 -- .../java/season_1/code10/BinaryTreeUtil.java | 125 ------- .../main/java/season_1/code10/FileList.java | 38 -- .../season_1/code11/BinarySearchTree.java | 305 ---------------- .../season_1/mini_jvm/attr/AttributeInfo.java | 19 - .../java/season_1/mini_jvm/attr/CodeAttr.java | 119 ------- .../season_1/mini_jvm/attr/ConstantValue.java | 21 -- .../mini_jvm/attr/LineNumberTable.java | 69 ---- .../mini_jvm/attr/LocalVariableTable.java | 98 ----- .../season_1/mini_jvm/attr/StackMapTable.java | 30 -- .../season_1/mini_jvm/clz/AccessFlag.java | 25 -- .../java/season_1/mini_jvm/clz/ClassFile.java | 121 ------- .../season_1/mini_jvm/clz/ClassIndex.java | 19 - .../java/season_1/mini_jvm/cmd/BiPushCmd.java | 29 -- .../mini_jvm/cmd/ByteCodeCommand.java | 158 --------- .../season_1/mini_jvm/cmd/CommandParser.java | 149 -------- .../season_1/mini_jvm/cmd/ComparisonCmd.java | 79 ----- .../season_1/mini_jvm/cmd/GetFieldCmd.java | 37 -- .../mini_jvm/cmd/GetStaticFieldCmd.java | 39 -- .../season_1/mini_jvm/cmd/IncrementCmd.java | 39 -- .../mini_jvm/cmd/InvokeSpecialCmd.java | 46 --- .../mini_jvm/cmd/InvokeVirtualCmd.java | 85 ----- .../java/season_1/mini_jvm/cmd/LdcCmd.java | 51 --- .../season_1/mini_jvm/cmd/NewObjectCmd.java | 39 -- .../season_1/mini_jvm/cmd/NoOperandCmd.java | 146 -------- .../season_1/mini_jvm/cmd/OneOperandCmd.java | 29 -- .../season_1/mini_jvm/cmd/PutFieldCmd.java | 44 --- .../season_1/mini_jvm/cmd/TwoOperandCmd.java | 67 ---- .../season_1/mini_jvm/constant/ClassInfo.java | 26 -- .../mini_jvm/constant/ConstantInfo.java | 28 -- .../mini_jvm/constant/ConstantPool.java | 25 -- .../mini_jvm/constant/FieldRefInfo.java | 49 --- .../mini_jvm/constant/MethodRefInfo.java | 52 --- .../mini_jvm/constant/NameAndTypeInfo.java | 45 --- .../mini_jvm/constant/NullConstantInfo.java | 12 - .../mini_jvm/constant/StringInfo.java | 25 -- .../season_1/mini_jvm/constant/UTF8Info.java | 33 -- .../mini_jvm/engine/ExecutionResult.java | 57 --- .../mini_jvm/engine/ExecutorEngine.java | 77 ---- .../java/season_1/mini_jvm/engine/Heap.java | 39 -- .../season_1/mini_jvm/engine/JavaObject.java | 71 ---- .../season_1/mini_jvm/engine/MethodArea.java | 90 ----- .../season_1/mini_jvm/engine/MiniJVM.java | 30 -- .../mini_jvm/engine/OperandStack.java | 26 -- .../season_1/mini_jvm/engine/StackFrame.java | 126 ------- .../java/season_1/mini_jvm/field/Field.java | 64 ---- .../mini_jvm/loader/ByteCodeIterator.java | 63 ---- .../mini_jvm/loader/ClassFileLoader.java | 144 -------- .../mini_jvm/loader/ClassFileParser.java | 159 --------- .../java/season_1/mini_jvm/method/Method.java | 151 -------- .../java/season_1/mini_jvm/util/Util.java | 22 -- .../java/season_1/code01/ArrayListTest.java | 67 ---- .../java/season_1/code01/BinaryTreeTest.java | 27 -- .../java/season_1/code01/LinkedListTest.java | 174 --------- .../test/java/season_1/code01/QueueTest.java | 24 -- .../test/java/season_1/code01/StackTest.java | 27 -- .../java/season_1/code02/ArrayUtilTest.java | 73 ---- .../code02/litestruts/StrutsTest.java | 43 --- .../season_1/code03/FileDownloaderTest.java | 60 ---- .../season_1/code04/LRUPageFrameTest.java | 38 -- .../java/season_1/code05/StackUtilTest.java | 79 ----- .../java/season_1/code06/InfixExprTest.java | 49 --- .../java/season_1/code07/PostfixExprTest.java | 42 --- .../java/season_1/code07/PrefixExprTest.java | 45 --- .../java/season_1/code08/JosephusTest.java | 32 -- .../season_1/code09/QuickMinStackTest.java | 34 -- .../code09/StackWithTwoQueuesTest.java | 27 -- .../season_1/code10/BinaryTreeUtilTest.java | 79 ----- .../season_1/code11/BinarySearchTreeTest.java | 93 ----- .../mini_jvm/ClassFileloaderTest.java | 335 ------------------ .../java/season_1/mini_jvm/EmployeeV1.java | 28 -- .../java/season_1/mini_jvm/EmployeeV2.java | 54 --- .../test/java/season_1/mini_jvm/Example.java | 16 - .../season_1/mini_jvm/HourlyEmployee.java | 27 -- .../java/season_1/mini_jvm/MiniJVMTest.java | 28 -- 121 files changed, 8198 deletions(-) delete mode 100644 students/769232552/season_one/main/java/season_1/code01/ArrayList.java delete mode 100644 students/769232552/season_one/main/java/season_1/code01/BinaryTree.java delete mode 100644 students/769232552/season_one/main/java/season_1/code01/Iterator.java delete mode 100644 students/769232552/season_one/main/java/season_1/code01/LinkedList.java delete mode 100644 students/769232552/season_one/main/java/season_1/code01/List.java delete mode 100644 students/769232552/season_one/main/java/season_1/code01/Queue.java delete mode 100644 students/769232552/season_one/main/java/season_1/code01/Stack.java delete mode 100644 students/769232552/season_one/main/java/season_1/code02/ArrayUtil.java delete mode 100644 students/769232552/season_one/main/java/season_1/code02/litestruts/ActionConfig.java delete mode 100644 students/769232552/season_one/main/java/season_1/code02/litestruts/Configuration.java delete mode 100644 students/769232552/season_one/main/java/season_1/code02/litestruts/LoginAction.java delete mode 100644 students/769232552/season_one/main/java/season_1/code02/litestruts/ReflectionUtil.java delete mode 100644 students/769232552/season_one/main/java/season_1/code02/litestruts/Struts.java delete mode 100644 students/769232552/season_one/main/java/season_1/code02/litestruts/View.java delete mode 100644 students/769232552/season_one/main/java/season_1/code03/v1/DownloadThread.java delete mode 100644 students/769232552/season_one/main/java/season_1/code03/v1/FileDownloader.java delete mode 100644 students/769232552/season_one/main/java/season_1/code03/v1/api/Connection.java delete mode 100644 students/769232552/season_one/main/java/season_1/code03/v1/api/ConnectionException.java delete mode 100644 students/769232552/season_one/main/java/season_1/code03/v1/api/ConnectionManager.java delete mode 100644 students/769232552/season_one/main/java/season_1/code03/v1/api/DownloadListener.java delete mode 100644 students/769232552/season_one/main/java/season_1/code03/v1/impl/ConnectionImpl.java delete mode 100644 students/769232552/season_one/main/java/season_1/code03/v1/impl/ConnectionManagerImpl.java delete mode 100644 students/769232552/season_one/main/java/season_1/code03/v2/DownloadThread.java delete mode 100644 students/769232552/season_one/main/java/season_1/code03/v2/FileDownloader.java delete mode 100644 students/769232552/season_one/main/java/season_1/code03/v2/api/Connection.java delete mode 100644 students/769232552/season_one/main/java/season_1/code03/v2/api/ConnectionException.java delete mode 100644 students/769232552/season_one/main/java/season_1/code03/v2/api/ConnectionManager.java delete mode 100644 students/769232552/season_one/main/java/season_1/code03/v2/api/DownloadListener.java delete mode 100644 students/769232552/season_one/main/java/season_1/code03/v2/impl/ConnectionImpl.java delete mode 100644 students/769232552/season_one/main/java/season_1/code03/v2/impl/ConnectionManagerImpl.java delete mode 100644 students/769232552/season_one/main/java/season_1/code04/LRUPageFrame.java delete mode 100644 students/769232552/season_one/main/java/season_1/code05/Stack.java delete mode 100644 students/769232552/season_one/main/java/season_1/code05/StackUtil.java delete mode 100644 students/769232552/season_one/main/java/season_1/code06/InfixExpr.java delete mode 100644 students/769232552/season_one/main/java/season_1/code07/InfixToPostfix.java delete mode 100644 students/769232552/season_one/main/java/season_1/code07/PostfixExpr.java delete mode 100644 students/769232552/season_one/main/java/season_1/code07/PrefixExpr.java delete mode 100644 students/769232552/season_one/main/java/season_1/code07/Token.java delete mode 100644 students/769232552/season_one/main/java/season_1/code07/TokenParser.java delete mode 100644 students/769232552/season_one/main/java/season_1/code08/CircleQueue.java delete mode 100644 students/769232552/season_one/main/java/season_1/code08/Josephus.java delete mode 100644 students/769232552/season_one/main/java/season_1/code08/Queue.java delete mode 100644 students/769232552/season_one/main/java/season_1/code08/QueueWithTwoStacks.java delete mode 100644 students/769232552/season_one/main/java/season_1/code09/QuickMinStack.java delete mode 100644 students/769232552/season_one/main/java/season_1/code09/StackWithTwoQueues.java delete mode 100644 students/769232552/season_one/main/java/season_1/code09/TwoStackInOneArray.java delete mode 100644 students/769232552/season_one/main/java/season_1/code10/BinaryTreeNode.java delete mode 100644 students/769232552/season_one/main/java/season_1/code10/BinaryTreeUtil.java delete mode 100644 students/769232552/season_one/main/java/season_1/code10/FileList.java delete mode 100644 students/769232552/season_one/main/java/season_1/code11/BinarySearchTree.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/attr/AttributeInfo.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/attr/CodeAttr.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/attr/ConstantValue.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/attr/LineNumberTable.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/attr/LocalVariableTable.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/attr/StackMapTable.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/clz/AccessFlag.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/clz/ClassFile.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/clz/ClassIndex.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/BiPushCmd.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/ByteCodeCommand.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/CommandParser.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/ComparisonCmd.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/GetFieldCmd.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/GetStaticFieldCmd.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/IncrementCmd.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/InvokeSpecialCmd.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/InvokeVirtualCmd.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/LdcCmd.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/NewObjectCmd.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/NoOperandCmd.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/OneOperandCmd.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/PutFieldCmd.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/cmd/TwoOperandCmd.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/ClassInfo.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/ConstantInfo.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/ConstantPool.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/FieldRefInfo.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/MethodRefInfo.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/NameAndTypeInfo.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/NullConstantInfo.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/StringInfo.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/constant/UTF8Info.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/engine/ExecutionResult.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/engine/ExecutorEngine.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/engine/Heap.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/engine/JavaObject.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/engine/MethodArea.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/engine/MiniJVM.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/engine/OperandStack.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/engine/StackFrame.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/field/Field.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/loader/ByteCodeIterator.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/loader/ClassFileLoader.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/loader/ClassFileParser.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/method/Method.java delete mode 100644 students/769232552/season_one/main/java/season_1/mini_jvm/util/Util.java delete mode 100644 students/769232552/season_one/test/java/season_1/code01/ArrayListTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/code01/BinaryTreeTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/code01/LinkedListTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/code01/QueueTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/code01/StackTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/code02/ArrayUtilTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/code02/litestruts/StrutsTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/code03/FileDownloaderTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/code04/LRUPageFrameTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/code05/StackUtilTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/code06/InfixExprTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/code07/PostfixExprTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/code07/PrefixExprTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/code08/JosephusTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/code09/QuickMinStackTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/code09/StackWithTwoQueuesTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/code10/BinaryTreeUtilTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/code11/BinarySearchTreeTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/mini_jvm/ClassFileloaderTest.java delete mode 100644 students/769232552/season_one/test/java/season_1/mini_jvm/EmployeeV1.java delete mode 100644 students/769232552/season_one/test/java/season_1/mini_jvm/EmployeeV2.java delete mode 100644 students/769232552/season_one/test/java/season_1/mini_jvm/Example.java delete mode 100644 students/769232552/season_one/test/java/season_1/mini_jvm/HourlyEmployee.java delete mode 100644 students/769232552/season_one/test/java/season_1/mini_jvm/MiniJVMTest.java diff --git a/students/769232552/season_one/main/java/season_1/code01/ArrayList.java b/students/769232552/season_one/main/java/season_1/code01/ArrayList.java deleted file mode 100644 index 6746de2a50..0000000000 --- a/students/769232552/season_one/main/java/season_1/code01/ArrayList.java +++ /dev/null @@ -1,138 +0,0 @@ -package code01; - -/** - * Created by yaoyuan on 2017/3/6. - */ -public class ArrayList implements List { - - private int max_size = 0;//总长度 - private int current_size = 0; //当前长度 - private float extendPercent = 2; //扩展系数 - - private Object[] elementData; - - /** - * 默认构造函数,初始化数组长度为100 - */ - public ArrayList(){ - this.elementData = new Object[100]; - this.max_size = 100; - } - /** - * 构造函数 - * @param size,初始化数组长度 - */ - public ArrayList(int size){ - this.elementData = new Object[size]; - this.max_size = size; - } - - /** - * 顺序添加元素,超出原始界限时,数组自动扩展 - */ - public void add(Object o) { - //如果越界了,需要复制原有的数组到扩充后的数组中 - if(this.current_size + 1 > this.max_size) { - this.max_size = (int) Math.floor(this.max_size * this.extendPercent); - this.elementData = copyToNew(this.elementData,this.max_size); - } - this.elementData[this.current_size] = o; - this.current_size ++; - - } - - /** - * 指定位置添加元素 - * 一种是在中间,一种是当前插入的位置尾部(如果超过尾部则默认添加到尾部) - */ - public void add(int index, Object o){ - assert(index>=0); - //如果越界了,需要复制原有的数组到扩充后的数组中 - if(this.current_size + 1 > this.max_size) { - //如果越界了,需要复制原有的数组到扩充后的数组中 - this.max_size = (int) Math.floor(this.max_size * this.extendPercent); - this.elementData = copyToNew(this.elementData,this.max_size); - } - //数组中间插入 - if(index < this.current_size){ - //需要把当前位置的元素往后移动 - for (int i = this.current_size - 1; i >= index; i--) { - this.elementData[i+1] = this.elementData[i]; - } - this.elementData[index] = o; - }else { - //后面加入 - this.elementData[this.current_size] = o; - } - this.current_size ++; - } - - public Object get(int index){ - if(index >= 0 && index <= this.current_size-1){ - return this.elementData[index]; - }else { - throw new ArrayIndexOutOfBoundsException(index); - } - } - - /** - * 删除指定位置的元素 - * @param index - * @return - */ - public Object remove(int index){ - Object result = null; - if(index >= 0 && index <= current_size-1){ - result = elementData[index]; - //删除操作 - if(index == current_size - 1){ - elementData[index] = null; - }else { - //需要把当前位置后面的元素往前移动 - for (int i = index; i < this.current_size-1 ; i++) { - this.elementData[i] = this.elementData[i+1]; - } - this.elementData[this.current_size-1] = null; - } - this.current_size --; - }else { - throw new ArrayIndexOutOfBoundsException(index); - } - return result; - } - - public int size(){ - return this.current_size; - } - - public Iterator iterator(){ - return new Iterator() { - int next_pos = 0; - int pos = -1; - public boolean hasNext() { - if(max_size <= 0){ - return false; - } - return next_pos < ArrayList.this.size(); - } - - public Object next() { - Object next = ArrayList.this.get(next_pos); - pos = next_pos ++; - return next; - } - public void remove(){ - ArrayList.this.remove(pos); - } - }; - } - - private Object[] copyToNew(Object[] oldArray, int extendSize){ - Object[] newArray = new Object[extendSize]; - for (int i = 0; i < size(); i++) { - newArray[i] = oldArray[i]; - } - return newArray; - } - -} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code01/BinaryTree.java b/students/769232552/season_one/main/java/season_1/code01/BinaryTree.java deleted file mode 100644 index b29fb960cb..0000000000 --- a/students/769232552/season_one/main/java/season_1/code01/BinaryTree.java +++ /dev/null @@ -1,97 +0,0 @@ -package code01; - -/** - * Created by yaoyuan on 2017/3/10. - */ -public class BinaryTree>{ - - private BinaryTreeNode root = null; - private int size = 0; - - public BinaryTreeNode createBinaryTree(T[] array){ - for(T data : array){ - this.insert(data); - } - return this.root; - } - - // recursive way, - // t is the node that roots the subtree. - public BinaryTreeNode insert(T data, BinaryTreeNode t){ - if(t == null){ - return new BinaryTreeNode(data); - } - int comparator = ((T) t.data).compareTo(data); - if(comparator > 0){ - t.left = insert(data,t.right); - }else if(comparator < 0){ - t.right = insert(data,t.left); - }else { - // do nothing - } - return t; - - } - - - //loop way - public void insert(T data){ - if(this.root == null){ - BinaryTreeNode node = new BinaryTreeNode(data); - this.root = node; - this.size ++; - return; - } - BinaryTreeNode cursor = this.root; - while (cursor != null){ - if(data.compareTo((T) cursor.data) <= 0){ - if(cursor.left == null) { - cursor.left = new BinaryTreeNode(data); - return; - } - cursor = cursor.left; - } - if(data.compareTo((T) cursor.data) > 0){ - if(cursor.right == null) { - cursor.right = new BinaryTreeNode(data); - return; - } - cursor = cursor.right; - } - } - this.size ++; - } - - public void leftOrderScan(BinaryTreeNode cursor){ - if(cursor == null){ - return; - } - leftOrderScan(cursor.left); - System.out.println(cursor.data.toString() + " "); - leftOrderScan(cursor.right); - } - - public BinaryTreeNode getRoot(){ - return this.root; - } - - class BinaryTreeNode { - - private T data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public BinaryTreeNode(T data, BinaryTreeNode left, BinaryTreeNode right) { - this.left = right; - this.right = left; - this.data = data; - } - - public BinaryTreeNode(T data) { - this.left = null; - this.right = null; - this.data = data; - } - - } -} diff --git a/students/769232552/season_one/main/java/season_1/code01/Iterator.java b/students/769232552/season_one/main/java/season_1/code01/Iterator.java deleted file mode 100644 index b4074067bb..0000000000 --- a/students/769232552/season_one/main/java/season_1/code01/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package code01; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - public void remove(); -} diff --git a/students/769232552/season_one/main/java/season_1/code01/LinkedList.java b/students/769232552/season_one/main/java/season_1/code01/LinkedList.java deleted file mode 100644 index f7bbc970a9..0000000000 --- a/students/769232552/season_one/main/java/season_1/code01/LinkedList.java +++ /dev/null @@ -1,327 +0,0 @@ -package code01; - - -public class LinkedList implements List { - - private Node head; - private Node tail; //指向链表最后一个元素的引用 - - private int size; //总长度 - - public LinkedList() { - this.head = null; - this.tail = null; - this.size = 0; - } - - /** - * 新增顺序添加一个元素 - * @param o - */ - public void add(Object o){ - Node node = new Node(); - node.data = o; - node.next = null; - //空链表 - if(head == null){ - this.head = node; - this.tail = node; - }else { //非空链表,要先找到链表尾部,再加入新解答 - this.tail.next = node; - this.tail = node; - } - this.size ++; - } - - /** - * 指定索引处添加node - */ - public void add(int index, Object o) { - assert(index >= 0); - Node node = new Node(); - node.data = o; - node.next = null; - - if(index == 0){ - //添加在头部 - node.next = head; - head = node; - }else if(index >= this.size){ - //添加在尾部 - this.tail.next = node; - }else { - //添加在中间 - Node cursor = this.head; - for (int i = 0; i < index - 1; i++) { - cursor = cursor.next; - } - node.next = cursor.next; - cursor.next = node; - } - this.size ++; - } - - public Object get(int index){ - assert(index < this.size); - Node cursor = this.head; - for (int i = 0; i < index; i++) { - cursor = cursor.next; - } - return cursor.data; - } - - public Object remove(int index){ - assert(index >= 0 && index < this.size); - Object result = null; - //删除的是链表尾部的元素 - if(index == this.size - 1){ - Node cursor = this.head; - for (int i = 0; i < index - 1; i++) { - cursor = cursor.next; - } - result = cursor.next.data; - tail = cursor; - cursor.next = null; - }else if(index == 0){ - //删除的是头部元素 - result = head.data; - head = head.next; - }else { - //删除的是链表中间的元素 - Node cursor = this.head; - for (int i = 0; i < index - 1; i++) { - cursor = cursor.next; - } - result = cursor.next.data; - cursor.next = cursor.next.next; - } - this.size --; - return result; - } - - public int size(){ - return this.size; - } - - public void addFirst(Object o){ - Node node = new Node(); - node.data = o; - node.next = null; - if(this.head == null){ - this.head = node; - this.tail = node; - }else { - node.next = head; - this.head = node; - } - this.size ++; - } - public void addLast(Object o){ - Node node = new Node(); - node.data = o; - node.next = null; - if(this.head == null){ - this.head = node; - this.tail = node; - }else { - this.tail.next = node; - this.tail = node; - } - this.size ++; - } - - public Object removeFirst(){ - Object first = null; - if(this.head != null){ - first = this.head.data; - head = head.next; - this.size --; - } - return first; - } - - public Object removeLast(){ - Object last = null; - if(this.tail != null){ - if(this.head != this.tail){ - Node cursor; - for (cursor = head;cursor.next!=tail;cursor=cursor.next); - last = this.tail.data; - this.tail = cursor; - this.tail.next = null; - }else { - last = this.tail.data; - this.head = null; - this.tail = null; - } - this.size --; - } - return last; - } - public Iterator iterator(){ - return null; - } - - /** - * 节点类 - */ - private static class Node{ - Object data; - Node next; - - } - - /** - * 把该链表逆置 - * 例如链表为 3->7->10 , 逆置后变为 10->7->3 - */ - public void reverse(){ - if(this.head == null || this.head == this.tail){ - return; - } - - Node pre_cursor = null; - Node cursor = this.head; - Node after_cursor = cursor.next; - - while(cursor != null){ - cursor.next = pre_cursor; - pre_cursor = cursor; - cursor = after_cursor; - if(after_cursor != null){ - after_cursor = after_cursor.next; - } - } - - Node tmpNode = this.head; - this.head = this.tail; - this.tail = tmpNode; - } - - /** - * 删除一个单链表的前半部分 - * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 - * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 - - */ - public void removeFirstHalf(){ - if(this.head == null || this.head.next == null){ - return; - } - if(this.head.next.next == null){ - this.head = this.head.next; - } - - Node stepOne = this.head; - Node stepTwo = this.head; - - while (stepTwo.next != null){ - stepOne = stepOne.next; - stepTwo = stepTwo.next.next; - } - this.head = stepOne; - } - - /** - * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 - * @param i - * @param length - */ - public void remove(int i, int length){ - Node current = head; - Node firstHalf = null; - for (int k = 0; k < i; k ++){ - if(current == null){ - return; - } - firstHalf = current; //记录待删除节点的前一个节点 - current = current.next; - } - - //移动length长度 - for (int j = 0; j < length; j++) { - if(current == null){ - return; - } - current = current.next; - } - - if(i == 0){ - head = current; - }else { - firstHalf.next = current; - } - } - /** - * 假定当前链表和list均包含已升序排列的整数 - * 从当前链表中取出那些list所指定的元素 - * 例如当前链表 = 11->101->201->301->401->501->601->701 - * listB = 1->3->4->6 - * 返回的结果应该是[101,301,401,601] - * @param list - */ - public static int[] getElements(LinkedList list){ - return null; - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 从当前链表中中删除在list中出现的元素 - - * @param list - */ - - public void subtract(LinkedList list){ - - } - - /** - * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) - */ - public void removeDuplicateValues(){ - if(this.head == null){ - return; - } - Node current = this.head; - Node current_next = this.head; - while (current_next != null){ - current_next = current_next.next; //如果放到下个while循环后面写,就需要判断一次current_next是不是null了 - while(current_next != null && current_next.data.equals(current.data)){ - //删除重复节点 - current.next = current_next.next; - current_next = current_next.next; - } - current = current_next; - } - } - - /** - * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 - * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) - * @param min - * @param max - */ - public void removeRange(int min, int max){ - //怎么才能高效呢 - } - - /** - * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) - * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 - * @param list - */ - public LinkedList intersection( LinkedList list){ - return null; - } - - /** - * 遍历列表 - */ - public void printList(){ - System.out.println(); - for (Node cursor = this.head;cursor!=null;cursor=cursor.next){ - System.out.print(cursor.data+" "); - } - } -} diff --git a/students/769232552/season_one/main/java/season_1/code01/List.java b/students/769232552/season_one/main/java/season_1/code01/List.java deleted file mode 100644 index 95bc37d172..0000000000 --- a/students/769232552/season_one/main/java/season_1/code01/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package code01; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} diff --git a/students/769232552/season_one/main/java/season_1/code01/Queue.java b/students/769232552/season_one/main/java/season_1/code01/Queue.java deleted file mode 100644 index d9956deb9a..0000000000 --- a/students/769232552/season_one/main/java/season_1/code01/Queue.java +++ /dev/null @@ -1,24 +0,0 @@ -package code01; - -public class Queue { - - private LinkedList linkedList = new LinkedList(); - - public void enQueue(Object o){ - linkedList.addFirst(o); - } - - public Object deQueue(){ - Object result = linkedList.removeLast(); - return result; - } - - public boolean isEmpty(){ - return linkedList.size() == 0; - } - - public int size(){ - return linkedList.size(); - } - -} diff --git a/students/769232552/season_one/main/java/season_1/code01/Stack.java b/students/769232552/season_one/main/java/season_1/code01/Stack.java deleted file mode 100644 index dbaeb91a48..0000000000 --- a/students/769232552/season_one/main/java/season_1/code01/Stack.java +++ /dev/null @@ -1,31 +0,0 @@ -package code01; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - elementData.add(o); - } - - public Object pop(){ - Object result = null; - if(elementData.size()!=0) { - result = elementData.remove(elementData.size() - 1); - } - return result; - } - - public Object peek(){ - Object result = null; - if(elementData.size()!=0) { - result = elementData.get(elementData.size() - 1); - } - return result; - } - public boolean isEmpty(){ - return elementData.size() == 0; - } - public int size(){ - return elementData.size(); - } -} diff --git a/students/769232552/season_one/main/java/season_1/code02/ArrayUtil.java b/students/769232552/season_one/main/java/season_1/code02/ArrayUtil.java deleted file mode 100644 index 23055ef138..0000000000 --- a/students/769232552/season_one/main/java/season_1/code02/ArrayUtil.java +++ /dev/null @@ -1,257 +0,0 @@ -package code02; -import org.apache.commons.lang.ArrayUtils; -import java.util.ArrayList; -import java.util.List; - -public class ArrayUtil { - - /** - * 给定一个整形数组a , 对该数组的值进行置换 - 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] - 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] - * @param origin - * @return - */ - public void reverseArray(int[] origin){ - if (origin == null || origin.length <= 1){ - return; - } - - int head = 0; - int tail = origin.length - 1; - int tmp; - while (head != tail){ - //调换位置 - tmp = origin[head]; - origin[head] = origin[tail]; - origin[tail] = tmp; - - head ++; - tail --; - } - - } - - /** - * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} - * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: - * {1,3,4,5,6,6,5,4,7,6,7,5} - * @param oldArray - * @return - */ - public int[] removeZero(int[] oldArray){ - if (oldArray == null || oldArray.length < 1){ - return null; - } - - List newList = new ArrayList(); - for(int number : oldArray){ - if(number != 0){ - newList.add(number); - } - } - - Integer[] result = new Integer[newList.size()]; - result = (Integer[]) newList.toArray(result); - return ArrayUtils.toPrimitive(result); - - - } - - /** - * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 - * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 - * @param array1 - * @param array2 - * @return - */ - - public int[] merge(int[] array1, int[] array2){ - if(array1 == null && array2 == null){ - return null; - } - if(array1 == null){ - return array2; - } - if(array2 == null){ - return array1; - } - int[] newArray = new int[array1.length + array2.length]; - int m = 0,n = 0, k = 0; - while (m < array1.length && n < array2.length){ - if(array1[m] <= array2[n]){ - newArray[k++] = array1[m++]; - }else { - newArray[k++] = array2[n++]; - } - } - if(m >= array1.length){ - while (n < array2.length){ - newArray[k++] = array2[n++]; - } - } - if(n >= array2.length){ - while (m < array1.length){ - newArray[k++] = array1[m++]; - } - } - return newArray; - } - /** - * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size - * 注意,老数组的元素在新数组中需要保持 - * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 - * [2,3,6,0,0,0] - * @param oldArray - * @param size - * @return - */ - public int[] grow(int [] oldArray, int size){ - int[] newArray = new int[oldArray.length + size]; - int i = 0; - for (; i < oldArray.length; i++) { - newArray[i] = oldArray[i]; - } - for (int j = 0; j < size; j++){ - newArray[i++] = 0; - } - return newArray; - } - - /** - * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 - * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] - * max = 1, 则返回空数组 [] - * @param max - * @return - */ - //也就是需要生成一个小于max值的fibonacci数组 - public int[] fibonacci(int max){ - if(max < 2){ - return new int[]{}; - } - if(max < 3){ - return new int[]{1,1}; - } - List list = new ArrayList(); - list.add(0,1); - list.add(1,1); - int i=0; - while (list.get(i) + list.get(i+1) < max){ - list.add(i+2,list.get(i) + list.get(i+1)); - i++; - } - - int[] newArray = new int[list.size()]; - for (int j = 0; j < list.size(); j++) { - newArray[j] = list.get(j).intValue(); - } - return newArray; - } - - /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * @param max - * @return - * - * 原理: - * 1,判断一个数字是否为素数,一个数 n 如果是合数,那么它的所有的因子不超过sqrt(n) - * 2,当i是素数的时候,i的所有的倍数必然是合数。 - */ - public int[] getPrimes(int max){ - - if(max <= 2){ - return null; - } - boolean[] prime = new boolean[max + 1]; - for (int i = 2; i <= max; i++) { - if(i%2 == 0){ - prime[i] = false; //偶数 - }else { - prime[i] = true; - } - } - - for (int i = 2; i <= Math.sqrt(max) ; i++) { - if(prime[i]){//奇数 - //如果i是素数,那么把i的倍数标记为非素数 - for(int j = i+i; j <= max; j += i){ - prime[j] = false; - } - } - } - - List num = new ArrayList(); - for (int i = 2; i <= max; i++) { - if(prime[i]){ - num.add(i); - } - } - - Integer[] result = new Integer[num.size()]; - result = (Integer[]) num.toArray(result); - return ArrayUtils.toPrimitive(result); - } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * @param max - * @return - */ - public int[] getPerfectNumbers(int max){ - - if(max < 6){ - return null; - } - - List perfectNumlist = new ArrayList(); - - for (int j = 6;j <= max; j++){ - List factorNumlist = new ArrayList(); - factorNumlist.add(1); - for (int i = 2; i < j; i++) { - if(j % i == 0){ - factorNumlist.add(i); - } - } - int sum = 0; - for(Integer num : factorNumlist){ - sum += num; - } - - if(sum == j){ - perfectNumlist.add(j); - } - } - Integer[] result = new Integer[perfectNumlist.size()]; - result = (Integer[]) perfectNumlist.toArray(result); - return ArrayUtils.toPrimitive(result); - } - - /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" - * @param array - * @param seperator - * @return - */ - public String join(int[] array, String seperator){ - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < array.length - 1; i++) { - sb.append(array[i]); - sb.append(seperator); - } - sb.append(array[array.length - 1]); - return sb.toString(); - } - - public void printArr(int[] array){ - for(int num : array){ - System.out.print(num + " "); - } - } - -} diff --git a/students/769232552/season_one/main/java/season_1/code02/litestruts/ActionConfig.java b/students/769232552/season_one/main/java/season_1/code02/litestruts/ActionConfig.java deleted file mode 100644 index b5e077e7a5..0000000000 --- a/students/769232552/season_one/main/java/season_1/code02/litestruts/ActionConfig.java +++ /dev/null @@ -1,28 +0,0 @@ -package code02.litestruts; - -import java.util.HashMap; -import java.util.Map; - -/** - * Created by yaoyuan on 2017/3/22. - */ -public class ActionConfig { - String name; - String clzName; - Map viewResult = new HashMap(); - - - public ActionConfig(String actionName, String clzName) { - this.name = actionName; - this.clzName = clzName; - } - public String getClassName(){ - return clzName; - } - public void addViewResult(String name, String viewName){ - viewResult.put(name, viewName); - } - public String getViewName(String resultName){ - return viewResult.get(resultName); - } -} diff --git a/students/769232552/season_one/main/java/season_1/code02/litestruts/Configuration.java b/students/769232552/season_one/main/java/season_1/code02/litestruts/Configuration.java deleted file mode 100644 index 85d3d98a1f..0000000000 --- a/students/769232552/season_one/main/java/season_1/code02/litestruts/Configuration.java +++ /dev/null @@ -1,64 +0,0 @@ -package code02.litestruts; - -import org.dom4j.Document; -import org.dom4j.DocumentException; -import org.dom4j.Element; -import org.dom4j.io.SAXReader; - -import java.io.File; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -/** - * Created by yaoyuan on 2017/3/21. - */ -public class Configuration { - - - private String path; - private final Map actionMap = new HashMap(); - - Configuration(String path){ - parseXML(path); - } - - //解析xml文件 - private void parseXML(String path){ - //读取文件 - File file = new File(path); - SAXReader reader = new SAXReader(); - Document document = null; - try { - document = reader.read(file); - } catch (DocumentException e) { - e.printStackTrace(); - } - Element root = document.getRootElement(); - - for (Iterator iterator = root.elementIterator("action"); iterator.hasNext();) { - Element e = iterator.next(); - String actionName = e.attributeValue("name"); - String clazName = e.attributeValue("class"); - ActionConfig actionConfig = new ActionConfig(actionName,clazName); - for(Iterator childIterator = e.elementIterator();childIterator.hasNext();){ - Element child = childIterator.next(); - String jspKey = child.attributeValue("name"); - String jspValue = child.getTextTrim(); - actionConfig.addViewResult(jspKey,jspValue); - } - actionMap.put(actionName,actionConfig); - } - } - - public String getView(String actionName, String result){ - String jspKey = actionName + "." + result; - return actionMap.get(actionName).getViewName(result); - } - - - public Map getActionMap() { - return actionMap; - } - -} diff --git a/students/769232552/season_one/main/java/season_1/code02/litestruts/LoginAction.java b/students/769232552/season_one/main/java/season_1/code02/litestruts/LoginAction.java deleted file mode 100644 index 0799eae71a..0000000000 --- a/students/769232552/season_one/main/java/season_1/code02/litestruts/LoginAction.java +++ /dev/null @@ -1,39 +0,0 @@ -package code02.litestruts; - -/** - * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 - * @author liuxin - * - */ -public class LoginAction{ - private String name ; - private String password; - private String message; - - public String getName() { - return name; - } - - public String getPassword() { - return password; - } - - public String execute(){ - if("test".equals(name) && "1234".equals(password)){ - this.message = "login successful"; - return "success"; - } - this.message = "login failed,please check your user/pwd"; - return "fail"; - } - - public void setName(String name){ - this.name = name; - } - public void setPassword(String password){ - this.password = password; - } - public String getMessage(){ - return this.message; - } -} diff --git a/students/769232552/season_one/main/java/season_1/code02/litestruts/ReflectionUtil.java b/students/769232552/season_one/main/java/season_1/code02/litestruts/ReflectionUtil.java deleted file mode 100644 index c4a8ed34fc..0000000000 --- a/students/769232552/season_one/main/java/season_1/code02/litestruts/ReflectionUtil.java +++ /dev/null @@ -1,120 +0,0 @@ -package code02.litestruts; - -import org.slf4j.LoggerFactory; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Created by yaoyuan on 2017/3/21. - */ -public class ReflectionUtil { - private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ReflectionUtil.class); - - private static final Map> clazzMap = new HashMap>(); - - //加载xml文件中的类 - public void initiateClazz(Configuration cfg){ - Map actionMap = cfg.getActionMap(); - - for (Map.Entry entry : actionMap.entrySet()) { - String actionName = entry.getKey(); //login - ActionConfig actionConfig =entry.getValue(); - String className = actionConfig.getClassName(); //code02.litestruts.LoginAction - Class cls; - try { - cls = Class.forName(className, true, Thread.currentThread().getContextClassLoader()); - clazzMap.put(actionName,cls); - } catch (Exception e) { - logger.warn("加载类 " + className + "出错!"); - } - } - - } - - - //返回实例对象 - public Object getInstance(String actionName){ - Object instance = null; - for (Map.Entry> entry : clazzMap.entrySet()) { - String action = entry.getKey(); //login - Class cls = entry.getValue(); //code02.litestruts.LoginAction.class - if(actionName.equals(action)){ - try { - instance = cls.newInstance(); - } catch (Exception e) { - logger.error("生成实例出错!", e); - throw new RuntimeException(e); - } - } - } - return instance; - } - - - //参数赋值 - public void setParameters(Object o, Map params) { - - List methods = getSetterMethods(o.getClass()); - for (String name : params.keySet()) { - String methodName = "set" + name; - for (Method m : methods) { - if (m.getName().equalsIgnoreCase(methodName)) { - try { - m.invoke(o, params.get(name)); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } - } - } - } - - //运行无参方法 - public Object runMethodWithoutParams(Object o , String methodName){ - Class clz = o.getClass(); - Object result = null; - try { - Method method = clz.getDeclaredMethod(methodName); - try { - result = method.invoke(o); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } - return result; - } - - //返回以set开头的方法 - public List getSetterMethods(Class clz){ - return getMethods(clz,"set"); - } - - //返回以get开头的方法 - public List getGetterMethods(Class clz){ - return getMethods(clz,"get"); - } - - private List getMethods(Class clz, String startWithName){ - List methodsList = new ArrayList(); - Method[] methods = clz.getDeclaredMethods(); - for (int i = 0; i < methods.length; i++) { - String methodName = methods[i].getName(); - if(methodName.startsWith(startWithName)){ - methodsList.add(methods[i]); - } - } - return methodsList; - } - -} diff --git a/students/769232552/season_one/main/java/season_1/code02/litestruts/Struts.java b/students/769232552/season_one/main/java/season_1/code02/litestruts/Struts.java deleted file mode 100644 index de2c22ea94..0000000000 --- a/students/769232552/season_one/main/java/season_1/code02/litestruts/Struts.java +++ /dev/null @@ -1,76 +0,0 @@ -package code02.litestruts; - -import org.slf4j.LoggerFactory; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - - -public class Struts { - private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Struts.class); - /* - 0. 读取配置文件struts.xml - - 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) - 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 - ("name"="test" , "password"="1234") , - 那就应该调用 setName和setPassword方法 - - - - - */ - public static View runAction(String actionName, Map parameters) { - View view = new View(); - Configuration cfg = new Configuration("src/main/resources/struts.xml"); - ReflectionUtil reflectionUtil = new ReflectionUtil(); - reflectionUtil.initiateClazz(cfg); - /* 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象)*/ - Object o = reflectionUtil.getInstance(actionName); - /*2. 根据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 - ("name"="test" , "password"="1234") ,那就应该调用 setName和setPassword方法*/ - reflectionUtil.setParameters(o,parameters); - /*3. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success"*/ - String result = (String) reflectionUtil.runMethodWithoutParams(o,"execute"); - /* 4. 通过反射找到对象的所有getter方法(例如 getMessage),通过反射来调用, - 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} ,放到View对象的parameters*/ - Map params = new HashMap(); - List methods = reflectionUtil.getGetterMethods(o.getClass()); - for(Method method : methods){ - String key = method.getName().substring(3); - String value = null; - try { - value = (String) method.invoke(o); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - params.put(key,value); - } - /*5. 根据struts.xml中的 配置,以及execute的返回值,确定哪一个jsp,放到View对象的jsp字段中。*/ - String jsp = cfg.getView(actionName,result); - view.setParameters(params); - view.setJsp(jsp); - - return view; - } - - public static void main(String[] args) throws InvocationTargetException, IllegalAccessException { - - String actionName = "login"; - HashMap params = new HashMap(); - params.put("name","test"); - params.put("password","12345"); - - View view = Struts.runAction(actionName,params); - System.out.println(view.getJsp()); - System.out.println(view.getParameters()); - - - } - -} diff --git a/students/769232552/season_one/main/java/season_1/code02/litestruts/View.java b/students/769232552/season_one/main/java/season_1/code02/litestruts/View.java deleted file mode 100644 index c7e630587c..0000000000 --- a/students/769232552/season_one/main/java/season_1/code02/litestruts/View.java +++ /dev/null @@ -1,23 +0,0 @@ -package code02.litestruts; - -import java.util.Map; - -public class View { - private String jsp; - private Map parameters; - - public String getJsp() { - return jsp; - } - public View setJsp(String jsp) { - this.jsp = jsp; - return this; - } - public Map getParameters() { - return parameters; - } - public View setParameters(Map parameters) { - this.parameters = parameters; - return this; - } -} diff --git a/students/769232552/season_one/main/java/season_1/code03/v1/DownloadThread.java b/students/769232552/season_one/main/java/season_1/code03/v1/DownloadThread.java deleted file mode 100644 index f91bff3bf1..0000000000 --- a/students/769232552/season_one/main/java/season_1/code03/v1/DownloadThread.java +++ /dev/null @@ -1,51 +0,0 @@ -package code03.v1; - -import code03.v1.api.Connection; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.io.RandomAccessFile; -import java.util.concurrent.CountDownLatch; - -/** - * 定义线程类 - */ - - -public class DownloadThread extends Thread{ - - private static final Logger logger = LoggerFactory.getLogger(DownloadThread.class); - - private Connection conn; - private int startPos; - private int endPos; - private CountDownLatch finished; - private String fileName; - - - public DownloadThread(Connection conn, int startPos, int endPos, CountDownLatch finished,String fileName){ - this.conn = conn; - this.startPos = startPos; - this.endPos = endPos; - this.finished = finished; - this.fileName = fileName; - } - - @Override - public void run(){ - logger.debug("thread {} begin to download from start {} to end {} ",Thread.currentThread().getName(),startPos,endPos); - - try { - byte[] data = conn.read(startPos,endPos); - RandomAccessFile rfile = new RandomAccessFile(fileName,"rw"); - rfile.seek(startPos); - rfile.write(data); - rfile.close(); - } catch (IOException e) { - e.printStackTrace(); - } - finished.countDown(); - logger.debug("thread {} end to download from start {} to end {} ",Thread.currentThread().getName(),startPos,endPos); - } -} diff --git a/students/769232552/season_one/main/java/season_1/code03/v1/FileDownloader.java b/students/769232552/season_one/main/java/season_1/code03/v1/FileDownloader.java deleted file mode 100644 index 542fd978c2..0000000000 --- a/students/769232552/season_one/main/java/season_1/code03/v1/FileDownloader.java +++ /dev/null @@ -1,115 +0,0 @@ -package code03.v1; - -import code03.v1.api.Connection; -import code03.v1.api.ConnectionException; -import code03.v1.api.ConnectionManager; -import code03.v1.api.DownloadListener; -import code03.v1.impl.ConnectionManagerImpl; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.CountDownLatch; - -/** - * 线程启动类 - */ - -public class FileDownloader { - private final static int THREAD_NUM = 5; - - private static final String fileHolder = "D:/test.png"; - private String url; - private DownloadListener listener; - private ConnectionManager cm; - private static boolean downloadFinished = false; - - static final CountDownLatch finished = new CountDownLatch(THREAD_NUM); //v2的版本中使用了CyclicBarrier方式 - - - public FileDownloader(String _url) { - this.url = _url; - } - - /* - (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) - (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 - 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。*/ - public void execute(){ - Connection conn = null; - try { - //启动线程 - int startPos = 0, endPos = 0; - List threads = new ArrayList(); - for (int i = 0; i < THREAD_NUM; i++) { - conn = cm.open(this.url); //每次都要重新获取一个connection.imputstream - int length = conn.getContentLength(); - startPos = length/THREAD_NUM * i; - endPos = length/THREAD_NUM * (i + 1)- 1; - DownloadThread downloadThread = new DownloadThread(conn,startPos,endPos,finished, fileHolder); - threads.add(downloadThread); - downloadThread.start(); - } - finished.await(); - //调用join方法,确保所有线程的工作已经完成 - /*for (int i = 0; i < THREAD_NUM; i++) { - try { - threads.get(i).join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - }*/ - listener.notifyFinished(); - } catch (ConnectionException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - e.printStackTrace(); - } finally { - if(conn != null){ - conn.close(); - } - } - } - - public void setListener(DownloadListener listener) { - this.listener = listener; - } - - public void setConnectionManager(ConnectionManager ucm){ - this.cm = ucm; - } - - public DownloadListener getListener(){ - return this.listener; - } - - - public static void main(String[] args) { - - String url = "http://litten.me/assets/blogImg/litten.png"; - FileDownloader fileDownloader = new FileDownloader(url); - ConnectionManager cm = new ConnectionManagerImpl(); - fileDownloader.setListener(new DownloadListener() { - @Override - public void notifyFinished() { - downloadFinished = true; - } - }); - fileDownloader.setConnectionManager(cm); - fileDownloader.execute(); - - - while (!downloadFinished){ - try { - System.out.println("还没有下载完成,休眠五秒"); - //休眠5秒 - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - System.out.println("download finished ! "); - - - } - -} diff --git a/students/769232552/season_one/main/java/season_1/code03/v1/api/Connection.java b/students/769232552/season_one/main/java/season_1/code03/v1/api/Connection.java deleted file mode 100644 index 92a3d4725a..0000000000 --- a/students/769232552/season_one/main/java/season_1/code03/v1/api/Connection.java +++ /dev/null @@ -1,23 +0,0 @@ -package code03.v1.api; - -import java.io.IOException; - -public interface Connection { - /** - * 给定开始和结束位置, 读取数据, 返回值是字节数组 - * @param startPos 开始位置, 从0开始 - * @param endPos 结束位置 - * @return - */ - public byte[] read(int startPos,int endPos) throws IOException; - /** - * 得到数据内容的长度 - * @return - */ - public int getContentLength(); - - /** - * 关闭连接 - */ - public void close(); -} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code03/v1/api/ConnectionException.java b/students/769232552/season_one/main/java/season_1/code03/v1/api/ConnectionException.java deleted file mode 100644 index bee02c3717..0000000000 --- a/students/769232552/season_one/main/java/season_1/code03/v1/api/ConnectionException.java +++ /dev/null @@ -1,9 +0,0 @@ -package code03.v1.api; - -public class ConnectionException extends Exception { - - public ConnectionException(String message,Throwable e){ - super(message,e); - } - -} diff --git a/students/769232552/season_one/main/java/season_1/code03/v1/api/ConnectionManager.java b/students/769232552/season_one/main/java/season_1/code03/v1/api/ConnectionManager.java deleted file mode 100644 index 4ec1b87667..0000000000 --- a/students/769232552/season_one/main/java/season_1/code03/v1/api/ConnectionManager.java +++ /dev/null @@ -1,10 +0,0 @@ -package code03.v1.api; - -public interface ConnectionManager { - /** - * 给定一个url , 打开一个连接 - * @param url - * @return - */ - public Connection open(String url) throws ConnectionException; -} diff --git a/students/769232552/season_one/main/java/season_1/code03/v1/api/DownloadListener.java b/students/769232552/season_one/main/java/season_1/code03/v1/api/DownloadListener.java deleted file mode 100644 index 8cd24bfd57..0000000000 --- a/students/769232552/season_one/main/java/season_1/code03/v1/api/DownloadListener.java +++ /dev/null @@ -1,5 +0,0 @@ -package code03.v1.api; - -public interface DownloadListener { - public void notifyFinished(); -} diff --git a/students/769232552/season_one/main/java/season_1/code03/v1/impl/ConnectionImpl.java b/students/769232552/season_one/main/java/season_1/code03/v1/impl/ConnectionImpl.java deleted file mode 100644 index 75c91bb5bb..0000000000 --- a/students/769232552/season_one/main/java/season_1/code03/v1/impl/ConnectionImpl.java +++ /dev/null @@ -1,95 +0,0 @@ -package code03.v1.impl; - -import code03.v1.api.Connection; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.BufferedInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URLConnection; - - -public class ConnectionImpl implements Connection{ - - private static final Logger logger = LoggerFactory.getLogger(ConnectionImpl.class); - - - private URLConnection urlConnection; - private int length = -1; - - - public ConnectionImpl(URLConnection urlConnection){ - this.urlConnection = urlConnection; - } - - /** - * 读取urlConnection.getInputStream()中的数据,返回byte[] - */ - @Override - public byte[] read(int startPos, int endPos) throws IOException { - int contentLength = getContentLength(); - if(startPos < 0 || endPos > contentLength || contentLength <= 0){ - logger.info("index out of range !"); - return null; - } - - InputStream raw = null; - BufferedInputStream in = null; - int size = endPos - startPos + 1; - byte[] data = new byte[size]; - try{ - raw = urlConnection.getInputStream(); - in = new BufferedInputStream(raw); - in.skip(startPos); - - int offset = 0; - while(offset < size){ - int bytesRead = in.read(data, offset, size - offset); - while (bytesRead == -1){break;} - offset += bytesRead; - } - //用这种方式比较好 - /* - int BUFFER_SIZE = 1024; - byte[] buff = new byte[BUFFER_SIZE]; - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - while(baos.size() < size){ - int bytesRead = in.read(buff); //缓存读取的数据,其SIZE大小不一定要等于总的SIZE - if(bytesRead<0) break; - baos.write(buff,0,bytesRead); - } - byte[] data = baos.toByteArray(); - Arrays.copyOf(data,size); - */ - - } catch (IOException e) { - e.printStackTrace(); - }finally { - raw.close(); - in.close(); - } - return data; - } - - @Override - public int getContentLength() { - if(length != -1){ - return length; - } - length = urlConnection.getContentLength(); - //if without content-length header - if(length == -1) { - throw new RuntimeException("content-length error"); - } - return length; - } - - @Override - public void close() { - if(urlConnection != null){ - urlConnection = null; - } - } - -} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code03/v1/impl/ConnectionManagerImpl.java b/students/769232552/season_one/main/java/season_1/code03/v1/impl/ConnectionManagerImpl.java deleted file mode 100644 index b4c9a1b90d..0000000000 --- a/students/769232552/season_one/main/java/season_1/code03/v1/impl/ConnectionManagerImpl.java +++ /dev/null @@ -1,34 +0,0 @@ -package code03.v1.impl; - -import code03.v1.api.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; - -/** - * 获取Connection实例 - */ - -public class ConnectionManagerImpl implements ConnectionManager { - private static final Logger logger = LoggerFactory.getLogger(ConnectionManagerImpl.class); - - @Override - public Connection open(String url) throws ConnectionException { - Connection connection = null; - try { - URL _url = new URL(url); - URLConnection urlConnection = _url.openConnection(); - connection = new ConnectionImpl(urlConnection); - } catch (MalformedURLException e) { - logger.error("url {} format error",url); - } catch (IOException e) { - e.printStackTrace(); - } - return connection; - } - -} diff --git a/students/769232552/season_one/main/java/season_1/code03/v2/DownloadThread.java b/students/769232552/season_one/main/java/season_1/code03/v2/DownloadThread.java deleted file mode 100644 index 54cc1d3d0b..0000000000 --- a/students/769232552/season_one/main/java/season_1/code03/v2/DownloadThread.java +++ /dev/null @@ -1,52 +0,0 @@ -package code03.v2; - -import code03.v2.api.Connection; - -import java.io.RandomAccessFile; -import java.util.concurrent.CyclicBarrier; - -public class DownloadThread extends Thread{ - - Connection conn; - int startPos; - int endPos; - CyclicBarrier barrier; - String localFile; - public DownloadThread( Connection conn, int startPos, int endPos, String localFile, CyclicBarrier barrier){ - - this.conn = conn; - this.startPos = startPos; - this.endPos = endPos; - this.localFile = localFile; - this.barrier = barrier; - } - - - - public void run(){ - - - try { - System.out.println("Begin to read [" + startPos +"-"+endPos+"]"); - - byte[] data = conn.read(startPos, endPos); - - RandomAccessFile file = new RandomAccessFile(localFile,"rw"); - - file.seek(startPos); - - file.write(data); - - file.close(); - - conn.close(); - - barrier.await(); //等待别的线程完成 - - } catch (Exception e) { - e.printStackTrace(); - - } - - } -} diff --git a/students/769232552/season_one/main/java/season_1/code03/v2/FileDownloader.java b/students/769232552/season_one/main/java/season_1/code03/v2/FileDownloader.java deleted file mode 100644 index 12c750145f..0000000000 --- a/students/769232552/season_one/main/java/season_1/code03/v2/FileDownloader.java +++ /dev/null @@ -1,133 +0,0 @@ -package code03.v2; - -import code03.v2.api.Connection; -import code03.v2.api.ConnectionManager; -import code03.v2.api.DownloadListener; - -import java.io.IOException; -import java.io.RandomAccessFile; -import java.util.concurrent.CyclicBarrier; - - - -public class FileDownloader { - - private String url; - private String localFile; - - DownloadListener listener; - - ConnectionManager cm; - - - private static final int DOWNLOAD_TRHEAD_NUM = 3; - - public FileDownloader(String _url, String localFile) { - this.url = _url; - this.localFile = localFile; - - } - - public void execute(){ - // 在这里实现你的代码, 注意: 需要用多线程实现下载 - // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 - // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) - // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 - // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 - // 具体的实现思路: - // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 - // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 - // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 - // 3. 把byte数组写入到文件中 - // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 - - // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 - - CyclicBarrier barrier = new CyclicBarrier(DOWNLOAD_TRHEAD_NUM , new Runnable(){ - public void run(){ - listener.notifyFinished(); - } - }); - - Connection conn = null; - try { - - conn = cm.open(this.url); - - int length = conn.getContentLength(); - - createPlaceHolderFile(this.localFile, length); - - int[][] ranges = allocateDownloadRange(DOWNLOAD_TRHEAD_NUM, length); - - for(int i=0; i< DOWNLOAD_TRHEAD_NUM; i++){ - - - DownloadThread thread = new DownloadThread( - cm.open(url), - ranges[i][0], - ranges[i][1], - localFile, - barrier); - thread.start(); - } - - } catch (Exception e) { - e.printStackTrace(); - }finally{ - if(conn != null){ - conn.close(); - } - } - - } - - private void createPlaceHolderFile(String fileName, int contentLen) throws IOException{ - - RandomAccessFile file = new RandomAccessFile(fileName,"rw"); - - for(int i=0; i totalLen){ - byte[] data = baos.toByteArray(); - return Arrays.copyOf(data, totalLen); - } - - return baos.toByteArray(); - } - - @Override - public int getContentLength() { - - URLConnection con; - try { - con = url.openConnection(); - - return con.getContentLength(); - - } catch (IOException e) { - e.printStackTrace(); - } - - return -1; - - - } - - @Override - public void close() { - - - } - -} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code03/v2/impl/ConnectionManagerImpl.java b/students/769232552/season_one/main/java/season_1/code03/v2/impl/ConnectionManagerImpl.java deleted file mode 100644 index 596beaa191..0000000000 --- a/students/769232552/season_one/main/java/season_1/code03/v2/impl/ConnectionManagerImpl.java +++ /dev/null @@ -1,16 +0,0 @@ -package code03.v2.impl; - - -import code03.v2.api.Connection; -import code03.v2.api.ConnectionException; -import code03.v2.api.ConnectionManager; - -public class ConnectionManagerImpl implements ConnectionManager { - - @Override - public Connection open(String url) throws ConnectionException { - - return new ConnectionImpl(url); - } - -} diff --git a/students/769232552/season_one/main/java/season_1/code04/LRUPageFrame.java b/students/769232552/season_one/main/java/season_1/code04/LRUPageFrame.java deleted file mode 100644 index 0ffce58b83..0000000000 --- a/students/769232552/season_one/main/java/season_1/code04/LRUPageFrame.java +++ /dev/null @@ -1,162 +0,0 @@ -package code04; - -/** - * 用双向链表实现LRU算法 - */ -public class LRUPageFrame { - - private class Node { - - Node prev; - Node next; - int pageNum; - Node() { - } - } - - private int size; - private int capacity; - - private Node first;// 链表头 - private Node last;// 链表尾 - - public LRUPageFrame(int capacity) { - this.capacity = capacity; - this.size = 0; - } - - /** - * 获取缓存中对象 - * 1、如果缓存中不存在,则直接加入表头 - * 2、如果缓存中存在,则把该对象移动到表头 - * @param pageNum - * @return - */ - public void access(int pageNum) { - Node node = hasContains(pageNum); - if(node != null){ - moveToHead(node); - }else { - addToHead(pageNum); - } - - } - - /** - * 对象是否存在缓存中,如存在则返回该对象在链表中的位置,否则返回null - * @return - */ - private Node hasContains(int key){ - Node node = this.first; - while (node != null){ - if(node.pageNum == key){ - return node; - } - node = node.next; - } - return node; - } - - /** - * 对象加入表头,先先判断缓存是否已经满了 - * @return - */ - private void addToHead(int key){ - Node node = new Node(); - node.pageNum = key; - if(size < capacity){ - addFirst(node); - }else { - removeLast(); - addFirst(node); - } - - } - - - /** - * 对象移动到表头 - * @return - */ - private void moveToHead(Node node){ - if(node == first){ - return; - } - if(node == last){ - node.next = first; - first.prev = node; - first = node; - last = node.prev; - last.next = null; - node.prev = null; - return; - } - node.prev.next = node.next; - node.next.prev = node.prev; - node.next = first; - node.prev = null; - first.prev = node; - first = node; - } - - /** - * 删除表尾 - * @return - */ - private void removeLast(){ - if(last != null){ - Node newLast = last.prev; - last.prev = null; - last = newLast; - last.next = null; - size --; - } - } - /** - * 添加元素到表头 - * @return - */ - private void addFirst(Node node){ - //0个节点 - if(first == null){ - first = node; - last = node; - size ++; - return; - } - //一个节点 - else if(first == last){ - first = node; - first.next = last; - last.prev = first; - size ++; - return; - }else { - node.next = first; - first.prev = node; - first = node; - size ++; - } - } - /** - * 当前链表空间 - * @return - */ - public int getSize() { - return size; - } - - public String toString(){ - StringBuilder buffer = new StringBuilder(); - Node node = first; - while(node != null){ - buffer.append(node.pageNum); - node = node.next; - if(node != null){ - buffer.append(","); - } - } - return buffer.toString(); - } - -} diff --git a/students/769232552/season_one/main/java/season_1/code05/Stack.java b/students/769232552/season_one/main/java/season_1/code05/Stack.java deleted file mode 100644 index 04e1f1aebb..0000000000 --- a/students/769232552/season_one/main/java/season_1/code05/Stack.java +++ /dev/null @@ -1,54 +0,0 @@ -package code05; - -import code01.ArrayList; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - if(o == null){ - return; - } - elementData.add(o); - } - - - public Object pop(){ - Object last = null; - int last_index = elementData.size() - 1; - if(last_index >= 0){ - last = elementData.get(last_index); - elementData.remove(last_index); - } - return last; - } - - public Object peek(){ - Object last = null; - int last_index = elementData.size() - 1 ; - if(last_index >= 0){ - last = elementData.get(last_index); - } - return last; - } - public boolean isEmpty(){ - return elementData.size() == 0; - } - public int size(){ - return elementData.size(); - } - - @Override - public String toString(){ - StringBuilder sb = new StringBuilder(); - sb.append("["); - int i = 0; - for (; i < size() - 1; i++) { - sb.append(elementData.get(i)); - sb.append(", "); - } - sb.append(elementData.get(i)); - sb.append("]"); - return sb.toString(); - } -} diff --git a/students/769232552/season_one/main/java/season_1/code05/StackUtil.java b/students/769232552/season_one/main/java/season_1/code05/StackUtil.java deleted file mode 100644 index fb9e90631a..0000000000 --- a/students/769232552/season_one/main/java/season_1/code05/StackUtil.java +++ /dev/null @@ -1,148 +0,0 @@ -package code05; - -public class StackUtil { - - - /** - * 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 - * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 - */ - - /** - * 此处是传值,将原有栈的地址 复制给 变量 s, s在函数内相当于局部变量,因此 s 重新赋值并没有什么用 - * @param s - */ - public static void bad_reverse(Stack s) { - if(s == null){ - return; - } - // need a new stack - Stack newStack = new Stack(); - while (s.peek() != null){ - newStack.push(s.pop()); - } - s = newStack; - } - - - public static void reverse(Stack s) { - if(s == null || s.isEmpty()){ - return; - } - - Stack tmp = new Stack(); - while(!s.isEmpty()){ - tmp.push(s.pop()); - } - while(!tmp.isEmpty()){ - int top = (Integer) tmp.pop(); - addToBottom(s,top);//加入到原来栈的栈底 - } - - - } - - public static void addToBottom(Stack s, int value){ - if(s.isEmpty()){ - s.push(value); - } else{ - int top = (Integer) s.pop(); - addToBottom(s,value); - s.push(top); - } - - } - - /** - * 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 - * @param o - */ - public static void remove(Stack s,Object o) { - if(s == null || o == null){ - return; - } - Stack newStack = new Stack(); - while (s.peek() != null){ - Object e = s.pop(); - if(!e.equals(o)){ - newStack.push(e); - } - } - while (newStack.peek() != null){ - s.push(newStack.pop()); - } - } - - /** - * 从栈顶取得len个元素, 原来的栈中元素保持不变 - * 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 - * @param len - * @return - */ - public static Object[] getTop(Stack s,int len) throws Exception { - if(s == null || s.isEmpty() || s.size() < len || len <= 0){ - return null; - } - Stack newStack = new Stack(); - for (int i = 0; i < len; i++) { - Object o = s.pop(); - newStack.push(o); - } - Object[] objects = new Object[len]; - for (int i = len - 1; i >= 0; i--) { - Object o = newStack.pop(); - s.push(o); - objects[i] = o; - } - return objects; - } - /** - * 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz - * 使用堆栈检查字符串s中的括号是不是成对出现的。 - * 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true - * 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false; - * @param s - * @return - */ - public static boolean isValidPairs(String s){ - char tag_1_start = '{'; - char tag_1_end = '}'; - char tag_2_start = '['; - char tag_2_end = ']'; - char tag_3_start = '('; - char tag_3_end = ')'; - - int length = s.length(); - Stack stack = new Stack(); - for (int i = 0; i < length; i++) { - char c = s.charAt(i); - if(c == tag_1_start || c == tag_2_start || c == tag_3_start){ - stack.push(c); - } - else if(c == tag_1_end){ - //pop all element after tag_1_start - Object t= stack.pop(); - if(!t.equals(tag_1_start)){ - return false; - } - } - else if(c == tag_2_end){ - //pop all element after tag_1_start - Object t= stack.pop(); - if(!t.equals(tag_2_start)){ - return false; - } - } - else if(c == tag_3_end){ - //pop all element after tag_1_start - Object t= stack.pop(); - if(!t.equals(tag_3_start)){ - return false; - } - } - } - return stack.size() == 0; - } - - -} diff --git a/students/769232552/season_one/main/java/season_1/code06/InfixExpr.java b/students/769232552/season_one/main/java/season_1/code06/InfixExpr.java deleted file mode 100644 index a13e190091..0000000000 --- a/students/769232552/season_one/main/java/season_1/code06/InfixExpr.java +++ /dev/null @@ -1,126 +0,0 @@ -package code06; - -import code05.Stack; - -import java.util.ArrayList; -import java.util.List; - -public class InfixExpr { - String expr = null; - - public InfixExpr(String expr) { - this.expr = expr; - } - - private float calc(float a, float b, String ops){ - float result = 0.0f; - if (ops.equals("+")) { - return b + a; - } - if (ops.equals("-")) { - return b - a; - } - if (ops.equals("*")) { - return b * a; - } - if (ops.equals("/")) { - return b / a; - } - return result; - } - - private boolean isOperator(String ch){ - boolean isOperator = (ch.equals("+") || ch.equals("-") || ch.equals("*") || ch.equals("/")); - return isOperator; - } - - //parse string to list - private List parser(){ - char[] chs = expr.toCharArray(); - List values = new ArrayList(); - int currentCharPos = 0; // 当前字符的位置 - while (currentCharPos < chs.length) { - String currentStr = String.valueOf(chs[currentCharPos]); - if(isOperator(currentStr)){ - values.add(currentStr); - currentCharPos ++; - } - else { - int numberOffset = 0; - while (currentCharPos + numberOffset < chs.length && !isOperator(String.valueOf(chs[currentCharPos+numberOffset]))){ - numberOffset ++ ; - } - if(numberOffset == 1){ - values.add(currentStr); - currentCharPos++; - }else { - String number = new String(chs,currentCharPos,numberOffset); - values.add(number); - currentCharPos = currentCharPos + numberOffset; - } - } - } - return values; - } - - private int morePriority(String peek, String e) { - boolean hasMorePriority = ((e.equals("+") || e.equals("-")) && (peek.equals("*") || peek.equals("/"))); - boolean hasLessPriority = ((e.equals("*") || e.equals("/")) && (peek.equals("+") || peek.equals("-"))); - if(hasMorePriority) { - return 1; - } - if(hasLessPriority){ - return -1; - } - return 0; - } - - public float evaluate() { - float result = 0.0f; - - Stack numberStack = new Stack(); - Stack opsStack = new Stack(); - - List elements = this.parser(); - for(String e : elements){ - if(!isOperator(e)){ //数字直接入栈 - float number = Float.valueOf(e); - numberStack.push(number); - }else {//操作符 - if(opsStack.isEmpty()){ - opsStack.push(e); - }else { - //栈顶符号有着相等或者更高的优先级 - if(morePriority((String) opsStack.peek(),e) >= 0){ - float a = (Float) numberStack.pop(); - float b = (Float) numberStack.pop(); - String ops = (String) opsStack.pop(); - - float value = calc(a,b,ops); - - numberStack.push(value); - opsStack.push(e); - }else { - opsStack.push(e); - } - } - } - } - while (!opsStack.isEmpty()) { - float a = (Float) numberStack.pop(); - float b = (Float) numberStack.pop(); - String ops = (String) opsStack.pop(); - float value = calc(a, b, ops); - numberStack.push(value); - } - - result = (Float) numberStack.pop(); - return result; - } - - public static void main(String[] args) { - InfixExpr expr = new InfixExpr("20+30.8*400/500"); - List elements = expr.parser(); - System.out.println(expr.evaluate()); - } -} diff --git a/students/769232552/season_one/main/java/season_1/code07/InfixToPostfix.java b/students/769232552/season_one/main/java/season_1/code07/InfixToPostfix.java deleted file mode 100644 index 7da7c7d58d..0000000000 --- a/students/769232552/season_one/main/java/season_1/code07/InfixToPostfix.java +++ /dev/null @@ -1,44 +0,0 @@ -package code07; - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -public class InfixToPostfix { - - public static List convert(String expr) { - - List tokens = new ArrayList(); - - Stack tokenStack = new Stack(); - List infixTokens =TokenParser.parseInfix(expr); - - for(Token t : infixTokens){ - if(t.isNumber()){ - tokens.add(t); - }else { - while (!tokenStack.isEmpty() && !t.hasHigherPriority(tokenStack.peek())){ - tokens .add(tokenStack.pop()); - } - tokenStack.push(t); - } - } - - while(!tokenStack.isEmpty()){ - tokens.add(tokenStack.pop()); - } - - return tokens; - } - - public static void main(String[] args) { - String expr = "2+3*4+5"; - List tokens = InfixToPostfix.convert(expr); - for(Token t : tokens){ - System.out.println(t.getStringValue()); - } - - } - - -} diff --git a/students/769232552/season_one/main/java/season_1/code07/PostfixExpr.java b/students/769232552/season_one/main/java/season_1/code07/PostfixExpr.java deleted file mode 100644 index cf9e3e6722..0000000000 --- a/students/769232552/season_one/main/java/season_1/code07/PostfixExpr.java +++ /dev/null @@ -1,47 +0,0 @@ -package code07; - -import java.util.List; -import java.util.Stack; - -public class PostfixExpr { -String expr = null; - - public PostfixExpr(String expr) { - this.expr = expr; - } - - public float evaluate() { - - Stack numStack = new Stack(); - List tokens = TokenParser.parseNoInfix(this.expr); - - for(Token token : tokens){ - if(token.isNumber()){ - numStack.push(new Float(token.getIntValue())); - } else{ - Float f2 = numStack.pop(); - Float f1 = numStack.pop(); - numStack.push(calc(f1,f2,token.getStringValue())); - } - } - - return numStack.pop().floatValue(); - } - - //如下类似排比句的代码写法就是卫语句 - private Float calc(Float f1, Float f2, String op){ - if(op.equals("+")){ - return f1+f2; - } - if(op.equals("-")){ - return f1-f2; - } - if(op.equals("*")){ - return f1*f2; - } - if(op.equals("/")){ - return f1/f2; - } - throw new RuntimeException(op + " is not supported"); - } -} diff --git a/students/769232552/season_one/main/java/season_1/code07/PrefixExpr.java b/students/769232552/season_one/main/java/season_1/code07/PrefixExpr.java deleted file mode 100644 index 1f8bcff595..0000000000 --- a/students/769232552/season_one/main/java/season_1/code07/PrefixExpr.java +++ /dev/null @@ -1,55 +0,0 @@ -package code07; - -import java.util.List; -import java.util.Stack; - -public class PrefixExpr { - String expr = null; - - public PrefixExpr(String expr) { - this.expr = expr; - } - - public float evaluate() { - Stack exprStack = new Stack(); - Stack numberStack = new Stack(); - - List tokens = TokenParser.parseNoInfix(this.expr); - - for(Token t: tokens){ - exprStack.push(t); - } - - while (!exprStack.isEmpty()){ - Token t = exprStack.pop(); - if(Token.OPERATOR == t.getType()){ - float a = numberStack.pop(); - float b = numberStack.pop(); - float result = calc(a,b,t.getStringValue()); - numberStack.push(result); - }else if(Token.NUMBER == t.getType()){ - numberStack.push(Float.parseFloat(t.getStringValue())); - }else { - System.out.println("char :["+t.getStringValue()+"] is not number or operator,ignore"); - } - } - - return numberStack.pop().floatValue(); - } - - private Float calc(Float f1, Float f2, String op){ - if(op.equals("+")){ - return f1+f2; - } - if(op.equals("-")){ - return f1-f2; - } - if(op.equals("*")){ - return f1*f2; - } - if(op.equals("/")){ - return f1/f2; - } - throw new RuntimeException(op + " is not supported"); - } -} diff --git a/students/769232552/season_one/main/java/season_1/code07/Token.java b/students/769232552/season_one/main/java/season_1/code07/Token.java deleted file mode 100644 index a5d6735427..0000000000 --- a/students/769232552/season_one/main/java/season_1/code07/Token.java +++ /dev/null @@ -1,63 +0,0 @@ -package code07; - -import java.lang.reflect.Array; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Created by yyglider on 2017/4/19. - */ -public class Token { - - public static final List OPERATORS = Arrays.asList("+","-","*","/"); - - private static final Map priorities = new HashMap(); - - static { - priorities.put("+",1); - priorities.put("-",1); - priorities.put("*",2); - priorities.put("/",2); - } - - public static final int OPERATOR = 1; - public static final int NUMBER = 2; - - private String value; - private int type; - - public Token(int type, String value) { - this.value = value; - this.type = type; - } - - public boolean isNumber(){ - return type == NUMBER; - } - - public boolean isOperator(){ - return type == OPERATOR; - } - - public int getIntValue(){ - return Integer.valueOf(value).intValue(); - } - - public String getStringValue(){ - return value; - } - - public int getType() { - return type; - } - - public boolean hasHigherPriority(Token t){ - if(!this.isOperator() && !t.isOperator()){ - throw new RuntimeException("numbers can't compare priority"); - } - return priorities.get(this.value) - priorities.get(t.value) > 0; - } - -} diff --git a/students/769232552/season_one/main/java/season_1/code07/TokenParser.java b/students/769232552/season_one/main/java/season_1/code07/TokenParser.java deleted file mode 100644 index df91e4d3dc..0000000000 --- a/students/769232552/season_one/main/java/season_1/code07/TokenParser.java +++ /dev/null @@ -1,89 +0,0 @@ -package code07; - -import java.util.ArrayList; -import java.util.List; - -/** - * Created by yyglider on 2017/4/20. - * 解析表达式成各个Token - */ -public class TokenParser { - - public static List parseNoInfix(String expr){ - List tokens = new ArrayList(); - - String[] chs = expr.split(" "); - - for (int i = 0; i < chs.length; i++) { - if(isOperator(chs[i])){ - tokens.add(new Token(Token.OPERATOR,chs[i])); - }else { - String value = chs[i]; - tokens.add(new Token(Token.NUMBER,value)); - } - } - return tokens; - } - - - public static List parseInfix(String expr){ - List tokens = new ArrayList(); - - char[] chs = expr.toCharArray(); - - int i = 0; - while (i < chs.length){ - if(isOperator(chs[i])){ - tokens.add(new Token(Token.OPERATOR,String.valueOf(chs[i]))); - i++; - }else if(Character.isDigit(chs[i])){ - - int nextOperatorPos = findNextOperatorPos(i,chs); - String value = expr.substring(i,nextOperatorPos); - tokens.add(new Token(Token.NUMBER,value)); - i = nextOperatorPos; - - }else { - System.out.println("char :["+chs[i]+"] is not number or operator,ignore"); - i++; - } - } - return tokens; - } - - private static int findNextOperatorPos(int i, char[] chs) { - while (Character.isDigit(chs[i])) { - i++; - if (i == chs.length) { - break; - } - } - return i; - } - - private static boolean isOperator(char c) { - String sc = String.valueOf(c); - return Token.OPERATORS.contains(sc); - } - - private static boolean isOperator(String c) { - return Token.OPERATORS.contains(c); - } - - public static void main(String[] args) { -/* - String expr = "20+30.8*400/500"; - List tokens = TokenParser.parseInfix(expr); - for(Token t: tokens){ - System.out.println(t.getStringValue()); - } -*/ - - - String expr2 = "- + + 6 / * 2 9 3 * 4 2 8"; - List tokens2 = TokenParser.parseNoInfix(expr2); - for(Token t: tokens2){ - System.out.println(t.getStringValue()); - } - } -} diff --git a/students/769232552/season_one/main/java/season_1/code08/CircleQueue.java b/students/769232552/season_one/main/java/season_1/code08/CircleQueue.java deleted file mode 100644 index 99ccd2cc57..0000000000 --- a/students/769232552/season_one/main/java/season_1/code08/CircleQueue.java +++ /dev/null @@ -1,93 +0,0 @@ -package code08; - -/** - * 用数组实现循环队列 - 杜绝“假上溢” - * 在入队和出队的操作中,头尾指针只增加不减小,致使被删除元素的空间永远无法重新利用。 - * - * 因此,尽管队列中实际的元素个数远远小于向量空间的规模,但也可能由于尾指针巳超出向量空间的上界而不能做入队操作。 - * 为充分利用向量空间。克服上述假上溢现象的方法是将向量空间想象为一个首尾相接的圆环,并称这种向量为循环队列。 - * 在循环队列中进行出队、入队操作时,头尾指针仍要加1,朝前移动。 - * 只不过当头尾指针指向向量上界(QueueSize-1)时,其加1操作的结果是指向向量的下界0。 - * @param - */ -public class CircleQueue { - - private int maxSize; - //用数组来保存循环队列的元素 - private Object[] elementData; - //队头 - private int front; - //队尾 - private int rear; - //元素个数 - private int count; - - public CircleQueue(int maxSize) { - this.maxSize = maxSize+1; //队列需要一个空的位置用于区分队列满和队列空 - this.elementData = new Object[maxSize+1]; - this.front = 0; - this.rear = 0; - } - - public int size() { - return count; - } - - public void enQueue(E data) { - if(this.isFull()){ - System.out.println("queue is full, enQueue failed"); - return; - } - elementData[rear] = data; - rear = (rear + 1) % this.maxSize; - count ++; - } - - public E deQueue() { - if(this.isEmpty()){ - System.out.println("queue is empty, deQueue failed"); - return null; - } - E e = (E) elementData[front]; - front = (front + 1) % this.maxSize; - count --; - return e; - } - - public boolean isFull(){ - return ((rear + 1) % this.maxSize == front); - } - - public boolean isEmpty() { - return rear == front; - } - - public static void main(String[] args) { - CircleQueue q = new CircleQueue(11); - q.enQueue("a1"); - q.enQueue("a2"); - q.enQueue("a3"); - q.enQueue("a4"); - q.enQueue("a5"); - q.enQueue("a6"); - q.enQueue("a7"); - q.enQueue("a8"); - q.enQueue("a9"); - q.enQueue("a10"); - System.out.println("current size is : " + q.size()); - - System.out.println("dequeue : " + q.deQueue()); - System.out.println("after deQueue , current size is : " + q.size()); - - q.enQueue("new1"); - System.out.println("after enQueue , current size is : " + q.size()); - - /*while(!q.isEmpty()){ - System.out.println(q.deQueue()); - System.out.println("current size is : " + q.size()); - }*/ - - - - } -} diff --git a/students/769232552/season_one/main/java/season_1/code08/Josephus.java b/students/769232552/season_one/main/java/season_1/code08/Josephus.java deleted file mode 100644 index 4c964631e2..0000000000 --- a/students/769232552/season_one/main/java/season_1/code08/Josephus.java +++ /dev/null @@ -1,55 +0,0 @@ -package code08; - -import java.util.ArrayList; -import java.util.List; - -/** - * 用Queue来实现Josephus问题 - * 在这个古老的问题当中, N个深陷绝境的人一致同意用这种方式减少生存人数: N个人围成一圈(位置记为0到N-1), 并且从第一个人报数, 报到M的人会被杀死, 直到最后一个人留下来 - * 该方法返回一个List, 包含了被杀死人的次序 - - 0 1 2 3 4 5 6 7 8 9 m=3, n=10 - - 第一个人(2)出列后的序列为: - 3 4 5 6 7 8 9 0 1 (0,1,2 出队列,0,1 再入队列) - - 第二个人(5)出列后的序列为: -   6 7 8 9 0 1 3 4 (3,4,5 出队列,3,4 再入队列) - - ... - - 如果剩余队列长度小于等于m则依次出队列 - - */ -public class Josephus { - - public static List execute(int n, int m){ - List kList = new ArrayList(); - CircleQueue circleQueue = new CircleQueue(n); - - for (int i = 0; i < n ; i++) { - circleQueue.enQueue(i); - } - - Queue tmpQueue = new Queue(); - while(!circleQueue.isEmpty()){ - - if(m > circleQueue.size()) { - m = m % circleQueue.size(); - } - - for (int j = 0; j < m-1; j++) { - tmpQueue.enQueue(circleQueue.deQueue()); - } - - Integer kill = circleQueue.deQueue(); - kList.add(kill); - - for (int j = 0; j < m-1; j++) { - circleQueue.enQueue(tmpQueue.deQueue()); - } - } - return kList; - } - -} diff --git a/students/769232552/season_one/main/java/season_1/code08/Queue.java b/students/769232552/season_one/main/java/season_1/code08/Queue.java deleted file mode 100644 index f4e1977b73..0000000000 --- a/students/769232552/season_one/main/java/season_1/code08/Queue.java +++ /dev/null @@ -1,61 +0,0 @@ -package code08; - -import java.util.NoSuchElementException; - -public class Queue { - private Node first; - private Node last; - private int size; - - - private static class Node { - private E item; - private Node next; - } - - - public Queue() { - first = null; - last = null; - size = 0; - } - - - public boolean isEmpty() { - return first == null; - } - - public int size() { - return size; - } - - - - public void enQueue(E data) { - Node oldlast = last; - last = new Node(); - last.item = data; - last.next = null; - if (isEmpty()) { - first = last; - } - else{ - oldlast.next = last; - } - size++; - } - - public E deQueue() { - if (isEmpty()) { - throw new NoSuchElementException("Queue underflow"); - } - E item = first.item; - first = first.next; - size--; - if (isEmpty()) { - last = null; - } - return item; - } - -} diff --git a/students/769232552/season_one/main/java/season_1/code08/QueueWithTwoStacks.java b/students/769232552/season_one/main/java/season_1/code08/QueueWithTwoStacks.java deleted file mode 100644 index 7f9700f573..0000000000 --- a/students/769232552/season_one/main/java/season_1/code08/QueueWithTwoStacks.java +++ /dev/null @@ -1,66 +0,0 @@ -package code08; - -import java.util.Stack; - -/** - * 用两个栈来实现一个队列 - * @param - */ -public class QueueWithTwoStacks { - private Stack stack1; - private Stack stack2; - - - public QueueWithTwoStacks() { - stack1 = new Stack(); - stack2 = new Stack(); - } - - - - public boolean isEmpty() { - return stack1.isEmpty() && stack2.isEmpty(); - } - - - public int size() { - return stack1.size() + stack2.size(); - } - - - public void enQueue(E item) { - stack1.push(item); - } - - public E deQueue() { - if(!stack2.isEmpty()){ - return stack2.pop(); - } - if(!stack1.isEmpty()){ - while (!stack1.isEmpty()){ - E e = stack1.pop(); - stack2.push(e); - } - return stack2.pop(); - } - return null; - } - - - public static void main(String[] args) { - QueueWithTwoStacks q = new QueueWithTwoStacks(); - q.enQueue("a"); - q.enQueue("b"); - q.enQueue("c"); - while (!q.isEmpty()){ - System.out.println(q.deQueue()); - } - q.enQueue("d"); - q.enQueue("e"); - while (!q.isEmpty()){ - System.out.println(q.deQueue()); - } - } - - } - diff --git a/students/769232552/season_one/main/java/season_1/code09/QuickMinStack.java b/students/769232552/season_one/main/java/season_1/code09/QuickMinStack.java deleted file mode 100644 index 7951a2bde1..0000000000 --- a/students/769232552/season_one/main/java/season_1/code09/QuickMinStack.java +++ /dev/null @@ -1,40 +0,0 @@ -package code09; - -import code05.Stack; - -/** - * 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素 - * finMin操作最坏的情形下时间复杂度应该是O(1),简单来讲,操作一次就可以得到最小值 - * - * 构造一个辅助栈,每次压入当前主栈中最小的元素 - */ -public class QuickMinStack { - - Stack mainStack = new Stack(); - Stack subStack = new Stack(); //辅助栈 - - public void push(int data){ - mainStack.push(data); - - //每次压入当前主栈中最小的元素 - if(subStack.isEmpty()){ - subStack.push(data); - } - else if(data >= (Integer)subStack.peek()){ - subStack.push(subStack.peek()); - } - else { - subStack.push(data); - } - - } - - public int pop(){ - subStack.pop(); - return (Integer) mainStack.pop(); - } - - public int findMin(){ - return (Integer) subStack.peek(); - } -} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code09/StackWithTwoQueues.java b/students/769232552/season_one/main/java/season_1/code09/StackWithTwoQueues.java deleted file mode 100644 index 926977502e..0000000000 --- a/students/769232552/season_one/main/java/season_1/code09/StackWithTwoQueues.java +++ /dev/null @@ -1,42 +0,0 @@ -package code09; - -import code08.Queue; - -/** - * 思路和2个栈实现一个队列是一致的,入的时候简单,出的时候麻烦 - */ - -public class StackWithTwoQueues { - - - Queue q1 = new Queue(); - Queue q2 = new Queue(); - - public void push(int data) { - q1.enQueue(data); - } - - public int pop() { - if(q1.isEmpty()){ - return -1; - } - - if(q1.size() == 1){ - return (Integer) q1.deQueue(); - } - - while (q1.size()>1){ - q2.enQueue(q1.deQueue()); - } - - int result = (Integer) q1.deQueue(); - - while (!q2.isEmpty()){ - q1.enQueue(q2.deQueue()); - } - - return result; - } - - -} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code09/TwoStackInOneArray.java b/students/769232552/season_one/main/java/season_1/code09/TwoStackInOneArray.java deleted file mode 100644 index 9390e16246..0000000000 --- a/students/769232552/season_one/main/java/season_1/code09/TwoStackInOneArray.java +++ /dev/null @@ -1,152 +0,0 @@ -package code09; - -import code01.ArrayList; - -/** - * 用一个数组实现两个栈 - * 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。 - */ -public class TwoStackInOneArray { - - Object[] data = new Object[10]; - int top1 = -1, end1 = 0; - int top2 = data.length, end2 = data.length - 1; - - /** - * 向第一个栈中压入元素 - * @param o - */ - public void push1(Object o){ - if(top1+1 < top2){ - data[++top1] = o; - return; - } - resize(); - data[++top1] = o; - } - - /** - * 从第一个栈中弹出元素 - * @return - */ - public Object pop1(){ - if(top1 < 0){ - return null; - } - return data[top1--]; - } - - /** - * 获取第一个栈的栈顶元素 - * @return - */ - - public Object peek1(){ - if(top1 < 0){ - return null; - } - return data[top1]; - } - - /** - * 向第二个栈压入元素 - */ - public void push2(Object o){ - - if(top2 - 1 > top1){ - data[--top2] = o; - return; - } - resize(); - data[--top2] = o; - } - - /** - * 从第二个栈弹出元素 - * @return - */ - public Object pop2(){ - if(top2 > data.length - 1){ - return null; - } - return data[top2++]; - } - /** - * 获取第二个栈的栈顶元素 - * @return - */ - - public Object peek2(){ - if(top2 > data.length - 1){ - return null; - } - return data[top2]; - } - - /** - * 重新分配空间 - */ - private void resize() { - System.out.println("resize data array ..."); - Object[] newData = new Object[20]; - //copy stack 1 - for (int i = end1; i <= top1; i++) { - newData[i] = this.data[i]; - } - //copy stack 2 - int newDataTop = newData.length; - for (int j = end2; j >= top2 ; j--) { - newData[--newDataTop] = this.data[j]; - } - this.top2 = newDataTop; - this.end2 = newData.length -1; - this.data = newData; - System.out.println("data array resize to " + this.data.length); - } - - @Override - public String toString(){ - StringBuilder sb = new StringBuilder(); - int i = this.end1; - sb.append("stack 1 : "); - for(; i <= this.top1; i++){ - sb.append(data[i].toString()+" "); - } - sb.append(", stack 2 : "); - int j = this.end2; - for(; j >= this.top2; j--){ - sb.append(data[j].toString()+" "); - } - return sb.toString(); - } - - public static void main(String[] args) { - TwoStackInOneArray s = new TwoStackInOneArray(); - s.push1(10); - s.push1(11); - s.push1(12); - s.push1(13); - s.push1(14); - - s.push2(20); - s.push2(21); - s.push2(22); - s.push2(23); - s.push2(24); - - System.out.println(s); - - s.push2(25); - - System.out.println(s); - - s.pop2(); - s.pop2(); - s.pop1(); - s.pop1(); - - System.out.println(s); - - } - -} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code10/BinaryTreeNode.java b/students/769232552/season_one/main/java/season_1/code10/BinaryTreeNode.java deleted file mode 100644 index a7f242c984..0000000000 --- a/students/769232552/season_one/main/java/season_1/code10/BinaryTreeNode.java +++ /dev/null @@ -1,39 +0,0 @@ -package code10; - -/** - * Created by on 2017/5/9. - */ - -public class BinaryTreeNode { - - public T data; - public BinaryTreeNode left; - public BinaryTreeNode right; - - public BinaryTreeNode(T data){ - this.data=data; - } - public T getData() { - return data; - } - public void setData(T data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - -} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code10/BinaryTreeUtil.java b/students/769232552/season_one/main/java/season_1/code10/BinaryTreeUtil.java deleted file mode 100644 index 144758651c..0000000000 --- a/students/769232552/season_one/main/java/season_1/code10/BinaryTreeUtil.java +++ /dev/null @@ -1,125 +0,0 @@ -package code10; - -import code01.BinaryTree; - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -/** - * Created by on 2017/5/9. - */ -public class BinaryTreeUtil { - /** - * 用递归的方式实现对二叉树的前序遍历 - * - * @param root - * @return - */ - public static List preOrderVisit(BinaryTreeNode root) { - List result = new ArrayList(); - preOrder(root,result); - return result; - } - - public static void preOrder(BinaryTreeNode root, List result){ - if(root == null){ - return; - } - result.add(root.getData()); - preOrder(root.getLeft(),result); - preOrder(root.getRight(),result); - } - - /** - * 用递归的方式实现对二叉树的中遍历 - * - * @param root - * @return - */ - public static List inOrderVisit(BinaryTreeNode root) { - List result = new ArrayList(); - inOrder(root,result); - return result; - } - - public static void inOrder(BinaryTreeNode root, List result){ - if(root == null){ - return; - } - inOrder(root.getLeft(),result); - result.add(root.getData()); - inOrder(root.getRight(),result); - } - - /** - * 用递归的方式实现对二叉树的后遍历 - * - * @param root - * @return - */ - public static List postOrderVisit(BinaryTreeNode root) { - List result = new ArrayList(); - postOrder(root,result); - return result; - } - - public static void postOrder(BinaryTreeNode root, List result){ - if(root == null){ - return; - } - postOrder(root.getLeft(),result); - postOrder(root.getRight(),result); - result.add(root.getData()); - } - - /** - * 用非递归的方式实现对二叉树的前序遍历(后序便历类似) - * @param root - * @return - */ - public static List preOrderWithoutRecursion(BinaryTreeNode root) { - - List result = new ArrayList(); - Stack visitStack = new Stack(); - - visitStack.push(root); - - while(!visitStack.isEmpty()){ - BinaryTreeNode node = (BinaryTreeNode) visitStack.pop(); - result.add((T) node.getData()); - if(node.getRight() != null){ - visitStack.push(node.getRight()); - } - if(node.getLeft() != null){ - visitStack.push(node.getLeft()); - } - - } - - return result; - } - /** - * 用非递归的方式实现对二叉树的中序遍历 - */ - public static List inOrderWithoutRecursion(BinaryTreeNode root) { - - List result = new ArrayList(); - - Stack visitStack = new Stack(); - - BinaryTreeNode node = root; - - while(node != null ||!visitStack.isEmpty()){ - while (node != null){ - visitStack.push(node); - node = node.left; - } - BinaryTreeNode pNode = (BinaryTreeNode) visitStack.pop(); - result.add((T) pNode.getData()); - node = pNode.right; - } - return result; - } - -} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/code10/FileList.java b/students/769232552/season_one/main/java/season_1/code10/FileList.java deleted file mode 100644 index 669d8e32e3..0000000000 --- a/students/769232552/season_one/main/java/season_1/code10/FileList.java +++ /dev/null @@ -1,38 +0,0 @@ -package code10; - -import java.io.File; - -/** - * Created by on 2017/5/9. - */ -public class FileList { - - public void list(File file){ - list(file,0); - } - - public void list(File file,int layer){ - showFileName(file,layer); - if(file.isDirectory()){ - File[] files = file.listFiles(); - for(File f : files){ - list(f,layer+1); - } - } - } - - public void showFileName(File file,int i){ - for (int j = 0; j < i; j++) { - System.out.print(" - "); - } - System.out.println(file.getName()); - } - - public static void main(String[] args) { - FileList fileList = new FileList(); - String path = "D:\\dir1"; - File file = new File(path); - fileList.list(file); - } - -} diff --git a/students/769232552/season_one/main/java/season_1/code11/BinarySearchTree.java b/students/769232552/season_one/main/java/season_1/code11/BinarySearchTree.java deleted file mode 100644 index 6b97488945..0000000000 --- a/students/769232552/season_one/main/java/season_1/code11/BinarySearchTree.java +++ /dev/null @@ -1,305 +0,0 @@ -package code11; - -import code10.BinaryTreeNode; - -import java.util.*; - -/** - * Created by yyglider on 2017/5/16. - */ -public class BinarySearchTree { - - BinaryTreeNode root; - - public BinarySearchTree(BinaryTreeNode root) { - this.root = root; - } - - public BinaryTreeNode getRoot() { - return root; - } - - public T findMin() { - if (this.root == null) { - return null; - } - - BinaryTreeNode pNode = this.root; - while (pNode != null && pNode.left != null) { - pNode = pNode.left; - } - return (T) pNode.getData(); - } - - public T findMax() { - if (this.root == null) { - return null; - } - - BinaryTreeNode pNode = this.root; - while (pNode != null && pNode.right != null) { - pNode = pNode.right; - } - return (T) pNode.getData(); - } - - public int height() { - if (this.root == null) { - return 0; - } - return getHeight(this.root); - } - - private int getHeight(BinaryTreeNode node) { - if (node == null) { - return 0; - } - int left = getHeight(node.left); - int right = getHeight(node.right); - return 1 + Math.max(left, right); - } - - public int size() { - if (this.root == null) { - return 0; - } - return getSize(this.root); - } - - private int getSize(BinaryTreeNode node) { - if (node == null) { - return 0; - } - int left = getSize(node.left); - int right = getSize(node.right); - return 1 + left + right; - } - - - public void remove(T e) { - if (this.root == null || e == null) { - return; - } - - if (e.equals(this.root.getData())) { - this.root = null; - return; - } - - BinaryTreeNode pNode = this.root; //记录待删除的元素位置 - BinaryTreeNode pPreNode = this.root; //记录待删除的元素的前一个位置 - - //find e - while (pNode != null) { - if (e.compareTo(pNode.getData()) > 0) { - pPreNode = pNode; - pNode = pNode.right; - } else if (e.compareTo(pNode.getData()) < 0) { - pPreNode = pNode; - pNode = pNode.left; - } else { - break; - } - - } - - //delete e - if (pNode != null && e.equals(pNode.getData())) { - BinaryTreeNode node; - BinaryTreeNode preNode; - - //如果其结点只包含左子树,或者右子树的话,此时直接删除该结点 - boolean hasOneChild = ((pNode.right != null && pNode.left == null) && (pNode.right == null && pNode.left != null)); - if (hasOneChild) { - if (pNode.left != null) { - node = pNode.left; - pNode.setData(node.getData()); - pNode.left = null; - return; - } - node = pNode.right; - pNode.setData(node.getData()); - pNode.right = null; - - } - - //如果其结点既包含左子树,也包含右子树 - //找到其右子树的的最小元素(实际上用左子树最大元素代替也是可以的) - if (pNode.right != null && pNode.left != null) { - preNode = pNode; - node = pNode.right; - while (node != null && node.left != null) { - preNode = node; - node = node.left; - } - //如果是叶子节点 - if (node.right == null) { - pNode.setData(node.getData()); - - preNode.right = null; - return; - } - - pNode.setData(node.getData()); - - //非叶子结点,用其右孩子的元素来替代 - preNode = node; - node = node.right; - preNode.setData(node.getData()); - preNode.right = null; - return; - } - - //其本身就是叶子节点 - pPreNode = null; - - } - } - - //层序遍历,使用队列的方式 - public List levelVisit() { - if (this.root == null) { - return null; - } - List results = new ArrayList(); - Queue queue = new ArrayDeque(); - - queue.add(this.root); - - while (!queue.isEmpty()) { - BinaryTreeNode node = queue.poll(); - results.add((T) node.getData()); - - if (node.left != null) { - queue.add(node.left); - } - if (node.right != null) { - queue.add(node.right); - } - } - return results; - } - - - /** - * 验证是否是二叉查找树 - */ - public boolean isValid() { - if (this.root == null) { - return true; - } - return checkValid(this.root); - } - - public boolean checkValid(BinaryTreeNode node) { - - if (node == null) { - return true; - } - T nodeData = (T) node.getData(); - boolean currentFlag = (((node.left != null && nodeData.compareTo(node.left.getData()) > 0) - || node.left == null) - && ((node.right != null && nodeData.compareTo(node.right.getData()) < 0 - || node.right == null))); - boolean leftFlag = checkValid(node.left); - boolean rightFlag = checkValid(node.right); - return currentFlag && leftFlag && rightFlag; - } - - - - /** - * 最近公共祖先(LCA)问题 - * 基本思想为:从树根开始,该节点的值为t, - * 如果t大于t1和t2,说明t1和t2都位于t的左侧,所以它们的共同祖先必定在t的左子树中,从t.left开始搜索; - * 如果t小于t1和t2,说明t1和t2都位于t的右侧,那么从t.right开始搜索; - * 如果t1<=t<= t2,说明t1和t2位于t的两侧(或t=t1,或t=t2),那么该节点t为公共祖先。 - */ - public T getLowestCommonAncestor(T n1, T n2) { - if (this.root == null || n1 == null || n2 == null) { - return null; - } - - BinaryTreeNode pNode = this.root; - - while (pNode != null) { - if (n1.compareTo(pNode.getData()) > 0 && n2.compareTo(pNode.getData()) > 0) { - pNode = pNode.right; - } else if (n1.compareTo(pNode.getData()) < 0 && n2.compareTo(pNode.getData()) < 0) { - pNode = pNode.left; - } else { - return (T) pNode.getData(); - } - } - return null; - } - - - /** - * 两个节点之间的最短路径 - */ - public List getNodesBetween(T n1, T n2) { - - if (this.root == null || n1 == null || n2 == null) { - return null; - } - - final List list1 = new ArrayList(); - List list2 = new ArrayList(); - BinaryTreeNode pNode1 = this.root; - BinaryTreeNode pNode2 = this.root; - - //find n1 path - while (pNode1 != null) { - list1.add((T) pNode1.getData()); - - if (n1.compareTo(pNode1.getData()) > 0) { - pNode1 = pNode1.right; - } else if (n1.compareTo(pNode1.getData()) < 0) { - pNode1 = pNode1.left; - } else { - break; - } - } - //find n2 path - while (pNode2 != null) { - list2.add((T) pNode2.getData()); - - if (n2.compareTo(pNode2.getData()) > 0) { - pNode2 = pNode2.right; - } else if (n2.compareTo(pNode2.getData()) < 0) { - pNode2 = pNode2.left; - } else { - break; - } - } - - //join path1 and path2 - int i = 0, j = 0; - T first = null; - List results = new ArrayList(); - - while (i < list1.size() && i < list2.size()) { - if (list1.get(i) != list2.get(i)) { - break; - } - i++; - } - if (i > 0) { - i--; - } - int resultsLen = list1.size() + list2.size() - 2 * i - 1; - for (int k = list1.size() - 1; k > i; k--) { - results.add(list1.get(k)); - } - for (int k = i; k < list2.size(); k++) { - results.add(list2.get(k)); - } - - Collections.reverse(list1); - - return results; - } - - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/attr/AttributeInfo.java b/students/769232552/season_one/main/java/season_1/mini_jvm/attr/AttributeInfo.java deleted file mode 100644 index 386305bbda..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/attr/AttributeInfo.java +++ /dev/null @@ -1,19 +0,0 @@ -package mini_jvm.attr; - -public abstract class AttributeInfo { - public static final String CODE = "Code"; - public static final String CONST_VALUE = "ConstantValue"; - public static final String EXCEPTIONS = "Exceptions"; - public static final String LINE_NUM_TABLE = "LineNumberTable"; - public static final String LOCAL_VAR_TABLE = "LocalVariableTable"; - public static final String STACK_MAP_TABLE = "StackMapTable"; - int attrNameIndex; - int attrLen ; - public AttributeInfo(int attrNameIndex, int attrLen) { - - this.attrNameIndex = attrNameIndex; - this.attrLen = attrLen; - } - - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/attr/CodeAttr.java b/students/769232552/season_one/main/java/season_1/mini_jvm/attr/CodeAttr.java deleted file mode 100644 index 4817c1eee8..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/attr/CodeAttr.java +++ /dev/null @@ -1,119 +0,0 @@ -package mini_jvm.attr; - - -import mini_jvm.clz.ClassFile; -import mini_jvm.cmd.ByteCodeCommand; -import mini_jvm.cmd.CommandParser; -import mini_jvm.constant.ConstantPool; -import mini_jvm.loader.ByteCodeIterator; - -public class CodeAttr extends AttributeInfo { - private int maxStack ; - private int maxLocals ; - private int codeLen ; - private String code; - public String getCode() { - return code; - } - - private ByteCodeCommand[] cmds ; - public ByteCodeCommand[] getCmds() { - return cmds; - } - - private LineNumberTable lineNumTable; - private LocalVariableTable localVarTable; - private StackMapTable stackMapTable; - - public CodeAttr(int attrNameIndex, int attrLen, int maxStack, int maxLocals, int codeLen,String code ,ByteCodeCommand[] cmds) { - super(attrNameIndex, attrLen); - this.maxStack = maxStack; - this.maxLocals = maxLocals; - this.codeLen = codeLen; - this.code = code; - this.cmds = cmds; - } - - public void setLineNumberTable(LineNumberTable t) { - this.lineNumTable = t; - } - - public void setLocalVariableTable(LocalVariableTable t) { - this.localVarTable = t; - } - - - public static CodeAttr parse(ClassFile clzFile, ByteCodeIterator iter){ - - int attrNameIndex = iter.nextU2ToInt(); - int attrLen = iter.nextU4ToInt(); - int maxStack = iter.nextU2ToInt(); - int maxLocals = iter.nextU2ToInt(); - int codeLen = iter.nextU4ToInt(); - - String code = iter.nextUxToHexString(codeLen); - - ByteCodeCommand[] cmds = CommandParser.parse(clzFile,code); - CodeAttr codeAttr = new CodeAttr(attrNameIndex,attrLen, maxStack,maxLocals,codeLen,code,cmds); - - int exceptionTableLen = iter.nextU2ToInt(); - //TODO 处理exception - if(exceptionTableLen>0){ - String exTable = iter.nextUxToHexString(exceptionTableLen); - System.out.println("Encountered exception table , just ignore it :" + exTable); - - } - - - int subAttrCount = iter.nextU2ToInt(); - - for(int x=1; x<=subAttrCount; x++){ - int subAttrIndex = iter.nextU2ToInt(); - String subAttrName = clzFile.getConstantPool().getUTF8String(subAttrIndex); - - //已经向前移动了U2, 现在退回去。 - iter.back(2); - //line item table - if(AttributeInfo.LINE_NUM_TABLE.equalsIgnoreCase(subAttrName)){ - LineNumberTable t = LineNumberTable.parse(iter); - codeAttr.setLineNumberTable(t); - } - else if(AttributeInfo.LOCAL_VAR_TABLE.equalsIgnoreCase(subAttrName)){ - LocalVariableTable t = LocalVariableTable.parse(iter); - codeAttr.setLocalVariableTable(t); - } - else if (AttributeInfo.STACK_MAP_TABLE.equalsIgnoreCase(subAttrName)){ - StackMapTable t = StackMapTable.parse(iter); - codeAttr.setStackMapTable(t); - } - else{ - throw new RuntimeException("Need code to process " + subAttrName); - } - - - } - return codeAttr; - } - - - public String toString(ConstantPool pool){ - StringBuilder buffer = new StringBuilder(); - //buffer.append("Code:").append(code).append("\n"); - /*for(int i=0;i items = new ArrayList(); - - public void addLineNumberItem(LineNumberItem item){ - this.items.add(item); - } - - public LineNumberTable(int attrNameIndex, int attrLen) { - super(attrNameIndex, attrLen); - } - - public static LineNumberTable parse(ByteCodeIterator iter){ - - int index = iter.nextU2ToInt(); - int len = iter.nextU4ToInt(); - - LineNumberTable table = new LineNumberTable(index,len); - - int itemLen = iter.nextU2ToInt(); - - for(int i=1; i<=itemLen; i++){ - LineNumberItem item = new LineNumberItem(); - item.setStartPC(iter.nextU2ToInt()); - item.setLineNum(iter.nextU2ToInt()); - table.addLineNumberItem(item); - } - return table; - } - - public String toString(){ - StringBuilder buffer = new StringBuilder(); - buffer.append("Line Number Table:\n"); - for(LineNumberItem item : items){ - buffer.append("startPC:"+item.getStartPC()).append(","); - buffer.append("lineNum:"+item.getLineNum()).append("\n"); - } - buffer.append("\n"); - return buffer.toString(); - - } - - private static class LineNumberItem{ - int startPC; - int lineNum; - public int getStartPC() { - return startPC; - } - public void setStartPC(int startPC) { - this.startPC = startPC; - } - public int getLineNum() { - return lineNum; - } - public void setLineNum(int lineNum) { - this.lineNum = lineNum; - } - } - - - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/attr/LocalVariableTable.java b/students/769232552/season_one/main/java/season_1/mini_jvm/attr/LocalVariableTable.java deleted file mode 100644 index 946ef5fa5d..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/attr/LocalVariableTable.java +++ /dev/null @@ -1,98 +0,0 @@ -package mini_jvm.attr; - - -import mini_jvm.constant.ConstantPool; -import mini_jvm.loader.ByteCodeIterator; - -import java.util.ArrayList; -import java.util.List; - - - -public class LocalVariableTable extends AttributeInfo{ - - List items = new ArrayList(); - - public LocalVariableTable(int attrNameIndex, int attrLen) { - super(attrNameIndex, attrLen); - } - - - private void addLocalVariableItem(LocalVariableItem item) { - this.items.add(item); - } - - public static LocalVariableTable parse(ByteCodeIterator iter){ - - int index = iter.nextU2ToInt(); - int len = iter.nextU4ToInt(); - - LocalVariableTable table = new LocalVariableTable(index,len); - - int itemLen = iter.nextU2ToInt(); - - for(int i=1; i<=itemLen; i++){ - LocalVariableItem item = new LocalVariableItem(); - item.setStartPC(iter.nextU2ToInt()); - item.setLength(iter.nextU2ToInt()); - item.setNameIndex(iter.nextU2ToInt()); - item.setDescIndex(iter.nextU2ToInt()); - item.setIndex(iter.nextU2ToInt()); - table.addLocalVariableItem(item); - } - return table; - } - - - public String toString(ConstantPool pool){ - StringBuilder buffer = new StringBuilder(); - buffer.append("Local Variable Table:\n"); - for(LocalVariableItem item : items){ - buffer.append("startPC:"+item.getStartPC()).append(","); - buffer.append("name:"+pool.getUTF8String(item.getNameIndex())).append(","); - buffer.append("desc:"+pool.getUTF8String(item.getDescIndex())).append(","); - buffer.append("slotIndex:"+ item.getIndex()).append("\n"); - } - buffer.append("\n"); - return buffer.toString(); - } - - private static class LocalVariableItem { - private int startPC; - private int length; - private int nameIndex; - private int descIndex; - private int index; - public int getStartPC() { - return startPC; - } - public void setStartPC(int startPC) { - this.startPC = startPC; - } - public int getLength() { - return length; - } - public void setLength(int length) { - this.length = length; - } - public int getNameIndex() { - return nameIndex; - } - public void setNameIndex(int nameIndex) { - this.nameIndex = nameIndex; - } - public int getDescIndex() { - return descIndex; - } - public void setDescIndex(int descIndex) { - this.descIndex = descIndex; - } - public int getIndex() { - return index; - } - public void setIndex(int index) { - this.index = index; - } - } - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/attr/StackMapTable.java b/students/769232552/season_one/main/java/season_1/mini_jvm/attr/StackMapTable.java deleted file mode 100644 index 65b3b24c82..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/attr/StackMapTable.java +++ /dev/null @@ -1,30 +0,0 @@ -package mini_jvm.attr; - - -import mini_jvm.loader.ByteCodeIterator; - -public class StackMapTable extends AttributeInfo{ - - private String originalCode; - - public StackMapTable(int attrNameIndex, int attrLen) { - super(attrNameIndex, attrLen); - } - - public static StackMapTable parse(ByteCodeIterator iter){ - int index = iter.nextU2ToInt(); - int len = iter.nextU4ToInt(); - StackMapTable t = new StackMapTable(index,len); - - //后面的StackMapTable太过复杂, 不再处理, 只把原始的代码读进来保存 - String code = iter.nextUxToHexString(len); - t.setOriginalCode(code); - - return t; - } - - private void setOriginalCode(String code) { - this.originalCode = code; - - } -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/clz/AccessFlag.java b/students/769232552/season_one/main/java/season_1/mini_jvm/clz/AccessFlag.java deleted file mode 100644 index b6717402da..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/clz/AccessFlag.java +++ /dev/null @@ -1,25 +0,0 @@ -package mini_jvm.clz; - -public class AccessFlag { - private int flagValue; - - public AccessFlag(int value) { - this.flagValue = value; - } - - public int getFlagValue() { - return flagValue; - } - - public void setFlagValue(int flag) { - this.flagValue = flag; - } - - public boolean isPublicClass(){ - return (this.flagValue & 0x0001) != 0; - } - public boolean isFinalClass(){ - return (this.flagValue & 0x0010) != 0; - } - -} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/clz/ClassFile.java b/students/769232552/season_one/main/java/season_1/mini_jvm/clz/ClassFile.java deleted file mode 100644 index d0884893aa..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/clz/ClassFile.java +++ /dev/null @@ -1,121 +0,0 @@ -package mini_jvm.clz; - -import mini_jvm.constant.ClassInfo; -import mini_jvm.constant.ConstantPool; -import mini_jvm.field.Field; -import mini_jvm.method.Method; - -import java.util.ArrayList; -import java.util.List; - -public class ClassFile { - - private int minorVersion; - private int majorVersion; - - private AccessFlag accessFlag; - private ClassIndex clzIndex; - private ConstantPool pool; - private List fields = new ArrayList(); - private List methods = new ArrayList(); - - public ClassIndex getClzIndex() { - return clzIndex; - } - public AccessFlag getAccessFlag() { - return accessFlag; - } - public void setAccessFlag(AccessFlag accessFlag) { - this.accessFlag = accessFlag; - } - - - - public ConstantPool getConstantPool() { - return pool; - } - public int getMinorVersion() { - return minorVersion; - } - public void setMinorVersion(int minorVersion) { - this.minorVersion = minorVersion; - } - public int getMajorVersion() { - return majorVersion; - } - public void setMajorVersion(int majorVersion) { - this.majorVersion = majorVersion; - } - public void setConstPool(ConstantPool pool) { - this.pool = pool; - - } - public void setClassIndex(ClassIndex clzIndex) { - this.clzIndex = clzIndex; - } - - public void setFields(List f){ - this.fields = f; - } - public List getFields(){ - return this.fields; - } - public void addField(Field f){this.fields.add(f);} - - - public void setMethods(List m){ - this.methods = m; - } - public List getMethods() { - return methods; - } - public void addMethod(Method m){this.methods.add(m);} - - - - public void print(){ - if(this.accessFlag.isPublicClass()){ - System.out.println("Access flag : public "); - } - System.out.println("Class Name:"+ getClassName()); - System.out.println("Super Class Name:"+ getSuperClassName()); - } - - private String getClassName(){ - int thisClassIndex = this.clzIndex.getThisClassIndex(); - ClassInfo thisClass = (ClassInfo)this.getConstantPool().getConstantInfo(thisClassIndex); - return thisClass.getClassName(); - } - public String getSuperClassName(){ - ClassInfo superClass = (ClassInfo)this.getConstantPool().getConstantInfo(this.clzIndex.getSuperClassIndex()); - return superClass.getClassName(); - } - - public Method getMethod(String methodName, String paramAndReturnType){ - - for(Method m :methods){ - - int nameIndex = m.getNameIndex(); - int descriptionIndex = m.getDescriptorIndex(); - - String name = this.getConstantPool().getUTF8String(nameIndex); - String desc = this.getConstantPool().getUTF8String(descriptionIndex); - if(name.equals(methodName) && desc.equals(paramAndReturnType)){ - return m; - } - } - return null; - } - public Method getMainMethod(){ - for(Method m :methods){ - int nameIndex = m.getNameIndex(); - int descIndex = m.getDescriptorIndex(); - String name = this.getConstantPool().getUTF8String(nameIndex); - String desc = this.getConstantPool().getUTF8String(descIndex); - if(name.equals("main") && desc.equals("([Ljava/lang/String;)V")){ - return m; - } - } - return null; - } -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/clz/ClassIndex.java b/students/769232552/season_one/main/java/season_1/mini_jvm/clz/ClassIndex.java deleted file mode 100644 index dcc59908f0..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/clz/ClassIndex.java +++ /dev/null @@ -1,19 +0,0 @@ -package mini_jvm.clz; - -public class ClassIndex { - private int thisClassIndex; - private int superClassIndex; - - public int getThisClassIndex() { - return thisClassIndex; - } - public void setThisClassIndex(int thisClassIndex) { - this.thisClassIndex = thisClassIndex; - } - public int getSuperClassIndex() { - return superClassIndex; - } - public void setSuperClassIndex(int superClassIndex) { - this.superClassIndex = superClassIndex; - } -} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/BiPushCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/BiPushCmd.java deleted file mode 100644 index bc2be8f9f8..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/BiPushCmd.java +++ /dev/null @@ -1,29 +0,0 @@ -package mini_jvm.cmd; - - -import mini_jvm.clz.ClassFile; -import mini_jvm.engine.ExecutionResult; -import mini_jvm.engine.Heap; -import mini_jvm.engine.JavaObject; -import mini_jvm.engine.StackFrame; - -public class BiPushCmd extends OneOperandCmd { - - public BiPushCmd(ClassFile clzFile, String opCode) { - super(clzFile,opCode); - } - - @Override - public String toString() { - - return this.getOffset()+":"+ this.getOpCode()+" " + this.getReadableCodeText() + " " + this.getOperand(); - } - public void execute(StackFrame frame, ExecutionResult result){ - int value = this.getOperand(); - JavaObject jo = Heap.getInstance().newInt(value); - frame.getOprandStack().push(jo); - - } - - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/ByteCodeCommand.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/ByteCodeCommand.java deleted file mode 100644 index cc877430c0..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/ByteCodeCommand.java +++ /dev/null @@ -1,158 +0,0 @@ -package mini_jvm.cmd; - -import mini_jvm.clz.ClassFile; -import mini_jvm.constant.ConstantInfo; -import mini_jvm.constant.ConstantPool; -import mini_jvm.engine.ExecutionResult; -import mini_jvm.engine.StackFrame; - -import java.util.HashMap; -import java.util.Map; - - - - -public abstract class ByteCodeCommand { - - String opCode; - ClassFile clzFile; - private int offset; - - public static final String aconst_null = "01"; - public static final String new_object = "BB"; - public static final String lstore = "37"; - public static final String invokespecial = "B7"; - public static final String invokevirtual = "B6"; - public static final String getfield = "B4"; - public static final String putfield = "B5"; - public static final String getstatic = "B2"; - public static final String ldc = "12"; - public static final String dup = "59"; - public static final String bipush = "10"; - public static final String aload_0 = "2A"; - public static final String aload_1 = "2B"; - public static final String aload_2 = "2C"; - public static final String iload = "15"; - public static final String iload_1 = "1B"; - public static final String iload_2 = "1C"; - public static final String iload_3 = "1D"; - public static final String fload_3 = "25"; - - public static final String voidreturn = "B1"; - public static final String ireturn = "AC"; - public static final String freturn = "AE"; - - public static final String astore_1 = "4C"; - public static final String if_icmp_ge = "A2"; - public static final String if_icmple = "A4"; - public static final String goto_no_condition = "A7"; - public static final String iconst_0 = "03"; - public static final String iconst_1 = "04"; - public static final String istore_1 = "3C"; - public static final String istore_2 = "3D"; - public static final String iadd = "60"; - public static final String iinc = "84"; - private static Map codeMap = new HashMap(); - - static{ - codeMap.put("01", "aconst_null"); - - codeMap.put("A2", "if_icmp_ge"); - codeMap.put("A4", "if_icmple"); - codeMap.put("A7", "goto"); - - codeMap.put("BB", "new"); - codeMap.put("37", "lstore"); - codeMap.put("B7", "invokespecial"); - codeMap.put("B6", "invokevirtual"); - codeMap.put("B4", "getfield"); - codeMap.put("B5", "putfield"); - codeMap.put("B2", "getstatic"); - - codeMap.put("2A", "aload_0"); - codeMap.put("2B", "aload_1"); - codeMap.put("2C", "aload_2"); - - codeMap.put("10", "bipush"); - codeMap.put("15", "iload"); - codeMap.put("1A", "iload_0"); - codeMap.put("1B", "iload_1"); - codeMap.put("1C", "iload_2"); - codeMap.put("1D", "iload_3"); - - codeMap.put("25", "fload_3"); - - codeMap.put("1E", "lload_0"); - - codeMap.put("24", "fload_2"); - codeMap.put("4C", "astore_1"); - - codeMap.put("A2", "if_icmp_ge"); - codeMap.put("A4", "if_icmple"); - - codeMap.put("A7", "goto"); - - codeMap.put("B1", "return"); - codeMap.put("AC", "ireturn"); - codeMap.put("AE", "freturn"); - - codeMap.put("03", "iconst_0"); - codeMap.put("04", "iconst_1"); - - codeMap.put("3C", "istore_1"); - codeMap.put("3D", "istore_2"); - - codeMap.put("59", "dup"); - - codeMap.put("60", "iadd"); - codeMap.put("84", "iinc"); - - codeMap.put("12", "ldc"); - } - - - - - - protected ByteCodeCommand(ClassFile clzFile, String opCode){ - this.clzFile = clzFile; - this.opCode = opCode; - } - - protected ClassFile getClassFile() { - return clzFile; - } - - public int getOffset() { - return offset; - } - - public void setOffset(int offset) { - this.offset = offset; - } - protected ConstantInfo getConstantInfo(int index){ - return this.getClassFile().getConstantPool().getConstantInfo(index); - } - - protected ConstantPool getConstantPool(){ - return this.getClassFile().getConstantPool(); - } - - - - public String getOpCode() { - return opCode; - } - - public abstract int getLength(); - - public String getReadableCodeText(){ - String txt = codeMap.get(opCode); - if(txt == null){ - return opCode; - } - return txt; - } - - public abstract void execute(StackFrame frame, ExecutionResult result); -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/CommandParser.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/CommandParser.java deleted file mode 100644 index 3effa7ae5d..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/CommandParser.java +++ /dev/null @@ -1,149 +0,0 @@ -package mini_jvm.cmd; - -import mini_jvm.clz.ClassFile; - -import java.util.ArrayList; -import java.util.List; - - -public class CommandParser { - - - - public static ByteCodeCommand[] parse(ClassFile clzFile, String codes) { - - if ((codes == null) || (codes.length() == 0) || (codes.length() % 2) != 0) { - throw new RuntimeException("the orignal code is not correct"); - - } - - codes = codes.toUpperCase(); - - CommandIterator iter = new CommandIterator(codes); - List cmds = new ArrayList(); - - while (iter.hasNext()) { - String opCode = iter.next2CharAsString(); - - if (ByteCodeCommand.new_object.equals(opCode)) { - NewObjectCmd cmd = new NewObjectCmd(clzFile, opCode); - - cmd.setOprand1(iter.next2CharAsInt()); - cmd.setOprand2(iter.next2CharAsInt()); - - cmds.add(cmd); - } else if (ByteCodeCommand.invokespecial.equals(opCode)) { - InvokeSpecialCmd cmd = new InvokeSpecialCmd(clzFile, opCode); - cmd.setOprand1(iter.next2CharAsInt()); - cmd.setOprand2(iter.next2CharAsInt()); - // System.out.println( cmd.toString(clzFile.getConstPool())); - cmds.add(cmd); - } else if (ByteCodeCommand.invokevirtual.equals(opCode)) { - InvokeVirtualCmd cmd = new InvokeVirtualCmd(clzFile, opCode); - cmd.setOprand1(iter.next2CharAsInt()); - cmd.setOprand2(iter.next2CharAsInt()); - - cmds.add(cmd); - } else if (ByteCodeCommand.getfield.equals(opCode)) { - GetFieldCmd cmd = new GetFieldCmd(clzFile, opCode); - cmd.setOprand1(iter.next2CharAsInt()); - cmd.setOprand2(iter.next2CharAsInt()); - cmds.add(cmd); - } else if (ByteCodeCommand.getstatic.equals(opCode)) { - GetStaticFieldCmd cmd = new GetStaticFieldCmd(clzFile, opCode); - cmd.setOprand1(iter.next2CharAsInt()); - cmd.setOprand2(iter.next2CharAsInt()); - cmds.add(cmd); - } else if (ByteCodeCommand.putfield.equals(opCode)) { - PutFieldCmd cmd = new PutFieldCmd(clzFile, opCode); - cmd.setOprand1(iter.next2CharAsInt()); - cmd.setOprand2(iter.next2CharAsInt()); - cmds.add(cmd); - } else if (ByteCodeCommand.ldc.equals(opCode)) { - LdcCmd cmd = new LdcCmd(clzFile, opCode); - cmd.setOperand(iter.next2CharAsInt()); - cmds.add(cmd); - } else if (ByteCodeCommand.bipush.equals(opCode)) { - BiPushCmd cmd = new BiPushCmd(clzFile, opCode); - cmd.setOperand(iter.next2CharAsInt()); - cmds.add(cmd); - }else if(ByteCodeCommand.if_icmp_ge.equals(opCode) - || ByteCodeCommand.if_icmple.equals(opCode) - || ByteCodeCommand.goto_no_condition.equals(opCode)){ - ComparisonCmd cmd = new ComparisonCmd(clzFile,opCode); - cmd.setOprand1(iter.next2CharAsInt()); - cmd.setOprand2(iter.next2CharAsInt()); - cmds.add(cmd); - } else if(ByteCodeCommand.iinc.equals(opCode)){ - IncrementCmd cmd = new IncrementCmd(clzFile,opCode); - cmd.setOprand1(iter.next2CharAsInt()); - cmd.setOprand2(iter.next2CharAsInt()); - cmds.add(cmd); - } - else if (ByteCodeCommand.dup.equals(opCode) - || ByteCodeCommand.aload_0.equals(opCode) - || ByteCodeCommand.aload_1.equals(opCode) - || ByteCodeCommand.aload_2.equals(opCode) - || ByteCodeCommand.iload_1.equals(opCode) - || ByteCodeCommand.iload_2.equals(opCode) - || ByteCodeCommand.iload_3.equals(opCode) - || ByteCodeCommand.fload_3.equals(opCode) - || ByteCodeCommand.iconst_0.equals(opCode) - || ByteCodeCommand.iconst_1.equals(opCode) - || ByteCodeCommand.istore_1.equals(opCode) - || ByteCodeCommand.istore_2.equals(opCode) - || ByteCodeCommand.voidreturn.equals(opCode) - || ByteCodeCommand.iadd.equals(opCode) - || ByteCodeCommand.astore_1.equals(opCode) - || ByteCodeCommand.ireturn.equals(opCode)) { - - NoOperandCmd cmd = new NoOperandCmd(clzFile, opCode); - cmds.add(cmd); - } else { - throw new RuntimeException("Sorry, the java instruction " + opCode + " has not been implemented"); - } - - } - - calcuateOffset(cmds); - - ByteCodeCommand[] result = new ByteCodeCommand[cmds.size()]; - cmds.toArray(result); - return result; - } - - private static void calcuateOffset(List cmds) { - - int offset = 0; - for (ByteCodeCommand cmd : cmds) { - cmd.setOffset(offset); - offset += cmd.getLength(); - } - - } - - private static class CommandIterator { - String codes = null; - int pos = 0; - - CommandIterator(String codes) { - this.codes = codes; - } - - public boolean hasNext() { - return pos < this.codes.length(); - } - - public String next2CharAsString() { - String result = codes.substring(pos, pos + 2); - pos += 2; - return result; - } - - public int next2CharAsInt() { - String s = this.next2CharAsString(); - return Integer.valueOf(s, 16).intValue(); - } - - } -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/ComparisonCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/ComparisonCmd.java deleted file mode 100644 index e195966a83..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/ComparisonCmd.java +++ /dev/null @@ -1,79 +0,0 @@ -package mini_jvm.cmd; - - -import mini_jvm.clz.ClassFile; -import mini_jvm.engine.ExecutionResult; -import mini_jvm.engine.JavaObject; -import mini_jvm.engine.StackFrame; - -public class ComparisonCmd extends TwoOperandCmd { - - protected ComparisonCmd(ClassFile clzFile, String opCode) { - super(clzFile, opCode); - - } - - - @Override - public void execute(StackFrame frame, ExecutionResult result) { - - if(ByteCodeCommand.if_icmp_ge.equals(this.getOpCode())){ - //注意次序 - JavaObject jo2 = frame.getOprandStack().pop(); - JavaObject jo1 = frame.getOprandStack().pop(); - - if(jo1.getIntValue() >= jo2.getIntValue()){ - - this.setJumpResult(result); - - } - - } else if(ByteCodeCommand.if_icmple.equals(this.getOpCode())){ - //注意次序 - JavaObject jo2 = frame.getOprandStack().pop(); - JavaObject jo1 = frame.getOprandStack().pop(); - - if(jo1.getIntValue() <= jo2.getIntValue()){ - this.setJumpResult(result); - } - - } else if(ByteCodeCommand.goto_no_condition.equals(this.getOpCode())){ - this.setJumpResult(result); - - } else{ - throw new RuntimeException(this.getOpCode() + "has not been implemented"); - } - - - - - } - - private int getOffsetFromStartCmd(){ - //If the comparison succeeds, the unsigned branchbyte1 and branchbyte2 - //are used to construct a signed 16-bit offset, where the offset is calculated - //to be (branchbyte1 << 8) | branchbyte2. Execution then proceeds at that - //offset from the address of the opcode of this if_icmp instruction - - - int index1 = this.getOprand1(); - int index2 = this.getOprand2(); - short offsetFromCurrent = (short)(index1 << 8 | index2); - return this.getOffset() + offsetFromCurrent ; - } - private void setJumpResult(ExecutionResult result){ - - int offsetFromStartCmd = this.getOffsetFromStartCmd(); - - result.setNextAction(ExecutionResult.JUMP); - result.setNextCmdOffset(offsetFromStartCmd); - } - - @Override - public String toString() { - int index = this.getIndex(); - String text = this.getReadableCodeText(); - return this.getOffset()+":"+ this.getOpCode() + " "+text + " " + this.getOffsetFromStartCmd(); - } - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/GetFieldCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/GetFieldCmd.java deleted file mode 100644 index b84aa9e15e..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/GetFieldCmd.java +++ /dev/null @@ -1,37 +0,0 @@ -package mini_jvm.cmd; - - -import mini_jvm.clz.ClassFile; -import mini_jvm.constant.FieldRefInfo; -import mini_jvm.engine.ExecutionResult; -import mini_jvm.engine.JavaObject; -import mini_jvm.engine.StackFrame; - -public class GetFieldCmd extends TwoOperandCmd { - - public GetFieldCmd(ClassFile clzFile, String opCode) { - super(clzFile,opCode); - } - - @Override - public String toString() { - - return super.getOperandAsField(); - } - - @Override - public void execute(StackFrame frame, ExecutionResult result) { - - FieldRefInfo fieldRef = (FieldRefInfo)this.getConstantInfo(this.getIndex()); - String fieldName = fieldRef.getFieldName(); - JavaObject jo = frame.getOprandStack().pop(); - JavaObject fieldValue = jo.getFieldValue(fieldName); - - frame.getOprandStack().push(fieldValue); - - - - } - - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/GetStaticFieldCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/GetStaticFieldCmd.java deleted file mode 100644 index a1696b7ffd..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/GetStaticFieldCmd.java +++ /dev/null @@ -1,39 +0,0 @@ -package mini_jvm.cmd; - - -import mini_jvm.clz.ClassFile; -import mini_jvm.constant.FieldRefInfo; -import mini_jvm.engine.ExecutionResult; -import mini_jvm.engine.Heap; -import mini_jvm.engine.JavaObject; -import mini_jvm.engine.StackFrame; - -public class GetStaticFieldCmd extends TwoOperandCmd { - - public GetStaticFieldCmd(ClassFile clzFile, String opCode) { - super(clzFile,opCode); - - } - - @Override - public String toString() { - - return super.getOperandAsField(); - } - - @Override - public void execute(StackFrame frame, ExecutionResult result) { - FieldRefInfo info = (FieldRefInfo)this.getConstantInfo(this.getIndex()); - String className = info.getClassName(); - String fieldName = info.getFieldName(); - String fieldType = info.getFieldType(); - - if("java/lang/System".equals(className) - && "out".equals(fieldName) - && "Ljava/io/PrintStream;".equals(fieldType)){ - JavaObject jo = Heap.getInstance().newObject(className); - frame.getOprandStack().push(jo); - } - //TODO 处理非System.out的情况 - } -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/IncrementCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/IncrementCmd.java deleted file mode 100644 index 3afe4df883..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/IncrementCmd.java +++ /dev/null @@ -1,39 +0,0 @@ -package mini_jvm.cmd; - - -import mini_jvm.clz.ClassFile; -import mini_jvm.engine.ExecutionResult; -import mini_jvm.engine.Heap; -import mini_jvm.engine.JavaObject; -import mini_jvm.engine.StackFrame; - -public class IncrementCmd extends TwoOperandCmd { - - public IncrementCmd(ClassFile clzFile, String opCode) { - super(clzFile, opCode); - - } - - @Override - public String toString() { - - return this.getOffset()+":"+this.getOpCode()+ " " +this.getReadableCodeText(); - } - - @Override - public void execute(StackFrame frame, ExecutionResult result) { - - int index = this.getOprand1(); - - int constValue = this.getOprand2(); - - int currentValue = frame.getLocalVariableValue(index).getIntValue(); - - JavaObject jo = Heap.getInstance().newInt(constValue+currentValue); - - frame.setLocalVariableValue(index, jo); - - - } - -} \ No newline at end of file diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/InvokeSpecialCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/InvokeSpecialCmd.java deleted file mode 100644 index ae0cd08d9b..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/InvokeSpecialCmd.java +++ /dev/null @@ -1,46 +0,0 @@ -package mini_jvm.cmd; - - -import mini_jvm.clz.ClassFile; -import mini_jvm.constant.MethodRefInfo; -import mini_jvm.engine.ExecutionResult; -import mini_jvm.engine.MethodArea; -import mini_jvm.engine.StackFrame; -import mini_jvm.method.Method; - -public class InvokeSpecialCmd extends TwoOperandCmd { - - public InvokeSpecialCmd(ClassFile clzFile, String opCode) { - super(clzFile,opCode); - - } - - @Override - public String toString() { - - return super.getOperandAsMethod(); - } - @Override - public void execute(StackFrame frame, ExecutionResult result) { - - - MethodRefInfo methodRefInfo = (MethodRefInfo)this.getConstantInfo(this.getIndex()); - - // 我们不用实现jang.lang.Object 的init方法 - if(methodRefInfo.getClassName().equals("java/lang/Object") - && methodRefInfo.getMethodName().equals("")){ - return ; - - } - Method nextMethod = MethodArea.getInstance().getMethod(methodRefInfo); - - - result.setNextAction(ExecutionResult.PAUSE_AND_RUN_NEW_FRAME); - result.setNextMethod(nextMethod); - - - - } - - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/InvokeVirtualCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/InvokeVirtualCmd.java deleted file mode 100644 index 0414b98f18..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/InvokeVirtualCmd.java +++ /dev/null @@ -1,85 +0,0 @@ -package mini_jvm.cmd; - - -import mini_jvm.clz.ClassFile; -import mini_jvm.constant.MethodRefInfo; -import mini_jvm.engine.ExecutionResult; -import mini_jvm.engine.JavaObject; -import mini_jvm.engine.MethodArea; -import mini_jvm.engine.StackFrame; -import mini_jvm.method.Method; - -public class InvokeVirtualCmd extends TwoOperandCmd { - - public InvokeVirtualCmd(ClassFile clzFile,String opCode) { - super(clzFile,opCode); - } - - @Override - public String toString() { - - return super.getOperandAsMethod(); - } - - private boolean isSystemOutPrintlnMethod(String className, String methodName){ - return "java/io/PrintStream".equals(className) - && "println".equals(methodName); - } - @Override - public void execute(StackFrame frame, ExecutionResult result) { - - //先得到对该方法的描述 - MethodRefInfo methodRefInfo = (MethodRefInfo)this.getConstantInfo(this.getIndex()); - - String className = methodRefInfo.getClassName(); - String methodName = methodRefInfo.getMethodName(); - - // 我们没有实现System.out.println方法, 所以也不用建立新的栈帧, 直接调用Java的方法, 打印出来即可。 - if(isSystemOutPrintlnMethod(className,methodName)){ - JavaObject jo = (JavaObject)frame.getOprandStack().pop(); - String value = jo.toString(); - System.err.println("-------------------"+value+"----------------"); - - // 这里就是那个out对象, 因为是个假的,直接pop出来 - frame.getOprandStack().pop(); - - return; - } - - //注意:多态, 这才是真正的对象, 先从该对象的class 中去找对应的方法,找不到的话再去找父类的方法 - JavaObject jo = frame.getOprandStack().peek(); - - MethodArea ma = MethodArea.getInstance(); - - Method m = null; - - String currentClassName = jo.getClassName(); - - while(currentClassName != null){ - - ClassFile currentClassFile = ma.findClassFile(currentClassName); - - m = currentClassFile.getMethod(methodRefInfo.getMethodName(), - methodRefInfo.getParamAndReturnType()); - if(m != null){ - - break; - - } else{ - //查找父类 - currentClassName = currentClassFile.getSuperClassName(); - } - } - - if(m == null){ - throw new RuntimeException("Can't find method for :" + methodRefInfo.toString()); - } - - - result.setNextAction(ExecutionResult.PAUSE_AND_RUN_NEW_FRAME); - - result.setNextMethod(m); - } - - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/LdcCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/LdcCmd.java deleted file mode 100644 index 2ada87ca6d..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/LdcCmd.java +++ /dev/null @@ -1,51 +0,0 @@ -package mini_jvm.cmd; - - -import mini_jvm.clz.ClassFile; -import mini_jvm.constant.ConstantInfo; -import mini_jvm.constant.ConstantPool; -import mini_jvm.constant.StringInfo; -import mini_jvm.engine.ExecutionResult; -import mini_jvm.engine.Heap; -import mini_jvm.engine.JavaObject; -import mini_jvm.engine.StackFrame; - -public class LdcCmd extends OneOperandCmd { - - public LdcCmd(ClassFile clzFile, String opCode) { - super(clzFile,opCode); - } - - @Override - public String toString() { - - ConstantInfo info = getConstantInfo(this.getOperand()); - - String value = "TBD"; - if(info instanceof StringInfo){ - StringInfo strInfo = (StringInfo)info; - value = strInfo.toString(); - } - - return this.getOffset()+":"+this.getOpCode()+" " + this.getReadableCodeText() + " "+ value; - - } - public void execute(StackFrame frame, ExecutionResult result){ - - ConstantPool pool = this.getConstantPool(); - ConstantInfo info = (ConstantInfo)pool.getConstantInfo(this.getOperand()); - - if(info instanceof StringInfo){ - StringInfo strInfo = (StringInfo)info; - String value = strInfo.toString(); - JavaObject jo = Heap.getInstance().newString(value); - frame.getOprandStack().push(jo); - } - else{ - //TBD 处理其他类型 - throw new RuntimeException("Only support StringInfo constant"); - } - - - } -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/NewObjectCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/NewObjectCmd.java deleted file mode 100644 index ef07cb7dcb..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/NewObjectCmd.java +++ /dev/null @@ -1,39 +0,0 @@ -package mini_jvm.cmd; - - -import mini_jvm.clz.ClassFile; -import mini_jvm.constant.ClassInfo; -import mini_jvm.engine.ExecutionResult; -import mini_jvm.engine.Heap; -import mini_jvm.engine.JavaObject; -import mini_jvm.engine.StackFrame; - -public class NewObjectCmd extends TwoOperandCmd{ - - public NewObjectCmd(ClassFile clzFile, String opCode){ - super(clzFile,opCode); - } - - @Override - public String toString() { - - return super.getOperandAsClassInfo(); - } - public void execute(StackFrame frame, ExecutionResult result){ - - int index = this.getIndex(); - - ClassInfo info = (ClassInfo)this.getConstantInfo(index); - - String clzName = info.getClassName(); - - //在Java堆上创建一个实例 - JavaObject jo = Heap.getInstance().newObject(clzName); - - frame.getOprandStack().push(jo); - - - - } - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/NoOperandCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/NoOperandCmd.java deleted file mode 100644 index da490b7931..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/NoOperandCmd.java +++ /dev/null @@ -1,146 +0,0 @@ -package mini_jvm.cmd; - - -import mini_jvm.clz.ClassFile; -import mini_jvm.engine.ExecutionResult; -import mini_jvm.engine.Heap; -import mini_jvm.engine.JavaObject; -import mini_jvm.engine.StackFrame; - -public class NoOperandCmd extends ByteCodeCommand{ - - public NoOperandCmd(ClassFile clzFile, String opCode) { - super(clzFile, opCode); - } - - @Override - public String toString() { - return this.getOffset()+":" +this.getOpCode() + " "+ this.getReadableCodeText(); - } - - @Override - public void execute(StackFrame frame, ExecutionResult result) { - - String opCode = this.getOpCode(); - - if(ByteCodeCommand.aload_0.equals(opCode)){ - - JavaObject jo = frame.getLocalVariableValue(0); - - frame.getOprandStack().push(jo); - - } else if(ByteCodeCommand.aload_1.equals(opCode)){ - - JavaObject jo = frame.getLocalVariableValue(1); - - frame.getOprandStack().push(jo); - - } else if(ByteCodeCommand.aload_2.equals(opCode)){ - - JavaObject jo = frame.getLocalVariableValue(2); - - frame.getOprandStack().push(jo); - - }else if(ByteCodeCommand.iload_1.equals(opCode)){ - - JavaObject jo = frame.getLocalVariableValue(1); - - frame.getOprandStack().push(jo); - - } else if (ByteCodeCommand.iload_2.equals(opCode)){ - - JavaObject jo = frame.getLocalVariableValue(2); - - frame.getOprandStack().push(jo); - - } else if (ByteCodeCommand.iload_3.equals(opCode)){ - - JavaObject jo = frame.getLocalVariableValue(3); - - frame.getOprandStack().push(jo); - - }else if (ByteCodeCommand.fload_3.equals(opCode)){ - - JavaObject jo = frame.getLocalVariableValue(3); - - frame.getOprandStack().push(jo); - - } - else if (ByteCodeCommand.voidreturn.equals(opCode)){ - - result.setNextAction(ExecutionResult.EXIT_CURRENT_FRAME); - - } else if(ByteCodeCommand.ireturn.equals(opCode)){ - StackFrame callerFrame = frame.getCallerFrame(); - JavaObject jo = frame.getOprandStack().pop(); - callerFrame.getOprandStack().push(jo); - - } else if(ByteCodeCommand.freturn.equals(opCode)){ - - StackFrame callerFrame = frame.getCallerFrame(); - JavaObject jo = frame.getOprandStack().pop(); - callerFrame.getOprandStack().push(jo); - } - - else if(ByteCodeCommand.astore_1.equals(opCode)){ - - JavaObject jo = frame.getOprandStack().pop(); - - frame.setLocalVariableValue(1, jo); - - } else if(ByteCodeCommand.dup.equals(opCode)){ - - JavaObject jo = frame.getOprandStack().peek(); - frame.getOprandStack().push(jo); - - } else if(ByteCodeCommand.iconst_0.equals(opCode)){ - - JavaObject jo = Heap.getInstance().newInt(0); - - frame.getOprandStack().push(jo); - - } else if(ByteCodeCommand.iconst_1.equals(opCode)){ - - JavaObject jo = Heap.getInstance().newInt(1); - - frame.getOprandStack().push(jo); - - } else if(ByteCodeCommand.istore_1.equals(opCode)){ - - JavaObject jo = frame.getOprandStack().pop(); - - frame.setLocalVariableValue(1, jo); - - } else if(ByteCodeCommand.istore_2.equals(opCode)){ - - JavaObject jo = frame.getOprandStack().pop(); - - frame.setLocalVariableValue(2, jo); - - } else if(ByteCodeCommand.iadd.equals(opCode)){ - - JavaObject jo1 = frame.getOprandStack().pop(); - JavaObject jo2 = frame.getOprandStack().pop(); - - JavaObject sum = Heap.getInstance().newInt(jo1.getIntValue()+jo2.getIntValue()); - - frame.getOprandStack().push(sum); - - } else if (ByteCodeCommand.aconst_null.equals(opCode)){ - - frame.getOprandStack().push(null); - - } - else{ - throw new RuntimeException("you must forget to implement the operation :" + opCode); - } - - - } - - - public int getLength(){ - return 1; - } - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/OneOperandCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/OneOperandCmd.java deleted file mode 100644 index 0ffadfbd2b..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/OneOperandCmd.java +++ /dev/null @@ -1,29 +0,0 @@ -package mini_jvm.cmd; - -import mini_jvm.clz.ClassFile; - - - -public abstract class OneOperandCmd extends ByteCodeCommand { - - private int operand; - - public OneOperandCmd(ClassFile clzFile, String opCode) { - super(clzFile, opCode); - - } - public int getOperand() { - - return this.operand; - } - - public void setOperand(int oprand1) { - this.operand = oprand1; - - } - public int getLength(){ - return 2; - } - - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/PutFieldCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/PutFieldCmd.java deleted file mode 100644 index 94652375fd..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/PutFieldCmd.java +++ /dev/null @@ -1,44 +0,0 @@ -package mini_jvm.cmd; - - -import mini_jvm.clz.ClassFile; -import mini_jvm.constant.ClassInfo; -import mini_jvm.constant.FieldRefInfo; -import mini_jvm.constant.NameAndTypeInfo; -import mini_jvm.engine.ExecutionResult; -import mini_jvm.engine.JavaObject; -import mini_jvm.engine.StackFrame; - -public class PutFieldCmd extends TwoOperandCmd { - - public PutFieldCmd(ClassFile clzFile, String opCode) { - super(clzFile,opCode); - } - - @Override - public String toString() { - - return super.getOperandAsField(); - } - @Override - public void execute(StackFrame frame, ExecutionResult result) { - - FieldRefInfo fieldRef = (FieldRefInfo)this.getConstantInfo(this.getIndex()); - - ClassInfo clzInfo = (ClassInfo)this.getConstantInfo(fieldRef.getClassInfoIndex()); - NameAndTypeInfo nameTypeInfo = (NameAndTypeInfo)this.getConstantInfo(fieldRef.getNameAndTypeIndex()); - // for example : name - String fieldName = nameTypeInfo.getName(); - // for example : Ljava/lang/String : 注意:我们不再检查类型 - String fieldType = nameTypeInfo.getTypeInfo(); - - JavaObject fieldValue = frame.getOprandStack().pop(); - JavaObject objectRef = frame.getOprandStack().pop(); - - objectRef.setFieldValue(fieldName, fieldValue); - - } - - - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/TwoOperandCmd.java b/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/TwoOperandCmd.java deleted file mode 100644 index 93ea4b02bf..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/cmd/TwoOperandCmd.java +++ /dev/null @@ -1,67 +0,0 @@ -package mini_jvm.cmd; - - -import mini_jvm.clz.ClassFile; -import mini_jvm.constant.ClassInfo; -import mini_jvm.constant.ConstantInfo; -import mini_jvm.constant.FieldRefInfo; -import mini_jvm.constant.MethodRefInfo; - -public abstract class TwoOperandCmd extends ByteCodeCommand{ - - int oprand1 = -1; - int oprand2 = -1; - - public int getOprand1() { - return oprand1; - } - - public void setOprand1(int oprand1) { - this.oprand1 = oprand1; - } - - public void setOprand2(int oprand2) { - this.oprand2 = oprand2; - } - - public int getOprand2() { - return oprand2; - } - - public TwoOperandCmd(ClassFile clzFile, String opCode) { - super(clzFile, opCode); - } - - public int getIndex(){ - int oprand1 = this.getOprand1(); - int oprand2 = this.getOprand2(); - int index = oprand1 << 8 | oprand2; - return index; - } - - protected String getOperandAsClassInfo(){ - int index = getIndex(); - String codeTxt = getReadableCodeText(); - ClassInfo info = (ClassInfo)getConstantInfo(index); - return this.getOffset()+":"+this.getOpCode()+" "+ codeTxt +" "+ info.getClassName(); - } - - protected String getOperandAsMethod(){ - int index = getIndex(); - String codeTxt = getReadableCodeText(); - ConstantInfo constInfo = this.getConstantInfo(index); - MethodRefInfo info = (MethodRefInfo)this.getConstantInfo(index); - return this.getOffset()+":"+this.getOpCode()+" " + codeTxt +" "+ info.toString(); - } - - protected String getOperandAsField(){ - int index = getIndex(); - - String codeTxt = getReadableCodeText(); - FieldRefInfo info = (FieldRefInfo)this.getConstantInfo(index); - return this.getOffset()+":"+this.getOpCode()+" " + codeTxt +" "+ info.toString(); - } - public int getLength(){ - return 3; - } -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ClassInfo.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ClassInfo.java deleted file mode 100644 index 8b974772fe..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ClassInfo.java +++ /dev/null @@ -1,26 +0,0 @@ -package mini_jvm.constant; - -public class ClassInfo extends ConstantInfo { - private final int type = ConstantInfo.CLASS_INFO; - - private int utf8Index ; - public ClassInfo(ConstantPool pool) { - super(pool); - } - - public int getUtf8Index() { - return utf8Index; - } - public void setUtf8Index(int utf8Index) { - this.utf8Index = utf8Index; - } - public int getType() { - return type; - } - - public String getClassName() { - int index = getUtf8Index(); - UTF8Info utf8Info = (UTF8Info)constantPool.getConstantInfo(index); - return utf8Info.getValue(); - } -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ConstantInfo.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ConstantInfo.java deleted file mode 100644 index 54f89aacd8..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ConstantInfo.java +++ /dev/null @@ -1,28 +0,0 @@ -package mini_jvm.constant; - -public abstract class ConstantInfo { - public static final int UTF8_INFO = 1; - public static final int FLOAT_INFO = 4; - public static final int CLASS_INFO = 7; - public static final int STRING_INFO = 8; - public static final int FIELD_INFO = 9; - public static final int METHOD_INFO = 10; - public static final int NAME_AND_TYPE_INFO = 12; - - protected ConstantPool constantPool; - - public ConstantInfo(){} - - public ConstantInfo(ConstantPool pool) { - this.constantPool = pool; - } - public abstract int getType(); - - public ConstantPool getConstantPool() { - return constantPool; - } - public ConstantInfo getConstantInfo(int index){ - return this.constantPool.getConstantInfo(index); - } - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ConstantPool.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ConstantPool.java deleted file mode 100644 index c5d8348d89..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/ConstantPool.java +++ /dev/null @@ -1,25 +0,0 @@ -package mini_jvm.constant; - -import java.util.ArrayList; -import java.util.List; - -public class ConstantPool { - - private List constantInfos = new ArrayList(); - - public ConstantPool(){} - - public void addConstantInfo(ConstantInfo info){ - this.constantInfos.add(info); - } - - public ConstantInfo getConstantInfo(int index){ - return this.constantInfos.get(index); - } - public String getUTF8String(int index){ - return ((UTF8Info)this.constantInfos.get(index)).getValue(); - } - public Object getSize() { - return this.constantInfos.size() -1; - } -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/FieldRefInfo.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/FieldRefInfo.java deleted file mode 100644 index be474180d8..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/FieldRefInfo.java +++ /dev/null @@ -1,49 +0,0 @@ -package mini_jvm.constant; - -public class FieldRefInfo extends ConstantInfo{ - private final int type = ConstantInfo.FIELD_INFO; - - private int classInfoIndex; - private int nameAndTypeIndex; - - public FieldRefInfo(ConstantPool pool) { - super(pool); - } - public int getType() { - return type; - } - - public int getClassInfoIndex() { - return classInfoIndex; - } - public void setClassInfoIndex(int classInfoIndex) { - this.classInfoIndex = classInfoIndex; - } - public int getNameAndTypeIndex() { - return nameAndTypeIndex; - } - public void setNameAndTypeIndex(int nameAndTypeIndex) { - this.nameAndTypeIndex = nameAndTypeIndex; - } - - public String toString(){ - NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); - return getClassName() +" : "+ typeInfo.getName() + ":" + typeInfo.getTypeInfo() +"]"; - } - - public String getClassName(){ - ClassInfo classInfo = (ClassInfo) this.getConstantInfo(this.getClassInfoIndex()); - UTF8Info utf8Info = (UTF8Info)this.getConstantInfo(classInfo.getUtf8Index()); - return utf8Info.getValue(); - } - - public String getFieldName(){ - NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); - return typeInfo.getName(); - } - - public String getFieldType(){ - NameAndTypeInfo typeInfo = (NameAndTypeInfo)this.getConstantInfo(this.getNameAndTypeIndex()); - return typeInfo.getTypeInfo(); - } -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/MethodRefInfo.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/MethodRefInfo.java deleted file mode 100644 index 8e3789e1b4..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/MethodRefInfo.java +++ /dev/null @@ -1,52 +0,0 @@ -package mini_jvm.constant; - -public class MethodRefInfo extends ConstantInfo { - - private final int type = ConstantInfo.METHOD_INFO; - - private int classInfoIndex; - private int nameAndTypeIndex; - - public MethodRefInfo(ConstantPool pool) { - super(pool); - } - - public int getType() { - return type; - } - - public int getClassInfoIndex() { - return classInfoIndex; - } - public void setClassInfoIndex(int classInfoIndex) { - this.classInfoIndex = classInfoIndex; - } - public int getNameAndTypeIndex() { - return nameAndTypeIndex; - } - public void setNameAndTypeIndex(int nameAndTypeIndex) { - this.nameAndTypeIndex = nameAndTypeIndex; - } - - public String toString(){ - return getClassName() +" : "+ this.getMethodName() + " : " + this.getParamAndReturnType() ; - } - - public String getClassName(){ - ConstantPool pool = this.getConstantPool(); - ClassInfo clzInfo = (ClassInfo)pool.getConstantInfo(this.getClassInfoIndex()); - return clzInfo.getClassName(); - } - - public String getMethodName(){ - ConstantPool pool = this.getConstantPool(); - NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); - return typeInfo.getName(); - } - - public String getParamAndReturnType(){ - ConstantPool pool = this.getConstantPool(); - NameAndTypeInfo typeInfo = (NameAndTypeInfo)pool.getConstantInfo(this.getNameAndTypeIndex()); - return typeInfo.getTypeInfo(); - } -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/NameAndTypeInfo.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/NameAndTypeInfo.java deleted file mode 100644 index 142787ce16..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/NameAndTypeInfo.java +++ /dev/null @@ -1,45 +0,0 @@ -package mini_jvm.constant; - -public class NameAndTypeInfo extends ConstantInfo{ - public final int type = ConstantInfo.NAME_AND_TYPE_INFO; - - private int index1; - private int index2; - - public NameAndTypeInfo(ConstantPool pool) { - super(pool); - } - - public int getIndex1() { - return index1; - } - public void setIndex1(int index1) { - this.index1 = index1; - } - public int getIndex2() { - return index2; - } - public void setIndex2(int index2) { - this.index2 = index2; - } - public int getType() { - return type; - } - - - public String getName(){ - ConstantPool pool = this.getConstantPool(); - UTF8Info utf8Info1 = (UTF8Info)pool.getConstantInfo(index1); - return utf8Info1.getValue(); - } - - public String getTypeInfo(){ - ConstantPool pool = this.getConstantPool(); - UTF8Info utf8Info2 = (UTF8Info)pool.getConstantInfo(index2); - return utf8Info2.getValue(); - } - - public String toString(){ - return "(" + getName() + "," + getTypeInfo()+")"; - } -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/NullConstantInfo.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/NullConstantInfo.java deleted file mode 100644 index 6c3bcad2df..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/NullConstantInfo.java +++ /dev/null @@ -1,12 +0,0 @@ -package mini_jvm.constant; - -public class NullConstantInfo extends ConstantInfo { - - public NullConstantInfo(){} - - @Override - public int getType() { - return -1; - } - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/StringInfo.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/StringInfo.java deleted file mode 100644 index 8b432cffb7..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/StringInfo.java +++ /dev/null @@ -1,25 +0,0 @@ -package mini_jvm.constant; - -public class StringInfo extends ConstantInfo{ - private final int type = ConstantInfo.STRING_INFO; - private int index; - - public StringInfo(ConstantPool pool) { - super(pool); - } - - public int getType() { - return type; - } - public int getIndex() { - return index; - } - public void setIndex(int index) { - this.index = index; - } - - public String toString(){ - return this.getConstantPool().getUTF8String(index); - } - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/UTF8Info.java b/students/769232552/season_one/main/java/season_1/mini_jvm/constant/UTF8Info.java deleted file mode 100644 index 2c2253e622..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/constant/UTF8Info.java +++ /dev/null @@ -1,33 +0,0 @@ -package mini_jvm.constant; - -public class UTF8Info extends ConstantInfo{ - private final int type = ConstantInfo.UTF8_INFO; - - private int length ; - private String value; - - public UTF8Info(ConstantPool pool) { - super(pool); - } - - public int getLength() { - return length; - } - public void setLength(int length) { - this.length = length; - } - public String getValue() { - return value; - } - public void setValue(String value) { - this.value = value; - } - public int getType() { - return type; - } - - @Override - public String toString() { - return "UTF8Info [type=" + type + ", length=" + length + ", value=" + value +")]"; - } -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/ExecutionResult.java b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/ExecutionResult.java deleted file mode 100644 index 58c9d26dca..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/ExecutionResult.java +++ /dev/null @@ -1,57 +0,0 @@ -package mini_jvm.engine; - - -import mini_jvm.method.Method; - -public class ExecutionResult { - public static final int RUN_NEXT_CMD = 1; - public static final int JUMP = 2; - public static final int EXIT_CURRENT_FRAME = 3; - public static final int PAUSE_AND_RUN_NEW_FRAME = 4; - - private int nextAction = RUN_NEXT_CMD; - - private int nextCmdOffset = 0; - - private Method nextMethod; - - public Method getNextMethod() { - return nextMethod; - } - public void setNextMethod(Method nextMethod) { - this.nextMethod = nextMethod; - } - - - - public void setNextAction(int action){ - this.nextAction = action; - } - public boolean isPauseAndRunNewFrame(){ - return this.nextAction == PAUSE_AND_RUN_NEW_FRAME; - } - public boolean isExitCurrentFrame(){ - return this.nextAction == EXIT_CURRENT_FRAME; - } - - public boolean isRunNextCmd(){ - return this.nextAction == RUN_NEXT_CMD; - } - - public boolean isJump(){ - return this.nextAction == JUMP; - } - - public int getNextCmdOffset() { - return nextCmdOffset; - } - - public void setNextCmdOffset(int nextCmdOffset) { - this.nextCmdOffset = nextCmdOffset; - } - - - - - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/ExecutorEngine.java b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/ExecutorEngine.java deleted file mode 100644 index d7ef541eb8..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/ExecutorEngine.java +++ /dev/null @@ -1,77 +0,0 @@ -package mini_jvm.engine; - -import mini_jvm.method.Method; - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - -public class ExecutorEngine { - - private Stack stack = new Stack(); - - public ExecutorEngine() { - - } - - public void execute(Method mainMethod){ - - StackFrame mainFrame = StackFrame.create(mainMethod); - stack.push(mainFrame); - - while(!stack.empty()){ - - StackFrame frame = stack.peek(); - - ExecutionResult result = frame.execute(); - - if(result.isPauseAndRunNewFrame()){ - - Method nextMethod = result.getNextMethod(); - StackFrame nextFrame = StackFrame.create(nextMethod); - nextFrame.setCallerFrame(frame); - setupFunctionCallParams(frame,nextFrame); - - stack.push(nextFrame); - - } else { - stack.pop(); - } - } - - } - - - - private void setupFunctionCallParams(StackFrame currentFrame,StackFrame nextFrame) { - - Method nextMethod = nextFrame.getMethod(); - - - List paramList = nextMethod.getParameterList(); - - //加上1 是因为要把this也传递过去 - - int paramNum = paramList.size() + 1; - - - List values = new ArrayList(); - - //数据结构知识: 从栈中取出栈顶的x个元素 - while(paramNum>0){ - values.add(currentFrame.getOprandStack().pop()); - paramNum --; - } - //数据结构知识: 把一个列表倒序排列 - List params = new ArrayList(); - - for(int i=values.size()-1; i>=0 ;i--){ - params.add(values.get(i)); - } - - - nextFrame.setLocalVariableTable(params); - - } - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/Heap.java b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/Heap.java deleted file mode 100644 index 4a197b98f6..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/Heap.java +++ /dev/null @@ -1,39 +0,0 @@ -package mini_jvm.engine; - -public class Heap { - - /** - * 没有实现垃圾回收, 所以对于下面新创建的对象, 并没有记录到一个数据结构当中 - */ - - private static Heap instance = new Heap(); - private Heap() { - } - public static Heap getInstance(){ - return instance; - } - public JavaObject newObject(String clzName){ - - JavaObject jo = new JavaObject(JavaObject.OBJECT); - jo.setClassName(clzName); - return jo; - } - - public JavaObject newString(String value){ - JavaObject jo = new JavaObject(JavaObject.STRING); - jo.setStringValue(value); - return jo; - } - - public JavaObject newFloat(float value){ - JavaObject jo = new JavaObject(JavaObject.FLOAT); - jo.setFloatValue(value); - return jo; - } - public JavaObject newInt(int value){ - JavaObject jo = new JavaObject(JavaObject.INT); - jo.setIntValue(value); - return jo; - } - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/JavaObject.java b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/JavaObject.java deleted file mode 100644 index 51185b1056..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/JavaObject.java +++ /dev/null @@ -1,71 +0,0 @@ -package mini_jvm.engine; - -import java.util.HashMap; -import java.util.Map; - -public class JavaObject { - public static final int OBJECT = 1; - public static final int STRING = 2; - public static final int INT = 3; - public static final int FLOAT = 4; - - int type; - private String className; - - private Map fieldValues = new HashMap(); - - private String stringValue; - - private int intValue; - - private float floatValue; - - public void setFieldValue(String fieldName, JavaObject fieldValue){ - fieldValues.put(fieldName, fieldValue); - } - public JavaObject(int type){ - this.type = type; - } - public void setClassName(String className){ - this.className = className; - } - public void setStringValue(String value){ - stringValue = value; - } - public String getStringValue(){ - return this.stringValue; - } - public void setIntValue(int value) { - this.intValue = value; - } - public int getIntValue(){ - return this.intValue; - } - public int getType(){ - return type; - } - public JavaObject getFieldValue(String fieldName){ - return this.fieldValues.get(fieldName); - } - public String toString(){ - switch(this.getType()){ - case INT: - return String.valueOf(this.intValue); - case STRING: - return this.stringValue; - case OBJECT: - return this.className +":"+ this.fieldValues; - case FLOAT : - return String.valueOf(this.floatValue); - default: - return null; - } - } - public String getClassName(){ - return this.className; - } - public void setFloatValue(float value) { - this.floatValue = value; - } - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/MethodArea.java b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/MethodArea.java deleted file mode 100644 index 29e7c5a380..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/MethodArea.java +++ /dev/null @@ -1,90 +0,0 @@ -package mini_jvm.engine; - -import mini_jvm.clz.ClassFile; -import mini_jvm.constant.MethodRefInfo; -import mini_jvm.loader.ClassFileLoader; -import mini_jvm.method.Method; - -import java.util.HashMap; -import java.util.Map; - - - -public class MethodArea { - - public static final MethodArea instance = new MethodArea(); - - /** - * 注意:我们做了极大的简化, ClassLoader 只有一个, 实际JVM中的ClassLoader,是一个双亲委托的模型 - */ - - private ClassFileLoader clzLoader = null; - - Map map = new HashMap(); - - private MethodArea(){ - } - - public static MethodArea getInstance(){ - return instance; - } - - public void setClassFileLoader(ClassFileLoader clzLoader){ - this.clzLoader = clzLoader; - } - - public Method getMainMethod(String className){ - - ClassFile clzFile = this.findClassFile(className); - - return clzFile.getMainMethod(); - } - - - public ClassFile findClassFile(String className){ - - if(map.get(className) != null){ - return map.get(className); - } - // 看来该class 文件还没有load过 - ClassFile clzFile = this.clzLoader.loadClass(className); - - map.put(className, clzFile); - - return clzFile; - - } - - - public Method getMethod(String className, String methodName, String paramAndReturnType){ - - ClassFile clz = this.findClassFile(className); - - Method m = clz.getMethod(methodName, paramAndReturnType); - - if(m == null){ - - throw new RuntimeException("method can't be found : \n" - + "class: " + className - + "method: " + methodName - + "paramAndReturnType: " + paramAndReturnType); - } - - return m; - } - - - public Method getMethod(MethodRefInfo methodRef){ - - ClassFile clz = this.findClassFile(methodRef.getClassName()); - - Method m = clz.getMethod(methodRef.getMethodName(), methodRef.getParamAndReturnType()); - - if(m == null){ - throw new RuntimeException("method can't be found : " + methodRef.toString()); - } - - return m; - - } -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/MiniJVM.java b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/MiniJVM.java deleted file mode 100644 index 6781fed5ad..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/MiniJVM.java +++ /dev/null @@ -1,30 +0,0 @@ -package mini_jvm.engine; - -import mini_jvm.loader.ClassFileLoader; - -import java.io.FileNotFoundException; -import java.io.IOException; - - - -public class MiniJVM { - - public void run(String[]classPaths , String className) throws FileNotFoundException, IOException{ - - ClassFileLoader loader = new ClassFileLoader(); - for(int i=0;i operands = new ArrayList(); - - public void push(JavaObject jo){ - operands.add(jo); - } - public JavaObject pop(){ - int index = size()-1; - JavaObject jo = (JavaObject)operands.get(index); - operands.remove(index); - return jo; - - } - public JavaObject top(){ - int index = size()-1; - return (JavaObject)operands.get(index); - } - public int size(){ - return operands.size(); - } -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/StackFrame.java b/students/769232552/season_one/main/java/season_1/mini_jvm/engine/StackFrame.java deleted file mode 100644 index 65b4bc119d..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/engine/StackFrame.java +++ /dev/null @@ -1,126 +0,0 @@ -package mini_jvm.engine; - -import mini_jvm.cmd.ByteCodeCommand; -import mini_jvm.method.Method; - -import java.util.ArrayList; -import java.util.List; -import java.util.Stack; - - -public class StackFrame { - - private List localVariableTable = new ArrayList(); - private Stack oprandStack = new Stack(); - - int index = 0; - - private Method m = null; - - private StackFrame callerFrame = null; - - public StackFrame getCallerFrame() { - return callerFrame; - } - - public void setCallerFrame(StackFrame callerFrame) { - this.callerFrame = callerFrame; - } - - public static StackFrame create(Method m){ - StackFrame frame = new StackFrame(m); - return frame; - } - - - private StackFrame(Method m) { - this.m = m; - } - - - - public JavaObject getLocalVariableValue(int index){ - return this.localVariableTable.get(index); - } - - public Stack getOprandStack(){ - return this.oprandStack; - } - - public int getNextCommandIndex(int offset){ - - ByteCodeCommand[] cmds = m.getCodeAttr().getCmds(); - for(int i=0;i values){ - this.localVariableTable = values; - } - - public void setLocalVariableValue(int index, JavaObject jo){ - //问题: 为什么要这么做?? - if(this.localVariableTable.size()-1 < index){ - for(int i=this.localVariableTable.size(); i<=index; i++){ - this.localVariableTable.add(null); - } - } - this.localVariableTable.set(index, jo); - - - } - - public Method getMethod(){ - return m; - } - - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/field/Field.java b/students/769232552/season_one/main/java/season_1/mini_jvm/field/Field.java deleted file mode 100644 index 1379bd3715..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/field/Field.java +++ /dev/null @@ -1,64 +0,0 @@ -package mini_jvm.field; - - -import mini_jvm.attr.AttributeInfo; -import mini_jvm.attr.ConstantValue; -import mini_jvm.constant.ConstantPool; -import mini_jvm.constant.UTF8Info; -import mini_jvm.loader.ByteCodeIterator; - -public class Field { - private int accessFlag; - private int nameIndex; - private int descriptorIndex; - - private ConstantPool pool; - private ConstantValue constValue; - - public Field( int accessFlag, int nameIndex, int descriptorIndex,ConstantPool pool) { - - this.accessFlag = accessFlag; - this.nameIndex = nameIndex; - this.descriptorIndex = descriptorIndex; - this.pool = pool; - } - - public String toString() { - String name = ((UTF8Info)pool.getConstantInfo(this.nameIndex)).getValue(); - - String desc = ((UTF8Info)pool.getConstantInfo(this.descriptorIndex)).getValue(); - return name +":"+ desc; - } - - - public static Field parse(ConstantPool pool,ByteCodeIterator iter){ - - int accessFlag = iter.nextU2ToInt(); - int nameIndex = iter.nextU2ToInt(); - int descIndex = iter.nextU2ToInt(); - int attribCount = iter.nextU2ToInt(); - //System.out.println("field attribute count:"+ attribCount); - - Field f = new Field(accessFlag, nameIndex, descIndex,pool); - - for( int i=1; i<= attribCount; i++){ - int attrNameIndex = iter.nextU2ToInt(); - String attrName = pool.getUTF8String(attrNameIndex); - - if(AttributeInfo.CONST_VALUE.equals(attrName)){ - int attrLen = iter.nextU4ToInt(); - ConstantValue constValue = new ConstantValue(attrNameIndex, attrLen); - constValue.setConstValueIndex(iter.nextU2ToInt()); - f.setConstantValue(constValue); - } else{ - throw new RuntimeException("the attribute " + attrName + " has not been implemented yet."); - } - } - - return f; - } - public void setConstantValue(ConstantValue constValue) { - this.constValue = constValue; - } - -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ByteCodeIterator.java b/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ByteCodeIterator.java deleted file mode 100644 index 318c48cf5b..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ByteCodeIterator.java +++ /dev/null @@ -1,63 +0,0 @@ -package mini_jvm.loader; - -import mini_jvm.util.Util; - -public class ByteCodeIterator { - - private byte[] byteCodes; - private int currentIndex = -1; - private int byteSize = 0; - - ByteCodeIterator(byte[] byteCodes){ - this.byteCodes = byteCodes; - this.byteSize = byteCodes.length; - } - - public boolean hasNext(){ - return currentIndex < byteSize; - } - - - public int nextU1ToInt(){ - return Util.byteToInt(new byte[]{byteCodes[++currentIndex]}); - } - - public int nextU2ToInt(){ - return Util.byteToInt(new byte[]{byteCodes[++currentIndex],byteCodes[++currentIndex]}); - } - - public int nextU4ToInt(){ - return Util.byteToInt(new byte[]{byteCodes[++currentIndex],byteCodes[++currentIndex], - byteCodes[++currentIndex],byteCodes[++currentIndex]}); - } - - public String nextU2ToHexString(){ - return Util.byteToHexString(new byte[]{byteCodes[++currentIndex],byteCodes[++currentIndex]}); - } - - public String nextU4ToHexString(){ - return Util.byteToHexString(new byte[]{byteCodes[++currentIndex],byteCodes[++currentIndex], - byteCodes[++currentIndex],byteCodes[++currentIndex]}); - } - - public String nextBytesLenAsString(int BytesLen) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < BytesLen; i++) { - char c = (char) byteCodes[++currentIndex]; - sb.append(c); - } - return sb.toString(); - } - - public String nextUxToHexString(int len) { - byte[] tmp = new byte[len]; - for (int i = 0; i < len; i++) { - tmp[i] = byteCodes[++currentIndex]; - } - return Util.byteToHexString(tmp).toLowerCase(); - } - - public void back(int n) { - this.currentIndex -= n; - } -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ClassFileLoader.java b/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ClassFileLoader.java deleted file mode 100644 index 974914032e..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ClassFileLoader.java +++ /dev/null @@ -1,144 +0,0 @@ -package mini_jvm.loader; - -import mini_jvm.clz.ClassFile; -import org.apache.commons.io.IOUtils; - -import java.io.*; -import java.util.ArrayList; -import java.util.List; - - -public class ClassFileLoader { - - private List clzPaths = new ArrayList(); - - public ClassFile loadClass(String className){ - byte[] byteCodes = readBinaryCode(className); - ClassFileParser classFileParser = new ClassFileParser(); - ClassFile clzFile = classFileParser.parse(byteCodes); - return clzFile; - } - - - /** - * 读取class文件的二进制代码 - * @param className - * @return - */ - public byte[] readBinaryCodeV1(String className) { - String clazzPath = getClassPath(className); - BufferedInputStream bins = null; - ByteArrayOutputStream bouts = new ByteArrayOutputStream(); - try { - bins = new BufferedInputStream(new FileInputStream(new File(clazzPath))); - byte[] buffer = new byte[1024]; - int length = -1; - while((length = bins.read(buffer)) != -1){ - bouts.write(buffer, 0, length); - } - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - byte[] codes = bouts.toByteArray(); - //关闭流 - try { - if(bins != null){ - //调用外层流的close方法就关闭其装饰的内层流 - bins.close(); - } - if(bouts != null){ - bouts.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - return codes; - } - - /** - * 使用IOUtils.toByteArray()读取流文件 - */ - public byte[] readBinaryCode(String className){ - String clzPath = getClassPath(className); - File f = new File(clzPath); - try{ - return IOUtils.toByteArray(new FileInputStream(f)); - } catch (FileNotFoundException e) { - e.printStackTrace(); - return null; - } catch (IOException e) { - e.printStackTrace(); - return null; - } - } - - - /** - * 从扫描根目录,获取指定类的绝对路径 - * @param className - * @return - */ - private String getClassPath(String className){ - String clazzPath = null; - //遍历clzPaths中所有路径 - for (String path : this.clzPaths){ - File file = new File(path); - clazzPath = getClassPath(className,file); - if(clazzPath!=null) break; - } - return clazzPath; - } - - private String getClassPath(String className, File file){ - String clazzPath = null; - if(file.exists()){ - //如果是目录,则遍历所有目录下的文件 - if(file.isDirectory()){ - File[] fs = file.listFiles(); - for (File f : fs){ - clazzPath = getClassPath(className,f); - } - }else { - //检查是否是该类对应的class文件 - if(isClazzFile(file.getName(),className)){ - clazzPath = file.getAbsolutePath(); - } - } - } - return clazzPath; - } - - private boolean isClazzFile(String filename , String className){ - String fileClazzName = null; - String [] names = filename.split("\\."); - if(names.length > 0){ - fileClazzName = names[0]; - } - return className.endsWith(fileClazzName); - } - - public void addClassPath(String path) { - clzPaths.add(path); - } - - public String getClassPath(){ - StringBuilder sb = new StringBuilder(); - int i = 0; - for (; i < clzPaths.size() - 1; i++) { - sb.append(clzPaths.get(i)); - sb.append(";"); - } - sb.append(clzPaths.get(i)); - return sb.toString(); - } - - public static void main(String[] args) { - String FULL_QUALIFIED_CLASS_NAME = "mini_jvm/test/EmployeeV1"; - String path1 = "D:\\worksapce\\gitRepo\\java_coding2017\\coding2017\\group23\\769232552\\coding\\target\\test-classes\\mini_jvm"; - ClassFileLoader loader = new ClassFileLoader(); - loader.addClassPath(path1); - String className = "mini_jvm.test.EmployeeV1"; - } -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ClassFileParser.java b/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ClassFileParser.java deleted file mode 100644 index 6ea12db446..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/loader/ClassFileParser.java +++ /dev/null @@ -1,159 +0,0 @@ -package mini_jvm.loader; - - -import mini_jvm.clz.AccessFlag; -import mini_jvm.clz.ClassFile; -import mini_jvm.clz.ClassIndex; -import mini_jvm.constant.*; -import mini_jvm.field.Field; -import mini_jvm.method.Method; - -public class ClassFileParser { - - public ClassFile parse(byte[] byteCodes) { - ByteCodeIterator iterator = new ByteCodeIterator(byteCodes); - ClassFile clzFile = new ClassFile(); - - //magic number - String magicNumber = iterator.nextU4ToHexString(); - if (!"cafebabe".equals(magicNumber)) { - throw new RuntimeException("not java .class file!"); - } - int minorVersion = iterator.nextU2ToInt(); //次版本号 - int majorVersion = iterator.nextU2ToInt(); //主版本号 - clzFile.setMajorVersion(majorVersion); - clzFile.setMinorVersion(minorVersion); - - ConstantPool constantPool = parseConstantPool(iterator); //常量池 - clzFile.setConstPool(constantPool); - - AccessFlag accessFlag = parseAccessFlag(iterator); //解析访问标识 - clzFile.setAccessFlag(accessFlag); - - ClassIndex clzIndex = parseClassIndex(iterator); //解析类索引 - clzFile.setClassIndex(clzIndex); - - //解析接口,此处暂不支持 - parseInterfaces(iterator); - - //解析字段 - parseField(clzFile,iterator); - - //解析方法 - parseMethod(clzFile,iterator); - - return clzFile; - } - - private void parseMethod(ClassFile clzFile, ByteCodeIterator iterator) { - //方法个数 - int methodCount = iterator.nextU2ToInt(); - for (int i = 0; i < methodCount; i++) { - Method m = Method.parse(clzFile,iterator); - clzFile.addMethod(m); - } - } - - //解析字段 - private void parseField(ClassFile clzFile, ByteCodeIterator iterator) { - //字段个数 - int fieldsCount = iterator.nextU2ToInt(); - for (int i = 0; i < fieldsCount; i++) { - Field f = Field.parse(clzFile.getConstantPool(),iterator); - clzFile.addField(f); - } - } - - - private AccessFlag parseAccessFlag(ByteCodeIterator iterator) { - int accessValue = iterator.nextU2ToInt(); - AccessFlag accessFlag = new AccessFlag(accessValue); - - return accessFlag; - } - - private ClassIndex parseClassIndex(ByteCodeIterator iterator) { - ClassIndex clzIndex = new ClassIndex(); - - int thisClzIndex = iterator.nextU2ToInt(); - int superClzIndex = iterator.nextU2ToInt(); - clzIndex.setThisClassIndex(thisClzIndex); - clzIndex.setSuperClassIndex(superClzIndex); - - return clzIndex; - } - - private ConstantPool parseConstantPool(ByteCodeIterator iterator) { - //常量池 - ConstantPool constantPool = new ConstantPool(); - //常量池成员数 - int constantPoolLength = iterator.nextU2ToInt(); - //第0位补上一个占位符 - NullConstantInfo nullConstantInfo = new NullConstantInfo(); - constantPool.addConstantInfo(nullConstantInfo); - for (int i = 1; i < constantPoolLength; i++) { - int tag = iterator.nextU1ToInt(); - // tag = 1, UTF8_INFO - if (tag == 1) { - UTF8Info utf8Info = new UTF8Info(constantPool); - int length = iterator.nextU2ToInt(); - String value = iterator.nextBytesLenAsString(length); - utf8Info.setLength(length); - utf8Info.setValue(value); - constantPool.addConstantInfo(utf8Info); - } - // tag = 7, CLASS_INFO - else if (tag == 7) { - ClassInfo classInfo = new ClassInfo(constantPool); - int nameIndex = iterator.nextU2ToInt(); - classInfo.setUtf8Index(nameIndex); - constantPool.addConstantInfo(classInfo); - } - // tag = 8, STRING_INFO - else if (tag == 8) { - StringInfo stringInfo = new StringInfo(constantPool); - int stringIndex = iterator.nextU2ToInt(); - stringInfo.setIndex(stringIndex); - constantPool.addConstantInfo(stringInfo); - } - // tag = 9, Fieldref - else if (tag == 9) { - FieldRefInfo fieldRefInfo = new FieldRefInfo(constantPool); - int classInfoIndex = iterator.nextU2ToInt(); - int nameAndTypeIndex = iterator.nextU2ToInt(); - fieldRefInfo.setClassInfoIndex(classInfoIndex); - fieldRefInfo.setNameAndTypeIndex(nameAndTypeIndex); - constantPool.addConstantInfo(fieldRefInfo); - } - // tag = 10, MethodRef - else if (tag == 10) { - MethodRefInfo methodRefInfo = new MethodRefInfo(constantPool); - int classInfoIndex = iterator.nextU2ToInt(); - int nameAndTypeIndex = iterator.nextU2ToInt(); - methodRefInfo.setClassInfoIndex(classInfoIndex); - methodRefInfo.setNameAndTypeIndex(nameAndTypeIndex); - constantPool.addConstantInfo(methodRefInfo); - } - // tag = 12, NameAndType - else if (tag == 12) { - NameAndTypeInfo nameAndTypeInfo = new NameAndTypeInfo(constantPool); - int nameAndTypeIndex = iterator.nextU2ToInt(); - int descriptorIndex = iterator.nextU2ToInt(); - nameAndTypeInfo.setIndex1(nameAndTypeIndex); - nameAndTypeInfo.setIndex2(descriptorIndex); - constantPool.addConstantInfo(nameAndTypeInfo); - } else { - throw new RuntimeException("not realized tag " + tag); - } - } - - return constantPool; - } - - private void parseInterfaces(ByteCodeIterator iterator) { - int interfaceCount = iterator.nextU2ToInt(); - // TODO : 如果实现了interface, 这里需要解析 - System.out.println("interfaceCount:" + interfaceCount); - } -} - diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/method/Method.java b/students/769232552/season_one/main/java/season_1/mini_jvm/method/Method.java deleted file mode 100644 index 6c026d3eb1..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/method/Method.java +++ /dev/null @@ -1,151 +0,0 @@ -package mini_jvm.method; - - -import mini_jvm.attr.AttributeInfo; -import mini_jvm.attr.CodeAttr; -import mini_jvm.clz.ClassFile; -import mini_jvm.cmd.ByteCodeCommand; -import mini_jvm.constant.ConstantPool; -import mini_jvm.constant.UTF8Info; -import mini_jvm.loader.ByteCodeIterator; - -import java.util.ArrayList; -import java.util.List; - -public class Method { - - private int accessFlag; - private int nameIndex; - private int descriptorIndex; - - private CodeAttr codeAttr; - private ClassFile clzFile; - - - public ClassFile getClzFile() { - return clzFile; - } - - public int getNameIndex() { - return nameIndex; - } - public int getDescriptorIndex() { - return descriptorIndex; - } - - public CodeAttr getCodeAttr() { - return codeAttr; - } - - public void setCodeAttr(CodeAttr code) { - this.codeAttr = code; - } - - public Method(ClassFile clzFile,int accessFlag, int nameIndex, int descriptorIndex) { - this.clzFile = clzFile; - this.accessFlag = accessFlag; - this.nameIndex = nameIndex; - this.descriptorIndex = descriptorIndex; - } - - public static Method parse(ClassFile clzFile, ByteCodeIterator iterator){ - int accessFlag = iterator.nextU2ToInt(); - int nameIndex = iterator.nextU2ToInt(); - int descIndex = iterator.nextU2ToInt(); - int attrCount = iterator.nextU2ToInt(); - - Method m = new Method(clzFile,accessFlag,nameIndex,descIndex); - - for (int i = 0; i < attrCount; i++) { - int attrNameIndex = iterator.nextU2ToInt(); - String attrName = clzFile.getConstantPool().getUTF8String(attrNameIndex); - iterator.back(2); - - if(AttributeInfo.CODE.equalsIgnoreCase(attrName)){ - CodeAttr codeAttr = CodeAttr.parse(clzFile,iterator); - m.setCodeAttr(codeAttr); - }else { - throw new RuntimeException("only CODE attribute is implemented , please implement the "+ attrName); - } - } - return m; - } - - - public String toString() { - - ConstantPool pool = this.clzFile.getConstantPool(); - StringBuilder buffer = new StringBuilder(); - String name = ((UTF8Info)pool.getConstantInfo(this.nameIndex)).getValue(); - String desc = ((UTF8Info)pool.getConstantInfo(this.descriptorIndex)).getValue(); - buffer.append(name).append(":").append(desc).append("\n"); - buffer.append(this.codeAttr.toString(pool)); - return buffer.toString(); - } - - - public ByteCodeCommand[] getCmds() { - return this.getCodeAttr().getCmds(); - } - - - private String getParamAndReturnType(){ - UTF8Info nameAndTypeInfo = (UTF8Info)this.getClzFile() - .getConstantPool().getConstantInfo(this.getDescriptorIndex()); - return nameAndTypeInfo.getValue(); - } - - public List getParameterList(){ - - // e.g. (Ljava/util/List;Ljava/lang/String;II)V - String paramAndType = getParamAndReturnType(); - - int first = paramAndType.indexOf("("); - int last = paramAndType.lastIndexOf(")"); - // e.g. Ljava/util/List;Ljava/lang/String;II - String param = paramAndType.substring(first+1, last); - - List paramList = new ArrayList(); - - if((null == param) || "".equals(param)){ - return paramList; - } - - while(!param.equals("")){ - - int pos = 0; - // 这是一个对象类型 - if(param.charAt(pos) == 'L'){ - - int end = param.indexOf(";"); - - if(end == -1){ - throw new RuntimeException("can't find the ; for a object type"); - } - paramList.add(param.substring(pos+1,end)); - - pos = end + 1; - - } - else if(param.charAt(pos) == 'I'){ - // int - paramList.add("I"); - pos ++; - - } - else if(param.charAt(pos) == 'F'){ - // float - paramList.add("F"); - pos ++; - - } else{ - throw new RuntimeException("the param has unsupported type:" + param); - } - - param = param.substring(pos); - - } - return paramList; - - } -} diff --git a/students/769232552/season_one/main/java/season_1/mini_jvm/util/Util.java b/students/769232552/season_one/main/java/season_1/mini_jvm/util/Util.java deleted file mode 100644 index 49f6bc0e39..0000000000 --- a/students/769232552/season_one/main/java/season_1/mini_jvm/util/Util.java +++ /dev/null @@ -1,22 +0,0 @@ -package mini_jvm.util; - -public class Util { - public static int byteToInt(byte[] codes){ - String s1 = byteToHexString(codes); - return Integer.valueOf(s1, 16).intValue(); - } - - public static String byteToHexString(byte[] codes){ - StringBuffer buffer = new StringBuffer(); - for(int i=0;i binaryTree1 = new BinaryTree(); - BinaryTree binaryTree2 = new BinaryTree(); - Integer[] array1 = new Integer[]{3,4,1,2,5}; - Integer[] array2 = new Integer[]{3,1,4,5,2}; - binaryTree1.createBinaryTree(array1); - binaryTree2.createBinaryTree(array2); - binaryTree1.leftOrderScan(binaryTree1.getRoot()); - binaryTree2.leftOrderScan(binaryTree2.getRoot()); - } - - @Test - public void testInsert(){ - BinaryTree binaryTree3 = new BinaryTree(); - } -} \ No newline at end of file diff --git a/students/769232552/season_one/test/java/season_1/code01/LinkedListTest.java b/students/769232552/season_one/test/java/season_1/code01/LinkedListTest.java deleted file mode 100644 index 5481783932..0000000000 --- a/students/769232552/season_one/test/java/season_1/code01/LinkedListTest.java +++ /dev/null @@ -1,174 +0,0 @@ -package code01; - -import org.junit.Assert; -import org.junit.Test; - -/** - * Created by yaoyuan on 2017/3/8. - */ -public class LinkedListTest { - - @Test - public void testAdd() throws Exception { - - LinkedList linklist = new LinkedList(); - String[] array = new String[]{"a","b","c","d","e"}; - for (String str : array){ - linklist.add(str); - } - - // size() - Assert.assertEquals(array.length,linklist.size()); - - //add(),get() - for (int i = 0; i < linklist.size(); i++){ - Assert.assertEquals(array[i],linklist.get(i)); - } - - } - - @Test - public void testAddWithIndex() throws Exception { - LinkedList linklist = new LinkedList(); - String[] array = new String[]{"a","b","c","d","e"}; - for (String str : array){ - linklist.add(str); - } - - //add(),get() - for (int i = 0; i < linklist.size(); i++){ - Assert.assertEquals(array[i],linklist.get(i)); - } - - String str = "new"; - linklist.add(0,str); - Assert.assertEquals(str,linklist.get(0)); - - linklist.add(3,str); - Assert.assertEquals(str,linklist.get(3)); - - linklist.add(linklist.size() ,str); - Assert.assertEquals(str,linklist.get(linklist.size()-1)); - } - - @Test - public void testRemove() throws Exception { - LinkedList linklist = new LinkedList(); - String[] array = new String[]{"a","b","c","d","e"}; - for (String str : array){ - linklist.add(str); - } - - //remove(),get() - Assert.assertEquals(linklist.remove(0),array[0]); - Assert.assertEquals(linklist.size(),array.length - 1); - - Assert.assertEquals(linklist.remove(linklist.size() - 1),array[array.length-1]); - Assert.assertEquals(linklist.size(),array.length - 2); - - } - - @Test - public void testAddFirst() throws Exception { - LinkedList linklist = new LinkedList(); - String[] array = new String[]{"a","b","c","d","e"}; - for (String str : array){ - linklist.add(str); - } - //addFirst(),get() - String str = "new"; - linklist.addFirst(str); - Assert.assertEquals(str,linklist.get(0)); - Assert.assertEquals(linklist.size(),array.length + 1); - } - - @Test - public void testAddLast() throws Exception { - LinkedList linklist = new LinkedList(); - String[] array = new String[]{"a","b","c","d","e"}; - for (String str : array){ - linklist.add(str); - } - //addLast(),get() - String str = "new"; - linklist.addLast(str); - Assert.assertEquals(str,linklist.get(linklist.size()-1)); - Assert.assertEquals(linklist.size(),array.length + 1); - } - - @Test - public void testRemoveFirst() throws Exception { - LinkedList linklist = new LinkedList(); - String[] array = new String[]{"a","b","c","d","e"}; - for (String str : array){ - linklist.add(str); - } - //removeFirst(),get() - Assert.assertEquals(linklist.removeFirst(),array[0]); - Assert.assertEquals(linklist.size(),array.length - 1); - } - - @Test - public void testRemoveLast() throws Exception { - LinkedList linklist = new LinkedList(); - String[] array = new String[]{"a","b","c","d","e"}; - for (String str : array){ - linklist.add(str); - } - //removeLast(),get() - Assert.assertEquals(linklist.removeLast(),array[array.length-1]); - Assert.assertEquals(linklist.size(),array.length - 1); - - } - - @Test - public void testReverse(){ - LinkedList linklist = new LinkedList(); - String[] array = new String[]{"a","b","c","d","e"}; - for (String str : array){ - linklist.add(str); - } - linklist.reverse(); - for(int i=0; i params = new HashMap(); - params.put("name","test"); - params.put("password","1234"); - - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); - Assert.assertEquals("login successful", view.getParameters().get("Message")); - } - - @Test - public void testLoginActionFailed() { - String actionName = "login"; - Map params = new HashMap(); - params.put("name","test"); - params.put("password","123456"); //密码和预设的不一致 - - View view = Struts.runAction(actionName,params); - - Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); - Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("Message")); - } -} diff --git a/students/769232552/season_one/test/java/season_1/code03/FileDownloaderTest.java b/students/769232552/season_one/test/java/season_1/code03/FileDownloaderTest.java deleted file mode 100644 index 64d2d93c99..0000000000 --- a/students/769232552/season_one/test/java/season_1/code03/FileDownloaderTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package code03; - -import code03.v1.FileDownloader; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import code03.v1.api.ConnectionManager; -import code03.v1.api.DownloadListener; -import code03.v1.impl.ConnectionManagerImpl; - -public class FileDownloaderTest { - boolean downloadFinished = false; - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testDownload() { - - String url = "http://litten.me/assets/blogImg/litten.png"; - - FileDownloader downloader = new FileDownloader(url); - - - ConnectionManager cm = new ConnectionManagerImpl(); - downloader.setConnectionManager(cm); - - downloader.setListener(new DownloadListener() { - @Override - public void notifyFinished() { - downloadFinished = true; - } - - }); - - - downloader.execute(); - - // 等待多线程下载程序执行完毕 - while (!downloadFinished) { - try { - System.out.println("还没有下载完成,休眠五秒"); - //休眠5秒 - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - System.out.println("下载完成!"); - - - - } - -} diff --git a/students/769232552/season_one/test/java/season_1/code04/LRUPageFrameTest.java b/students/769232552/season_one/test/java/season_1/code04/LRUPageFrameTest.java deleted file mode 100644 index 7448e3ee56..0000000000 --- a/students/769232552/season_one/test/java/season_1/code04/LRUPageFrameTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package code04; - -import org.junit.Assert; - -import org.junit.Test; - - -public class LRUPageFrameTest { - - @Test - public void testAccess() { - LRUPageFrame frame = new LRUPageFrame(3); - frame.access(7); - frame.access(0); - frame.access(1); - //1,0,7 - Assert.assertEquals("1,0,7", frame.toString()); - frame.access(2); - //2,1,0 - Assert.assertEquals("2,1,0", frame.toString()); - frame.access(0); - //0,2,1 - Assert.assertEquals("0,2,1", frame.toString()); - frame.access(0); - //0,2,1 - Assert.assertEquals("0,2,1", frame.toString()); - frame.access(3); - //3,0,2 - Assert.assertEquals("3,0,2", frame.toString()); - frame.access(0); - //0,3,2 - Assert.assertEquals("0,3,2", frame.toString()); - frame.access(4); - //4,0,3 - Assert.assertEquals("4,0,3", frame.toString()); - } - -} diff --git a/students/769232552/season_one/test/java/season_1/code05/StackUtilTest.java b/students/769232552/season_one/test/java/season_1/code05/StackUtilTest.java deleted file mode 100644 index e745e60fbc..0000000000 --- a/students/769232552/season_one/test/java/season_1/code05/StackUtilTest.java +++ /dev/null @@ -1,79 +0,0 @@ -package code05; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - -/** - * Created by yaoyuan on 2017/4/6. - */ -public class StackUtilTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testAddToBottom() { - Stack s = new Stack(); - s.push(1); - s.push(2); - s.push(3); - - StackUtil.addToBottom(s, 0); - Assert.assertEquals("[0, 1, 2, 3]", s.toString()); - - } - - @Test - public void testReverse() { - Stack s = new Stack(); - s.push(1); - s.push(2); - s.push(3); - s.push(4); - s.push(5); - Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString()); - StackUtil.reverse(s); - Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString()); - } - - @Test - public void testRemove() { - Stack s = new Stack(); - s.push(1); - s.push(2); - s.push(3); - StackUtil.remove(s, 2); - Assert.assertEquals("[1, 3]", s.toString()); - } - - @Test - public void testGetTop() throws Exception { - Stack s = new Stack(); - s.push(1); - s.push(2); - s.push(3); - s.push(4); - s.push(5); - { - Object[] values = StackUtil.getTop(s, 3); - Assert.assertEquals(5, values[0]); - Assert.assertEquals(4, values[1]); - Assert.assertEquals(3, values[2]); - } - } - - @Test - public void testIsValidPairs() { - Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])")); - Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})")); - } - -} \ No newline at end of file diff --git a/students/769232552/season_one/test/java/season_1/code06/InfixExprTest.java b/students/769232552/season_one/test/java/season_1/code06/InfixExprTest.java deleted file mode 100644 index af0ea63370..0000000000 --- a/students/769232552/season_one/test/java/season_1/code06/InfixExprTest.java +++ /dev/null @@ -1,49 +0,0 @@ -package code06; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - -public class InfixExprTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEvaluate() { - //InfixExpr expr = new InfixExpr("300*20+12*5-20/4"); - { - InfixExpr expr = new InfixExpr("2+3*4+5"); - expr.evaluate(); - Assert.assertEquals(19.0, expr.evaluate(), 0.001f); - } - { - InfixExpr expr = new InfixExpr("3*20+12*5-40/2"); - Assert.assertEquals(100.0, expr.evaluate(), 0.001f); - } - - { - InfixExpr expr = new InfixExpr("3*20/2"); - Assert.assertEquals(30, expr.evaluate(), 0.001f); - } - - { - InfixExpr expr = new InfixExpr("20/2*3"); - Assert.assertEquals(30, expr.evaluate(), 0.001f); - } - - { - InfixExpr expr = new InfixExpr("10-30+50"); - Assert.assertEquals(30, expr.evaluate(), 0.001f); - } - - } - -} diff --git a/students/769232552/season_one/test/java/season_1/code07/PostfixExprTest.java b/students/769232552/season_one/test/java/season_1/code07/PostfixExprTest.java deleted file mode 100644 index ace42b0db2..0000000000 --- a/students/769232552/season_one/test/java/season_1/code07/PostfixExprTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package code07; - - - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class PostfixExprTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEvaluate() { - { - PostfixExpr expr = new PostfixExpr("6 5 2 3 + 8 * + 3 + *"); - Assert.assertEquals(288, expr.evaluate(),0.0f); - } - - { - //9+(3-1)*3+10/2 - PostfixExpr expr = new PostfixExpr("9 3 1 - 3 * + 10 2 / +"); - Assert.assertEquals(20, expr.evaluate(),0.0f); - } - - { - //10-2*3+50 - PostfixExpr expr = new PostfixExpr("10 2 3 * - 50 +"); - Assert.assertEquals(54, expr.evaluate(),0.0f); - } - } - -} diff --git a/students/769232552/season_one/test/java/season_1/code07/PrefixExprTest.java b/students/769232552/season_one/test/java/season_1/code07/PrefixExprTest.java deleted file mode 100644 index 6626b3ba01..0000000000 --- a/students/769232552/season_one/test/java/season_1/code07/PrefixExprTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package code07; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - -public class PrefixExprTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testEvaluate() { - { - // 2*3+4*5 - PrefixExpr expr = new PrefixExpr("+ * 2 3 * 4 5"); - Assert.assertEquals(26, expr.evaluate(),0.001f); - } - { - // 4*2 + 6+9*2/3 -8 - PrefixExpr expr = new PrefixExpr("- + + 6 / * 2 9 3 * 4 2 8"); - Assert.assertEquals(12, expr.evaluate(),0.001f); - } - { - //(3+4)*5-6 - PrefixExpr expr = new PrefixExpr("- * + 3 4 5 6"); - Assert.assertEquals(29, expr.evaluate(),0.001f); - } - { - //1+((2+3)*4)-5 - PrefixExpr expr = new PrefixExpr("- + 1 * + 2 3 4 5"); - Assert.assertEquals(16, expr.evaluate(),0.001f); - } - - - } - -} diff --git a/students/769232552/season_one/test/java/season_1/code08/JosephusTest.java b/students/769232552/season_one/test/java/season_1/code08/JosephusTest.java deleted file mode 100644 index 2212b059c1..0000000000 --- a/students/769232552/season_one/test/java/season_1/code08/JosephusTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package code08; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - - - -public class JosephusTest { - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testExecute() { - Assert.assertEquals("[1, 3, 5, 0, 4, 2, 6]", Josephus.execute(7, 2).toString()); - } - - @Test - public void testExecute2() { - - Assert.assertEquals("[2, 5, 1, 6, 4, 0, 3]", Josephus.execute(7, 3).toString()); - - } - -} diff --git a/students/769232552/season_one/test/java/season_1/code09/QuickMinStackTest.java b/students/769232552/season_one/test/java/season_1/code09/QuickMinStackTest.java deleted file mode 100644 index 8eac5ec714..0000000000 --- a/students/769232552/season_one/test/java/season_1/code09/QuickMinStackTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package code09; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * Created by yyglider on 2017/5/4. - */ -public class QuickMinStackTest { - QuickMinStack stack = new QuickMinStack(); - - @Before - public void before(){ - stack.push(3); - stack.push(4); - stack.push(2); - stack.push(1); - } - - @Test - public void findMin() throws Exception { - Assert.assertEquals(1,stack.findMin()); - stack.pop(); - Assert.assertEquals(2,stack.findMin()); - stack.pop(); - Assert.assertEquals(3,stack.findMin()); - stack.push(0); - Assert.assertEquals(0,stack.findMin()); - } - -} \ No newline at end of file diff --git a/students/769232552/season_one/test/java/season_1/code09/StackWithTwoQueuesTest.java b/students/769232552/season_one/test/java/season_1/code09/StackWithTwoQueuesTest.java deleted file mode 100644 index a01b6a5a07..0000000000 --- a/students/769232552/season_one/test/java/season_1/code09/StackWithTwoQueuesTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package code09; - -import org.junit.Assert; -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * Created by yyglider on 2017/5/4. - */ -public class StackWithTwoQueuesTest { - @Test - public void pop() throws Exception { - StackWithTwoQueues stack = new StackWithTwoQueues(); - stack.push(1); - stack.push(2); - stack.push(3); - stack.push(4); - - Assert.assertEquals(4,stack.pop()); - Assert.assertEquals(3,stack.pop()); - Assert.assertEquals(2,stack.pop()); - Assert.assertEquals(1,stack.pop()); - - } - -} \ No newline at end of file diff --git a/students/769232552/season_one/test/java/season_1/code10/BinaryTreeUtilTest.java b/students/769232552/season_one/test/java/season_1/code10/BinaryTreeUtilTest.java deleted file mode 100644 index 827ff1794b..0000000000 --- a/students/769232552/season_one/test/java/season_1/code10/BinaryTreeUtilTest.java +++ /dev/null @@ -1,79 +0,0 @@ -package code10; - -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import java.util.List; - -import static org.junit.Assert.*; - -/** - * Created by yyglider on 2017/5/9. - */ -public class BinaryTreeUtilTest { - - BinaryTreeNode root = null; - @Before - public void setUp() throws Exception { - root = new BinaryTreeNode(1); - root.setLeft(new BinaryTreeNode(2)); - root.setRight(new BinaryTreeNode(5)); - root.getLeft().setLeft(new BinaryTreeNode(3)); - root.getLeft().setRight(new BinaryTreeNode(4)); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testPreOrderVisit() { - - List result = BinaryTreeUtil.preOrderVisit(root); - Assert.assertEquals("[1, 2, 3, 4, 5]", result.toString()); - - - } - @Test - public void testInOrderVisit() { - - - List result = BinaryTreeUtil.inOrderVisit(root); - Assert.assertEquals("[3, 2, 4, 1, 5]", result.toString()); - - } - - @Test - public void testPostOrderVisit() { - - - List result = BinaryTreeUtil.postOrderVisit(root); - Assert.assertEquals("[3, 4, 2, 5, 1]", result.toString()); - - } - - - @Test - public void testInOrderVisitWithoutRecursion() { - BinaryTreeNode node = root.getLeft().getRight(); - node.setLeft(new BinaryTreeNode(6)); - node.setRight(new BinaryTreeNode(7)); - - List result = BinaryTreeUtil.inOrderWithoutRecursion(root); - Assert.assertEquals("[3, 2, 6, 4, 7, 1, 5]", result.toString()); - - } - @Test - public void testPreOrderVisitWithoutRecursion() { - BinaryTreeNode node = root.getLeft().getRight(); - node.setLeft(new BinaryTreeNode(6)); - node.setRight(new BinaryTreeNode(7)); - - List result = BinaryTreeUtil.preOrderWithoutRecursion(root); - Assert.assertEquals("[1, 2, 3, 4, 6, 7, 5]", result.toString()); - - } - -} \ No newline at end of file diff --git a/students/769232552/season_one/test/java/season_1/code11/BinarySearchTreeTest.java b/students/769232552/season_one/test/java/season_1/code11/BinarySearchTreeTest.java deleted file mode 100644 index e2f3d6a05d..0000000000 --- a/students/769232552/season_one/test/java/season_1/code11/BinarySearchTreeTest.java +++ /dev/null @@ -1,93 +0,0 @@ -package code11; - -import static org.junit.Assert.fail; - -import code10.BinaryTreeNode; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import java.util.Arrays; - - -public class BinarySearchTreeTest { - - BinarySearchTree tree = null; - - @Before - public void setUp() throws Exception { - BinaryTreeNode root = new BinaryTreeNode(6); - root.left = new BinaryTreeNode(2); - root.right = new BinaryTreeNode(8); - root.left.left = new BinaryTreeNode(1); - root.left.right = new BinaryTreeNode(4); - root.left.right.left = new BinaryTreeNode(3); - tree = new BinarySearchTree(root); - } - - @After - public void tearDown() throws Exception { - tree = null; - } - - @Test - public void testFindMin() { - Assert.assertEquals(1, tree.findMin().intValue()); - - } - - @Test - public void testFindMax() { - Assert.assertEquals(8, tree.findMax().intValue()); - } - - @Test - public void testHeight() { - Assert.assertEquals(4, tree.height()); - } - - @Test - public void testSize() { - Assert.assertEquals(6, tree.size()); - } - - @Test - public void testRemoveLeaf() { - tree.remove(3); - BinaryTreeNode root= tree.getRoot(); - Assert.assertEquals(4, root.left.right.data.intValue()); - - } - - @Test - public void testRemoveMiddleNode() { - tree.remove(2); - BinaryTreeNode root= tree.getRoot(); - Assert.assertEquals(3, root.left.data.intValue()); - Assert.assertEquals(4, root.left.right.data.intValue()); - } - - @Test - public void testLevelVisit(){ - Assert.assertEquals(Arrays.asList(6, 2, 8, 1, 4, 3), tree.levelVisit()); - } - - @Test - public void testGetLowestCommonAncestor(){ - Assert.assertEquals(new Integer(6),tree.getLowestCommonAncestor(2,8)); - Assert.assertEquals(new Integer(2),tree.getLowestCommonAncestor(1,4)); - } - - @Test - public void testGetNodesBetween(){ - Assert.assertEquals(Arrays.asList(1,2,4), tree.getNodesBetween(1,4)); - } - - @Test - public void testIsValid(){ - Assert.assertTrue(tree.isValid()); - tree.root.left = new BinaryTreeNode(12); - Assert.assertFalse(tree.isValid()); - } -} \ No newline at end of file diff --git a/students/769232552/season_one/test/java/season_1/mini_jvm/ClassFileloaderTest.java b/students/769232552/season_one/test/java/season_1/mini_jvm/ClassFileloaderTest.java deleted file mode 100644 index dca7123987..0000000000 --- a/students/769232552/season_one/test/java/season_1/mini_jvm/ClassFileloaderTest.java +++ /dev/null @@ -1,335 +0,0 @@ -package mini_jvm; - -import mini_jvm.clz.ClassFile; -import mini_jvm.clz.ClassIndex; -import mini_jvm.cmd.BiPushCmd; -import mini_jvm.cmd.ByteCodeCommand; -import mini_jvm.cmd.OneOperandCmd; -import mini_jvm.cmd.TwoOperandCmd; -import mini_jvm.constant.*; -import mini_jvm.field.Field; -import mini_jvm.loader.ClassFileLoader; -import mini_jvm.method.Method; -import mini_jvm.util.Util; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import java.util.List; - - -public class ClassFileloaderTest { - - - private static final String FULL_QUALIFIED_CLASS_NAME = "mini_jvm/test/EmployeeV1"; - static String path1 = "D:\\worksapce\\gitRepo\\coding2017\\group23\\769232552\\coding\\src\\test\\resources"; - - static String path2 = "C:\\temp"; - - static ClassFile clzFile = null; - static { - ClassFileLoader loader = new ClassFileLoader(); - loader.addClassPath(path1); - String className = "mini_jvm.test.EmployeeV1"; - - clzFile = loader.loadClass(className); - clzFile.print(); - } - - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testClassPath(){ - - ClassFileLoader loader = new ClassFileLoader(); - loader.addClassPath(path1); - loader.addClassPath(path2); - - String clzPath = loader.getClassPath(); - - Assert.assertEquals(path1+";"+path2,clzPath); - - } - - @Test - public void testClassFileLength() { - - ClassFileLoader loader = new ClassFileLoader(); - loader.addClassPath(path1); - - String className = "com.coderising.jvm.test.EmployeeV1"; - - byte[] byteCodes = loader.readBinaryCode(className); - - // 注意:这个字节数可能和你的JVM版本有关系, 你可以看看编译好的类到底有多大 - Assert.assertEquals(1056, byteCodes.length); - - } - - - @Test - public void testMagicNumber(){ - ClassFileLoader loader = new ClassFileLoader(); - loader.addClassPath(path1); - String className = "com.coderising.jvm.test.EmployeeV1"; - byte[] byteCodes = loader.readBinaryCode(className); - byte[] codes = new byte[]{byteCodes[0],byteCodes[1],byteCodes[2],byteCodes[3]}; - - - String acctualValue = Util.byteToHexString(codes); - - Assert.assertEquals("cafebabe", acctualValue); - } - - - - - - /** - * ---------------------------------------------------------------------- - */ - - - @Test - public void testVersion(){ - - Assert.assertEquals(0, clzFile.getMinorVersion()); - Assert.assertEquals(52, clzFile.getMajorVersion()); - - } - - @Test - public void testConstantPool(){ - - - ConstantPool pool = clzFile.getConstantPool(); - - Assert.assertEquals(53, pool.getSize()); - - { - ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(1); - Assert.assertEquals(2, clzInfo.getUtf8Index()); - - UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(2); - Assert.assertEquals("com/coderising/jvm/test/EmployeeV1", utf8Info.getValue()); - } - { - ClassInfo clzInfo = (ClassInfo) pool.getConstantInfo(3); - Assert.assertEquals(4, clzInfo.getUtf8Index()); - - UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(4); - Assert.assertEquals("java/lang/Object", utf8Info.getValue()); - } - { - UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(5); - Assert.assertEquals("name", utf8Info.getValue()); - - utf8Info = (UTF8Info) pool.getConstantInfo(6); - Assert.assertEquals("Ljava/lang/String;", utf8Info.getValue()); - - utf8Info = (UTF8Info) pool.getConstantInfo(7); - Assert.assertEquals("age", utf8Info.getValue()); - - utf8Info = (UTF8Info) pool.getConstantInfo(8); - Assert.assertEquals("I", utf8Info.getValue()); - - utf8Info = (UTF8Info) pool.getConstantInfo(9); - Assert.assertEquals("", utf8Info.getValue()); - - utf8Info = (UTF8Info) pool.getConstantInfo(10); - Assert.assertEquals("(Ljava/lang/String;I)V", utf8Info.getValue()); - - utf8Info = (UTF8Info) pool.getConstantInfo(11); - Assert.assertEquals("Code", utf8Info.getValue()); - } - - { - MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(12); - Assert.assertEquals(3, methodRef.getClassInfoIndex()); - Assert.assertEquals(13, methodRef.getNameAndTypeIndex()); - } - - { - NameAndTypeInfo nameAndType = (NameAndTypeInfo) pool.getConstantInfo(13); - Assert.assertEquals(9, nameAndType.getIndex1()); - Assert.assertEquals(14, nameAndType.getIndex2()); - } - //抽查几个吧 - { - MethodRefInfo methodRef = (MethodRefInfo)pool.getConstantInfo(45); - Assert.assertEquals(1, methodRef.getClassInfoIndex()); - Assert.assertEquals(46, methodRef.getNameAndTypeIndex()); - } - - { - UTF8Info utf8Info = (UTF8Info) pool.getConstantInfo(53); - Assert.assertEquals("EmployeeV1.java", utf8Info.getValue()); - } - } - @Test - public void testClassIndex(){ - - ClassIndex clzIndex = clzFile.getClzIndex(); - ClassInfo thisClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getThisClassIndex()); - ClassInfo superClassInfo = (ClassInfo)clzFile.getConstantPool().getConstantInfo(clzIndex.getSuperClassIndex()); - - - Assert.assertEquals("com/coderising/jvm/test/EmployeeV1", thisClassInfo.getClassName()); - Assert.assertEquals("java/lang/Object", superClassInfo.getClassName()); - } - - /** - * 下面是第三次JVM课应实现的测试用例 - */ - @Test - public void testReadFields(){ - - List fields = clzFile.getFields(); - Assert.assertEquals(2, fields.size()); - { - Field f = fields.get(0); - Assert.assertEquals("name:Ljava/lang/String;", f.toString()); - } - { - Field f = fields.get(1); - Assert.assertEquals("age:I", f.toString()); - } - } - @Test - public void testMethods(){ - - List methods = clzFile.getMethods(); - ConstantPool pool = clzFile.getConstantPool(); - - { - Method m = methods.get(0); - assertMethodEquals(pool,m, - "", - "(Ljava/lang/String;I)V", - "2ab7000c2a2bb5000f2a1cb50011b1"); - - } - { - Method m = methods.get(1); - assertMethodEquals(pool,m, - "setName", - "(Ljava/lang/String;)V", - "2a2bb5000fb1"); - - } - { - Method m = methods.get(2); - assertMethodEquals(pool,m, - "setAge", - "(I)V", - "2a1bb50011b1"); - } - { - Method m = methods.get(3); - assertMethodEquals(pool,m, - "sayHello", - "()V", - "b2001c1222b60024b1"); - - } - { - Method m = methods.get(4); - assertMethodEquals(pool,m, - "main", - "([Ljava/lang/String;)V", - "bb000159122b101db7002d4c2bb6002fb1"); - } - } - - private void assertMethodEquals(ConstantPool pool,Method m , String expectedName, String expectedDesc,String expectedCode){ - String methodName = pool.getUTF8String(m.getNameIndex()); - String methodDesc = pool.getUTF8String(m.getDescriptorIndex()); - String code = m.getCodeAttr().getCode(); - Assert.assertEquals(expectedName, methodName); - Assert.assertEquals(expectedDesc, methodDesc); - Assert.assertEquals(expectedCode, code); - } - - @Test - public void testByteCodeCommand(){ - { - Method initMethod = this.clzFile.getMethod("", "(Ljava/lang/String;I)V"); - ByteCodeCommand [] cmds = initMethod.getCmds(); - - assertOpCodeEquals("0: aload_0", cmds[0]); - assertOpCodeEquals("1: invokespecial #12", cmds[1]); - assertOpCodeEquals("4: aload_0", cmds[2]); - assertOpCodeEquals("5: aload_1", cmds[3]); - assertOpCodeEquals("6: putfield #15", cmds[4]); - assertOpCodeEquals("9: aload_0", cmds[5]); - assertOpCodeEquals("10: iload_2", cmds[6]); - assertOpCodeEquals("11: putfield #17", cmds[7]); - assertOpCodeEquals("14: return", cmds[8]); - } - - { - Method setNameMethod = this.clzFile.getMethod("setName", "(Ljava/lang/String;)V"); - ByteCodeCommand [] cmds = setNameMethod.getCmds(); - - assertOpCodeEquals("0: aload_0", cmds[0]); - assertOpCodeEquals("1: aload_1", cmds[1]); - assertOpCodeEquals("2: putfield #15", cmds[2]); - assertOpCodeEquals("5: return", cmds[3]); - - } - - { - Method sayHelloMethod = this.clzFile.getMethod("sayHello", "()V"); - ByteCodeCommand [] cmds = sayHelloMethod.getCmds(); - - assertOpCodeEquals("0: getstatic #28", cmds[0]); - assertOpCodeEquals("3: ldc #34", cmds[1]); - assertOpCodeEquals("5: invokevirtual #36", cmds[2]); - assertOpCodeEquals("8: return", cmds[3]); - - } - - { - Method mainMethod = this.clzFile.getMainMethod(); - - ByteCodeCommand [] cmds = mainMethod.getCmds(); - - assertOpCodeEquals("0: new #1", cmds[0]); - assertOpCodeEquals("3: dup", cmds[1]); - assertOpCodeEquals("4: ldc #43", cmds[2]); - assertOpCodeEquals("6: bipush 29", cmds[3]); - assertOpCodeEquals("8: invokespecial #45", cmds[4]); - assertOpCodeEquals("11: astore_1", cmds[5]); - assertOpCodeEquals("12: aload_1", cmds[6]); - assertOpCodeEquals("13: invokevirtual #47", cmds[7]); - assertOpCodeEquals("16: return", cmds[8]); - } - - } - - private void assertOpCodeEquals(String expected, ByteCodeCommand cmd){ - - String acctual = cmd.getOffset()+": "+cmd.getReadableCodeText(); - - if(cmd instanceof OneOperandCmd){ - if(cmd instanceof BiPushCmd){ - acctual += " " + ((OneOperandCmd)cmd).getOperand(); - } else{ - acctual += " #" + ((OneOperandCmd)cmd).getOperand(); - } - } - if(cmd instanceof TwoOperandCmd){ - acctual += " #" + ((TwoOperandCmd)cmd).getIndex(); - } - Assert.assertEquals(expected, acctual); - } - -} diff --git a/students/769232552/season_one/test/java/season_1/mini_jvm/EmployeeV1.java b/students/769232552/season_one/test/java/season_1/mini_jvm/EmployeeV1.java deleted file mode 100644 index b06f72b9e2..0000000000 --- a/students/769232552/season_one/test/java/season_1/mini_jvm/EmployeeV1.java +++ /dev/null @@ -1,28 +0,0 @@ -package mini_jvm; - -public class EmployeeV1 { - - - private String name; - private int age; - - public EmployeeV1(String name, int age) { - this.name = name; - this.age = age; - } - - public void setName(String name) { - this.name = name; - } - public void setAge(int age){ - this.age = age; - } - public void sayHello() { - System.out.println("Hello , this is class Employee "); - } - public static void main(String[] args){ - EmployeeV1 p = new EmployeeV1("Andy",29); - p.sayHello(); - - } -} \ No newline at end of file diff --git a/students/769232552/season_one/test/java/season_1/mini_jvm/EmployeeV2.java b/students/769232552/season_one/test/java/season_1/mini_jvm/EmployeeV2.java deleted file mode 100644 index 54a7ce4713..0000000000 --- a/students/769232552/season_one/test/java/season_1/mini_jvm/EmployeeV2.java +++ /dev/null @@ -1,54 +0,0 @@ -package mini_jvm; - -public class EmployeeV2 { - - public final static String TEAM_NAME = "Dev Team"; - private String name; - private int age; - public EmployeeV2(String name, int age) { - this.name = name; - this.age = age; - } - - public void sayHello() { - System.out.println("Hello , this is class Employee "); - System.out.println(TEAM_NAME); - System.out.println(this.name); - } - - public void setName(String name) { - this.name = name; - } - - public void setAge(int age) { - this.age = age; - } - - - - public void isYouth() { - if (age < 40) { - System.out.println("You're still young"); - } else { - System.out.println("You're old"); - } - } - - - - public void testAdd() { - int sum = 0; - for (int i = 1; i <= 100; i++) { - sum += i; - } - System.out.println(sum); - } - - - public static void main(String[] args) { - EmployeeV2 p = new EmployeeV2("Andy", 35); - p.sayHello(); - p.isYouth(); - p.testAdd(); - } -} \ No newline at end of file diff --git a/students/769232552/season_one/test/java/season_1/mini_jvm/Example.java b/students/769232552/season_one/test/java/season_1/mini_jvm/Example.java deleted file mode 100644 index 01526633b6..0000000000 --- a/students/769232552/season_one/test/java/season_1/mini_jvm/Example.java +++ /dev/null @@ -1,16 +0,0 @@ -package mini_jvm; - -public class Example{ - public void disp(char c){ - System.out.println(c); - } - public void disp(int c){ - System.out.println(c ); - } - public static void main(String args[]){ - Example obj = new Example(); - obj.disp('a'); - obj.disp(5); - } -} - diff --git a/students/769232552/season_one/test/java/season_1/mini_jvm/HourlyEmployee.java b/students/769232552/season_one/test/java/season_1/mini_jvm/HourlyEmployee.java deleted file mode 100644 index 289ece0647..0000000000 --- a/students/769232552/season_one/test/java/season_1/mini_jvm/HourlyEmployee.java +++ /dev/null @@ -1,27 +0,0 @@ -package mini_jvm; - -public class HourlyEmployee extends EmployeeV2 { - - int hourlySalary; - - public HourlyEmployee(String name, - int age, int hourlySalary) { - super(name, age); - this.hourlySalary = hourlySalary; - } - - public void sayHello(){ - System.out.println("Hello , this is Hourly Employee"); - } - public static void main(String[] args){ - EmployeeV2 e = new HourlyEmployee("Lisa", 20, 40); - e.sayHello(); - } - - public int getHourlySalary(){ - return this.hourlySalary; - } - - - -} diff --git a/students/769232552/season_one/test/java/season_1/mini_jvm/MiniJVMTest.java b/students/769232552/season_one/test/java/season_1/mini_jvm/MiniJVMTest.java deleted file mode 100644 index 2ecc54f150..0000000000 --- a/students/769232552/season_one/test/java/season_1/mini_jvm/MiniJVMTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package mini_jvm; - - -import mini_jvm.engine.MiniJVM; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -public class MiniJVMTest { - - static final String PATH = "C:\\Users\\liuxin\\git\\coding2017\\liuxin\\mini-jvm\\answer\\bin"; - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testMain() throws Exception{ - String[] classPaths = {PATH}; - MiniJVM jvm = new MiniJVM(); - jvm.run(classPaths, "com.coderising.jvm.test.HourlyEmployee"); - - } - -} From e92eb19a345e4dd0feea8c4da1876bcf315b1fdb Mon Sep 17 00:00:00 2001 From: yuyingzhi828 <81681981@qq.com> Date: Mon, 19 Jun 2017 15:13:33 +0800 Subject: [PATCH 213/214] Update .gitignore --- .gitignore | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.gitignore b/.gitignore index f1e9957cfa..275756fb99 100644 --- a/.gitignore +++ b/.gitignore @@ -280,6 +280,22 @@ target liuxin/.DS_Store liuxin/src/.DS_Store +students/1005475328/* +students/1329920463/* +students/1452302762/* +students/14703250/* +students/2842295913/* +students/383117348/* +students/404481481/* +students/406400373/* +students/549739951/* +students/582161208/* +students/592146505/* +students/740707954/* +students/844620174/* +students/87049319/* +students/183549495/* + From b81c9a1584cb0e0556bdb0970db6d83b68447d79 Mon Sep 17 00:00:00 2001 From: onlyliuxin <14703250@qq.com> Date: Mon, 19 Jun 2017 15:23:03 +0800 Subject: [PATCH 214/214] remove code --- .../coderising/ood/config/Configuration.java | 23 ------- .../ood/config/ConfigurationKeys.java | 9 --- .../coderising/ood/file/product_promotion.txt | 4 -- .../java/com/coderising/ood/pojo/Email.java | 49 --------------- .../ood/pojo/EmailServiceConfig.java | 45 ------------- .../java/com/coderising/ood/pojo/Product.java | 37 ----------- .../java/com/coderising/ood/pojo/User.java | 29 --------- .../coderising/ood/service/EmailService.java | 55 ---------------- .../ood/service/ProductService.java | 52 --------------- .../coderising/ood/service/PromotionMail.java | 63 ------------------- .../coderising/ood/service/UserService.java | 44 ------------- 11 files changed, 410 deletions(-) delete mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/config/Configuration.java delete mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/config/ConfigurationKeys.java delete mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/file/product_promotion.txt delete mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/Email.java delete mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/EmailServiceConfig.java delete mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/Product.java delete mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/User.java delete mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/EmailService.java delete mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/ProductService.java delete mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/PromotionMail.java delete mode 100644 group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/UserService.java diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/config/Configuration.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/config/Configuration.java deleted file mode 100644 index a3f51236bc..0000000000 --- a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/config/Configuration.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.coderising.ood.config; -import java.util.HashMap; -import java.util.Map; - -public class Configuration { - - static Map configurations = new HashMap(); - static{ - configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); - configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); - configurations.put(ConfigurationKeys.EMAIL_ADMIN, "admin@company.com"); - } - /** - * 应该从配置文件读, 但是这里简化为直接从一个map 中去读 - * @param key - * @return - */ - public String getProperty(String key) { - - return configurations.get(key); - } - -} diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/config/ConfigurationKeys.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/config/ConfigurationKeys.java deleted file mode 100644 index e7c449f10c..0000000000 --- a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/config/ConfigurationKeys.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.coderising.ood.config; - -public class ConfigurationKeys { - - public static final String SMTP_SERVER = "smtp.server"; - public static final String ALT_SMTP_SERVER = "alt.smtp.server"; - public static final String EMAIL_ADMIN = "email.admin"; - -} diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/file/product_promotion.txt b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/file/product_promotion.txt deleted file mode 100644 index b7a974adb3..0000000000 --- a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/file/product_promotion.txt +++ /dev/null @@ -1,4 +0,0 @@ -P8756 iPhone8 -P3946 XiaoMi10 -P8904 Oppo_R15 -P4955 Vivo_X20 \ No newline at end of file diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/Email.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/Email.java deleted file mode 100644 index ed92dfec0b..0000000000 --- a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/Email.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.coderising.ood.pojo; - -/** - * @author xyy - * @create 2017-06-19 9:44 - **/ -public class Email { - - - private String toAddress; - private String subject; - private String message; - - public Email() { - } - - public Email(String toAddress, String subject, String message) { - this.toAddress = toAddress; - this.subject = subject; - this.message = message; - } - - public String getToAddress() { - return toAddress; - } - - public Email setToAddress(String toAddress) { - this.toAddress = toAddress; - return this; - } - - public String getSubject() { - return subject; - } - - public Email setSubject(String subject) { - this.subject = subject; - return this; - } - - public String getMessage() { - return message; - } - - public Email setMessage(String message) { - this.message = message; - return this; - } -} diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/EmailServiceConfig.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/EmailServiceConfig.java deleted file mode 100644 index 091583b3ed..0000000000 --- a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/EmailServiceConfig.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.coderising.ood.pojo; - -/** - * @author xyy - * @create 2017-06-19 10:00 - **/ -public class EmailServiceConfig { - - private String smtpHost; - private String altSmtpHost; - private String fromAddress; - - public EmailServiceConfig(String smtpHost, String altSmtpHost, String fromAddress) { - this.smtpHost = smtpHost; - this.altSmtpHost = altSmtpHost; - this.fromAddress = fromAddress; - } - - public String getSmtpHost() { - return smtpHost; - } - - public EmailServiceConfig setSmtpHost(String smtpHost) { - this.smtpHost = smtpHost; - return this; - } - - public String getAltSmtpHost() { - return altSmtpHost; - } - - public EmailServiceConfig setAltSmtpHost(String altSmtpHost) { - this.altSmtpHost = altSmtpHost; - return this; - } - - public String getFromAddress() { - return fromAddress; - } - - public EmailServiceConfig setFromAddress(String fromAddress) { - this.fromAddress = fromAddress; - return this; - } -} diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/Product.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/Product.java deleted file mode 100644 index f869bf1f12..0000000000 --- a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/Product.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.coderising.ood.pojo; - -/** - * 产品类 - * - * @author xyy - * @create 2017-06-19 9:30 - **/ -public class Product { - - - private String productID; - private String productDesc; - - - - - - - public String getProductID() { - return productID; - } - - public Product setProductID(String productID) { - this.productID = productID; - return this; - } - - public String getProductDesc() { - return productDesc; - } - - public Product setProductDesc(String productDesc) { - this.productDesc = productDesc; - return this; - } -} diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/User.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/User.java deleted file mode 100644 index 69975f6cbd..0000000000 --- a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/pojo/User.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.coderising.ood.pojo; - -/** - * @author xyy - * @create 2017-06-19 9:48 - **/ -public class User { - - private String name; - private String email; - - public String getName() { - return name; - } - - public User setName(String name) { - this.name = name; - return this; - } - - public String getEmail() { - return email; - } - - public User setEmail(String email) { - this.email = email; - return this; - } -} diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/EmailService.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/EmailService.java deleted file mode 100644 index 2fece21ec2..0000000000 --- a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/EmailService.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.coderising.ood.service; - -import com.coderising.ood.pojo.Email; -import com.coderising.ood.pojo.EmailServiceConfig; -import com.coderising.ood.pojo.Product; -import com.coderising.ood.pojo.User; - -import java.io.IOException; - -/** - * @author xyy - * @create 2017-06-19 9:44 - **/ -public class EmailService { - - - public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, - boolean debug) { - //假装发了一封邮件 - StringBuilder buffer = new StringBuilder(); - buffer.append("From:").append(fromAddress).append("\n"); - buffer.append("To:").append(toAddress).append("\n"); - buffer.append("Subject:").append(subject).append("\n"); - buffer.append("Content:").append(message).append("\n"); - System.out.println(buffer.toString()); - - } - - - public static Email configureEMail(User user, Product product) throws IOException { - String toAddress = user.getEmail(); - String name = ""; - if (toAddress.length() > 0) { - name = user.getName(); - } - String subject = "您关注的产品降价了"; - String message = "尊敬的 " + name + ", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!"; - Email email = new Email(toAddress, subject, message); - return email; - } - - public static void sendEmail(EmailServiceConfig emailServiceConfig, Email email, boolean debug) { - try { - if (email.getToAddress().length() > 0) { - sendEmail(email.getToAddress(), emailServiceConfig.getFromAddress(), email.getSubject(), email.getMessage(), emailServiceConfig.getSmtpHost(), debug); - } - } catch (Exception e) { - try { - sendEmail(email.getToAddress(), emailServiceConfig.getFromAddress(), email.getSubject(), email.getMessage(), emailServiceConfig.getAltSmtpHost(), debug); - } catch (Exception e2) { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } - } -} diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/ProductService.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/ProductService.java deleted file mode 100644 index ccacef7bce..0000000000 --- a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/ProductService.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.coderising.ood.service; - -import com.coderising.ood.pojo.Product; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -/** - * @author xyy - * @create 2017-06-19 9:34 - **/ -public class ProductService { - - - public static List getAllProductFromFile(File file) throws IOException { - List productList = new ArrayList(); - BufferedReader br = null; - - try { - - br = new BufferedReader(new FileReader(file)); - String temp = null; - while ((temp = br.readLine()) != null) { - String[] data = temp.split(" "); - Product product = new Product(); - product.setProductID(data[0]); - product.setProductDesc(data[1]); - System.out.println("产品ID = " + product.getProductID() + "\n"); - System.out.println("产品描述 = " + product.getProductDesc() + "\n"); - productList.add(product); - } - - return productList; - - } catch (IOException e) { - e.printStackTrace(); - } finally { - try { - br.close(); - } catch (IOException e) { - e.printStackTrace(); - } - - } - return null; - } - -} diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/PromotionMail.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/PromotionMail.java deleted file mode 100644 index f89aff500a..0000000000 --- a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/PromotionMail.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.coderising.ood.service; - -import com.coderising.ood.config.Configuration; -import com.coderising.ood.config.ConfigurationKeys; -import com.coderising.ood.pojo.Email; -import com.coderising.ood.pojo.EmailServiceConfig; -import com.coderising.ood.pojo.Product; -import com.coderising.ood.pojo.User; - -import java.io.File; -import java.util.List; - -public class PromotionMail { - - public static void main(String[] args) throws Exception { - //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - File file = new File("D:\\product_promotion.txt"); - //1.获得产品信息 - ProductService productService = new ProductService(); - List productList = productService.getAllProductFromFile(file); - boolean emailDebug = false; - - PromotionMail pe = new PromotionMail(productList, emailDebug); - } - - - - public PromotionMail(List productList, boolean mailDebug) throws Exception { - //2.邮件服务器配置 - Configuration config = new Configuration(); - EmailServiceConfig emailServiceConfig = new EmailServiceConfig(config.getProperty(ConfigurationKeys.SMTP_SERVER), config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER), config.getProperty(ConfigurationKeys.EMAIL_ADMIN)); - //3.发送邮件 - sendEMails(mailDebug, productList, emailServiceConfig); - - } - - - public void sendEMails(boolean debug, List productList, EmailServiceConfig emailServiceConfig) throws Exception { - System.out.println("开始发送邮件"); - if (productList != null) { - for (Product product : productList) { - List userList = UserService.getSendEmailUser(product); - if (null != userList && userList.size() > 0) { - for (User user : userList) { - Email email = EmailService.configureEMail((user), product); - if (email.getToAddress().length() > 0) { - EmailService.sendEmail(emailServiceConfig,email,debug); - } - } - } - } - - } else { - System.out.println("没有邮件发送"); - - } - - } - - -} - - diff --git a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/UserService.java b/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/UserService.java deleted file mode 100644 index a7c6d8fe38..0000000000 --- a/group08/1425809544/06-21/ood-assignment/src/main/java/com/coderising/ood/service/UserService.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.coderising.ood.service; - -import com.coderising.ood.pojo.Product; -import com.coderising.ood.pojo.User; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author xyy - * @create 2017-06-19 9:48 - **/ -public class UserService { - - - public static List getSendEmailUser(Product product) throws Exception { - - - setLoadQuery(product); - - List userList = new ArrayList(); - - for (int i = 0; i < 3; i++) { - User user = new User(); - user.setName("user" + i); - user.setEmail(user.getName() + "@qq.com"); - userList.add(user); - } - return userList; - } - - - //通过产品id获取关注了产品的用户 - public static void setLoadQuery(Product product) throws Exception { - - String sql = "Select name from subscriptions " - + "where product_id= '" + product.getProductID() + "' " - + "and send_mail=1 "; - - System.out.println("loadQuery set"); - } - - -}