From 5d9991e983c7c1e99b06c240996ffab92fa28ab3 Mon Sep 17 00:00:00 2001 From: palmshe Date: Sun, 11 Jun 2017 19:37:43 +0800 Subject: [PATCH 01/26] =?UTF-8?q?=E6=96=B0=E5=A2=9EQQ=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/2842295913/Readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/2842295913/Readme.md diff --git a/students/2842295913/Readme.md b/students/2842295913/Readme.md new file mode 100644 index 0000000000..fdae662c31 --- /dev/null +++ b/students/2842295913/Readme.md @@ -0,0 +1 @@ +### 学着使用git \ No newline at end of file From 25004a75a57f392c1c93eee965f3a2b59d38cd42 Mon Sep 17 00:00:00 2001 From: KingJA Date: Sun, 11 Jun 2017 21:45:09 +0800 Subject: [PATCH 02/26] first pull --- students/87049319/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 students/87049319/README.md diff --git a/students/87049319/README.md b/students/87049319/README.md new file mode 100644 index 0000000000..56d6816ace --- /dev/null +++ b/students/87049319/README.md @@ -0,0 +1,3 @@ +PULL LIST +* 2017/06/11 +first pull \ No newline at end of file From f382ec5cd4b825285b9fdfb7fb00ca68e7e83e76 Mon Sep 17 00:00:00 2001 From: cenkailun Date: Sun, 11 Jun 2017 22:29:52 +0800 Subject: [PATCH 03/26] =?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 --- .../readme.md" | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 "students/406400373(\344\270\212\346\265\267-\345\207\257\344\274\246)/readme.md" diff --git "a/students/406400373(\344\270\212\346\265\267-\345\207\257\344\274\246)/readme.md" "b/students/406400373(\344\270\212\346\265\267-\345\207\257\344\274\246)/readme.md" new file mode 100644 index 0000000000..0fc025be21 --- /dev/null +++ "b/students/406400373(\344\270\212\346\265\267-\345\207\257\344\274\246)/readme.md" @@ -0,0 +1,2 @@ +406400373(上海-凯伦) +第二季的代码仓库 From 52d321931e9da2ea54cc79650c99732567f2fd2a Mon Sep 17 00:00:00 2001 From: cenkailun Date: Sun, 11 Jun 2017 22:30:24 +0800 Subject: [PATCH 04/26] =?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 --- .../readme.md" => students/406400373/readme.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "students/406400373(\344\270\212\346\265\267-\345\207\257\344\274\246)/readme.md" => students/406400373/readme.md (100%) diff --git "a/students/406400373(\344\270\212\346\265\267-\345\207\257\344\274\246)/readme.md" b/students/406400373/readme.md similarity index 100% rename from "students/406400373(\344\270\212\346\265\267-\345\207\257\344\274\246)/readme.md" rename to students/406400373/readme.md From 6f50edcd047633b96ad43a8ec2e5c90b7cf34541 Mon Sep 17 00:00:00 2001 From: tianmengwei <1329920463@qq.com> Date: Sun, 11 Jun 2017 22:36:15 +0800 Subject: [PATCH 05/26] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/1329920463/src/com/tm/test/Test.java | 8 ++++++++ students/14703250/readme.md | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 students/1329920463/src/com/tm/test/Test.java diff --git a/students/1329920463/src/com/tm/test/Test.java b/students/1329920463/src/com/tm/test/Test.java new file mode 100644 index 0000000000..1e4ea28432 --- /dev/null +++ b/students/1329920463/src/com/tm/test/Test.java @@ -0,0 +1,8 @@ +package com.tm.test; + +public class Test { + + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} 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号命名 ,把自荐的代码放到里边去 From 41798e7912d6606570eaa20c9ed3e8d02719b698 Mon Sep 17 00:00:00 2001 From: palmshe Date: Sun, 11 Jun 2017 22:53:03 +0800 Subject: [PATCH 06/26] =?UTF-8?q?=E6=8A=BD=E5=8F=96=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=95=86=E5=93=81=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/2842295913/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 +++ .../com/coderising/ood/srp/InitDataUtils.java | 61 +++++ .../java/com/coderising/ood/srp/MailUtil.java | 18 ++ .../com/coderising/ood/srp/PromotionMail.java | 209 ++++++++++++++++++ .../coderising/ood/srp/product_promotion.txt | 4 + 8 files changed, 381 insertions(+) create mode 100644 students/2842295913/ood-assignment/pom.xml create mode 100644 students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java create 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/MailUtil.java create mode 100644 students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt diff --git a/students/2842295913/ood-assignment/pom.xml b/students/2842295913/ood-assignment/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/2842295913/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/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 new file mode 100644 index 0000000000..f328c1816a --- /dev/null +++ b/students/2842295913/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/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java b/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/2842295913/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/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/2842295913/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/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 new file mode 100644 index 0000000000..35cdb939f6 --- /dev/null +++ b/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/InitDataUtils.java @@ -0,0 +1,61 @@ +/** + * 版权 (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 new file mode 100644 index 0000000000..9f9e749af7 --- /dev/null +++ b/students/2842295913/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/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 new file mode 100644 index 0000000000..f293eb1f45 --- /dev/null +++ b/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,209 @@ +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)); + } +} diff --git a/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/2842295913/ood-assignment/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/2842295913/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 94cb915ac29ad239f3f063c18360b3c63a64f5cf Mon Sep 17 00:00:00 2001 From: lx520 <740707954@qq,com> Date: Sun, 11 Jun 2017 23:26:50 +0800 Subject: [PATCH 07/26] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/740707954/readMe | 1 + 1 file changed, 1 insertion(+) create mode 100644 students/740707954/readMe diff --git a/students/740707954/readMe b/students/740707954/readMe new file mode 100644 index 0000000000..3b12464976 --- /dev/null +++ b/students/740707954/readMe @@ -0,0 +1 @@ +TEST \ No newline at end of file From 12767a1297312761fd6988d09d552227f8ea32f1 Mon Sep 17 00:00:00 2001 From: lx520 <740707954@qq,com> Date: Sun, 11 Jun 2017 23:37:12 +0800 Subject: [PATCH 08/26] =?UTF-8?q?update=202017=E5=B9=B46=E6=9C=8811?= =?UTF-8?q?=E6=97=A5=2023:37:08?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/740707954/readMe | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/students/740707954/readMe b/students/740707954/readMe index 3b12464976..a930ed0c81 100644 --- a/students/740707954/readMe +++ b/students/740707954/readMe @@ -1 +1 @@ -TEST \ No newline at end of file +TEST1 \ No newline at end of file From bcd38c54caaf1f2029c30e6af00168e28d13e7c8 Mon Sep 17 00:00:00 2001 From: baoheping Date: Mon, 12 Jun 2017 07:17:22 +0800 Subject: [PATCH 09/26] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/coderising/ood/srp/Configuration.java | 30 +++++++++ .../coderising/ood/srp/ConfigurationKeys.java | 9 +++ .../src/com/coderising/ood/srp/DBUtil.java | 25 +++++++ .../src/com/coderising/ood/srp/FileUtil.java | 30 +++++++++ .../src/com/coderising/ood/srp/MailUtil.java | 50 ++++++++++++++ .../com/coderising/ood/srp/PromotionMail.java | 67 +++++++++++++++++++ .../404481481/day01/src/product_promotion.txt | 4 ++ 7 files changed, 215 insertions(+) create mode 100644 students/404481481/day01/src/com/coderising/ood/srp/Configuration.java create mode 100644 students/404481481/day01/src/com/coderising/ood/srp/ConfigurationKeys.java create mode 100644 students/404481481/day01/src/com/coderising/ood/srp/DBUtil.java create mode 100644 students/404481481/day01/src/com/coderising/ood/srp/FileUtil.java create mode 100644 students/404481481/day01/src/com/coderising/ood/srp/MailUtil.java create mode 100644 students/404481481/day01/src/com/coderising/ood/srp/PromotionMail.java create mode 100644 students/404481481/day01/src/product_promotion.txt diff --git a/students/404481481/day01/src/com/coderising/ood/srp/Configuration.java b/students/404481481/day01/src/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..f2d42038ad --- /dev/null +++ b/students/404481481/day01/src/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,30 @@ +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); + } + + + public static void init(PromotionMail promotion){ + Configuration config = new Configuration(); + promotion.smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); + promotion.altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); + promotion.fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); + } +} diff --git a/students/404481481/day01/src/com/coderising/ood/srp/ConfigurationKeys.java b/students/404481481/day01/src/com/coderising/ood/srp/ConfigurationKeys.java new file mode 100644 index 0000000000..8695aed644 --- /dev/null +++ b/students/404481481/day01/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/404481481/day01/src/com/coderising/ood/srp/DBUtil.java b/students/404481481/day01/src/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..82e9261d18 --- /dev/null +++ b/students/404481481/day01/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/404481481/day01/src/com/coderising/ood/srp/FileUtil.java b/students/404481481/day01/src/com/coderising/ood/srp/FileUtil.java new file mode 100644 index 0000000000..dca25210f6 --- /dev/null +++ b/students/404481481/day01/src/com/coderising/ood/srp/FileUtil.java @@ -0,0 +1,30 @@ +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 void readFile(File file,PromotionMail promotion) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + promotion.productID = data[0]; + promotion.productDesc = data[1]; + + System.out.println("产品ID = " + promotion.productID + "\n"); + System.out.println("产品描述 = " + promotion.productDesc + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } +} diff --git a/students/404481481/day01/src/com/coderising/ood/srp/MailUtil.java b/students/404481481/day01/src/com/coderising/ood/srp/MailUtil.java new file mode 100644 index 0000000000..da0ccb3153 --- /dev/null +++ b/students/404481481/day01/src/com/coderising/ood/srp/MailUtil.java @@ -0,0 +1,50 @@ +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()); + + } + + public static void sendEMails(boolean debug, List mailingList, PromotionMail promotion) throws IOException { + System.out.println("开始发送邮件"); + if (mailingList != null) { + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + promotion.configureEMail((HashMap) iter.next()); + try { + if (promotion.toAddress.length() > 0) + MailUtil.sendEmail(promotion.toAddress, promotion.fromAddress, promotion.subject, + promotion.message, promotion.smtpHost, debug); + } catch (Exception e) { + + try { + MailUtil.sendEmail(promotion.toAddress, promotion.fromAddress, promotion.subject, + promotion.message, promotion.altSmtpHost, debug); + + } catch (Exception e2) { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + } else { + System.out.println("没有邮件发送"); + + } + + } +} diff --git a/students/404481481/day01/src/com/coderising/ood/srp/PromotionMail.java b/students/404481481/day01/src/com/coderising/ood/srp/PromotionMail.java new file mode 100644 index 0000000000..2472e23d3b --- /dev/null +++ b/students/404481481/day01/src/com/coderising/ood/srp/PromotionMail.java @@ -0,0 +1,67 @@ +package com.coderising.ood.srp; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +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 final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + public static void main(String[] args) throws Exception { + + File f = new File("G:\\Java\\github\\coding2017\\students\\404481481\\day01\\src\\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, this); + Configuration.init(this); + setLoadQuery(); + + MailUtil.sendEMails(mailDebug, loadMailingList(), this); + + } + + 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(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); + } + +} diff --git a/students/404481481/day01/src/product_promotion.txt b/students/404481481/day01/src/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/404481481/day01/src/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file 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 10/26] =?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 11/26] 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 242c149bdf5f5d227d8853203f46e3b984113f34 Mon Sep 17 00:00:00 2001 From: robert <592146505@qq.com> Date: Mon, 12 Jun 2017 13:20:22 +0800 Subject: [PATCH 12/26] 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 13/26] 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 14/26] =?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 15/26] =?UTF-8?q?383117348=20=E7=AC=AC=E4=B8=80=E6=AC=A1oo?= =?UTF-8?q?d=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 16/26] =?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 17/26] 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 18/26] 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 19/26] =?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 2d6c31eee79df9a75c1d714818e7d57efcd0c703 Mon Sep 17 00:00:00 2001 From: yanghuichi Date: Mon, 12 Jun 2017 20:42:32 +0800 Subject: [PATCH 20/26] 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 21/26] 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 22/26] =?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 23/26] 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 24/26] =?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 25/26] =?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 26/26] =?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