From 15c93a26d6420b265fcb31ea7977536db867820a Mon Sep 17 00:00:00 2001 From: zhan2016 Date: Sun, 5 Mar 2017 21:43:20 +0800 Subject: [PATCH 1/7] 20170305 --- group16/2562124714/.idea/misc.xml | 18 +- group16/2562124714/.idea/workspace.xml | 794 +++++++++++++++--- .../src/com/coderising/array/ArrayUtil.java | 200 +++++ .../coderising/litestruts/LoginAction.java | 39 + .../src/com/coderising/litestruts/Struts.java | 34 + .../com/coderising/litestruts/StrutsTest.java | 43 + .../src/com/coderising/litestruts/View.java | 23 + .../src/com/coderising/litestruts/struts.xml | 11 + .../src/com/coding/basic/ArrayList.java | 13 + 9 files changed, 1061 insertions(+), 114 deletions(-) create mode 100644 group16/2562124714/src/com/coderising/array/ArrayUtil.java create mode 100644 group16/2562124714/src/com/coderising/litestruts/LoginAction.java create mode 100644 group16/2562124714/src/com/coderising/litestruts/Struts.java create mode 100644 group16/2562124714/src/com/coderising/litestruts/StrutsTest.java create mode 100644 group16/2562124714/src/com/coderising/litestruts/View.java create mode 100644 group16/2562124714/src/com/coderising/litestruts/struts.xml diff --git a/group16/2562124714/.idea/misc.xml b/group16/2562124714/.idea/misc.xml index e97ef03f44..05483570e0 100644 --- a/group16/2562124714/.idea/misc.xml +++ b/group16/2562124714/.idea/misc.xml @@ -1,22 +1,6 @@ - + - - - - - 1.7 - - - - - - - \ No newline at end of file diff --git a/group16/2562124714/.idea/workspace.xml b/group16/2562124714/.idea/workspace.xml index d357c0f9a1..bba44e297b 100644 --- a/group16/2562124714/.idea/workspace.xml +++ b/group16/2562124714/.idea/workspace.xml @@ -13,49 +13,60 @@ + + + + + - - - - - - - - - - - + - - - - + + + + + + + + + + + + - + - - - + + + + + - - + + - - + + @@ -63,46 +74,78 @@ - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -130,8 +173,6 @@ + + + + true + DEFINITION_ORDER + @@ -166,6 +216,7 @@ + @@ -209,20 +260,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + + + + @@ -410,6 +521,136 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -468,6 +717,15 @@ + + + + + + + + + + + + + + + @@ -529,6 +793,22 @@ + + + + + + + + + + + + + + + + @@ -557,6 +837,8 @@ @@ -568,40 +850,56 @@ + + + + + + + + + + + - + - - + - + + - + + + + @@ -616,66 +914,269 @@ + + + + + + + + + + + + + - - + + - - - - + + + + + + - + + + + + + + + + + + + + + + + + + + + + - + - - + + + + + - + - - + + - - + + + + - + - - + + - + - + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -683,7 +1184,6 @@ - @@ -699,82 +1199,182 @@ - - - - - + + + + + + + - + - + + + - + - + + + - - + + + + + + + - - + + + + + + + - + - - - - + + + + + + + + + + + + - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1.8 + + + + + + \ No newline at end of file diff --git a/group16/2562124714/src/com/coderising/array/ArrayUtil.java b/group16/2562124714/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..215aae9f64 --- /dev/null +++ b/group16/2562124714/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,200 @@ +package com.coderising.array; + +import com.coding.basic.ArrayList; +import com.coding.basic.List; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + if (1 == origin.length || 0 == origin.length) + { + return; + } + + int temp = 0; + for (int i = 0; i < origin.length / 2; i++) + { + temp = origin[i]; + origin[i] = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = temp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public Integer[] removeZero(int[] oldArray){ + ArrayList blist = new ArrayList(); + //int j = 0; + + for(int i = 0; i < oldArray.length; i++) + { + if (0 != oldArray[i]) + { + blist.add(oldArray[i]); + } + } + + Object[] newArray = blist.ToArray(); + + return (Integer[])newArray; + } + + /** + * 给定两个已经排序好的整形数组, 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 Integer[] merge(int[] array1, int[] array2){ + ArrayList blist = new ArrayList(); + int i = 0; + + for (i = 0; i < array1.length; i++) + { + blist.add(array1[0]); + } + + for(i = 0; i < array2.length; i++) + { + for (int j = 0; j < blist.size(); j ++) + { + if (array2[i] >= (int)blist.get(j + 1)) + { + if (array2[i] == (int)blist.get(j + 1)) + { + break; + } + //已经到最后了 + if (j == blist.size() - 1) + { + if (array2[i] == (int)blist.get(j + 1)) + { + break; + } + else + { + blist.add(j + 1, array2[i]); + break; + } + } + else + { + if (array2[i] <= (int)blist.get(j + 2)) + { + if (array2[i] == (int)blist.get(j + 2)) + { + break; + } + else + { + blist.add(j + 1, array2[i]); + break; + } + } + } + + } + else + { + if (j == 0) + { + blist.add(j + 1, array2[i]); + break; + } + else + { + continue; + } + } + } + } + + return (Integer[]) blist.ToArray(); + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size){ + int[] NewArray = new int[oldArray.length + size]; + + for(int i = 0; i < NewArray.length; i++) + { + if (i < oldArray.length) { + NewArray[i] = oldArray[i]; + } + else + { + NewArray[i] = 0; + } + } + + return NewArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + 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 + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + +} diff --git a/group16/2562124714/src/com/coderising/litestruts/LoginAction.java b/group16/2562124714/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group16/2562124714/src/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/group16/2562124714/src/com/coderising/litestruts/Struts.java b/group16/2562124714/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/group16/2562124714/src/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/group16/2562124714/src/com/coderising/litestruts/StrutsTest.java b/group16/2562124714/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group16/2562124714/src/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/group16/2562124714/src/com/coderising/litestruts/View.java b/group16/2562124714/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group16/2562124714/src/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/group16/2562124714/src/com/coderising/litestruts/struts.xml b/group16/2562124714/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..a6cfe43e6c --- /dev/null +++ b/group16/2562124714/src/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/group16/2562124714/src/com/coding/basic/ArrayList.java b/group16/2562124714/src/com/coding/basic/ArrayList.java index f1d5a9fdd9..a39b8e9d42 100644 --- a/group16/2562124714/src/com/coding/basic/ArrayList.java +++ b/group16/2562124714/src/com/coding/basic/ArrayList.java @@ -1,5 +1,7 @@ package com.coding.basic; +import java.util.Objects; + public class ArrayList implements List { private int size = 0; @@ -96,5 +98,16 @@ public int size(){ public Iterator iterator(){ return null; } + + public Object[] ToArray() + { + Object [] Array = new Object[this.size]; + for (int i = 0 ; i < this.size; i ++) + { + Array[i] = this.elementData[i]; + } + + return Array; + } } From 159c0b0dea4334b11eca8ce2936930c3ba4c2b68 Mon Sep 17 00:00:00 2001 From: wanghongdi56 <313001956@qq.com> Date: Wed, 8 Mar 2017 08:44:57 +0800 Subject: [PATCH 2/7] =?UTF-8?q?0305=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group16/313001956/.classpath | 1 + .../313001956/RemoteSystemsTempFiles/.project | 12 ++ .../WebContent/WEB-INF/resource/struts.xml | 11 + .../src/com/coderising/array/ArrayUtil.java | 202 ++++++++++++++++++ .../coderising/litestruts/LoginAction.java | 42 ++++ .../src/com/coderising/litestruts/Struts.java | 108 ++++++++++ .../com/coderising/litestruts/StrutsTest.java | 43 ++++ .../src/com/coderising/litestruts/View.java | 26 +++ .../src/com/coderising/litestruts/struts.xml | 11 + .../src/com/coding/basic/ArrayList.java | Bin 1503 -> 1570 bytes .../src/com/coding/basic/LinkedList.java | Bin 2332 -> 2303 bytes 11 files changed, 456 insertions(+) create mode 100644 group16/313001956/RemoteSystemsTempFiles/.project create mode 100644 group16/313001956/WebContent/WEB-INF/resource/struts.xml create mode 100644 group16/313001956/src/com/coderising/array/ArrayUtil.java create mode 100644 group16/313001956/src/com/coderising/litestruts/LoginAction.java create mode 100644 group16/313001956/src/com/coderising/litestruts/Struts.java create mode 100644 group16/313001956/src/com/coderising/litestruts/StrutsTest.java create mode 100644 group16/313001956/src/com/coderising/litestruts/View.java create mode 100644 group16/313001956/src/com/coderising/litestruts/struts.xml diff --git a/group16/313001956/.classpath b/group16/313001956/.classpath index b42037dde2..ff7b1ffa64 100644 --- a/group16/313001956/.classpath +++ b/group16/313001956/.classpath @@ -8,5 +8,6 @@ + diff --git a/group16/313001956/RemoteSystemsTempFiles/.project b/group16/313001956/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group16/313001956/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group16/313001956/WebContent/WEB-INF/resource/struts.xml b/group16/313001956/WebContent/WEB-INF/resource/struts.xml new file mode 100644 index 0000000000..dd598a3664 --- /dev/null +++ b/group16/313001956/WebContent/WEB-INF/resource/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/group16/313001956/src/com/coderising/array/ArrayUtil.java b/group16/313001956/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..158b1bc6df --- /dev/null +++ b/group16/313001956/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,202 @@ + +package com.coderising.array; + +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import org.omg.PortableInterceptor.SYSTEM_EXCEPTION; + +import com.coding.basic.ArrayList; + +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 size = origin.length; + if (size == 0) { + return; + } + int semi = size / 2; + int temp; + for (int i = 0; i < semi; i++) { + temp = origin[i]; + origin[i] = origin[size - 1 - i]; + origin[size - 1 - i] = temp; + } + } + + /** + * µһ飺 int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * ҪֵΪ0ȥΪ0ֵһµ飬ɵΪ {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + ArrayList arrayList = new ArrayList(); + int size = oldArray.length; + for (int i = 0; i < size; i++) { + if (oldArray[i] != 0) + arrayList.add(oldArray[i]); + } + + return arrayListToArray(arrayList); + } + + /** + * Ѿõ飬 a1a2 , һµa3, ʹa3 a1a2 Ԫأ Ȼ 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) { + ArrayList arraylist = new ArrayList(); + int size1 = array1.length; + int size2 = array2.length; + int j = 0; + for (int i = 0; i < size1; i++) { + if (j >= size2) + arraylist.add(array1[i]); + else { + for (; j < size2; j++) { + if (array1[i] < array2[j]) { + arraylist.add(array1[i]); + break; + } else if (array1[i] == array2[j]) { + arraylist.add(array2[j]); + j++; + break; + } else { + arraylist.add(array2[j]); + } + } + } + } + return arrayListToArray(arraylist); + } + + private int[] arrayListToArray(ArrayList arraylist) { + int newSize = arraylist.size(); + int[] newArray = new int[newSize]; + for (int i = 0; i < newSize; i++) + newArray[i] = Integer.parseInt(arraylist.get(i).toString()); + return newArray; + } + + /** + * һѾݵ oldArrayչ չݴСΪoldArray.length + size + * ע⣬ԪҪ oldArray = [2,3,6] , size = 3,򷵻صΪ + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int newsize = oldArray.length + size; + int[] newArray = new int[newsize]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 쳲Ϊ1123581321...... һֵ Сڸֵ 磬 max = 15 , + * 򷵻صӦΪ [11235813] max = 1, 򷵻ؿ [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + int array[] = null; + ArrayList arraylist = new ArrayList(); + arraylist.add(1); + arraylist.add(1); + if (max == 1) + return null; + int temp = 1; + for (int i = 1; (temp = Integer.parseInt(arraylist.get(i).toString()) + + Integer.parseInt(arraylist.get(i - 1).toString())) <= max; i++) { + + arraylist.add(temp); + } + + return arrayListToArray(arraylist); + } + + /** + * Сڸֵmax max = 23, صΪ[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + ArrayList al = new ArrayList(); + if (max == 1) { + return null; + } else if (max == 2) { + al.add(2); + } else { + for (int i = 2; i < max; i++) { + for (int j = 2; j <= Math.sqrt(max); j++) { + if (i % j == 0) + break; + } + al.add(i); + } + } + return arrayListToArray(al); + } + + /** + * ν ָǡõ֮ͣ6=1+2+3 һֵmax һ飬 Сmax + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + ArrayList al = new ArrayList(); + int num = 0; + for (int i = 1; i < max; i++) { + num = 0; + for (int j = 1; j < i; j++) { + if (i % j == 0) + num += j; + } + if (num == i) + al.add(i); + } + return arrayListToArray(al); + } + + /** + * seperator array array= [3,8,9], seperator = "-" 򷵻ֵΪ"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + String s = ""; + int lenth = array.length; + for (int i = 0; i < lenth; i++) { + if (i == 0) + s += i; + else { + s += seperator + i; + } + } + return s; + } + +} diff --git a/group16/313001956/src/com/coderising/litestruts/LoginAction.java b/group16/313001956/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..bc492a3dfb --- /dev/null +++ b/group16/313001956/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +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/group16/313001956/src/com/coderising/litestruts/Struts.java b/group16/313001956/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..cdaa7d0214 --- /dev/null +++ b/group16/313001956/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,108 @@ +package com.coderising.litestruts; + +//import java.awt.List; +import java.io.File; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +//import javax.print.attribute.standard.Media; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class Struts { + + @SuppressWarnings("unchecked") + public static View runAction(String actionName, Map parameters) { + + /* + * + * 0. ȡļstruts.xml + * + * 1. actionNameҵӦclass LoginAction, ͨʵ + * parametersеݣösetter parametersе ("name"="test" , + * "password"="1234") , ǾӦõ setNamesetPassword + * + * 2. ͨöexectue ÷ֵ"success" + * + * 3. ͨҵgetter getMessage, ͨã ֵγһHashMap , + * {"message": "¼ɹ"} , ŵViewparameters + * + * 4. struts.xmlе ,Լexecuteķֵ ȷһjsp + * ŵViewjspֶС + * + */ + View view = new View(); + Map map = new HashMap(); + view.setParameters(map); + try { + + SAXReader reader = new SAXReader(); + String dir = System.getProperty("user.dir"); + + Document document = reader.read(new File(dir + "/src/com/coderising/litestruts/struts.xml")); + Element struts = document.getRootElement(); + java.util.List list_action = struts.elements("action"); + + Element item = null; + for (int i = 0; i < list_action.size(); i++) { + item = list_action.get(i); + String nm = item.attributeValue("name"); + if (actionName.equals(nm)) { + break; + } + } + String str_class = item.attributeValue("class"); + // String real_class=dir+"/"+str_class.replace('.', '/'); + // Class cl = Class.forName( dir.replace('\\', + // '.')+".src."+str_class); + Class cl = Class.forName(str_class); + Object instance = cl.newInstance(); + + String dNmae = parameters.get("name"); + String dpassword = parameters.get("password"); + Method mName = cl.getMethod("setName", String.class); + Method mPassword = cl.getMethod("setPassword", String.class); + mName.invoke(instance, dNmae); + mPassword.invoke(instance, dpassword); + + Method mExectue = cl.getMethod("execute"); + Object result = mExectue.invoke(instance); + + Method[] methods = cl.getMethods(); + for (Method method : methods) { + if (isGetter(method)) { + String mGettername = method.getName().substring(3); + Object mResult = method.invoke(instance); + view.getParameters().put(mGettername.toLowerCase(), mResult); + } + } + + java.util.List resulList = item.elements(); + for (Element el : resulList) { + if (result.toString().equals(el.attributeValue("name"))) { + view.setJsp(el.getTextTrim()); + break; + } + } + + } catch (Exception e) { + e.printStackTrace(); + } + return view; + } + + // жǷgetter + public static boolean isGetter(Method method) { + if (!method.getName().startsWith("get")) + return false; + if (method.getParameterTypes().length != 0) + return false; + if (void.class.equals(method.getReturnType())) + return false; + return true; + } + +} diff --git a/group16/313001956/src/com/coderising/litestruts/StrutsTest.java b/group16/313001956/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..9e98836f5f --- /dev/null +++ b/group16/313001956/src/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"); //ԤIJһ + + 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/group16/313001956/src/com/coderising/litestruts/View.java b/group16/313001956/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..f1e7fcfa19 --- /dev/null +++ b/group16/313001956/src/com/coderising/litestruts/View.java @@ -0,0 +1,26 @@ +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/group16/313001956/src/com/coderising/litestruts/struts.xml b/group16/313001956/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..ff7623e6e1 --- /dev/null +++ b/group16/313001956/src/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/group16/313001956/src/com/coding/basic/ArrayList.java b/group16/313001956/src/com/coding/basic/ArrayList.java index 3bec144013a94278e6a7ef25d65622c3430e8e27..03d2547c307022a3f314f5d4698bb1328689b7c7 100644 GIT binary patch literal 1570 zcmcJO`#%#30L8Jisby}FkZALoZER-dk+6k4Vr(eF@|;KVF5weJm}nj`%A>qKY{_1wUUq-^kx! zYfrTKZGVJ0yC35a1kEAIc^2xZ?=;9)1OxHp5l}ss4_QMTl=)NqUxS>I!Bm@WNJxXN zS~_YPNlz5!A8hYeP2N~^iD7YGl7;D=7Sh>(*MWI)+~=!EcsgAq@Wb*{!YbZ)aTOKqGfEE#RL!*xQtjml$iFj zp}1A7cs!N}I%>p#sl{Z>pf*7f-^R?+*pe{>R%J?(-s;^TrpyMO8?^}L1o6P}svF9~ zG1HNOqF2;(;%@RjwmR<&7)WG?(eJgMy8Pnyc-V<(IwhnZXOu}z^>$K5Ax8p!43~&L zehFWK@Qw4-(Wd9jRcWFF-*_$wzglOXrDqhSvXPWC-L(&-$j-vWNd<>IqfIQP&a8Oo zSYYEMY{0YHH`%xBs-f7v7X}w(OesKcRS#h%B`bJb=NeSJ>vK8P{)Wdu#cP!zkn(ca z?0UE2vs>MZCk&&BdqauH-0wbOfN?+Df-^4(f~BQfvRlv8dHgwXsl+Lkd!E?(iQA1v zNry+;oAROKaA@sz9jW^eFU68o7K@G1Om^r@pE2S;WXPsWUi>yE;PYg;kmquk#tb>_ zW_8#TrBL^GLVPKJBLa_YmK7+n25#(5m{I#S0m?owR1O@W`YTB!|Dw{`1Y{%wK(kvB z4%a~!^8zTLcl8D%YR*Fl)qDNWyHZa5UC42B_fr6W@r57#gppsLo>#oE>hMg# zi3B%rT93-QHGLg*xw)&u-o{iOFDfQ$g*T8Y93KU?U8y+fZ8_Yx$CMmR-UF`sX}-hw zsFvlbXAPQr^(H>JMB_pBuvRKmsR~}Y{PA+V@N2r8v85)hkd}~7a>`u0i@K{Ou8*s}ESxCJf`)nTv4&Vn$L_E9aq2ximqyA%Env01v4 z_?km*M>_2>QBNcCQPr1dk^!GuxSgZCP&BFy8_xP~e-nj5hnlNF2EJCb)hm0)oo`c- z8U*WR;8>$^2*=uVCHzhN7|sfYQI-xCpO_v5&|6mfwpm8G5;c;lX1zllU?=&1mI|)s z&jzgDpiF|YZjv$4ESnsKFQEd3E^?&nc8QJTQ5eE2;Ldfm)YL6tl{+rjwGVo&k52le zZ3bj0UGEAli&K%Rzx?}A=0=h@e>!cfSIKg2YQokaS_2`(WyUhs@l;QGReY1iyeGu5 z#|i0)_^adjLN*S^BzjxKeQ!;+u+he3Kc1JB8sK_*#2W@3JAL|l><8lu5dQSfWkCYfKsqNm3K9*Krhh9+C-Bdq7 zeu1%!AM&ugnga+mAJv*F9t^98dmz%fNId*dn!fzHxvu_poQv^JJ0b)=Z;MRPsevV= WZsm&(_M-DElKo;>EgapZ!~X&Z6SK?! literal 1503 zcmcIky>8nu5S|$z?{K3G%Z@xJDbT;cKmiXO4G%PRcGya!Kv7QIB=6HcL8hYZmIvtC z*XZ1%NZFKKAVtv*0z~q7{O-H&zKV#tV$Ow|EwT zu~$Z4gCaVo?rhfB+3JOER9?Tn5m;HNRRH-nrMcqXZflF>m^+mTU2S82QjaK&g%kE* z2$R(7ZRDelxoXSl$YOh{oEG*(g%+*RCOfL4cKsuQT&V_aV^mzs+(QidJi=tK)EoSt zuo&wUN}7t483Fr~4*70G_jjFZ9U*iGMlkVRa=W8ZEABvw({sNsou70Q9s=Ff5F5?& z)S}^58<62A8Qeyc2dfDW1W$SR{P59H9{j7F+1~JZX9+&C07-M%H?Y0SLJ(_}IOCDPsb&3(#EWO~{uf`I*!=(i diff --git a/group16/313001956/src/com/coding/basic/LinkedList.java b/group16/313001956/src/com/coding/basic/LinkedList.java index de886c9084ccf244f58d8a1cf62b65ce290385c0..eb3edc6dd94c0c4091233cf0b682a3c8ca4d2d80 100644 GIT binary patch literal 2303 zcmbW1_ct2~8^>)GM5~k-wPH(1wKhdW2rFYKR9+}Il2FP{67znpz2)nbuX{i z#u7;osvI%uuWZiNe(|{dpVs(_x^A;E{(ZISqnlBT1I^S*>95Wgs@~S~rZJa+&@VbY zF^clhR!3>onAV*%$@C2G>iIEMlIF%FB|7(S~Zq(}oO7|^n-R7an|Di^%>N16$_ ztm~k|4%|0RD-n6yOv}`heliu`)ETW+#qw^$1oX zl6~W%ENpjw#Y4Bm^t$+8S)j|9ye(mT2 z)*H&}V%hiB3rqJ?nLQ`JTvZqG#`j@VefKE$4r#sGVmMNxxZMF2^X2m>(wU-}TdqW$ z{{|1IESVJ#dE!oBZe*9SQ>r?P9c*iP0MV-V39L)G%EGsqgN{4W^s%!Xy4IrfPql75!L?n=U1+|`f!09Im7e3(wn);!8N-yQqr5=k`Xg=* zq853sSE`$iwbZ42KS|-`MmYiPgj8YOW!Rd$0<6eB(Z3n$8BpT1j>$X=glaELSjHd^ z>@Qm8K>>aSAw5;w1u{3IcnozTYUe0)+Sgbg&XffwQY4FWwz7u z3@xvQ_7+o&ra^cAJi>9MU>ym_!f`cdhnD6R;9LFuIKuL3kM9#1CQs13%hkcP_0Tr7 zMW(irf3Gk?e3aJ6jY6xgKdCaeJMPkgVS%q#LWUIxRFvCS!63!vKzmsKE)45iXl?Mz zuty`g;Befgy2#`_`-p{ReDFDF#4Jm<|OkI`?ADun(p?7 z3mT=QH-0U@(sF z3cYo6!22&fv@i@wA1?H6e~SHC`{@uh@6xtuIZzzY-%tv)1#PZoJH(%PH}>&=Kl6U9 zq33er9*d5=SKd&3U}*7coTHDrln~Nj!HtuL@w(41&77{VyY#P0Nt!c!cYDvI{N{Bo zrO>tIh6QBuC{sm#RU;B}`%j2&i9rAv{e&yZE8Xd83*z`4IUs&e1n&FEgz;vSsU4Cc z9=(%vp$n*vOzf~2_D!J*1m;_+hLx*Y`PB_uG10W>%0{WQdVlWTeoA-TBb-qjhY2g~ z>{M)Y{wXr?HoV`U@Y%CsbhD1pa5sc(bLvVb@1qGw`vD>M zTsV#|!ARqr8psrNIk1KIf&MJXW=>{XKnmL=vujA4sXEOP1^(uLUr8k&69j3W!X2W5 z4!Z4nzPmEW9Bj`%@~Rs^T@yGF>+nA3wAZZzguxE+=^C{U4Z!B)$o{5r$^*F@wSgM7 zSKcLeKmkKmZFsP-`UUz#U$LW*GZ!mMY10mts8frk*dt<`y>w_hU z$@^CF0bW%lf%p*p2fD+ISI1dF>~BG(J|JoP!Rcv+hQUh;pAl{nwUSdIb}8$jWg z4jcI5Y)K1~$1Cm0q8xAPeMdwlz!Ij6L7>S-)=hY~qDP^mJHufmJye;WB4;bAE( literal 2332 zcmcguOK#gR5M47s?l8zoL24six6YzKTLdubMNdFW6Pt}p3KIQklN_e#IdY5Mq1Py9 z_=zkz1_T7&QRIByy!ok;`Mq2qB(4qAZ+^bg8t3M*TvzD}vz{li6i3^5ji<#~Y&x zkVTQr=07mEU|32BcQuub&CDgaIIKbrl`e4i4m#TE4l;g$k8ll-k6sLJ7+>%p?6Njn z(71(LGBR7++1)3z)(b>h{=jVTaeqS&Of*}OirqE$B0lhBEXwKiUJ)$S` zDuS-Zg|reTU?>*x1Xwm0$Pj|L)b4eGmd^8^49DXqBcv0gaI1-V=h&fcdvV|NvhPU5 zTa7zQN@F@O2I0@%5@~v5g7?0TVu7bLrZ`%u`q zyk7#bSHGy*b!J4=DuOp^i#IBFLc_sx4-P2x7$*;)6NKrYb$XxZ5A(y<$I!;-A{Y>I zh|<@;&+lt_JK9r;%twv_fs~&rZ&JQ#PjKw{~Yozu56)lOx;N!GSYJTPj?yT=Yw$oXHgdO&moF eAkH{dRM&W_vW{_<^?W5x!UAL}oj8%fTK@qj;+JOt From 64dad85218a70fcc06b80b5099042e856a76cd65 Mon Sep 17 00:00:00 2001 From: zhan2016 Date: Wed, 8 Mar 2017 17:35:28 +0800 Subject: [PATCH 3/7] =?UTF-8?q?20170308=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/coderising/array/ArrayUtil.java | 118 ++++++++++++++++-- .../coderising/litestruts/LoginAction.java | 39 ------ .../src/com/coderising/litestruts/Struts.java | 66 +++++++++- .../src/com/coding/basic/ArrayList.java | 5 + 4 files changed, 175 insertions(+), 53 deletions(-) delete mode 100644 group16/2562124714/src/com/coderising/litestruts/LoginAction.java diff --git a/group16/2562124714/src/com/coderising/array/ArrayUtil.java b/group16/2562124714/src/com/coderising/array/ArrayUtil.java index 215aae9f64..4b496a41f7 100644 --- a/group16/2562124714/src/com/coderising/array/ArrayUtil.java +++ b/group16/2562124714/src/com/coderising/array/ArrayUtil.java @@ -1,7 +1,6 @@ package com.coderising.array; -import com.coding.basic.ArrayList; -import com.coding.basic.List; +import com.*; public class ArrayUtil { @@ -36,7 +35,8 @@ public void reverseArray(int[] origin){ */ public Integer[] removeZero(int[] oldArray){ - ArrayList blist = new ArrayList(); + com.coding.basic.ArrayList blist = new com.coding.basic.ArrayList(); + //int j = 0; for(int i = 0; i < oldArray.length; i++) @@ -61,7 +61,7 @@ public Integer[] removeZero(int[] oldArray){ */ public Integer[] merge(int[] array1, int[] array2){ - ArrayList blist = new ArrayList(); + com.coding.basic.ArrayList blist = new com.coding.basic.ArrayList(); int i = 0; for (i = 0; i < array1.length; i++) @@ -159,9 +159,37 @@ public int[] grow(int [] oldArray, int size){ * @param max * @return */ - public int[] fibonacci(int max){ + public Integer[] fibonacci(int max){ + com.coding.basic.ArrayList result = new com.coding.basic.ArrayList(); + int i = 0; + int TempMax = 0; + + + while (true) + { + TempMax = CaculateFibonacci(i++); + if (TempMax <= max) + { + result.add(TempMax); + continue; + } + else + { + break; + } + } - return null; + return (Integer[])result.ToArray(); + } + + public int CaculateFibonacci(int i) + { + if (1 == i) + return 1; + else if (2 == i) + return 1; + else + return CaculateFibonacci(i - 1) + CaculateFibonacci(i - 2); } /** @@ -170,20 +198,74 @@ public int[] fibonacci(int max){ * @param max * @return */ - public int[] getPrimes(int max){ - return null; + public Integer[] getPrimes(int max){ + com.coding.basic.ArrayList result = new com.coding.basic.ArrayList(); + + + + for(int i = 2; i < max; i ++) + { + if(CaculatePrimes(i)) + { + result.add(i); + } + } + + return (Integer[])result.ToArray(); } - + + //计算素数函数 算法好像不高明啊! + public boolean CaculatePrimes(int Num) + { + for (int i = 2; i < Math.sqrt(Num); i++) + { + if (Num % i == 0) + { + return false; + } + } + return true; + } + /** * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 * @param max * @return */ - public int[] getPerfectNumbers(int max){ - return null; + public Integer[] getPerfectNumbers(int max){ + com.coding.basic.ArrayList result = new com.coding.basic.ArrayList(); + + for (int i = 6; i < max; i++) + { + if (IsPerfectNumber(i)) + { + result.add(i); + } + } + return (Integer[])result.ToArray(); + } + + //计算所有的因子之和 算法并不高明啊! + public boolean IsPerfectNumber(int Num) + { + int temp = 0; + for (int i = 1; i < Num; i++) + { + if (Num % i == 0) + { + temp += i; + } + } + if (temp == Num) + { + return true; + } + else + { + return false; + } } - /** * 用seperator 把数组 array给连接起来 * 例如array= [3,8,9], seperator = "-" @@ -193,7 +275,17 @@ public int[] getPerfectNumbers(int max){ * @return */ public String join(int[] array, String seperator){ - return null; + String result = ""; + + for (int i = 0; i < array.length - 1; i++) + { + result += Integer.toString(array[i])+ seperator; + } + + result += Integer.toString(array[array.length]); + + + return result; } diff --git a/group16/2562124714/src/com/coderising/litestruts/LoginAction.java b/group16/2562124714/src/com/coderising/litestruts/LoginAction.java deleted file mode 100644 index dcdbe226ed..0000000000 --- a/group16/2562124714/src/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/group16/2562124714/src/com/coderising/litestruts/Struts.java b/group16/2562124714/src/com/coderising/litestruts/Struts.java index 85e2e22de3..5e0950e7cf 100644 --- a/group16/2562124714/src/com/coderising/litestruts/Struts.java +++ b/group16/2562124714/src/com/coderising/litestruts/Struts.java @@ -1,5 +1,18 @@ package com.coderising.litestruts; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.helpers.DefaultHandler; + +import javax.print.Doc; +import org.w3c.dom.Document; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import java.io.File; +import java.lang.reflect.Method; import java.util.Map; @@ -8,10 +21,61 @@ public class Struts { public static View runAction(String actionName, Map parameters) { + //0. SAX Parser is faster and uses less memory than DOM parser. Dom解析功能强大,可增删改查,操作时会将xml文档以文档对象的方式读取到内存中,因此适用于小文档 + //Sax解析是从头到尾逐行逐个元素读取内容,修改较为不便,但适用于只读的大文档 + + long lasting = System.currentTimeMillis(); + + try { + File f = new File("struts.xml"); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(f); + NodeList nl = doc.getElementsByTagName("struts"); + for (int i = 0; i < nl.getLength(); i++) + { + Node node = doc.getElementsByTagName("action").item(i); //get action node + System.out.print("action name is " + doc.getElementsByTagName("action").item(i).getFirstChild().getNodeValue()); + Element e = (Element)node; + //1 获取相应的class 设置用户名和密码 + System.out.print("action name is " + e.getAttribute("name") + " action class is " + e.getAttribute("class")); + Class ActionClass = Class.forName(e.getAttribute("class")); + //强制类型转换 + Object Action = ActionClass.newInstance(); + for (Map.Entry entry: parameters.entrySet() + ) { + if (entry.getKey() == "name") + { + //设置姓名 + //2 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + Method fun_setName = ActionClass.getDeclaredMethod("setName"); + fun_setName.invoke(Action,entry.getValue()); + + } + else if (entry.getKey() == "password") + { + //设置密码 + Method fun_setName = ActionClass.getDeclaredMethod("setPassword"); + fun_setName.invoke(Action,entry.getValue()); + } + else + { + continue; + } + } + + + + } + + } catch (Exception e) { + e.printStackTrace(); + } /* 0. 读取配置文件struts.xml - + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , "password"="1234") , diff --git a/group16/2562124714/src/com/coding/basic/ArrayList.java b/group16/2562124714/src/com/coding/basic/ArrayList.java index a39b8e9d42..c73fcfdd27 100644 --- a/group16/2562124714/src/com/coding/basic/ArrayList.java +++ b/group16/2562124714/src/com/coding/basic/ArrayList.java @@ -102,6 +102,11 @@ public Iterator iterator(){ public Object[] ToArray() { Object [] Array = new Object[this.size]; + if(this.size == 0) + { + return new Object[0]; + } + for (int i = 0 ; i < this.size; i ++) { Array[i] = this.elementData[i]; From 960ed2ca002b8ada147136a3e5e3cd79d4d45fcc Mon Sep 17 00:00:00 2001 From: zhan2016 Date: Thu, 9 Mar 2017 15:03:29 +0800 Subject: [PATCH 4/7] 20170309 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 完成第二次作业 --- .../litestruts => Test}/StrutsTest.java | 6 +- group16/2562124714/src/Test/TestRunner.java | 2 +- .../com/coderising/action/LoginAction.java | 41 +++++++++ .../src/com/coderising/litestruts/Struts.java | 88 +++++++++++++------ .../src/com/coderising/litestruts/struts.xml | 4 +- .../src/com/coding/basic/ArrayList.java | 1 + 6 files changed, 107 insertions(+), 35 deletions(-) rename group16/2562124714/src/{com/coderising/litestruts => Test}/StrutsTest.java (80%) create mode 100644 group16/2562124714/src/com/coderising/action/LoginAction.java diff --git a/group16/2562124714/src/com/coderising/litestruts/StrutsTest.java b/group16/2562124714/src/Test/StrutsTest.java similarity index 80% rename from group16/2562124714/src/com/coderising/litestruts/StrutsTest.java rename to group16/2562124714/src/Test/StrutsTest.java index b8c81faf3c..663c9dba3b 100644 --- a/group16/2562124714/src/com/coderising/litestruts/StrutsTest.java +++ b/group16/2562124714/src/Test/StrutsTest.java @@ -1,4 +1,4 @@ -package com.coderising.litestruts; +package Test; import java.util.HashMap; import java.util.Map; @@ -22,7 +22,7 @@ public void testLoginActionSuccess() { params.put("password","1234"); - View view = Struts.runAction(actionName,params); + com.coderising.litestruts.View view = com.coderising.litestruts.Struts.runAction(actionName,params); Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); Assert.assertEquals("login successful", view.getParameters().get("message")); @@ -35,7 +35,7 @@ public void testLoginActionFailed() { params.put("name","test"); params.put("password","123456"); //密码和预设的不一致 - View view = Struts.runAction(actionName,params); + com.coderising.litestruts.View view = com.coderising.litestruts.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/group16/2562124714/src/Test/TestRunner.java b/group16/2562124714/src/Test/TestRunner.java index 2bf465f832..963fb955d3 100644 --- a/group16/2562124714/src/Test/TestRunner.java +++ b/group16/2562124714/src/Test/TestRunner.java @@ -10,7 +10,7 @@ */ public class TestRunner { public static void main(String[] args) { - org.junit.runner.Result result = JUnitCore.runClasses(BinaryTreeNodeTest.class); + org.junit.runner.Result result = JUnitCore.runClasses(StrutsTest.class); for (Failure failure:result.getFailures()) { System.out.println(failure.toString()); } diff --git a/group16/2562124714/src/com/coderising/action/LoginAction.java b/group16/2562124714/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..8f1ea73abb --- /dev/null +++ b/group16/2562124714/src/com/coderising/action/LoginAction.java @@ -0,0 +1,41 @@ +package com.coderising.action; + +/** + * Created by zhangwj on 2017/3/9. + */ +public class LoginAction { + private String Name; + private String Password; + private String Message; + + public void setName(String name) + { + this.Name = name; + } + public void setPassword(String pass) + { + this.Password = pass; + } + + public String exectue() + { + if (this.Name == "test" && this.Password == "1234") + { + this.Message = "login successful"; + return "success"; + } + else + { + this.Message = "login failed,please check your user/pwd"; + return "fail"; + } + } + + public String getMessage() + { + return this.Message; + } + + + +} diff --git a/group16/2562124714/src/com/coderising/litestruts/Struts.java b/group16/2562124714/src/com/coderising/litestruts/Struts.java index 5e0950e7cf..2bc79f1199 100644 --- a/group16/2562124714/src/com/coderising/litestruts/Struts.java +++ b/group16/2562124714/src/com/coderising/litestruts/Struts.java @@ -1,5 +1,6 @@ package com.coderising.litestruts; +import com.sun.org.apache.regexp.internal.RE; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -13,6 +14,7 @@ import javax.xml.parsers.SAXParserFactory; import java.io.File; import java.lang.reflect.Method; +import java.util.HashMap; import java.util.Map; @@ -27,45 +29,73 @@ public static View runAction(String actionName, Map parameters) { long lasting = System.currentTimeMillis(); try { - File f = new File("struts.xml"); + File f = new File("C:\\Users\\zhangwj\\Desktop\\java课程\\coding\\coding2017-1\\group16\\2562124714\\src\\com\\coderising\\litestruts\\struts.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(f); NodeList nl = doc.getElementsByTagName("struts"); - for (int i = 0; i < nl.getLength(); i++) - { - Node node = doc.getElementsByTagName("action").item(i); //get action node - System.out.print("action name is " + doc.getElementsByTagName("action").item(i).getFirstChild().getNodeValue()); - Element e = (Element)node; - //1 获取相应的class 设置用户名和密码 - System.out.print("action name is " + e.getAttribute("name") + " action class is " + e.getAttribute("class")); - Class ActionClass = Class.forName(e.getAttribute("class")); - //强制类型转换 - Object Action = ActionClass.newInstance(); - for (Map.Entry entry: parameters.entrySet() - ) { - if (entry.getKey() == "name") - { - //设置姓名 - //2 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" - Method fun_setName = ActionClass.getDeclaredMethod("setName"); - fun_setName.invoke(Action,entry.getValue()); + for (int i = 0; i < nl.getLength(); i++) { + Node node = doc.getElementsByTagName("action").item(i); //get action node + //System.out.print("action name is " + doc.getElementsByTagName("action").item(i).getFirstChild().getNodeValue()); + Element e = (Element) node; + System.out.printf("attribute of name is "+ e.getAttribute("name") + " actionName Need is" + actionName); + if (e.getAttribute("name").toString().equals(actionName)) { + //1 获取相应的class 设置用户名和密码 + // System.out.print("action name is " + e.getAttribute("name") + " action class is " + e.getAttribute("class")); + Class ActionClass = Class.forName(e.getAttribute("class")); + //强制类型转换 + Object Action = ActionClass.newInstance(); + for (Map.Entry entry : parameters.entrySet() + ) { + if (entry.getKey() == "name") { + //设置姓名 + //2 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + Method fun_setName = ActionClass.getDeclaredMethod("setName", String.class); + fun_setName.invoke(Action, entry.getValue()); + + } else if (entry.getKey() == "password") { + //设置密码 + Method fun_setName = ActionClass.getDeclaredMethod("setPassword", String.class); + fun_setName.invoke(Action, entry.getValue()); + } else { + continue; + } } - else if (entry.getKey() == "password") - { - //设置密码 - Method fun_setName = ActionClass.getDeclaredMethod("setPassword"); - fun_setName.invoke(Action,entry.getValue()); - } - else - { - continue; + + Method ExecuteMethod = ActionClass.getDeclaredMethod("exectue"); + //2 调用execute方法 + String ss = "11"; + Object ExecuteResultValue = ExecuteMethod.invoke(Action); + + //3通过反射找到对象的所有getter方法(例如 getMessage), + //通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + //放到View对象的parameters + Method Getter_Method = ActionClass.getDeclaredMethod("getMessage"); + Object message = Getter_Method.invoke(Action); + Map messageMap = new HashMap(); + messageMap.put("message", (String) message); + com.coderising.litestruts.View view = new com.coderising.litestruts.View(); + view.setParameters(messageMap); + + //4 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + //放到View对象的jsp字段中。 + //首先获取result节点 + NodeList ResultNL = ((Element) node).getElementsByTagName("result"); + for (int j = 0; j < ResultNL.getLength(); j++ + ) { + Node node1 = ResultNL.item(j); + Element e1 = (Element) node1; + System.out.println("name is " + e1.getAttribute("name") + "return Value is" + (String) ExecuteResultValue); + if (e1.getAttribute("name").toString().equals((String) ExecuteResultValue)) { + view.setJsp(node1.getFirstChild().getNodeValue()); + } } - } + return view; + } } } catch (Exception e) { diff --git a/group16/2562124714/src/com/coderising/litestruts/struts.xml b/group16/2562124714/src/com/coderising/litestruts/struts.xml index a6cfe43e6c..90cf18b7da 100644 --- a/group16/2562124714/src/com/coderising/litestruts/struts.xml +++ b/group16/2562124714/src/com/coderising/litestruts/struts.xml @@ -5,7 +5,7 @@ /jsp/showLogin.jsp - /jsp/welcome.jsp - /jsp/error.jsp + /jsp/welcome.jsp + /jsp/error.jsp \ No newline at end of file diff --git a/group16/2562124714/src/com/coding/basic/ArrayList.java b/group16/2562124714/src/com/coding/basic/ArrayList.java index c73fcfdd27..acdfadd83d 100644 --- a/group16/2562124714/src/com/coding/basic/ArrayList.java +++ b/group16/2562124714/src/com/coding/basic/ArrayList.java @@ -107,6 +107,7 @@ public Object[] ToArray() return new Object[0]; } + //使用System.arraycopy()来复制数组是更优的办法 zwj 20170309 for (int i = 0 ; i < this.size; i ++) { Array[i] = this.elementData[i]; From add38bd64d85f0314dcdf15f0d83578e193b6416 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=AD=E5=B7=9E-=E5=A4=A7=E6=98=8E?= <420355244@qq.com> Date: Sat, 11 Mar 2017 19:20:55 +0800 Subject: [PATCH 5/7] homework homework --- .../src/com/coderising/array/ArrayUtil.java | 108 ++++++++++++--- group16/420355244/Homework3/.classpath | 7 + group16/420355244/Homework3/.gitignore | 1 + group16/420355244/Homework3/.project | 17 +++ .../src/com/coderising/array/ArrayUtil.java | 96 ++++++++++++++ .../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 ++++++ .../src/com/coderising/litestruts/Struts.java | 34 +++++ .../com/coderising/litestruts/StrutsTest.java | 43 ++++++ .../src/com/coderising/litestruts/View.java | 23 ++++ .../src/com/coderising/litestruts/struts.xml | 11 ++ .../src/com/coding/basic/ArrayList.java | 32 +++++ .../src/com/coding/basic/BinaryTreeNode.java | 32 +++++ .../src/com/coding/basic/Iterator.java | 7 + .../src/com/coding/basic/LinkedList.java | 124 ++++++++++++++++++ .../Homework3/src/com/coding/basic/List.java | 9 ++ .../Homework3/src/com/coding/basic/Queue.java | 19 +++ .../Homework3/src/com/coding/basic/Stack.java | 22 ++++ 26 files changed, 845 insertions(+), 16 deletions(-) create mode 100644 group16/420355244/Homework3/.classpath create mode 100644 group16/420355244/Homework3/.gitignore create mode 100644 group16/420355244/Homework3/.project create mode 100644 group16/420355244/Homework3/src/com/coderising/array/ArrayUtil.java create mode 100644 group16/420355244/Homework3/src/com/coderising/download/DownloadThread.java create mode 100644 group16/420355244/Homework3/src/com/coderising/download/FileDownloader.java create mode 100644 group16/420355244/Homework3/src/com/coderising/download/FileDownloaderTest.java create mode 100644 group16/420355244/Homework3/src/com/coderising/download/api/Connection.java create mode 100644 group16/420355244/Homework3/src/com/coderising/download/api/ConnectionException.java create mode 100644 group16/420355244/Homework3/src/com/coderising/download/api/ConnectionManager.java create mode 100644 group16/420355244/Homework3/src/com/coderising/download/api/DownloadListener.java create mode 100644 group16/420355244/Homework3/src/com/coderising/download/impl/ConnectionImpl.java create mode 100644 group16/420355244/Homework3/src/com/coderising/download/impl/ConnectionManagerImpl.java create mode 100644 group16/420355244/Homework3/src/com/coderising/litestruts/LoginAction.java create mode 100644 group16/420355244/Homework3/src/com/coderising/litestruts/Struts.java create mode 100644 group16/420355244/Homework3/src/com/coderising/litestruts/StrutsTest.java create mode 100644 group16/420355244/Homework3/src/com/coderising/litestruts/View.java create mode 100644 group16/420355244/Homework3/src/com/coderising/litestruts/struts.xml create mode 100644 group16/420355244/Homework3/src/com/coding/basic/ArrayList.java create mode 100644 group16/420355244/Homework3/src/com/coding/basic/BinaryTreeNode.java create mode 100644 group16/420355244/Homework3/src/com/coding/basic/Iterator.java create mode 100644 group16/420355244/Homework3/src/com/coding/basic/LinkedList.java create mode 100644 group16/420355244/Homework3/src/com/coding/basic/List.java create mode 100644 group16/420355244/Homework3/src/com/coding/basic/Queue.java create mode 100644 group16/420355244/Homework3/src/com/coding/basic/Stack.java diff --git a/group16/420355244/Homework2/src/com/coderising/array/ArrayUtil.java b/group16/420355244/Homework2/src/com/coderising/array/ArrayUtil.java index 7c6522e50a..2c1fca67d4 100644 --- a/group16/420355244/Homework2/src/com/coderising/array/ArrayUtil.java +++ b/group16/420355244/Homework2/src/com/coderising/array/ArrayUtil.java @@ -26,13 +26,21 @@ public static void reverseArray(int[] origin){ */ public static int[] removeZero(int[] oldArray){ - int[] notZero = new int[oldArray.length]; - for(int i = 0;i < oldArray.length ;i++){ + int zeroCount = 0; + for(int i = 0;i < oldArray.length; i++){ + if(oldArray[i] == 0){ + zeroCount++; + } + } + int[] newArr = new int[oldArray.length - zeroCount]; + int index = 0; + for(int i = 0;i < oldArray.length; i++){ if(oldArray[i] != 0){ - + newArr[index] = oldArray[i]; + index++; } } - return oldArray; + return newArr; } /** @@ -60,22 +68,29 @@ public static int[] merge(int[] array1, int[] array2){ combineArr[i] = array1[i]; } for(int i = 0;i < array2.length; i++){ + int index = array1.length -1; + boolean same = false; for(int j = 0;j < repeatedNum.length; j++){ - if(array2[i] != repeatedNum[j]){ - combineArr[array1.length + i] = array2[i]; + if(array2[i] == repeatedNum[j]){ + same = true; } } + if(!same){ + index += 1; + combineArr[index] = array2[i]; + } } //冒泡排序 for(int i = 0;i < combineArr.length;i++){ for(int j = i + 1;j < combineArr.length;j++){ - int x = combineArr[i]; - if(x > combineArr[j]){ - + if(combineArr[i] > combineArr[j]){ + int x = combineArr[i]; + combineArr[i] = combineArr[j]; + combineArr[j] = x; } } } - return null; + return combineArr; } /** * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size @@ -87,7 +102,11 @@ public static int[] merge(int[] array1, int[] array2){ * @return */ public static int[] grow(int [] oldArray, int size){ - return null; + int[] newArr = new int[oldArray.length + size]; + for(int i = 0;i < oldArray.length; i++){ + newArr[i] = oldArray[i]; + } + return newArr; } /** @@ -98,7 +117,31 @@ public static int[] grow(int [] oldArray, int size){ * @return */ public static int[] fibonacci(int max){ - return null; + if(max == 1){ + return null; + }else{ + int length = 0; + int dataBefore = 0; + int dataAfter = 1; + while(dataAfter < max){ + int date = dataAfter; + dataAfter = dataAfter + dataBefore; + dataBefore = date; + length++; + } + int index = 0; + int[] result = new int[length]; + dataBefore = 0; + dataAfter = 1; + while(dataAfter < max){ + result[index] = dataAfter; + int date = dataAfter; + dataAfter = dataAfter + dataBefore; + dataBefore = date; + index ++; + } + return result; + } } /** @@ -108,6 +151,12 @@ public static int[] fibonacci(int max){ * @return */ public static int[] getPrimes(int max){ + int i = 1; + int length = 0; + while(i < max){ + i++; + int search = 1; + } return null; } @@ -130,7 +179,14 @@ public static int[] getPerfectNumbers(int max){ * @return */ public static String join(int[] array, String seperator){ - return null; + StringBuilder sb = new StringBuilder(); + for(int i=0 ;i < array.length; i++){ + sb.append(String.valueOf(array[i])); + if(i != array.length - 1){ + sb.append(seperator); + } + } + return sb.toString(); } public static void main(String[] args) { @@ -139,9 +195,29 @@ public static void main(String[] args) { for (int i : a) { System.out.print(i+","); }*/ - int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} ; - removeZero(oldArr); - for (int i : oldArr) { + /*int[] oldArr = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} ; + int[] newArr = removeZero(oldArr); + for (int i : newArr) { + System.out.print(i+","); + }*/ + /*int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + int[] merge = merge(a1,a2); + for (int i : merge) { + System.out.print(i+","); + }*/ + /*int[] oldArray = {2,3,6}; + int size = 3; + int[] newArr = grow(oldArray, size); + for (int i : newArr) { + System.out.print(i+","); + }*/ + /*int[] array= {3,8,9}; + String seperator = "-"; + String join = join(array, seperator); + System.out.println(join);*/ + int[] fibonacci = fibonacci(15); + for (int i : fibonacci) { System.out.print(i+","); } } diff --git a/group16/420355244/Homework3/.classpath b/group16/420355244/Homework3/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group16/420355244/Homework3/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group16/420355244/Homework3/.gitignore b/group16/420355244/Homework3/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group16/420355244/Homework3/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group16/420355244/Homework3/.project b/group16/420355244/Homework3/.project new file mode 100644 index 0000000000..57ff5cb4f2 --- /dev/null +++ b/group16/420355244/Homework3/.project @@ -0,0 +1,17 @@ + + + Homework3 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/420355244/Homework3/src/com/coderising/array/ArrayUtil.java b/group16/420355244/Homework3/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..e5ddb476a6 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,96 @@ +package com.coderising.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/group16/420355244/Homework3/src/com/coderising/download/DownloadThread.java b/group16/420355244/Homework3/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..900a3ad358 --- /dev/null +++ b/group16/420355244/Homework3/src/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/group16/420355244/Homework3/src/com/coderising/download/FileDownloader.java b/group16/420355244/Homework3/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..c3c8a3f27d --- /dev/null +++ b/group16/420355244/Homework3/src/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/group16/420355244/Homework3/src/com/coderising/download/FileDownloaderTest.java b/group16/420355244/Homework3/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..4ff7f46ae0 --- /dev/null +++ b/group16/420355244/Homework3/src/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/group16/420355244/Homework3/src/com/coderising/download/api/Connection.java b/group16/420355244/Homework3/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group16/420355244/Homework3/src/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/group16/420355244/Homework3/src/com/coderising/download/api/ConnectionException.java b/group16/420355244/Homework3/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group16/420355244/Homework3/src/com/coderising/download/api/ConnectionManager.java b/group16/420355244/Homework3/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group16/420355244/Homework3/src/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/group16/420355244/Homework3/src/com/coderising/download/api/DownloadListener.java b/group16/420355244/Homework3/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group16/420355244/Homework3/src/com/coderising/download/impl/ConnectionImpl.java b/group16/420355244/Homework3/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..36a9d2ce15 --- /dev/null +++ b/group16/420355244/Homework3/src/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/group16/420355244/Homework3/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group16/420355244/Homework3/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..172371dd55 --- /dev/null +++ b/group16/420355244/Homework3/src/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/group16/420355244/Homework3/src/com/coderising/litestruts/LoginAction.java b/group16/420355244/Homework3/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group16/420355244/Homework3/src/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/group16/420355244/Homework3/src/com/coderising/litestruts/Struts.java b/group16/420355244/Homework3/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/group16/420355244/Homework3/src/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/group16/420355244/Homework3/src/com/coderising/litestruts/StrutsTest.java b/group16/420355244/Homework3/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group16/420355244/Homework3/src/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/group16/420355244/Homework3/src/com/coderising/litestruts/View.java b/group16/420355244/Homework3/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group16/420355244/Homework3/src/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/group16/420355244/Homework3/src/com/coderising/litestruts/struts.xml b/group16/420355244/Homework3/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..171848ecd1 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group16/420355244/Homework3/src/com/coding/basic/ArrayList.java b/group16/420355244/Homework3/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..1f185736f9 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coding/basic/ArrayList.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +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/group16/420355244/Homework3/src/com/coding/basic/BinaryTreeNode.java b/group16/420355244/Homework3/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group16/420355244/Homework3/src/com/coding/basic/Iterator.java b/group16/420355244/Homework3/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group16/420355244/Homework3/src/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/group16/420355244/Homework3/src/com/coding/basic/LinkedList.java b/group16/420355244/Homework3/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..4fdb03db8a --- /dev/null +++ b/group16/420355244/Homework3/src/com/coding/basic/LinkedList.java @@ -0,0 +1,124 @@ +package com.coding.basic; + + + +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){ + + } + /** + * 假定当前链表和list均包含已升序排列的整数 + * 从当前链表中取出那些list所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public static int[] getElements(LinkedList list){ + return null; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在list中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于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/group16/420355244/Homework3/src/com/coding/basic/List.java b/group16/420355244/Homework3/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group16/420355244/Homework3/src/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/group16/420355244/Homework3/src/com/coding/basic/Queue.java b/group16/420355244/Homework3/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..36e516e266 --- /dev/null +++ b/group16/420355244/Homework3/src/com/coding/basic/Queue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class Queue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group16/420355244/Homework3/src/com/coding/basic/Stack.java b/group16/420355244/Homework3/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a5a04de76d --- /dev/null +++ b/group16/420355244/Homework3/src/com/coding/basic/Stack.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +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; + } +} From a48640043d40688fad36433144cf6a3f1ed7b70b Mon Sep 17 00:00:00 2001 From: wanghongdi56 <313001956@qq.com> Date: Wed, 15 Mar 2017 08:59:45 +0800 Subject: [PATCH 6/7] =?UTF-8?q?0312=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group16/313001956/.classpath | 2 +- .../coderising/download/DownloadThread.java | 57 ++++++++++ .../coderising/download/FileDownloader.java | 97 ++++++++++++++++++ .../download/FileDownloaderTest.java | 59 +++++++++++ .../coderising/download/api/Connection.java | 43 ++++++++ .../download/api/ConnectionException.java | 5 + .../download/api/ConnectionManager.java | 10 ++ .../download/api/DownloadListener.java | 5 + .../download/impl/ConnectionImpl.java | 80 +++++++++++++++ .../download/impl/ConnectionManagerImpl.java | 35 +++++++ .../src/com/coding/basic/LinkedList.java | Bin 2303 -> 6881 bytes .../src/com/coding/basic/LinkedListTest.java | 28 +++++ 12 files changed, 420 insertions(+), 1 deletion(-) create mode 100644 group16/313001956/src/com/coderising/download/DownloadThread.java create mode 100644 group16/313001956/src/com/coderising/download/FileDownloader.java create mode 100644 group16/313001956/src/com/coderising/download/FileDownloaderTest.java create mode 100644 group16/313001956/src/com/coderising/download/api/Connection.java create mode 100644 group16/313001956/src/com/coderising/download/api/ConnectionException.java create mode 100644 group16/313001956/src/com/coderising/download/api/ConnectionManager.java create mode 100644 group16/313001956/src/com/coderising/download/api/DownloadListener.java create mode 100644 group16/313001956/src/com/coderising/download/impl/ConnectionImpl.java create mode 100644 group16/313001956/src/com/coderising/download/impl/ConnectionManagerImpl.java create mode 100644 group16/313001956/src/com/coding/basic/LinkedListTest.java diff --git a/group16/313001956/.classpath b/group16/313001956/.classpath index ff7b1ffa64..249d4729ec 100644 --- a/group16/313001956/.classpath +++ b/group16/313001956/.classpath @@ -8,6 +8,6 @@ - + diff --git a/group16/313001956/src/com/coderising/download/DownloadThread.java b/group16/313001956/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..0653f71d80 --- /dev/null +++ b/group16/313001956/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,57 @@ +package com.coderising.download; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.RandomAccessFile; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.DownloadListener; +import com.coding.basic.ArrayList; + +public class DownloadThread extends Thread { + + Connection conn; + Integer startPos; + Integer endPos; + DownloadListener listener; + File file; + int threadNum; + ArrayList threadDone; + + public DownloadThread(Connection conn, int startPos, int endPos, DownloadListener listener, File file, + Integer threadNum, ArrayList threadDone) { + + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.listener = listener; + this.file = file; + this.threadNum = threadNum; + this.threadDone = threadDone; + // run(); + } + + @Override + public synchronized void run() { + try { + byte[] bt = conn.read(startPos, endPos, file); + + threadDone.add(1); + + if (conn != null) { + conn.close(); + } + if (threadDone.size() == threadNum) { + + listener.notifyFinished(); + } + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } +} diff --git a/group16/313001956/src/com/coderising/download/FileDownloader.java b/group16/313001956/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..6903506b6b --- /dev/null +++ b/group16/313001956/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,97 @@ +package com.coderising.download; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.RandomAccessFile; +import java.util.List; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coding.basic.ArrayList; + +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, Ƕ߳أ Ŀͻ˲֪ʲôʱҪʵֵ + // ̶ִ߳Ժ listenernotifiedFinished ͻ˾յ֪ͨ + // ʵ˼· + // 1. ҪConnectionManageropenӣ + // ȻͨConnection.getContentLengthļij + // 2. 3߳أ עÿ߳ҪȵConnectionManageropen + // Ȼread readжȡļĿʼλúͽλõIJ ֵbyte[] + // 3. byteд뵽ļ + // 4. е̶߳Ժ ҪlistenernotifiedFinished + + // Ĵʾ룬 Ҳ˵ֻһ̣߳ Ҫɶ̵߳ġ + Connection conn = null; + try { + Integer threadNum = 3; + //Integer threadDone = 0; + ArrayList threadDone=new ArrayList(); + conn = cm.open(this.url); + if (conn.getConn().getResponseCode() == 200) { + int length = conn.getContentLength(); + int size = (length % threadNum == 0 ? length / threadNum : length / threadNum + 1); + + String filename = url.substring(url.lastIndexOf('/')); + String filePath = "C:\\Users\\Administrator\\Desktop\\" + filename; + File file = new File(filePath); + RandomAccessFile raf = new RandomAccessFile(file, "rw"); + raf.setLength(length); + raf.close(); + + for (int i = 0; i < threadNum; i++) { + Connection connThread = cm.open(this.url); + new DownloadThread(connThread, i * size, (i + 1) * size - 1, listener, file, threadNum, + threadDone).start(); + } + } + } catch (ConnectionException e) { + e.printStackTrace(); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + 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/group16/313001956/src/com/coderising/download/FileDownloaderTest.java b/group16/313001956/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..cca82ea5da --- /dev/null +++ b/group16/313001956/src/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://10.10.1.65:1024/wxl.jpg"; + String url = "http://10.10.1.65:1024/java.pdf"; + 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/group16/313001956/src/com/coderising/download/api/Connection.java b/group16/313001956/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..3a3edf5835 --- /dev/null +++ b/group16/313001956/src/com/coderising/download/api/Connection.java @@ -0,0 +1,43 @@ +package com.coderising.download.api; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.HttpURLConnection ; + +public interface Connection { + URL fileurl = null; + HttpURLConnection conn = null; + InputStream inStream = null; + + /** + * ʼͽλã ȡݣ ֵֽ + * + * @param startPos + * ʼλã 0ʼ + * @param endPos + * λ + * @return + */ + public byte[] read(int startPos, int endPos,File file) throws IOException; + + /** + * õݵij + * + * @return + */ + public int getContentLength(); + + /** + * ر + */ + public void close(); + + public void setConn(HttpURLConnection conn); + public void setFileurl(URL fileurl); + public HttpURLConnection getConn(); + public URL getFileurl(URL fileurl) ; + public void setinStream(InputStream inStream); + public InputStream getinStream(); +} diff --git a/group16/313001956/src/com/coderising/download/api/ConnectionException.java b/group16/313001956/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..1551a80b3d --- /dev/null +++ b/group16/313001956/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + +} diff --git a/group16/313001956/src/com/coderising/download/api/ConnectionManager.java b/group16/313001956/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group16/313001956/src/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/group16/313001956/src/com/coderising/download/api/DownloadListener.java b/group16/313001956/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group16/313001956/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group16/313001956/src/com/coderising/download/impl/ConnectionImpl.java b/group16/313001956/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..94fd2c9b67 --- /dev/null +++ b/group16/313001956/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,80 @@ +package com.coderising.download.impl; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.net.URL; +import java.net.HttpURLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection { + URL fileurl = null; + HttpURLConnection uRLconn = null; + InputStream inStream = null; + + @Override + public byte[] read(int startPos, int endPos, File file) throws IOException { + uRLconn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + if (inStream == null) + inStream = uRLconn.getInputStream(); + int size = endPos - startPos + 1; + + byte[] bt = new byte[size]; + + RandomAccessFile raf = new RandomAccessFile(file, "rw"); + + raf.seek(startPos); + int lenth=0; + //lenth = inStream.read(bt,0,size); + while ((lenth = inStream.read(bt,0,size)) != -1) + raf.write(bt, 0, lenth); + raf.close(); + + return bt; + + } + + @Override + public int getContentLength() { + int fileSize = uRLconn.getContentLength(); + return fileSize; + } + + @Override + public void close() { + if (inStream != null) + try { + inStream.close(); + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void setConn(HttpURLConnection uRLconn) { + this.uRLconn = uRLconn; + } + + public HttpURLConnection getConn() { + return this.uRLconn; + } + + public void setFileurl(URL fileurl) { + this.fileurl = fileurl; + } + + public URL getFileurl(URL fileurl) { + return this.fileurl; + } + + public void setinStream(InputStream inStream) { + this.inStream = inStream; + } + + public InputStream getinStream() { + return this.inStream; + } +} diff --git a/group16/313001956/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group16/313001956/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..ff96aaa595 --- /dev/null +++ b/group16/313001956/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,35 @@ +package com.coderising.download.impl; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +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 { + try { + URL fileurl=new URL(url); + HttpURLConnection uRlconn = (HttpURLConnection)fileurl.openConnection(); + //ӵ + uRlconn.setRequestMethod("GET"); + uRlconn.setReadTimeout(5000); + Connection conn = new ConnectionImpl(); + conn.setFileurl(fileurl); + + conn.setConn(uRlconn); + return conn; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/group16/313001956/src/com/coding/basic/LinkedList.java b/group16/313001956/src/com/coding/basic/LinkedList.java index eb3edc6dd94c0c4091233cf0b682a3c8ca4d2d80..14fee116552ad4c3e753cdfc05e6ca402fb9b68e 100644 GIT binary patch literal 6881 zcmcgxTW=Fb6n@@H{SVVVIm9su(1HYsn$Q~(0;!eyf_T{2ld!>AyX$pGZ$AdCwjqkt z5V9pC+*;g*1OdizO+l(kFM>){UrH5}(nvj*nc0h-w5cdB8}H0HXU?4S&3DdDigxz6 zJuF4GFq+90a*jKk8MI4IcJK4gJ^x&>JeYT~BAd5MC2`PkkIURar{oJ~w3wHp()Do^ z-akNLtLQmnwlBq@LQaZf($0YmGw8U!C^_#-vTG^Bp~+N-GV&3d;)TZwPEOdlT@w7;2J3}HxGh(0U79W1dVxo`yQ!7~hF|Tk! zxblQJfJP3N{lh~8g|eF~y*8eeMc*m7sTUg;FFvfCe386BH!KTnt~KK>qjkKAGZ(Fp1&DS5)h$iu?m zb#xVY$C>y-mL7HGxQ|L1{6Rig{F^PICVPfUVG27t@jVuG6=84^JX3L3TYsj}rf7K5 zFMBS9pB%?fh|OYXSz3icw9)Q7Ia(N#&vb*Ex-Pmz593G!+@#U$Ba%wWZ0?hMVeB9P{h4mUr76#6ZApb4^l zD7ScwfYb>$t=_kup98-1Y`@?MNBu|~B&-mTtw!r-BIhlviYyEJ=_X$cO&C<4IOgOf zlsPDIRO(c;ETq9G(>qM&T)Z?3wz^dEZQnEkwAu%lRs{^5F!B9hxl@HQta&N+uhaI(=E~hr{*7>`58ugx@mYP>28Oo>U3yt zo*a{2Nv6WKN93V>B0oS>g^z1EPGsWeB|e0S)L2y6c2V^O9@m?rt@?7bi1IKW768k< z!c=@E?n^LKN{d263NV1ybgaN_4zP*W(xtQO^@X|B;A0F_P+gp!{&nW^xhBP~GLM@8 z8#}uCUZP0tf%uV1WByT4Z3YlS24vhAEr8m+FoR;qXYc1{zLz7Ffb?~y&IU`qyqTwZWpcH$q9c;W8n6Tz9qX(W*O8+Z^0F0D=f zH9u9qy|iBY{j0COs0Gz;o-D5mz^syM)~=mAyINhWohJF~Yt1<@QbjZq^JqEIORy6? zJxC_-*ny9o_}GPym+zeizKJ_PJo;+n{>!hx1QXKAF3^ym$mm zrNKxVY@|W^D2wq^oecDg1|pCL^d32i{oQNod%6|)iDEIJ0MPGw_In4BOzk{4h@X6B zD9TtsV7LJaN+v;P)EN*GbhVAck{b*rEVL72D^do_2V7qcOD_Y*za-%$#Y@4SQcg$4 zFTCx0aH){$P#tSQIg=p){IpgRc0Pk&`us`HfZ^!axKp)7905Vz$I%VFH6rXWL>=x7 z(999%Xgildegl71OGL`JT!J&}x0{oQ(k1{Bp;m7+gW$Wy>Pn4aTA7+TRc~-9yz_YW z!R#r(%=wd*7;u|4EM|>+6u4&X?CPy1TVwd}vkiz~BHCS)%7eaVXZ?Q-AqXa2dHf4} zpr~}=RwLGMa=k;hsZ-f79hk1C0-hLQf z(w8o%+@1)d(jo8ZJC(5fGW(IVp}CM5LrriZB3uu?W9Q4Vq}z2(JP-})o6I-CAp}>^lb9&XmkuF8fF{*U z*lux=RsHW?U2;*35}_+6Vxy3e@0RU+DMhomXosF|pM8}CBOY}rlJWSka$mzBl5XX@ z5@6%u%^zYAdD5(=R*!@OHl?%>78?NKKQYsCqc#iu`}eg^gX;C4&P}L3P$#_^%syJV zH|n^H_1a1JVUIIcRVJ?efiklX8uz~W@%EY;Wk?7DhEN;^%JFx5d=ucjWxK~e- z0K%dwTZjHXlmO$6Aygp`2a+YuB&M~#A-Y(vPk_S)Rgv*t5pDo7J?KgMxXN%G1Dir| zqn0ejFy`36Ivb;rRA8SmI4XvT-bf=Zw}Qb7cb9(Aq%?nH=H~J`)(URS5Sy3Q>J6%z zR}xio;>J{c0nx-LY6f$UV-zE9HS3d$kCy&CyHxuUE$y#Nef=|2&r7kX9 z_^dLuSO-yUb){BMtjnm3VH0g6!5Qhupf2JUeU|}ugI_EW1DDySBaiIg`>PS7IiXvEJ6G3!6 CS>Ja6 literal 2303 zcmbW1_ct2~8^>)GM5~k-wPH(1wKhdW2rFYKR9+}Il2FP{67znpz2)nbuX{i z#u7;osvI%uuWZiNe(|{dpVs(_x^A;E{(ZISqnlBT1I^S*>95Wgs@~S~rZJa+&@VbY zF^clhR!3>onAV*%$@C2G>iIEMlIF%FB|7(S~Zq(}oO7|^n-R7an|Di^%>N16$_ ztm~k|4%|0RD-n6yOv}`heliu`)ETW+#qw^$1oX zl6~W%ENpjw#Y4Bm^t$+8S)j|9ye(mT2 z)*H&}V%hiB3rqJ?nLQ`JTvZqG#`j@VefKE$4r#sGVmMNxxZMF2^X2m>(wU-}TdqW$ z{{|1IESVJ#dE!oBZe*9SQ>r?P9c*iP0MV-V39L)G%EGsqgN{4W^s%!Xy4IrfPql75!L?n=U1+|`f!09Im7e3(wn);!8N-yQqr5=k`Xg=* zq853sSE`$iwbZ42KS|-`MmYiPgj8YOW!Rd$0<6eB(Z3n$8BpT1j>$X=glaELSjHd^ z>@Qm8K>>aSAw5;w1u{3IcnozTYUe0)+Sgbg&XffwQY4FWwz7u z3@xvQ_7+o&ra^cAJi>9MU>ym_!f`cdhnD6R;9LFuIKuL3kM9#1CQs13%hkcP_0Tr7 zMW(irf3Gk?e3aJ6jY6xgKdCaeJMPkgVS%q#LWUIxRFvCS!63!vKzmsKE)45iXl?Mz zuty`g;Befgy2#`_`-p{ReDFDF#4Jm<|OkI`?ADun(p?7 z3mT=QH-0U@(sF z3cYo6!22&fv@i@wA1?H6e~SHC`{@uh@6xtuIZzzY-%tv)1#PZoJH(%PH}>&=Kl6U9 zq33er9*d5=SKd&3U}*7coTHDrln~Nj!HtuL@w(41&77{VyY#P0Nt!c!cYDvI{N{Bo zrO>tIh6QBuC{sm#RU;B}`%j2&i9rAv{e&yZE8Xd83*z`4IUs&e1n&FEgz;vSsU4Cc z9=(%vp$n*vOzf~2_D!J*1m;_+hLx*Y`PB_uG10W>%0{WQdVlWTeoA-TBb-qjhY2g~ z>{M)Y{wXr?HoV`U@Y%CsbhD1pa5sc(bLvVb@1qGw`vD>M zTsV#|!ARqr8psrNIk1KIf&MJXW=>{XKnmL=vujA4sXEOP1^(uLUr8k&69j3W!X2W5 z4!Z4nzPmEW9Bj`%@~Rs^T@yGF>+nA3wAZZzguxE+=^C{U4Z!B)$o{5r$^*F@wSgM7 zSKcLeKmkKmZFsP-`UUz#U$LW*GZ!mMY10mts8frk*dt<`y>w_hU z$@^CF0bW%lf%p*p2fD+ISI1dF>~BG(J|JoP!Rcv+hQUh;pAl{nwUSdIb}8$jWg z4jcI5Y)K1~$1Cm0q8xAPeMdwlz!Ij6L7>S-)=hY~qDP^mJHufmJye;WB4;bAE( diff --git a/group16/313001956/src/com/coding/basic/LinkedListTest.java b/group16/313001956/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..2acd774160 --- /dev/null +++ b/group16/313001956/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,28 @@ +package com.coding.basic; + +import org.junit.Assert; +import org.junit.Test; + +public class LinkedListTest { + + @Test + public final void testReverse() { + + LinkedList list=new LinkedList(); + list.add(3); + list.add(7); + list.add(10); + + LinkedList testlist=new LinkedList(); + testlist.add(10); + testlist.add(7); + testlist.add(3); + + list.reverse(list); + Assert.assertEquals(list.size(), testlist.size()); + Assert.assertEquals(list.get(0), testlist.get(0)); + Assert.assertEquals(list.get(1), testlist.get(1)); + Assert.assertEquals(list.get(2), testlist.get(2)); + } + +} From 8a16d7818054c5120275b76851a0e4e0a70c6119 Mon Sep 17 00:00:00 2001 From: stackwei Date: Mon, 20 Mar 2017 09:41:54 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group16/1012075117/.DS_Store | Bin 0 -> 6148 bytes .../{DataStructure219 => }/.classpath | 2 + .../{DataStructure219 => }/.project | 0 .../.settings/org.eclipse.jdt.core.prefs | 0 .../1012075117/src/com/array/ArrayUtil.java | 257 ++++++++++++++++++ .../src/com/array/ArrayUtilTest.java | 161 +++++++++++ .../com/list}/ArrayList.java | 2 +- .../com/list}/LinkedList.java | 2 +- .../DataStructure => src/com/list}/List.java | 2 +- .../DataStructure => src/com/list}/Queue.java | 2 +- .../DataStructure => src/com/list}/Stack.java | 2 +- .../src/com/litestruts/LoginAction.java | 34 +++ .../1012075117/src/com/litestruts/Struts.java | 176 ++++++++++++ .../src/com/litestruts/StrutsTest.java | 39 +++ .../1012075117/src/com/litestruts/View.java | 26 ++ .../1012075117/src/com/litestruts/struts.xml | 11 + 16 files changed, 711 insertions(+), 5 deletions(-) create mode 100644 group16/1012075117/.DS_Store rename group16/1012075117/{DataStructure219 => }/.classpath (61%) rename group16/1012075117/{DataStructure219 => }/.project (100%) rename group16/1012075117/{DataStructure219 => }/.settings/org.eclipse.jdt.core.prefs (100%) create mode 100644 group16/1012075117/src/com/array/ArrayUtil.java create mode 100644 group16/1012075117/src/com/array/ArrayUtilTest.java rename group16/1012075117/{DataStructure219/src/com/stackwei/DataStructure => src/com/list}/ArrayList.java (98%) rename group16/1012075117/{DataStructure219/src/com/stackwei/DataStructure => src/com/list}/LinkedList.java (98%) rename group16/1012075117/{DataStructure219/src/com/stackwei/DataStructure => src/com/list}/List.java (83%) rename group16/1012075117/{DataStructure219/src/com/stackwei/DataStructure => src/com/list}/Queue.java (95%) rename group16/1012075117/{DataStructure219/src/com/stackwei/DataStructure => src/com/list}/Stack.java (95%) create mode 100644 group16/1012075117/src/com/litestruts/LoginAction.java create mode 100644 group16/1012075117/src/com/litestruts/Struts.java create mode 100644 group16/1012075117/src/com/litestruts/StrutsTest.java create mode 100644 group16/1012075117/src/com/litestruts/View.java create mode 100644 group16/1012075117/src/com/litestruts/struts.xml diff --git a/group16/1012075117/.DS_Store b/group16/1012075117/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 + + diff --git a/group16/1012075117/DataStructure219/.project b/group16/1012075117/.project similarity index 100% rename from group16/1012075117/DataStructure219/.project rename to group16/1012075117/.project diff --git a/group16/1012075117/DataStructure219/.settings/org.eclipse.jdt.core.prefs b/group16/1012075117/.settings/org.eclipse.jdt.core.prefs similarity index 100% rename from group16/1012075117/DataStructure219/.settings/org.eclipse.jdt.core.prefs rename to group16/1012075117/.settings/org.eclipse.jdt.core.prefs diff --git a/group16/1012075117/src/com/array/ArrayUtil.java b/group16/1012075117/src/com/array/ArrayUtil.java new file mode 100644 index 0000000000..e3fb19b62f --- /dev/null +++ b/group16/1012075117/src/com/array/ArrayUtil.java @@ -0,0 +1,257 @@ +package com.array; + +import java.util.Arrays; + +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 length; + int[] temp; + + length = origin.length; + temp = new int[length]; + for (int i = 0; i < length; i++) { + temp[length - i - 1] = origin[i]; + } + for (int i = 0; i < length; i++) { + origin[i] = temp[i]; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + int flag = 0; + int j = 0; + int length; + length = oldArray.length; + int[] newArray; + + for (int i = 0; i < length; i++) { + if (oldArray[i] != 0) { + flag++; + } + } + newArray = new int[flag]; + for (int i = 0; i < length; i++) { + if (oldArray[i] != 0) { + newArray[j] = oldArray[i]; + j++; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, 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) { + int[] temp; + int[] array3; + int flag = 0; + int repeat = 0; + boolean boolea = true; + int length1 = array1.length; + int length2 = array2.length; + temp = new int[length1 + length2]; + + // 先把a1添加到temp + for (int i = 0; i < length1; i++) { + temp[i] = array1[i]; + } + // 把a2中不重复的添加到temp + for (int i = 0; i < length2; i++) { + for (int j = 0; j < length1; j++) { + if (temp[j] == array2[i]) { + boolea = false; + repeat++; + } + } + if (boolea) { + temp[length1 + flag] = array2[i]; + flag++; + } + boolea = true; + } + // 有重复就new一个数组长度减去重复的长度的a3,排序并返回 + if (repeat != 0) { + array3 = new int[length1 + length2 - repeat]; + for (int i = 0; i < (temp.length - repeat); i++) { + array3[i] = temp[i]; + } + Arrays.sort(array3); + return array3; + } + // 无重复就排序并返回 + Arrays.sort(temp); + return temp; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] temp; + int length; + + length = oldArray.length; + temp = new int[length + size]; + for (int i = 0; i < length; i++) { + temp[i] = oldArray[i]; + } + oldArray = null; + oldArray = temp; + + return oldArray; + } + + /** + * 斐波那契数列为: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) { + int[] temp = new int[2]; + int[] array; + int f1 = 1; + int f2 = 1; + int length = 2; + + if (max <= 1) { + return null; + } + + temp[0] = f1; + temp[1] = f2; + if (max == 2) { + return temp; + } + + for (int i = 2; temp[i - 1] < max; i++) { + if ((f1 + f2) >= max) + break; + if (i + 1 > temp.length) { + temp = new ArrayUtil().grow(temp, 1); + } + temp[i] = f1 + f2; + f1 = temp[i - 1]; + f2 = temp[i]; + length++; + } + array = new int[length]; + for (int i = 0; i < length; i++) { + array[i] = temp[i]; + } + + return array; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int[] temp = new int[1]; + boolean flag = true; + int i = 0; + + if (max < 3) + return null; + + for (int j = 2; j < max; j++) { + for (int k = 2; k <= Math.sqrt(j); k++) { + if (j % k == 0) { + flag = false; + break; + } + } + if (flag) { + if (i + 1 > temp.length) + temp = new ArrayUtil().grow(temp, 1); + temp[i] = j; + i++; + } + flag = true; + } + return temp; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int[] temp = new int[1]; + int i = 0; + + if (max < 6) + return null; + + for (int j = 1; j < max; j++) { + int total = 0; + for (int k = 1; k < j / 2 + 1; k++) { + if (j % k == 0) + total += k; + } + if (total == j) { + if (i + 1 > temp.length) + temp = new ArrayUtil().grow(temp, 1); + temp[i] = j; + i++; + } + } + return temp; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + if(array == null || array.length ==0 || seperator == null) + return null; + + StringBuilder sb = new StringBuilder(); + int length = array.length; + for(int i=0;i parameters) throws Exception { + + /* + * + * 0. 读取配置文件struts.xml + * + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , + * "password"="1234") , 那就应该调用 setName和setPassword方法 + * + * 2. 通过反射调用对象的execute 方法, 并获得返回值,例如"success" + * + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如 + * {"message": "登录成功"} , 放到View对象的parameters + * + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + * + */ + + int flag = 0; + String className; + String executeResult; + String jsp; + Element resultElement; + List actionList = new ArrayList<>(); + Map classNameMap = new HashMap(); + Map messagesMap = new HashMap(); + View view = new View(); + + actionList = getRootElement("src/com/litestruts/struts.xml");// 获取所有节点 + classNameMap = getClassName(actionList, actionName, classNameMap);// 获取action的类名并放到Map中 + + className = (String) classNameMap.get("className"); + messagesMap = getResult(className, parameters);// messages包含了,调用execute()后的返回值result,和所有getter方法的值和属性 + + executeResult = (String) messagesMap.get("result"); + messagesMap.remove("result"); + flag = (int) classNameMap.get("flag"); + resultElement = actionList.get(flag); + jsp = getJSP(executeResult, resultElement);// 获取到里的jsp + + view.setJsp(jsp); + view.setParameters(messagesMap); + + return view; + } + + /** + * 获取所有节点 + * + * @param fileName + * @return + */ + private static List getRootElement(String fileName) { + File inputXml = new File(fileName); + SAXReader saxReader = new SAXReader(); + Document document = null; + try { + document = saxReader.read(inputXml); + } catch (DocumentException e) { + e.printStackTrace(); + } + Element root = document.getRootElement(); + List al = new ArrayList(); + for (Iterator i = root.elementIterator(); i.hasNext();) { // 获取所有action节点 + Element action = (Element) i.next(); + al.add(action); + } + return al; + } + + /** + * 根据给定的actionName,获取对应的class名字 + * + * @param al + * @param actionName + * @param map + * @return + */ + private static Map getClassName(List al, String actionName, Map map) { + String className = null; + for(int i=0;i getResult(String className, Map parameters) throws Exception { + Class actionClass = null; + Constructor constructor = null; + Object object = null; + Method method = null; + Map map = new HashMap(); + try { + actionClass = Class.forName(className); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + try { + constructor = actionClass.getConstructor(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } + object = constructor.newInstance(); + Set keySet = parameters.keySet(); + // 据parameters中的数据,调用对象的setter方法 + for (String key : keySet) { + if (key.equals("name")) { + method = actionClass.getMethod("setName", String.class); + method.invoke(object, parameters.get(key)); + } + if (key.equals("password")) { + method = actionClass.getMethod("setPassword", String.class); + method.invoke(object, parameters.get(key)); + } + } + // 通过反射调用对象的execute 方法,并获得返回值,例如"success" + method = actionClass.getMethod("execute"); + String result = (String) method.invoke(object); + map.put("result", result); + + //找到对象的所有getter方法,把值和属性形成一个HashMap + Method getName = actionClass.getMethod("getName"); + Method getPassword = actionClass.getMethod("getPassword"); + Method getMessage = actionClass.getMethod("getMessage"); + map.put("name", getName.invoke(object)); + map.put("password", getPassword.invoke(object)); + map.put("message", getMessage.invoke(object)); + + return map; + } + + private static String getJSP(String result, Element actionElement) { + String jsp = null; + for (Iterator i = actionElement.elementIterator(); i.hasNext();) { // 获取所有action子节点result + Element resultElement = (Element) i.next(); + if(resultElement.attribute("name").getValue().equals(result)) { + jsp = resultElement.getTextTrim(); + } + } + return jsp; + } + +} \ No newline at end of file diff --git a/group16/1012075117/src/com/litestruts/StrutsTest.java b/group16/1012075117/src/com/litestruts/StrutsTest.java new file mode 100644 index 0000000000..f404679192 --- /dev/null +++ b/group16/1012075117/src/com/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +package com.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws Exception { + + 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() throws Exception { + 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/group16/1012075117/src/com/litestruts/View.java b/group16/1012075117/src/com/litestruts/View.java new file mode 100644 index 0000000000..227f6c2ed2 --- /dev/null +++ b/group16/1012075117/src/com/litestruts/View.java @@ -0,0 +1,26 @@ +package com.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/group16/1012075117/src/com/litestruts/struts.xml b/group16/1012075117/src/com/litestruts/struts.xml new file mode 100644 index 0000000000..90d26a0822 --- /dev/null +++ b/group16/1012075117/src/com/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