diff --git a/group01/1298552064/.classpath b/group01/1298552064/.classpath new file mode 100644 index 0000000000..05cf0dba9e --- /dev/null +++ b/group01/1298552064/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group01/1298552064/.gitignore b/group01/1298552064/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group01/1298552064/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group01/1298552064/.project b/group01/1298552064/.project new file mode 100644 index 0000000000..ddbb7719d0 --- /dev/null +++ b/group01/1298552064/.project @@ -0,0 +1,17 @@ + + + 1298552064Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group01/1298552064/src/week01/basic/MyLinkedList.java b/group01/1298552064/src/week01/basic/MyLinkedList.java index 4894c5ff6c..88db213864 100644 --- a/group01/1298552064/src/week01/basic/MyLinkedList.java +++ b/group01/1298552064/src/week01/basic/MyLinkedList.java @@ -124,7 +124,7 @@ public Object removeLast() { Node p = head; for (int i = 0; i < size; i++) { if (p.next.next == null) { - removeObject = p.next; + removeObject = p.next.data; p.next = null; break; } else { diff --git a/group01/1298552064/src/week02/array/ArrayUtil.java b/group01/1298552064/src/week02/array/ArrayUtil.java new file mode 100644 index 0000000000..9b6c0bebaf --- /dev/null +++ b/group01/1298552064/src/week02/array/ArrayUtil.java @@ -0,0 +1,245 @@ +package week02.array; + +import java.util.Arrays; + +public class ArrayUtil { + + // 工具类,不予许创建实例 + private 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 static void reverseArray(int[] origin) { + if (origin != null && origin.length > 0) { + int temp = 0; + + // 数组首尾元素置换 + for (int i = 0; i < origin.length / 2; i++) { + temp = origin[i]; + origin[i] = origin[origin.length - i - 1]; + origin[origin.length - i - 1] = 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 static int[] removeZero(int[] oldArray) { + int[] newArray = null; + if (oldArray != null) { + newArray = new int[oldArray.length]; + int size = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArray[size] = oldArray[i]; + size++; + } + } + newArray = Arrays.copyOf(newArray, size); + } + 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 static int[] merge(int[] array1, int[] array2) { + int[] newArray = null; + if (array1 != null && array2 != null) { + int size = 0; + + // index1、index2表示array1和array2数组的比较索引 + int index1 = 0, index2 = 0; + newArray = new int[array1.length + array2.length]; + + while (index1 < array1.length && index2 < array2.length) { + if (array1[index1] == array2[index2]) { + newArray[size++] = array1[index1]; + index1++; + index2++; + } else if (array1[index1] < array2[index2]) { + // 数组array1去重 + if (size > 0 && array1[index1] == newArray[size - 1]) { + size--; + } + newArray[size++] = array1[index1]; + index1++; + } else { + // 数组array2去重 + if (size > 0 && array2[index2] == newArray[size - 1]) { + size--; + } + newArray[size++] = array2[index2]; + index2++; + } + } + + // 将数组array1剩下的元素放入 + while (index1 < array1.length) { + newArray[size++] = array1[index1++]; + } + + // 将数组array2剩下的元素放入 + while (index2 < array2.length) { + newArray[size++] = array2[index2++]; + } + + // 合并后有序数组 + newArray = Arrays.copyOf(newArray, size); + } + return newArray; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + int[] newArray = null; + if (oldArray != null) { + newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + + // 计算方法:f(n) = f(n-1) + f(n-2) 采用数组计算 + int[] result = null; + if (max <= 1) { + result = new int[] {}; + } else { + int i = 2; + result = new int[max]; + result[0] = result[1] = 1; + for (; i < max; i++) { + if (result[i - 1] + result[i - 2] < max) { + result[i] = result[i - 1] + result[i - 2]; + } else { + break; + } + } + result = Arrays.copyOf(result, i); + } + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + int[] newArray = new int[] {}; + if (max > 2) { + newArray = new int[max]; + int size = 0, j = 0; + for (int i = 2; i < max; i++) { + for (j = 2; j < i / 2 + 1; j++) { + if (i % j == 0) { + break; + } + } + + if (j == i / 2 + 1) { + newArray[size++] = i; + } + } + newArray = Arrays.copyOf(newArray, size); + } + return newArray; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + int[] newArray = new int[] {}; + if (max > 0) { + newArray = new int[max]; + int size = 0, sum = 0; + for (int i = 1; i < max; i++) { + sum = 0; + for (int j = 1; j < i / 2 + 1; j++) { + if (i % j == 0) { + sum += j; + } + } + if (i == sum) { + newArray[size++] = i; + } + } + newArray = Arrays.copyOf(newArray, size); + } + return newArray; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public static String join(int[] array, String seperator) { + String joinResult = null; + if (array != null) { + joinResult = ""; + for (int i = 0; i < array.length; i++) { + joinResult += array[i] + seperator; + } + joinResult = joinResult.equals("") ? "" : joinResult.substring(0, joinResult.length() - 1); + } + return joinResult; + } + + public static void main(String[] args) { + int[] a = new ArrayUtil().getPerfectNumbers(1000); + for (int i = 0; i < a.length; i++) { + System.out.println(a[i]); + } + + // [2,3,5,7,11,13,17,19] + a = new ArrayUtil().getPrimes(20); + for (int i = 0; i < a.length; i++) { + System.out.println(a[i]); + } + } +} diff --git a/group01/1298552064/src/week02/litestruts/LoginAction.java b/group01/1298552064/src/week02/litestruts/LoginAction.java new file mode 100644 index 0000000000..aec243dd1b --- /dev/null +++ b/group01/1298552064/src/week02/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +package week02.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/group01/1298552064/src/week02/litestruts/Struts.java b/group01/1298552064/src/week02/litestruts/Struts.java new file mode 100644 index 0000000000..3cef26c396 --- /dev/null +++ b/group01/1298552064/src/week02/litestruts/Struts.java @@ -0,0 +1,106 @@ +package week02.litestruts; + +import java.io.InputStream; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +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字段中。 + * + */ + + try { + + // 0. 读取配置文件struts.xml + SAXReader reader = new SAXReader(); + InputStream in = Struts.class.getResourceAsStream("struts.xml"); + Document document = reader.read(in); + Element root = document.getRootElement(); + + // 与actionName匹配的Element + Element actionElement = null; + String className = null; + + for (Iterator iterator = root.elementIterator("action"); iterator.hasNext();) { + Element e = iterator.next(); + if (e.attributeValue("name").equals(actionName)) { + actionElement = e; + className = e.attributeValue("class"); + break; + } + } + + Class clazz = Class.forName(className); + Object action = clazz.newInstance(); + + // 1. 反射设置属性 + if (parameters != null) { + for (Map.Entry entry : parameters.entrySet()) { + String fieldName = entry.getKey(); + String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); + Class fieldType = clazz.getDeclaredField(fieldName).getType(); + Method method = clazz.getDeclaredMethod(methodName, fieldType); + method.invoke(action, entry.getValue()); + } + } + + // 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + Method execute = clazz.getDeclaredMethod("execute"); + String result = (String) execute.invoke(action); + + // 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap + Method[] methods = clazz.getDeclaredMethods(); + Map param = new HashMap(); + for (Method method : methods) { + String methodName = method.getName(); + if (method.getName().startsWith("get")) { + String fieldName = methodName.substring(3, 4).toLowerCase() + methodName.substring(4); + Object fieldValue = method.invoke(action); + param.put(fieldName, fieldValue); + } + } + + View view = new View(); + view.setParameters(param); + + // 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + // 放到View对象的jsp字段中。 + for (Iterator iterator = actionElement.elementIterator("result"); iterator.hasNext();) { + Element resultElement = iterator.next(); + if (resultElement.attributeValue("name").equals(result)) { + view.setJsp(resultElement.getText()); + break; + } + } + + return view; + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } +} diff --git a/group01/1298552064/src/week02/litestruts/View.java b/group01/1298552064/src/week02/litestruts/View.java new file mode 100644 index 0000000000..a286412f0e --- /dev/null +++ b/group01/1298552064/src/week02/litestruts/View.java @@ -0,0 +1,26 @@ +package week02.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/group01/1298552064/src/week02/litestruts/struts.xml b/group01/1298552064/src/week02/litestruts/struts.xml new file mode 100644 index 0000000000..01398e9c3d --- /dev/null +++ b/group01/1298552064/src/week02/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/group01/1298552064/src/week02/test/ArrayUtilTest.java b/group01/1298552064/src/week02/test/ArrayUtilTest.java new file mode 100644 index 0000000000..e796b5f845 --- /dev/null +++ b/group01/1298552064/src/week02/test/ArrayUtilTest.java @@ -0,0 +1,75 @@ +package week02.test; + +import org.junit.Assert; +import org.junit.Test; + +import week02.array.ArrayUtil; + +public class ArrayUtilTest { + + @Test + public void testReverseArray() { + int[] a = new int[] { 7, 9, 30, 3 }; + int[] b = new int[] { 7, 9, 30, 3, 4 }; + + ArrayUtil.reverseArray(a); + ArrayUtil.reverseArray(b); + + Assert.assertArrayEquals(new int[] { 3, 30, 9, 7 }, a); + Assert.assertArrayEquals(new int[] { 4, 3, 30, 9, 7 }, b); + } + + @Test + public void testRemoveZero() { + int[] oldArr = new int[] { 1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 }; + int[] newArray = ArrayUtil.removeZero(oldArr); + Assert.assertArrayEquals(new int[] { 1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5 }, newArray); + } + + @Test + public void testMerge() { + int[] a1 = new int[] { 3, 5, 7, 8 }; + int[] a2 = new int[] { 4, 5, 6, 6, 7, 7 }; + int[] a3 = ArrayUtil.merge(a1, a2); + Assert.assertArrayEquals(new int[] { 3, 4, 5, 6, 7, 8 }, a3); + } + + @Test + public void testGrow() { + int[] oldArray = new int[] { 2, 3, 6 }; + int size = 3; + int[] newArray = ArrayUtil.grow(oldArray, size); + Assert.assertArrayEquals(new int[] { 2, 3, 6, 0, 0, 0 }, newArray); + } + + @Test + public void testFibonacci() { + int max = 15; + int max2 = 1; + int[] newArray = ArrayUtil.fibonacci(max); + int[] newArray2 = ArrayUtil.fibonacci(max2); + Assert.assertArrayEquals(new int[] { 1, 1, 2, 3, 5, 8, 13 }, newArray); + Assert.assertArrayEquals(new int[] {}, newArray2); + } + + @Test + public void testGetPrimes() { + int[] newArray = ArrayUtil.getPrimes(23); + Assert.assertArrayEquals(new int[] { 2, 3, 5, 7, 11, 13, 17, 19 }, newArray); + } + + @Test + public void testGetPerfectNumbers() { + int[] newArray = ArrayUtil.getPerfectNumbers(1000); + Assert.assertArrayEquals(new int[] { 6, 28, 496 }, newArray); + } + + @Test + public void testJoin() { + int[] array = new int[] { 3, 8, 9 }; + String seperator = "-"; + String result = ArrayUtil.join(array, seperator); + Assert.assertEquals("3-8-9", result); + } + +} diff --git a/group01/1298552064/src/week02/test/StrutsTest.java b/group01/1298552064/src/week02/test/StrutsTest.java new file mode 100644 index 0000000000..3eb6d01fd0 --- /dev/null +++ b/group01/1298552064/src/week02/test/StrutsTest.java @@ -0,0 +1,41 @@ +package week02.test; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +import week02.litestruts.Struts; +import week02.litestruts.View; + +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/group01/1664823950/src/com/coderising/array/ArrayUtil.java b/group01/1664823950/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..afb557277f --- /dev/null +++ b/group01/1664823950/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,209 @@ +package com.coderising.array; +import java.util.*; + +import com.sun.org.apache.bcel.internal.generic.NEWARRAY; + +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[] a = origin; + for (int i = 0; i < a.length; i++) + { + origin[i] = a[a.length-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) + { + ArrayList a= new ArrayList(); + + for (int i : oldArray) + { + if (i != 0) + { + a.add(i); + } + } + + int[] newArray = new int[a.size()]; + for (int i = 0; i < a.size(); i++) + { + newArray[i] = a.get(i); + } + 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) + { + for (int i = 0; i < array1.length; i++) + { + for (int j = 0; j < array2.length; j++) + { + if(array1[i] == array2[j]) + { + array2[j] = 0; + } + } + } + + removeZero(array2); + + int[] array3 = new int[array1.length + array2.length]; + + for (int i = 0; i < array1.length; i++) + { + array3[i] = array1[i]; + } + + for (int i = 0; i < array2.length; i++) + { + array3[array1.length + i] = array2[i]; + } + + Arrays.sort(array3); + return array3; + } + /** + * 把一个已经存满数据的数组 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 new int[oldArray.length + size]; + } + + /** + * 斐波那契数列为: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 top = 1; + int sec = 1; + ArrayList tem = new ArrayList(); + while (top < max) + { + tem.add(top); + int a = top; + top += sec; + sec = a; + } + + + return toArray(tem); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max) + { + ArrayList tem = new ArrayList(); + for (int i = 0; i < max; i++) + { + if (isPrimes(i)) + { + tem.add(i); + } + } + + return toArray(tem); + } + + private boolean isPrimes(int i) + { + return true; + } + + private int[] toArray(ArrayList tem) + { + int[] newArr = new int[tem.size()]; + for (int i = 0; i < newArr.length; i++) + { + newArr[i] = tem.get(i); + } + return newArr; + } + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) + { + ArrayList tem = new ArrayList(); + for (int i = 0; i < max; i++) + { + if (isPerfectNumbers(i)) + { + tem.add(i); + } + } + + return toArray(tem); + } + + + private boolean isPerfectNumbers(int i) + { + return true; + } + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) + { + String newStr = ""; + for (int i = 0; i < array.length; i++) + { + if(i == array.length-1) + { + seperator = ""; + } + newStr += array[i] + seperator; + } + return newStr; + } + +} diff --git a/group01/1664823950/src/com/coderising/litestruts/LoginAction.java b/group01/1664823950/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group01/1664823950/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/group01/1664823950/src/com/coderising/litestruts/Struts.java b/group01/1664823950/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..44cc35bf01 --- /dev/null +++ b/group01/1664823950/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/group01/1664823950/src/com/coderising/litestruts/StrutsTest.java b/group01/1664823950/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group01/1664823950/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/group01/1664823950/src/com/coderising/litestruts/View.java b/group01/1664823950/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group01/1664823950/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/group01/1664823950/src/com/coderising/litestruts/struts.xml b/group01/1664823950/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..99063bcb0c --- /dev/null +++ b/group01/1664823950/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/group01/1814014897/zhouhui/src/.project b/group01/1814014897/zhouhui/src/.project new file mode 100644 index 0000000000..f7d6de6781 --- /dev/null +++ b/group01/1814014897/zhouhui/src/.project @@ -0,0 +1,11 @@ + + + src + + + + + + + + diff --git a/group01/1814014897/zhouhui/src/week02/array/ArrayUtil.java b/group01/1814014897/zhouhui/src/week02/array/ArrayUtil.java new file mode 100644 index 0000000000..dd939e7d2c --- /dev/null +++ b/group01/1814014897/zhouhui/src/week02/array/ArrayUtil.java @@ -0,0 +1,222 @@ +package week02.array; + +/** + * + * @author Hui Zhou + * @version 1.0 2017-02-28 + * + */ + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + if(origin == null) return; + + int mid = origin.length/2; + for(int i=0;iarray4[j+1]){ + int sto = array4[j]; + array4[j] = array4[j+1]; + array4[j+1] = sto; + } + } + } + return array4; + } + /** + * 把一个已经存满数据的数组 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){ + if(size<0 || oldArray==null) return null; + + int[] newArray = new int[oldArray.length + size]; + for(int i=0;i 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字段中。 + + */ + + //读取配置文件struts.xml + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder; + Document doc = null; + View view = new View(); //实例化View(后面调用view,存储parameters以及jsp,最后return view) + try { + builder = factory.newDocumentBuilder(); + File f = new File("src/week02/litestruts/struts.xml"); + doc = builder.parse(f); + } catch (ParserConfigurationException|SAXException|IOException e) { + e.printStackTrace(); + } + + //根据actionName找到相对应的action + Element root = doc.getDocumentElement(); + NodeList actionNode = root.getElementsByTagName("action"); + Element action = null; + for(int i=0;i cls = Class.forName(actionClass); + Object obj = cls.newInstance(); + Method setName = cls.getMethod("setName", String.class); + Method setPassword = cls.getMethod("setPassword", String.class); + setName.invoke(obj, parameters.get("name")); + setPassword.invoke(obj, parameters.get("password")); + + //通过反射调用对象的exectue 方法,并获得返回值 + Method execute = cls.getMethod("execute"); + String exe_val = (String) execute.invoke(obj); + + //通过反射找到对象的所有getter方法,通过反射来调用 + Method[] met = cls.getDeclaredMethods(); + List list = new LinkedList(); + for(int i=0;i param = new HashMap<>(); + for(int i=0;i 配置,以及execute的返回值,确定哪一个jsp,放到View对象的jsp字段中 + if(exe_val.equals("success")) + view.setJsp("/jsp/homepage.jsp"); + else view.setJsp("/jsp/showLogin.jsp"); + + } catch (ClassNotFoundException|InstantiationException|IllegalAccessException + |NoSuchMethodException|SecurityException|IllegalArgumentException|InvocationTargetException e) { + e.printStackTrace(); + } + + return view; + } +} diff --git a/group01/1814014897/zhouhui/src/week02/litestruts/StrutsTest.java b/group01/1814014897/zhouhui/src/week02/litestruts/StrutsTest.java new file mode 100644 index 0000000000..e65f6525bd --- /dev/null +++ b/group01/1814014897/zhouhui/src/week02/litestruts/StrutsTest.java @@ -0,0 +1,41 @@ +package week02.litestruts; + +import java.util.*; +import org.junit.*; + +/** + * @author Hui Zhou + * @version 1.0 2017-02-28 + */ + +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/group01/1814014897/zhouhui/src/week02/litestruts/View.java b/group01/1814014897/zhouhui/src/week02/litestruts/View.java new file mode 100644 index 0000000000..3043fb5d5a --- /dev/null +++ b/group01/1814014897/zhouhui/src/week02/litestruts/View.java @@ -0,0 +1,28 @@ +package week02.litestruts; + +import java.util.Map; + +/** + * @author Hui Zhou + * @version 1.0 2017-02-28 + */ + +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/group01/1814014897/zhouhui/src/week02/litestruts/struts.xml b/group01/1814014897/zhouhui/src/week02/litestruts/struts.xml new file mode 100644 index 0000000000..f449db14dd --- /dev/null +++ b/group01/1814014897/zhouhui/src/week02/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/group01/2137642225/work01/README.md b/group01/2137642225/work01/README.md new file mode 100644 index 0000000000..46aae880a3 --- /dev/null +++ b/group01/2137642225/work01/README.md @@ -0,0 +1,6 @@ +- 实现基本的数据结构 +# ArrayList +# LinkedList +# Stack +# Queue +# BinaryTree \ No newline at end of file diff --git a/group01/2137642225/work02/.classpath b/group01/2137642225/work02/.classpath new file mode 100644 index 0000000000..f832756744 --- /dev/null +++ b/group01/2137642225/work02/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group01/2137642225/work02/.gitignore b/group01/2137642225/work02/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group01/2137642225/work02/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group01/2137642225/work02/.project b/group01/2137642225/work02/.project new file mode 100644 index 0000000000..e340a1dc5b --- /dev/null +++ b/group01/2137642225/work02/.project @@ -0,0 +1,17 @@ + + + work02 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group01/2137642225/work02/.settings/org.eclipse.jdt.core.prefs b/group01/2137642225/work02/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..838bd9d694 --- /dev/null +++ b/group01/2137642225/work02/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/group01/2137642225/work02/README.md b/group01/2137642225/work02/README.md new file mode 100644 index 0000000000..ce9e536748 --- /dev/null +++ b/group01/2137642225/work02/README.md @@ -0,0 +1 @@ +-- 实现数组工具类和读取xml \ No newline at end of file diff --git a/group01/2137642225/work02/src/com/coderising/array/ArrayUtil.java b/group01/2137642225/work02/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..f760a015f9 --- /dev/null +++ b/group01/2137642225/work02/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,236 @@ +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){ + if (origin != null && origin.length > 1) { + int len = origin.length; + int temp; + for(int left = 0,right = len - 1; left < right; left++,right = len - left - 1){ + temp = origin[left]; + origin[left] = origin[right]; + origin[right] = 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){ + int[] newArray = null; + if (oldArray != null && oldArray.length > 0) { + int[] indexArray = new int[oldArray.length]; + int j = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i] != 0){ + indexArray[j++] = i; + } + } + newArray = new int[j]; + for (int i = 0; i < j; i++) { + newArray[i] = oldArray[indexArray[i]]; + } + indexArray = null; + } + 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){ + if(array1 == null || array1.length <= 0){ + return array2; + } + if(array2 == null || array2.length <= 0){ + return array1; + } + int[] tempArray = new int[array1.length + array2.length]; + int i = 0,j = 0,k = 0; + for (; i < array1.length && j < array2.length; ) { + if (array1[i] > array2[j]) { + tempArray[k++] = array2[j++]; + } + else if(array1[i] < array2[j]){ + tempArray[k++] = array1[i++]; + } + else { + tempArray[k++] = array1[i++]; + j++; + } + } + // 以array1为结束点 + if(array1[array1.length - 1] > array2[array2.length - 1]){ + for (; i < array1.length;) { + tempArray[k++] = array1[i++]; + } + } else { // 以array2为结束点 + for (; j < array2.length;) { + tempArray[k++] = array1[j++]; + } + } + int[] mergeArray = new int[k]; + for (int l = 0; l < mergeArray.length; l++) { + mergeArray[l] = tempArray[l]; + } + tempArray = null; + return mergeArray; + } + /** + * 把一个已经存满数据的数组 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){ + if(size <= 0){ + throw new RuntimeException("size大于0"); + } + int[] newArray = null; + if(oldArray != null && oldArray.length > 0){ + newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + if(max <= 1){ + return new int[0]; + } + int[] tempArray = new int[max]; + int i = 0; + tempArray[i++] = 1; + tempArray[i] = 1; + while(tempArray[i] < max){ + i++; + tempArray[i] = tempArray[i - 1] + tempArray[i - 2]; + } + int[] array = new int[i]; + for (int j = 0; j < array.length; j++) { + array[j] = tempArray[j]; + } + tempArray = null; + return array; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max <= 2){ + return new int[0]; + } + int[] tempArray = new int[max]; + int j = 0; + for (int i = 2; i < max; i++) { + if(isPrime(i)){ + tempArray[j++] = i; + } + } + int[] array = new int[j]; + for (int i = 0; i < j; i++) { + array[i] = tempArray[i]; + } + tempArray = null; + return array; + } + + private boolean isPrime(int i) { + for (int j = 2; j < i; j++) { + if(i % j == 0){ + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if(max <= 2){ + return new int[0]; + } + int[] tempArray = new int[max]; + int j = 0; + for (int i = 3; i < max; i++) { + if(isPerfectNumber(i)){ + tempArray[j++] = i; + } + } + int[] array = new int[j]; + for (int i = 0; i < j; i++) { + array[i] = tempArray[i]; + } + tempArray = null; + return array; + } + + private boolean isPerfectNumber(int num) { + int sum = 1; + for(int i = 2; i < num; i++){ + if(num % i == 0){ + sum += i; + } + } + if(sum == num){ + return true; + } + return false; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + char[] chars = new char[array.length<<1]; + for (int i = 0,j = 1; i < chars.length; i+=2,j+=2) { + chars[i] = (char) (array[i>>1] + 48); + chars[j] = seperator.charAt(0); + } + return new String(chars, 0, chars.length - 1); + } + + +} diff --git a/group01/2137642225/work02/src/com/coderising/litestruts/LoginAction.java b/group01/2137642225/work02/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group01/2137642225/work02/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/group01/2137642225/work02/src/com/coderising/litestruts/Struts.java b/group01/2137642225/work02/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..ab57e27477 --- /dev/null +++ b/group01/2137642225/work02/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,338 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLDecoder; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + +@SuppressWarnings("unchecked") +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + if(actionName == null || actionName.trim().equals("")){ + throw new RuntimeException("传入的actionName不能为null或者空"); + } + + // 0. 读取配置文件struts.xml ok + URL resource = Struts.class.getResource("/com/coderising/litestruts"); + String path = ""; + try { + path = URLDecoder.decode(resource.getPath(), "UTF-8"); + } catch (UnsupportedEncodingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Map> actionMap = xmlParse(path + File.separator + "struts.xml"); + + // 找到访问的action通过actionName + Map action = findAction(actionName,actionMap); + + //1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + //据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + //("name"="test" , "password"="1234") , + //那就应该调用 setName和setPassword方法 + // 实例化对象 + String className = (String) action.get("class"); + Class clazz = getActionClassByClassName(className); + Object actionObject = buildActionObject(clazz,parameters); + + //2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + // 执行访问的方法 + String result = (String) executeAccessMethod(actionObject,clazz,"execute"); + + //3. 通过反射找到对象的所有getter方法(例如 getMessage), + //通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + //放到View对象的parameters + Map parameterMap = getActionObjectParameters(actionObject,clazz); + + //4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + //放到View对象的jsp字段中。 + String jsp = getViewPath(action,result); + View v = buildView(jsp,parameterMap); + + return v; + } + + private static Class getActionClassByClassName(String className) { + + if(className == null || className.trim().equals("")){ + throw new RuntimeException("没有配置action的class属性"); + } + + try { + return Class.forName(className); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return null; + } + + /** + * 获取配置文件中视图的路径 + * @param action + * @param result + * @return + */ + private static String getViewPath(Map action, String result) { + + if(result != null && !result.trim().equals("")){ + + List> resultList = (List>) action.get("childElementList"); + + if(resultList != null && !resultList.isEmpty()){ + for (Map map : resultList) { + String readResult = (String) map.get("name"); + if(result.equals(readResult)){ + Object jsp = map.get("text"); + if(jsp == null){ + throw new RuntimeException("未找到与返回结果[" + result + "]之对应的视图"); + } + return (String) jsp; + } + } + } + } + return null; + } + + /** + * 执行访问的方法 + * @param actionObject 访问的action实例化的对象 + * @param clazz 访问的action Class + * @param methodName 访问的方法名称 + * @return 方法的执行结果 + */ + private static Object executeAccessMethod(Object actionObject, Class clazz,String methodName) { + try { + Method method = clazz.getMethod(methodName); + return method.invoke(actionObject); + } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + return null; + } + + /** + * 执行action对象的getter方法,将执行结果放入map中 + * @param actionObject 访问的action实例化的对象 + * @param clazz 访问的action Class + * @return + */ + private static Map getActionObjectParameters(Object actionObject, Class clazz) { + + Map parameterMap = new HashMap<>(); + Object result = null; + Method[] declaredMethods = clazz.getDeclaredMethods(); + Class[] parameterTypes = null; + if (declaredMethods != null && declaredMethods.length > 0) { + try { + for (Method method : declaredMethods) { + if (isGetMethod(method)) { + parameterTypes = method.getParameterTypes(); + result = method.invoke(actionObject, (Object[])parameterTypes); + + String methodName = method.getName(); + // getMessage 截取 M(转小写) + 截取essage + String subMethodName = Character.toLowerCase(methodName.charAt(3)) + + methodName.substring(4, methodName.length()); + + parameterMap.put(subMethodName, result); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + return parameterMap; + } + + /** + * 建立action对象 + * @param clazz + * @param parameters 传过来的参数 + * @return + */ + private static Object buildActionObject(@SuppressWarnings("rawtypes") Class clazz, Map parameters) { + + Object actionObj = null; + try { + actionObj = clazz.newInstance(); + // 给action对象的属性设置传过来的参数值 + setProperties(clazz, actionObj, parameters); + return actionObj; + } catch (Exception e) { + e.printStackTrace(); + } + if(actionObj == null){ + throw new RuntimeException("无法实例化action:"); + } + return actionObj; + } + + /** + * 建立View对象 + * @param jsp + * @param parameters + * @return + */ + private static View buildView(String jsp,Map parameters) { + + View v = new View(); + v.setJsp(jsp); + v.setParameters(parameters); + + return v; + } + + /** + * 通过actionName在action列表中查找对应的action(map) + * @param actionName + * @param actionMap action列表 + * @return + */ + private static Map findAction(String actionName, Map> actionMap) { + Map action = (actionMap != null && !actionMap.isEmpty()) ? actionMap.get(actionName) : null; + if(action == null){ + throw new RuntimeException("访问的action[" + actionName + "]不存在"); + } + return action; + } + + /** + * 是否是getter方法 + * @param method2 + * @return + */ + private static boolean isGetMethod(Method method2) { + if(method2.getName().startsWith("get")){ + return true; + } + return false; + } + + /** + * 为Action对象设置属性,也就是执行与参数对应的setter方法 + * @param clazz + * @param actionObj + * @param parameters 参数 + * @throws NoSuchMethodException + * @throws SecurityException + * @throws InstantiationException + * @throws IllegalAccessException + * @throws IllegalArgumentException + * @throws InvocationTargetException + */ + private static void setProperties(@SuppressWarnings("rawtypes") Class clazz, Object actionObj, Map parameters) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + + if(parameters != null && !parameters.isEmpty()){ + Set> entrySet = parameters.entrySet(); + for (Entry entry : entrySet) { + String key = entry.getKey(); + String value = entry.getValue(); + Method method = clazz.getMethod("set" + Character.toUpperCase(key.charAt(0)) + key.substring(1, key.length()),String.class); + method.invoke(actionObj, value); + } + } + } + + /** + * 解析xml配置文件 + * @param xmlFilePath + * @return + */ + private static Map> xmlParse(String xmlFilePath) { + File file = new File(xmlFilePath); + SAXReader saxReader = new SAXReader(); + + Map> actionMap = new HashMap<>(); + try { + Document document = saxReader.read(file); + Element rootElement = document.getRootElement(); + actionMap = readActionElement(rootElement); + return actionMap; + } catch (DocumentException e) { + e.printStackTrace(); + } + return null; + } + + /** + * 读取action节点元素 + * @param rootElement + * @return + */ + private static Map> readActionElement(Element rootElement) { + // 存储所有action的信息 + Map> actionMap = new HashMap<>(); + for (Iterator i = rootElement.elementIterator();i.hasNext();) { + Element element = i.next(); + Map action = readElement(element ); + // 设置actionMap key[action的name] value[action map] + actionMap.put((String) action.get("name"), action); + } + return actionMap; + } + + /** + * 读取元素信息 + * @param element + * @return + */ + private static Map readElement(Element element) { + // 读属性 + Map map = readAttribute(element); + String text = readText(element); + map.put("text", text); + List> childElementList = new ArrayList<>(); + // 查找子元素 + for(Iterator iterator = element.elementIterator();iterator.hasNext();){ + childElementList.add(readElement(iterator.next())); + } + if(childElementList != null && !childElementList.isEmpty()){ + map.put("childElementList", childElementList); + } + return map; + } + + /** + * 读取节点的text "text" + * @param element + * @return + */ + private static String readText(Element element) { + return element.getText(); + } + + + /** + * 读取节点的属性值 + * @param element + * @return map key:属性名称 value:属性值 + */ + private static Map readAttribute(Element element) { + Map attrMap = new HashMap<>(); + for (Iterator a = element.attributeIterator();a.hasNext();) { + Attribute attr = a.next(); + attrMap.put(attr.getName(), attr.getData()); + } + return attrMap; + } + +} diff --git a/group01/2137642225/work02/src/com/coderising/litestruts/StrutsTest.java b/group01/2137642225/work02/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a0408661a5 --- /dev/null +++ b/group01/2137642225/work02/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,44 @@ +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/group01/2137642225/work02/src/com/coderising/litestruts/View.java b/group01/2137642225/work02/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..b94168e223 --- /dev/null +++ b/group01/2137642225/work02/src/com/coderising/litestruts/View.java @@ -0,0 +1,26 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + @SuppressWarnings("rawtypes") + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + @SuppressWarnings("rawtypes") + public Map getParameters() { + return parameters; + } + @SuppressWarnings("rawtypes") + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group01/2137642225/work02/src/com/coderising/litestruts/struts.xml b/group01/2137642225/work02/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..a4ca47c733 --- /dev/null +++ b/group01/2137642225/work02/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/group01/2137642225/work02/src/com/coderising/test/ArrayUtilTest.java b/group01/2137642225/work02/src/com/coderising/test/ArrayUtilTest.java new file mode 100644 index 0000000000..2be9e8bbfd --- /dev/null +++ b/group01/2137642225/work02/src/com/coderising/test/ArrayUtilTest.java @@ -0,0 +1,76 @@ +package com.coderising.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import com.coderising.array.ArrayUtil; + +public class ArrayUtilTest { + + private ArrayUtil arrayUtil; + @Before + public void setUp() throws Exception { + arrayUtil = new ArrayUtil(); + } + + @Test + public void testReverseArray() { + int[] array = new int[]{7, 9, 30, 3, 4}; + arrayUtil.reverseArray(array); + assertArrayEquals(new int[]{4,3,30,9,7}, array); + } + + @Test + public void testRemoveZero() { + int[] oldArray = new int[]{1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] newArray = arrayUtil.removeZero(oldArray); + assertEquals(12, newArray.length); + assertArrayEquals(new int[]{1,3,4,5,6,6,5,4,7,6,7,5}, newArray); + } + + @Test + public void testMerge() { + int[] array1 = new int[]{3, 5, 7,8}; + int[] array2 = new int[]{4, 5, 6,7}; + int[] merge = arrayUtil.merge(array1, array2); + assertArrayEquals(new int[]{3,4,5,6,7,8}, merge); + assertEquals(6, merge.length); + } + + @Test + public void testGrow() { + int[] array = new int[]{2,3,6}; + int[] grow = arrayUtil.grow(array, 3); + assertArrayEquals(new int[]{2,3,6,0,0,0}, grow); + assertEquals(6, grow.length); + } + + @Test + public void testFibonacci() { + int[] fibonacci = arrayUtil.fibonacci(15); + assertArrayEquals(new int[]{1,1,2,3,5,8,13}, fibonacci); + assertEquals(7, fibonacci.length); + } + + @Test + public void testGetPrimes() { + int[] primes = arrayUtil.getPrimes(23); + assertArrayEquals(new int[]{2,3,5,7,11,13,17,19}, primes); + assertEquals(8, primes.length); + } + + @Test + public void testGetPerfectNumbers() { + int[] perfectNumbers = arrayUtil.getPerfectNumbers(1000); + assertArrayEquals(new int[]{6,28,496}, perfectNumbers); + } + + @Test + public void testJoin() { + String join = arrayUtil.join(new int[]{3,8,9}, "-"); + assertEquals("3-8-9", join); + } + +} diff --git a/group01/280646174/basic/pom.xml b/group01/280646174/basic/pom.xml index 421a346d70..ed06a3dfdc 100644 --- a/group01/280646174/basic/pom.xml +++ b/group01/280646174/basic/pom.xml @@ -12,6 +12,14 @@ basic + + org.jvnet.hudson.dom4j + dom4j + + + com.google.guava + guava + junit diff --git a/group01/280646174/basic/src/main/java/com/coding2017/array/ArrayUtil.java b/group01/280646174/basic/src/main/java/com/coding2017/array/ArrayUtil.java new file mode 100644 index 0000000000..aca4036e16 --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/array/ArrayUtil.java @@ -0,0 +1,275 @@ +package com.coding2017.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) { + if (nullOrEmpty(origin) || origin.length == 1) { + return; + } + + int length = origin.length; + for (int i = 0; i < length / 2; i++) { + swap(origin, i, length - 1 - i); + } + } + + private void swap(int[] array, int index1, int index2) { + int temp = array[index1]; + array[index1] = array[index2]; + array[index2] = 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) { + if (nullOrEmpty(oldArray)) { + return new int[0]; + } + + int[] tempArray = new int[oldArray.length]; + int newLength = 0; + for (int e : oldArray) { + if (e != 0) { + tempArray[newLength++] = e; + } + } + int[] newArray = new int[newLength]; + System.arraycopy(tempArray, 0, newArray, 0, newLength); + 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) { + if (nullOrEmpty(array1)) { + return array2; + } + if (nullOrEmpty(array2)) { + return array1; + } + + int index1 = 0; + int index2 = 0; + int length1 = array1.length; + int length2 = array2.length; + int[] tempArray = new int[length1 + length2]; + int pos = 0; + while (index1 < length1 && index2 < length2) { + if (array1[index1] == array2[index2]) { + // 元素相同情况下, 忽略其中一个 + index1++; + } else if (array1[index1] < array2[index2]) { + tempArray[pos++] = array1[index1++]; + } else { + tempArray[pos++] = array2[index2++]; + } + } + + while (index1 < length1) { + tempArray[pos++] = array1[index1++]; + } + while (index2 < length2) { + tempArray[pos++] = array2[index2++]; + } + + if (pos == tempArray.length) { + return tempArray; + } + int[] result = new int[pos]; + System.arraycopy(tempArray, 0, result, 0, pos); + return result; + } + + private boolean nullOrEmpty(int[] array) { + return array == null || array.length == 0; + } + + /** + * 把一个已经存满数据的数组 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) { + assert oldArray != null; + assert size >= 0; + + int[] result = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, result, 0, oldArray.length); + return result; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + if (max <= 1) { + return new int[0]; + } + int f1 = 1; + int f2 = 1; + // 超过max的那个pos, 也就是最终长度 + int maxPos = 2; + while (true) { + int curData = f1 + f2; + if (curData > max) { + break; + } + f1 = f2; + f2 = curData; + maxPos++; + } + + int[] result = new int[maxPos]; + f1 = 1; + f2 = 1; + for (int i = 0; i < maxPos; i++) { + if (i == 0 || i == 1) { + result[i] = 1; + } else { + result[i] = f1 + f2; + f1 = f2; + f2 = result[i]; + } + } + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + if (max <= 2) { + return new int[0]; + } + + boolean[] data = new boolean[max]; + + // 1. 把2放置为true + data[2] = true; + + // 2. 把奇数首先初始化为true + for (int i = 3; i < max; i += 2) { + data[i] = true; + } + + // 3. 从3开始到max/2检查素数, 然后把这个数的倍数全都置为false + for (int i = 3; i < max / 2; i += 2) { + data[i] = isPrime(i); + for (int j = 3; i * j < max; j += 2) { + data[i * j] = false; + } + } + + // 4. 查一共多少个 + int primeCount = 0; + for (int i = 0; i < max; i++) { + if (data[i]) { + primeCount++; + } + } + + // 5. 构造结果 + int[] result = new int[primeCount]; + int pos = 0; + for (int i = 0; i < max; i++) { + if (data[i]) { + result[pos++] = i; + } + } + return result; + } + + private boolean isPrime(int n) { + for (int i = 2; i <= n / 2; i++) { + if (n % i == 0) { + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + boolean[] data = new boolean[max]; + int count = 0; + for (int i = 0; i < max; i++) { + if (isPerfectNumber(i)) { + data[i] = true; + count++; + } + } + + int[] result = new int[count]; + int pos = 0; + for (int i = 0; i < max; i++) { + if (data[i]) { + result[pos++] = i; + } + } + return result; + } + + private boolean isPerfectNumber(int n) { + if (n <= 0) { + return false; + } + + int temp = 0; + for (int i = 1; i <= n / 2; i++) { + if (n % i == 0) { + temp += i; + } + } + + return temp == n; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + if (nullOrEmpty(array)) { + return ""; + } + StringBuilder builder = new StringBuilder(String.valueOf(array[0])); + for (int i = 1; i < array.length; i++) { + builder.append(seperator).append(array[i]); + } + return builder.toString(); + } +} diff --git a/group01/280646174/basic/src/main/java/com/coding2017/litestruts/LoginAction.java b/group01/280646174/basic/src/main/java/com/coding2017/litestruts/LoginAction.java new file mode 100644 index 0000000000..e03337fbd3 --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +package com.coding2017.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/group01/280646174/basic/src/main/java/com/coding2017/litestruts/Struts.java b/group01/280646174/basic/src/main/java/com/coding2017/litestruts/Struts.java new file mode 100644 index 0000000000..eba40d8412 --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/litestruts/Struts.java @@ -0,0 +1,127 @@ +package com.coding2017.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import com.google.common.base.Strings; + +public class Struts { + + private static final String STRUTS_FILE_PATH = "/struts.xml"; + + private static final String ACTION_EXECUTE_METHOD = "execute"; + + /** + * 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字段中。 + * + * @param actionName + * @param parameters + * @return + */ + public static View runAction(String actionName, Map parameters) { + + if (Strings.isNullOrEmpty(actionName)) { + return null; + } + + StrutsDefinition strutsDefinition = StrutsXmlUtil + .parseResource(Struts.class.getResourceAsStream(STRUTS_FILE_PATH)); + if (strutsDefinition == null) { + return null; + } + StrutsDefinition.ActionDefinition actionDefinition = findActionDefinition(strutsDefinition, actionName); + if (actionDefinition == null) { + return null; + } + try { + Class actionClass = Class.forName(actionDefinition.getClazz()); + Object action = actionClass.newInstance(); + setParameter(actionClass, action, parameters); + + Method executeMethod = actionClass.getMethod(ACTION_EXECUTE_METHOD); + String actionResult = (String) executeMethod.invoke(action); + + StrutsDefinition.ResultDefinition resultDefinition = findResultDefinition(actionDefinition, actionResult); + if (resultDefinition == null) { + return null; + } + + View view = new View(); + view.setJsp(resultDefinition.getValue()); + view.setParameters(makeParameters(action, actionClass)); + return view; + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } + } + + private static Map makeParameters(Object action, Class actionClass) + throws InvocationTargetException, IllegalAccessException { + Method[] methods = actionClass.getMethods(); + Map map = new HashMap(); + for (Method method : methods) { + if (method.getName().startsWith("get")) { + map.put(getField(method.getName()), method.invoke(action)); + } + } + return map; + } + + private static String getField(String getMethodName) { + String field = getMethodName.substring(3); + return field.substring(0, 1).toLowerCase() + field.substring(1); + } + + private static StrutsDefinition.ResultDefinition findResultDefinition( + StrutsDefinition.ActionDefinition actionDefinition, String actionResult) { + for (StrutsDefinition.ResultDefinition resultDefinition : actionDefinition.getResultDefinitions()) { + if (resultDefinition.getName().equals(actionResult)) { + return resultDefinition; + } + } + return null; + } + + private static void setParameter(Class actionClass, Object action, Map parameters) + throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { + for (Map.Entry paramEntry : parameters.entrySet()) { + String setMethodName = getMethodName(paramEntry.getKey()); + Method method = actionClass.getMethod(setMethodName, String.class); + method.invoke(action, paramEntry.getValue()); + } + } + + private static String getMethodName(String field) { + return "set" + field.substring(0, 1).toUpperCase() + field.substring(1); + } + + private static StrutsDefinition.ActionDefinition findActionDefinition(StrutsDefinition strutsDefinition, + String actionName) { + for (StrutsDefinition.ActionDefinition definition : strutsDefinition.getActionDefinitionList()) { + if (actionName.equals(definition.getName())) { + return definition; + } + } + return null; + } + +} diff --git a/group01/280646174/basic/src/main/java/com/coding2017/litestruts/StrutsDefinition.java b/group01/280646174/basic/src/main/java/com/coding2017/litestruts/StrutsDefinition.java new file mode 100644 index 0000000000..a54c3c9eb7 --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/litestruts/StrutsDefinition.java @@ -0,0 +1,69 @@ +package com.coding2017.litestruts; + +import java.util.List; + +/** + * Created by kaitao.li on 2017/3/4. + */ +public class StrutsDefinition { + private List actionDefinitionList; + + public List getActionDefinitionList() { + return actionDefinitionList; + } + + public void setActionDefinitionList(List actionDefinitionList) { + this.actionDefinitionList = actionDefinitionList; + } + + public static class ActionDefinition { + private String name; + private String clazz; + private List resultDefinitions; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClazz() { + return clazz; + } + + public void setClazz(String clazz) { + this.clazz = clazz; + } + + public List getResultDefinitions() { + return resultDefinitions; + } + + public void setResultDefinitions(List resultDefinitions) { + this.resultDefinitions = resultDefinitions; + } + } + + public static class ResultDefinition { + private String name; + private String value; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + } +} \ No newline at end of file diff --git a/group01/280646174/basic/src/main/java/com/coding2017/litestruts/StrutsXmlUtil.java b/group01/280646174/basic/src/main/java/com/coding2017/litestruts/StrutsXmlUtil.java new file mode 100644 index 0000000000..b6f15ddb90 --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/litestruts/StrutsXmlUtil.java @@ -0,0 +1,58 @@ +package com.coding2017.litestruts; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +/** + * Created by kaitao.li on 2017/3/4. + */ +public class StrutsXmlUtil { + + public static StrutsDefinition parseResource(InputStream inputStream) { + + SAXReader saxReader = new SAXReader(); + Document document; + try { + document = saxReader.read(inputStream); + } catch (DocumentException e) { + throw new RuntimeException("解析xml出错"); + } + + // 获取根节点对象 + Element rootElement = document.getRootElement(); + StrutsDefinition strutsDefinition = new StrutsDefinition(); + Iterator actionIterator = rootElement.elements("action").iterator(); + List actionDefinitions = new ArrayList(); + strutsDefinition.setActionDefinitionList(actionDefinitions); + while (actionIterator.hasNext()) { + Element actionElement = actionIterator.next(); + StrutsDefinition.ActionDefinition actionDefinition = new StrutsDefinition.ActionDefinition(); + actionDefinition.setName(actionElement.attributeValue("name")); + actionDefinition.setClazz(actionElement.attributeValue("class")); + actionDefinitions.add(actionDefinition); + + Iterator resultIterator = actionElement.elements("result").iterator(); + List resultDefinitions = new ArrayList(); + actionDefinition.setResultDefinitions(resultDefinitions); + while (resultIterator.hasNext()) { + Element resultElement = resultIterator.next(); + StrutsDefinition.ResultDefinition resultDefinition = new StrutsDefinition.ResultDefinition(); + resultDefinition.setName(resultElement.attributeValue("name")); + resultDefinition.setValue(resultElement.getTextTrim()); + resultDefinitions.add(resultDefinition); + } + } + return strutsDefinition; + } + + public static void main(String[] args) { + StrutsXmlUtil.parseResource(StrutsXmlUtil.class.getResourceAsStream("/struts.xml")); + } +} \ No newline at end of file diff --git a/group01/280646174/basic/src/main/java/com/coding2017/litestruts/View.java b/group01/280646174/basic/src/main/java/com/coding2017/litestruts/View.java new file mode 100644 index 0000000000..af3374ce24 --- /dev/null +++ b/group01/280646174/basic/src/main/java/com/coding2017/litestruts/View.java @@ -0,0 +1,26 @@ +package com.coding2017.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/group01/280646174/basic/src/main/resources/struts.xml b/group01/280646174/basic/src/main/resources/struts.xml new file mode 100644 index 0000000000..01fc673ed6 --- /dev/null +++ b/group01/280646174/basic/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group01/280646174/basic/src/test/java/com/coding2017/array/ArrayUtilTest.java b/group01/280646174/basic/src/test/java/com/coding2017/array/ArrayUtilTest.java new file mode 100644 index 0000000000..23f650dd57 --- /dev/null +++ b/group01/280646174/basic/src/test/java/com/coding2017/array/ArrayUtilTest.java @@ -0,0 +1,72 @@ +package com.coding2017.array; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * Created by kaitao.li on 2017/3/4. + */ +public class ArrayUtilTest { + + private ArrayUtil arrayUtil = new ArrayUtil(); + + @Test + public void reverseArray() throws Exception { + int[] oddArray = new int[] { 1, 2, 3 }; + arrayUtil.reverseArray(oddArray); + assertArrayEquals(oddArray, new int[] { 3, 2, 1 }); + + int[] evenArray = new int[] { 1, 2, 3, 4 }; + arrayUtil.reverseArray(evenArray); + assertArrayEquals(evenArray, new int[] { 4, 3, 2, 1 }); + } + + @Test + public void removeZero() throws Exception { + int oldArr[] = new int[] { 1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 }; + int[] newArray = arrayUtil.removeZero(oldArr); + assertArrayEquals(newArray, new int[] { 1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5 }); + } + + @Test + public void merge() throws Exception { + int[] a1 = new int[] { 3, 5, 7, 8 }; + int[] a2 = new int[] { 4, 5, 6, 7 }; + int[] merge = arrayUtil.merge(a1, a2); + assertArrayEquals(merge, new int[] { 3, 4, 5, 6, 7, 8 }); + } + + @Test + public void grow() throws Exception { + int[] oldArray = new int[] { 2, 3, 6 }; + int[] grow = arrayUtil.grow(oldArray, 3); + assertArrayEquals(grow, new int[] { 2, 3, 6, 0, 0, 0 }); + } + + @Test + public void fibonacci() throws Exception { + int[] fibonacci = arrayUtil.fibonacci(15); + assertArrayEquals(fibonacci, new int[] { 1, 1, 2, 3, 5, 8, 13 }); + } + + @Test + public void getPrimes() throws Exception { + int[] primes = arrayUtil.getPrimes(23); + assertArrayEquals(primes, new int[] { 2, 3, 5, 7, 11, 13, 17, 19 }); + } + + @Test + public void getPerfectNumbers() throws Exception { + int[] perfectNumbers = arrayUtil.getPerfectNumbers(1000); + assertArrayEquals(perfectNumbers, new int[] { 6, 28, 496 }); + } + + @Test + public void join() throws Exception { + int[] array = new int[] { 1, 2, 3 }; + assertTrue("1-2-3".equals(arrayUtil.join(array, "-"))); + } + +} \ No newline at end of file diff --git a/group01/280646174/basic/src/test/java/com/coding2017/litestruts/StrutsTest.java b/group01/280646174/basic/src/test/java/com/coding2017/litestruts/StrutsTest.java new file mode 100644 index 0000000000..3d877a0eab --- /dev/null +++ b/group01/280646174/basic/src/test/java/com/coding2017/litestruts/StrutsTest.java @@ -0,0 +1,38 @@ +package com.coding2017.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/group01/280646174/pom.xml b/group01/280646174/pom.xml index c99644072b..75422ec638 100644 --- a/group01/280646174/pom.xml +++ b/group01/280646174/pom.xml @@ -19,6 +19,17 @@ + + org.jvnet.hudson.dom4j + dom4j + 1.6.1-hudson-3 + + + + com.google.guava + guava + 20.0 + junit diff --git a/group01/360176196/.classpath b/group01/360176196/.classpath index fb5011632c..3c662f3c9b 100644 --- a/group01/360176196/.classpath +++ b/group01/360176196/.classpath @@ -2,5 +2,8 @@ + + + diff --git a/group01/360176196/.settings/org.eclipse.core.resources.prefs b/group01/360176196/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..99f26c0203 --- /dev/null +++ b/group01/360176196/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/group01/360176196/src/xqfGit/dataStructure/conderising/ArrayUtil.java b/group01/360176196/src/xqfGit/dataStructure/conderising/ArrayUtil.java new file mode 100644 index 0000000000..93762e4818 --- /dev/null +++ b/group01/360176196/src/xqfGit/dataStructure/conderising/ArrayUtil.java @@ -0,0 +1,179 @@ +package xqfGit.dataStructure.conderising; + +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 [] temp = new int[origin.length]; + for(int i =0;i array2[i]){ + temp[index] = array2[i]; + index++; + } if(array1[j] == array2[i]){ + temp[index] = array2[i]; + index++; + continue; + } if(array1[j] < array2[i]){ + temp[index] = array1[j]; + index++; + continue; + } + } + } + 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){ + return Arrays.copyOf(oldArray, oldArray.length+size); + } + + /** + * 斐波那契数列为: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 n=0; + int[] temp ={}; + if(max == 1){ + return temp; + }else{ + for(int i =0;i<30;i++){ + if(fibo(i) > max){ + n = i; + break; + } + } + for(int i = 0;i < n;i++){ + temp = Arrays.copyOf(temp, i+1); + temp[temp.length-1] = fibo(i); + } + return temp; + } + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + int[] temp ={}; + for(int i =2;i 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字段中。 + + */ + + SAXReader reader = new SAXReader(); + try { + //解析Struts.xml + Document document = reader.read(new File("struts.xml")); + Element root = document.getRootElement(); + List list_child = root.elements(); + String actionname = list_child.get(0).attributeValue("name"); + String actionClass = list_child.get(0).attributeValue("class"); + + //通过反射,创建Action,调用方法。 + try { + Class clazz = Class.forName(actionClass); + Method setName = clazz.getMethod("setName"); + } catch (Exception e) { + e.printStackTrace(); + } + } catch (DocumentException e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/group01/360176196/src/xqfGit/dataStructure/litileStruts/StrutsTest.java b/group01/360176196/src/xqfGit/dataStructure/litileStruts/StrutsTest.java new file mode 100644 index 0000000000..8c9b6cd92e --- /dev/null +++ b/group01/360176196/src/xqfGit/dataStructure/litileStruts/StrutsTest.java @@ -0,0 +1,43 @@ +package xqfGit.dataStructure.litileStruts; + +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/group01/360176196/src/xqfGit/dataStructure/litileStruts/View.java b/group01/360176196/src/xqfGit/dataStructure/litileStruts/View.java new file mode 100644 index 0000000000..2ec6974941 --- /dev/null +++ b/group01/360176196/src/xqfGit/dataStructure/litileStruts/View.java @@ -0,0 +1,23 @@ +package xqfGit.dataStructure.litileStruts; + +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/group01/360176196/src/xqfGit/dataStructure/litileStruts/struts.xml b/group01/360176196/src/xqfGit/dataStructure/litileStruts/struts.xml new file mode 100644 index 0000000000..865d390eb6 --- /dev/null +++ b/group01/360176196/src/xqfGit/dataStructure/litileStruts/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/group01/378213871/.classpath b/group01/378213871/.classpath index fb5011632c..d815a5f517 100644 --- a/group01/378213871/.classpath +++ b/group01/378213871/.classpath @@ -2,5 +2,7 @@ + + diff --git a/group01/378213871/src/com/coderising/week02/array/ArrayUtil.java b/group01/378213871/src/com/coderising/week02/array/ArrayUtil.java new file mode 100644 index 0000000000..476e0ff7a8 --- /dev/null +++ b/group01/378213871/src/com/coderising/week02/array/ArrayUtil.java @@ -0,0 +1,255 @@ +package com.coderising.week02.array; + +import java.util.Stack; + +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){ + Stack stack = new Stack(); + for(int i = 0; i < origin.length; i++ ) { + stack.push(origin[i]); + } + for(int i = 0; i < origin.length; i++ ) { + int j = (int) stack.pop(); + origin[i] = j; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray){ + if (oldArray == null) { + throw new IllegalArgumentException(); + } + //算出oldArr数组中0的个数 + int zeronum = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) { + zeronum++; + } + } + int j = 0; //新数组的索引 + int[] newArray = new int[oldArray.length - zeronum]; + //遍历旧数组 + for (int i = 0; i < oldArray.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 array1length = array1.length; + int array2length = array2.length; + int repeat = 0; //初始化两个数组中重复数字的个数为0 + for (int i = 0; i < array1length; i++) { + for (int j = 0; j < array2length; j++) { + if (array1[i] == array2[j]) { + repeat++; + } + } + } + //合并后的数组长度为两个数组长度相加减去重复数字的个数 + int[] mergearray = new int[array1length+array2length-repeat]; + int index1 = 0; //array1数组的当前索引 + int index2 = 0; //array2数组的当前索引 + int indexMerge = 0; //mergearray数组的当前索引 + //循环,只要array1和array2都没有循环完就一直循环 + while (index1 < array1length && index2 < array2length) { + //如果当前array1[index1]比array2[index2]小,则将mergearray[indexMerge]元素置为array1[index1] + //同时index1++,indexMerge++ + if (array1[index1] < array2[index2]) { + mergearray[indexMerge++] = array1[index1++]; + } + //如果当前array1[index1]比array2[index2]大,则将mergearray[indexMerge]元素置为array2[index2] + //同时index2++,indexMerge++ + if (array1[index1] > array2[index2]) { + mergearray[indexMerge++] = array2[index2++]; + } + //如果当前array1[index1]等于array2[index2],则将mergearray[indexMerge]元素置为array1[index1] + //同时index1++,index2++,indexMerge++ + else { + mergearray[indexMerge] = array1[index1]; + index1++; + index2++; + indexMerge++; + } + } + //上个循环能够结束,说明array1已经循环完或array2已经循环完 + //下述两个循环只能有一个满足循环条件 + //只要array1没有循环完,就把array1中剩下的元素依次放入mergearray中 + while (index1 < array1length) { + mergearray[indexMerge++] = array1[index1++]; + } + //只要array2没有循环完,就把array2中剩下的元素依次放入mergearray中 + while (index2 < array2length) { + mergearray[indexMerge++] = array2[index2++]; + } + + return mergearray; + } + /** + * 把一个已经存满数据的数组 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 oldLength = oldArray.length; + int[] newArray = new int[oldLength+size]; + System.arraycopy(oldArray, 0, newArray, 0, oldLength); + 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 static int[] fibonacci(int max){ + int count = 0; //数列项数 + //循环得到数组的长度 + while (fiboindex(count) < max) { + count++; + } + int[] fiboArray = new int[count]; + if(max <= 1) { + fiboArray = new int[] {}; + } else { + for (int i = 0; i < count; i++) { + fiboArray[i] = fiboindex(i); + } + } + return fiboArray; + } + //通过递归的方式推导斐波那契数列 + public static int fiboindex(int n) { + if (n < 2) { + return 1; + } + else { + return fiboindex(n-1) + fiboindex(n-2); + } + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if (max < 2) { + throw new IllegalArgumentException(); + } + int i,j; + int count = 0; + int[] maxArray = new int[max]; + for(i = 2; i < max; i++) { + for (j = 2; j <= i; j++) { + if (i % j == 0) { + break; + } + } + if(j >= i) { + maxArray[count]=i; + count++; + } + } + int[] primeArray = new int[count]; + System.arraycopy(maxArray, 0, primeArray, 0, count); + return primeArray; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if (max < 0) { + throw new IllegalArgumentException(); + } + + int i,j; + int count = 0; + int[] maxArray = new int[max]; + + + for(i = 1; i < max; i++) { + int sum = 0; + for(j = 1; j < i; j++) { + if (i % j == 0) { + sum += j; + } + } + if (sum == i) { + maxArray[count] = i; + count++; + } + } + + int[] perfectArray = new int[count]; + System.arraycopy(maxArray, 0, perfectArray, 0, count); + + return perfectArray; + } + + /** + * 用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) { + throw new IllegalArgumentException(); + } + if (array.length == 0) { + return ""; + } + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + sb.append(array[i]); + if(i != array.length - 1) { + sb.append(seperator); + } + } + + return sb.toString(); + } + + +} diff --git a/group01/378213871/src/com/coderising/week02/array/ArrayUtilTest.java b/group01/378213871/src/com/coderising/week02/array/ArrayUtilTest.java new file mode 100644 index 0000000000..8d82ff3095 --- /dev/null +++ b/group01/378213871/src/com/coderising/week02/array/ArrayUtilTest.java @@ -0,0 +1,90 @@ +package com.coderising.week02.array; + + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ArrayUtilTest { + + private ArrayUtil arrayUtil; + + @Before + public void setUp() { + arrayUtil = new ArrayUtil(); + } + + @Test + public void testReverseArray() { + int[] a = {}; + arrayUtil.reverseArray(a); + Assert.assertArrayEquals(new int[] {}, a); + + int[] b = {1, 2, 3}; + arrayUtil.reverseArray(b); + Assert.assertArrayEquals(new int[] {3, 2, 1}, b); + + } + + @Test + public void testRemoveZero() { + int oldArr1[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + int newArr1[] = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}; + Assert.assertArrayEquals(newArr1, arrayUtil.removeZero(oldArr1)); + + int oldArr2[] = {0, 0, 0, 0, 0}; + int newArr2[] = {}; + Assert.assertArrayEquals(newArr2, arrayUtil.removeZero(oldArr2)); + + int oldArr3[] = {0, 0, 0, 0, 0, 1, 2, 3}; + int newArr3[] = {1, 2, 3}; + Assert.assertArrayEquals(newArr3, arrayUtil.removeZero(oldArr3)); + + } + + @Test + public void testMerge() { + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + Assert.assertArrayEquals(new int[] {3, 4, 5, 6, 7, 8}, arrayUtil.merge(a1, a2)); + } + + @Test + public void testGrow() { + int[] a1 = {2, 3, 6}; + Assert.assertArrayEquals(new int[] {2, 3, 6, 0, 0, 0}, arrayUtil.grow(a1, 3)); + Assert.assertArrayEquals(new int[] {2, 3, 6}, arrayUtil.grow(a1, 0)); + } + + @Test + public void testFibonacci() { + Assert.assertArrayEquals(new int[] {}, arrayUtil.fibonacci(1)); + Assert.assertArrayEquals(new int[] {1, 1, 2, 3, 5, 8, 13}, arrayUtil.fibonacci(15)); + + int[] a = new int[] {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, + 2584, 4181, 6765}; + Assert.assertArrayEquals(a, arrayUtil.fibonacci(6766)); + + } + + @Test + public void testGetPrimes() { + Assert.assertArrayEquals(new int[] {2, 3, 5, 7, 11, 13, 17, 19}, arrayUtil.getPrimes(23)); + } + + @Test + public void testGetPerfectNumbers() { + Assert.assertArrayEquals(new int[] {6, 28, 496}, arrayUtil.getPerfectNumbers(497)); + Assert.assertArrayEquals(new int[] {6, 28, 496, 8128}, arrayUtil.getPerfectNumbers(8129)); + + } + + @Test + public void testJoin() { + Assert.assertEquals("", arrayUtil.join(new int[] {}, "-")); + Assert.assertEquals("1", arrayUtil.join(new int[] {1}, "-")); + Assert.assertEquals("1-2-8-3-4-5", arrayUtil.join(new int[] {1, 2, 8, 3, 4, 5}, "-")); + + } + +} diff --git a/group01/378213871/src/com/coderising/week02/litestruts/LoginAction.java b/group01/378213871/src/com/coderising/week02/litestruts/LoginAction.java new file mode 100644 index 0000000000..01e4011dfd --- /dev/null +++ b/group01/378213871/src/com/coderising/week02/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.week02.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/group01/378213871/src/com/coderising/week02/litestruts/Struts.java b/group01/378213871/src/com/coderising/week02/litestruts/Struts.java new file mode 100644 index 0000000000..103cb3c7ed --- /dev/null +++ b/group01/378213871/src/com/coderising/week02/litestruts/Struts.java @@ -0,0 +1,110 @@ +package com.coderising.week02.litestruts; + +import java.io.InputStream; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +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字段中。 + + */ + try { + //将xml转换为输出流 + InputStream inputStream = Struts.class.getResourceAsStream("struts.xml"); + //创建SAXReader读取器,专门用于读取xml + SAXReader saxReader = new SAXReader(); + //读取xml文件,获得Document对象 + Document document = saxReader.read(inputStream); + //actionName对应的类名 + String className = ""; + //获取文档的根节点 + Element rootElement = document.getRootElement(); + Iterator iterator = rootElement.elementIterator("action"); + //actionName对应的action + Element targetAction = null; + //对action节点下的所有子节点进行遍历 + while (iterator.hasNext()) { + Element element = (Element) iterator.next(); + String name = element.attributeValue("name"); + if(name.equals(actionName)) { + className = element.attributeValue("class"); + targetAction = element; + break; + } + } + + Class clazz = Class.forName(className); + Object instance = clazz.newInstance(); + + Set keySet = parameters.keySet(); + for(String key : keySet) { + // 将变量名称拼成set方法名 + String methodName = "set" + key.substring(0, 1).toUpperCase() + key.substring(1); + Class type = clazz.getDeclaredField(key).getType(); + Method method = clazz.getDeclaredMethod(methodName, type); + //依次调用相应的set方法 + method.invoke(instance, parameters.get(key)); + } + //通过反射调用对象的exectue方法,并获得返回值 + String result = (String)clazz.getDeclaredMethod("execute").invoke(instance); + + Method[] declaredMethods = clazz.getDeclaredMethods(); + HashMap map = new HashMap<>(); + for (int i = 0; i < declaredMethods.length; i++) { + if (declaredMethods[i].getName().startsWith("get")) { + String fieldValue = (String) declaredMethods[i].invoke(instance); + String fieldName = declaredMethods[i].getName().substring(3, 4).toLowerCase() + + declaredMethods[i].getName().substring(4); + map.put(fieldName, fieldValue); + } + } + + View view = new View(); + view.setParameters(map); + + Iterator elementIterator = targetAction.elementIterator("result"); + while(elementIterator.hasNext()) { + Element element = (Element) elementIterator.next(); + if (result.equals(element.attributeValue("name"))) { + view.setJsp(element.getText()); + } + } + + return view; + + + } catch(Exception e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/group01/378213871/src/com/coderising/week02/litestruts/StrutsTest.java b/group01/378213871/src/com/coderising/week02/litestruts/StrutsTest.java new file mode 100644 index 0000000000..ee10f9d60f --- /dev/null +++ b/group01/378213871/src/com/coderising/week02/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.week02.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/group01/378213871/src/com/coderising/week02/litestruts/View.java b/group01/378213871/src/com/coderising/week02/litestruts/View.java new file mode 100644 index 0000000000..3e85e82938 --- /dev/null +++ b/group01/378213871/src/com/coderising/week02/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.week02.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/group01/378213871/src/com/coderising/week02/litestruts/struts.xml b/group01/378213871/src/com/coderising/week02/litestruts/struts.xml new file mode 100644 index 0000000000..3254cb8ba6 --- /dev/null +++ b/group01/378213871/src/com/coderising/week02/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/group01/496740686/build.gradle b/group01/496740686/build.gradle new file mode 100644 index 0000000000..864adec57e --- /dev/null +++ b/group01/496740686/build.gradle @@ -0,0 +1,23 @@ + +apply plugin: 'java' +apply plugin: 'maven' + +group = 'com' +version = '0.0.1-SNAPSHOT' + +description = """camile""" + +sourceCompatibility = 1.7 +targetCompatibility = 1.7 + + + +repositories { + + maven { url "http://maven.aliyun.com/nexus/content/groups/public" } +} +dependencies { + // https://mvnrepository.com/artifact/commons-digester/commons-digester + compile group: 'commons-digester', name: 'commons-digester', version: '2.1' + testCompile group: 'junit', name: 'junit', version:'4.12' +} diff --git a/group01/496740686/gradle/wrapper/gradle-wrapper.properties b/group01/496740686/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..f9c28ae23b --- /dev/null +++ b/group01/496740686/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sun Mar 05 11:04:29 CST 2017 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-all.zip diff --git a/group01/496740686/gradlew b/group01/496740686/gradlew new file mode 100644 index 0000000000..9d82f78915 --- /dev/null +++ b/group01/496740686/gradlew @@ -0,0 +1,160 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/group01/496740686/gradlew.bat b/group01/496740686/gradlew.bat new file mode 100644 index 0000000000..72d362dafd --- /dev/null +++ b/group01/496740686/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/group01/496740686/settings.gradle b/group01/496740686/settings.gradle new file mode 100644 index 0000000000..e034102924 --- /dev/null +++ b/group01/496740686/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'camile' diff --git a/group01/496740686/src/main/java/com/camile/App.java b/group01/496740686/src/main/java/com/camile/App.java new file mode 100644 index 0000000000..862f102bc3 --- /dev/null +++ b/group01/496740686/src/main/java/com/camile/App.java @@ -0,0 +1,13 @@ +package com.camile; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/group01/496740686/src/Impl/MyArraryList.java b/group01/496740686/src/main/java/com/camile/_1/Impl/MyArraryList.java similarity index 93% rename from group01/496740686/src/Impl/MyArraryList.java rename to group01/496740686/src/main/java/com/camile/_1/Impl/MyArraryList.java index 20fb5dcfdf..8185620b6e 100644 --- a/group01/496740686/src/Impl/MyArraryList.java +++ b/group01/496740686/src/main/java/com/camile/_1/Impl/MyArraryList.java @@ -1,8 +1,8 @@ -package Impl; +package com.camile._1.Impl; -import Interface.ArrayList; -import Interface.Iterator; -import ex.MyArrest; +import com.camile._1.Interface.ArrayList; +import com.camile._1.Interface.Iterator; +import com.camile._1.ex.MyArrest; /** * Created by Administrator on 2017/2/25. @@ -89,7 +89,7 @@ public ArrayListIterator(MyArraryList arraryList) { this.index = arraryList.size - 1; } - @Override + public boolean hasNext() { if (index > arraryList.size) { return true; @@ -98,7 +98,7 @@ public boolean hasNext() { } } - @Override + public Object next() { Object obj = arraryList.get(index); index++; diff --git a/group01/496740686/src/Impl/MyLinkedList.java b/group01/496740686/src/main/java/com/camile/_1/Impl/MyLinkedList.java similarity index 92% rename from group01/496740686/src/Impl/MyLinkedList.java rename to group01/496740686/src/main/java/com/camile/_1/Impl/MyLinkedList.java index 017bac5baf..367b722025 100644 --- a/group01/496740686/src/Impl/MyLinkedList.java +++ b/group01/496740686/src/main/java/com/camile/_1/Impl/MyLinkedList.java @@ -1,8 +1,8 @@ -package Impl; +package com.camile._1.Impl; -import Interface.Iterator; -import Interface.LinkedList; -import ex.MyArrest; +import com.camile._1.Interface.Iterator; +import com.camile._1.Interface.LinkedList; +import com.camile._1.ex.MyArrest; /** * Created by Administrator on 2017/2/26. diff --git a/group01/496740686/src/Impl/MyQueue.java b/group01/496740686/src/main/java/com/camile/_1/Impl/MyQueue.java similarity index 90% rename from group01/496740686/src/Impl/MyQueue.java rename to group01/496740686/src/main/java/com/camile/_1/Impl/MyQueue.java index 1a029738d2..bcddc8680a 100644 --- a/group01/496740686/src/Impl/MyQueue.java +++ b/group01/496740686/src/main/java/com/camile/_1/Impl/MyQueue.java @@ -1,6 +1,6 @@ -package Impl; +package com.camile._1.Impl; -import Interface.Queue; +import com.camile._1.Interface.Queue; /** * Created by Administrator on 2017/2/26. diff --git a/group01/496740686/src/Impl/MyStack.java b/group01/496740686/src/main/java/com/camile/_1/Impl/MyStack.java similarity index 88% rename from group01/496740686/src/Impl/MyStack.java rename to group01/496740686/src/main/java/com/camile/_1/Impl/MyStack.java index 3a7c119e99..119798a218 100644 --- a/group01/496740686/src/Impl/MyStack.java +++ b/group01/496740686/src/main/java/com/camile/_1/Impl/MyStack.java @@ -1,7 +1,7 @@ -package Impl; +package com.camile._1.Impl; -import Interface.ArrayList; -import Interface.Stack; +import com.camile._1.Interface.ArrayList; +import com.camile._1.Interface.Stack; /** diff --git a/group01/496740686/src/Interface/ArrayList.java b/group01/496740686/src/main/java/com/camile/_1/Interface/ArrayList.java similarity index 92% rename from group01/496740686/src/Interface/ArrayList.java rename to group01/496740686/src/main/java/com/camile/_1/Interface/ArrayList.java index 567c1996b1..fb9bf7c2f9 100644 --- a/group01/496740686/src/Interface/ArrayList.java +++ b/group01/496740686/src/main/java/com/camile/_1/Interface/ArrayList.java @@ -1,4 +1,4 @@ -package Interface; +package com.camile._1.Interface; diff --git a/group01/496740686/src/Interface/BinaryTreeNode.java b/group01/496740686/src/main/java/com/camile/_1/Interface/BinaryTreeNode.java similarity index 94% rename from group01/496740686/src/Interface/BinaryTreeNode.java rename to group01/496740686/src/main/java/com/camile/_1/Interface/BinaryTreeNode.java index c5480a614f..a38f18788e 100644 --- a/group01/496740686/src/Interface/BinaryTreeNode.java +++ b/group01/496740686/src/main/java/com/camile/_1/Interface/BinaryTreeNode.java @@ -1,4 +1,4 @@ -package Interface; +package com.camile._1.Interface; public class BinaryTreeNode { diff --git a/group01/496740686/src/Interface/Iterator.java b/group01/496740686/src/main/java/com/camile/_1/Interface/Iterator.java similarity index 71% rename from group01/496740686/src/Interface/Iterator.java rename to group01/496740686/src/main/java/com/camile/_1/Interface/Iterator.java index 77e3c0b216..ed642f8295 100644 --- a/group01/496740686/src/Interface/Iterator.java +++ b/group01/496740686/src/main/java/com/camile/_1/Interface/Iterator.java @@ -1,4 +1,4 @@ -package Interface; +package com.camile._1.Interface; public interface Iterator { public boolean hasNext(); diff --git a/group01/496740686/src/Interface/LinkedList.java b/group01/496740686/src/main/java/com/camile/_1/Interface/LinkedList.java similarity index 94% rename from group01/496740686/src/Interface/LinkedList.java rename to group01/496740686/src/main/java/com/camile/_1/Interface/LinkedList.java index b7166a1731..25f042f135 100644 --- a/group01/496740686/src/Interface/LinkedList.java +++ b/group01/496740686/src/main/java/com/camile/_1/Interface/LinkedList.java @@ -1,4 +1,4 @@ -package Interface; +package com.camile._1.Interface; public class LinkedList implements List { diff --git a/group01/496740686/src/main/java/com/camile/_1/Interface/List.java b/group01/496740686/src/main/java/com/camile/_1/Interface/List.java new file mode 100644 index 0000000000..4d1febe487 --- /dev/null +++ b/group01/496740686/src/main/java/com/camile/_1/Interface/List.java @@ -0,0 +1,9 @@ +package com.camile._1.Interface; + +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/group01/496740686/src/Interface/Queue.java b/group01/496740686/src/main/java/com/camile/_1/Interface/Queue.java similarity index 85% rename from group01/496740686/src/Interface/Queue.java rename to group01/496740686/src/main/java/com/camile/_1/Interface/Queue.java index 8e3a5d5f43..4cc27c9f5f 100644 --- a/group01/496740686/src/Interface/Queue.java +++ b/group01/496740686/src/main/java/com/camile/_1/Interface/Queue.java @@ -1,4 +1,4 @@ -package Interface; +package com.camile._1.Interface; public class Queue { diff --git a/group01/496740686/src/Interface/Stack.java b/group01/496740686/src/main/java/com/camile/_1/Interface/Stack.java similarity index 89% rename from group01/496740686/src/Interface/Stack.java rename to group01/496740686/src/main/java/com/camile/_1/Interface/Stack.java index 7e536e250a..2c138b8493 100644 --- a/group01/496740686/src/Interface/Stack.java +++ b/group01/496740686/src/main/java/com/camile/_1/Interface/Stack.java @@ -1,4 +1,4 @@ -package Interface; +package com.camile._1.Interface; public class Stack { private ArrayList elementData = new ArrayList(); diff --git a/group01/496740686/src/ex/MyArrest.java b/group01/496740686/src/main/java/com/camile/_1/ex/MyArrest.java similarity index 95% rename from group01/496740686/src/ex/MyArrest.java rename to group01/496740686/src/main/java/com/camile/_1/ex/MyArrest.java index 9987b83535..12cc3df902 100644 --- a/group01/496740686/src/ex/MyArrest.java +++ b/group01/496740686/src/main/java/com/camile/_1/ex/MyArrest.java @@ -1,4 +1,4 @@ -package ex; +package com.camile._1.ex; /** * Created by Administrator on 2017/2/26. diff --git a/group01/496740686/src/main/java/com/camile/_1/package-info.java b/group01/496740686/src/main/java/com/camile/_1/package-info.java new file mode 100644 index 0000000000..c5edbab3ef --- /dev/null +++ b/group01/496740686/src/main/java/com/camile/_1/package-info.java @@ -0,0 +1,8 @@ +/** + * 基本的数据结构 + */ +/** + * @author Camile + * + */ +package com.camile._1; \ No newline at end of file diff --git a/group01/496740686/src/main/java/com/camile/_2/array/ArrayUtil.java b/group01/496740686/src/main/java/com/camile/_2/array/ArrayUtil.java new file mode 100644 index 0000000000..7cf22302bf --- /dev/null +++ b/group01/496740686/src/main/java/com/camile/_2/array/ArrayUtil.java @@ -0,0 +1,100 @@ +package com.camile._2.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 size = origin.length; + int[] newArr = new int[size]; + for (int index = 0; index + * 配置,以及execute的返回值, 确定哪一个jsp, 放到View对象的jsp字段中 + */ +public class Struts { + + public View runAction(String actionName, Map parameters) { + + Structs structs = makeObjFromXml(readProfiles("struts.xml")); + for (Action a : structs.getActions()) { + if (a.getName().equals(actionName)) { + try { + Class actionClass = Class.forName(a.getClazz()); + Object instance = actionClass.newInstance(); + Field field; + // get field + for (Map.Entry entry : parameters.entrySet()) { + field = actionClass.getDeclaredField(entry.getKey()); + field.setAccessible(true); + field.set(instance, entry.getValue()); + } + Method execute = actionClass.getMethod("execute"); + String resultString = (String) execute.invoke(instance); + Method getMessage = actionClass.getMethod("getMessage"); + String message = (String) getMessage.invoke(instance); + Map map = new HashMap<>(); + map.put("message", message); + View view = new View(); + String viewPath; + for (Result path : a.getResults()) { + if (path.getName().equals(resultString)) { + viewPath = path.getValue(); + view.setParameters(map); + view.setJsp(viewPath); + } + } + return view; + + } catch (NoSuchFieldException | SecurityException e) { + System.out.println("获取参数失败"); + e.printStackTrace(); + } catch (ClassNotFoundException e) { + System.out.println("并没有在xml中找到相关action"); + e.printStackTrace(); + } catch (InstantiationException | IllegalAccessException e) { + System.out.println("获取反射对象失败"); + e.printStackTrace(); + } catch (NoSuchMethodException e) { + System.out.println("获取方法"); + e.printStackTrace(); + } catch (IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + + return null; + } + + private URL readProfiles(String filePath) { + ClassLoader classLoader = Struts.class.getClassLoader(); + URL resource = classLoader.getResource(filePath); + if (resource == null) + throw new RuntimeException("文件不存在"); + return resource; + } + + private Structs makeObjFromXml(URL resource) { + Digester digester = new Digester(); + digester.addObjectCreate("struts", Structs.class); + digester.addObjectCreate("struts/action", Action.class); + digester.addSetProperties("struts/action", new String[] { "name", "class" }, new String[] { "name", "clazz" }); + digester.addSetNext("struts/action", "addAction"); + digester.addObjectCreate("struts/action/result", Result.class); + digester.addSetProperties("struts/action/result"); + digester.addBeanPropertySetter("struts/action/result", "value"); + digester.addSetNext("struts/action/result", "addResult"); + try { + return (Structs) digester.parse(resource); + } catch (IOException | SAXException e) { + e.printStackTrace(); + throw new RuntimeException("解析XML文件时发生错误"); + } + } + + public static void main(String[] args) { + + } +} diff --git a/group01/496740686/src/main/java/com/camile/_2/litestruts/View.java b/group01/496740686/src/main/java/com/camile/_2/litestruts/View.java new file mode 100644 index 0000000000..2a4bb815bd --- /dev/null +++ b/group01/496740686/src/main/java/com/camile/_2/litestruts/View.java @@ -0,0 +1,23 @@ +package com.camile._2.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/group01/496740686/src/main/java/com/camile/_2/litestruts/bean/Action.java b/group01/496740686/src/main/java/com/camile/_2/litestruts/bean/Action.java new file mode 100644 index 0000000000..2b3ff88897 --- /dev/null +++ b/group01/496740686/src/main/java/com/camile/_2/litestruts/bean/Action.java @@ -0,0 +1,34 @@ +package com.camile._2.litestruts.bean; + +import java.util.ArrayList; +import java.util.List; + +public class Action { + private String name; + private String clazz; + private List results = new ArrayList<>(); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClazz() { + return clazz; + } + + public void setClazz(String clazz) { + this.clazz = clazz; + } + + public void addResult(Result result) { + this.results.add(result); + } + + public List getResults() { + return results; + } +} diff --git a/group01/496740686/src/main/java/com/camile/_2/litestruts/bean/Result.java b/group01/496740686/src/main/java/com/camile/_2/litestruts/bean/Result.java new file mode 100644 index 0000000000..5850984c27 --- /dev/null +++ b/group01/496740686/src/main/java/com/camile/_2/litestruts/bean/Result.java @@ -0,0 +1,22 @@ +package com.camile._2.litestruts.bean; + +public class Result { + private String name; + private String value; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/group01/496740686/src/main/java/com/camile/_2/litestruts/bean/Structs.java b/group01/496740686/src/main/java/com/camile/_2/litestruts/bean/Structs.java new file mode 100644 index 0000000000..855de8a083 --- /dev/null +++ b/group01/496740686/src/main/java/com/camile/_2/litestruts/bean/Structs.java @@ -0,0 +1,16 @@ +package com.camile._2.litestruts.bean; + +import java.util.ArrayList; +import java.util.List; + +public class Structs { + private List actions = new ArrayList<>(); + + public void addAction(Action action) { + actions.add(action); + } + + public List getActions() { + return actions; + } +} diff --git a/group01/496740686/src/main/java/com/camile/_2/litestruts/bean/package-info.java b/group01/496740686/src/main/java/com/camile/_2/litestruts/bean/package-info.java new file mode 100644 index 0000000000..0763881974 --- /dev/null +++ b/group01/496740686/src/main/java/com/camile/_2/litestruts/bean/package-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * @author Administrator + * + */ +package com.camile._2.litestruts.bean; \ No newline at end of file diff --git a/group01/496740686/src/main/java/com/camile/_2/package-info.java b/group01/496740686/src/main/java/com/camile/_2/package-info.java new file mode 100644 index 0000000000..46f85287db --- /dev/null +++ b/group01/496740686/src/main/java/com/camile/_2/package-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * @author Administrator + * + */ +package com.camile._2; \ No newline at end of file diff --git a/group01/496740686/src/main/resources/struts.xml b/group01/496740686/src/main/resources/struts.xml new file mode 100644 index 0000000000..84ca94a962 --- /dev/null +++ b/group01/496740686/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group01/496740686/src/test/java/com/camile/AppTest.java b/group01/496740686/src/test/java/com/camile/AppTest.java new file mode 100644 index 0000000000..e277ecea87 --- /dev/null +++ b/group01/496740686/src/test/java/com/camile/AppTest.java @@ -0,0 +1,38 @@ +package com.camile; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} diff --git a/group01/496740686/src/test/java/com/camile/_2/StrutsTest.java b/group01/496740686/src/test/java/com/camile/_2/StrutsTest.java new file mode 100644 index 0000000000..5e7a12f2dc --- /dev/null +++ b/group01/496740686/src/test/java/com/camile/_2/StrutsTest.java @@ -0,0 +1,46 @@ +package com.camile._2; + +import java.util.HashMap; +import java.util.Map; + +import com.camile._2.litestruts.Struts; +import com.camile._2.litestruts.View; +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"); + + Struts struts = new Struts(); + 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"); //密码和预设的不一致 + + Struts struts = new Struts(); + 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/group01/496740686/src/Interface/List.java b/group01/553624797/code2017/src/com/xxt/DataStructure/List.java similarity index 85% rename from group01/496740686/src/Interface/List.java rename to group01/553624797/code2017/src/com/xxt/DataStructure/List.java index 98d6a4da0a..473354e821 100644 --- a/group01/496740686/src/Interface/List.java +++ b/group01/553624797/code2017/src/com/xxt/DataStructure/List.java @@ -1,4 +1,4 @@ -package Interface; +package com.xxt.DataStructure; public interface List { public void add(Object o); diff --git a/group01/553624797/code2017/src/com/xxt/DataStructure/MyArrayList.java b/group01/553624797/code2017/src/com/xxt/DataStructure/MyArrayList.java new file mode 100644 index 0000000000..9d3f3e8f03 --- /dev/null +++ b/group01/553624797/code2017/src/com/xxt/DataStructure/MyArrayList.java @@ -0,0 +1,99 @@ +package com.xxt.DataStructure; + +import java.util.ArrayList; +import java.util.Arrays; + +/** + * Created by star on 2017/2/26. + */ +public class MyArrayList implements List { + + + + private Object[] elementData; + private int size = elementData.length; + + + public MyArrayList(int initialCapacity) { + if (initialCapacity > 0) { + this.elementData = new Object[initialCapacity]; + } else { + throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); + } + } + + public MyArrayList() { + this(10); + } + + + @Override + public void add(Object o) { + ensureCapacity(size + 1); + elementData[size + 1] = o; + } + + @Override + public void add(int index, Object o) { + //判断数组下标是否越界 + if(index < 0 || index > elementData.length){ + throw new IndexOutOfBoundsException("index : "+index+"size : "+size); + } + ensureCapacity(index); + System.arraycopy(elementData, index,elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + @Override + public Object get(int index) { + return elementData[index]; + } + + @Override + public Object remove(int index) { + //判断数组下标是否越界 + if(index < 0 || index > elementData.length){ + throw new IndexOutOfBoundsException("index : "+index+"size : "+size); + } + + + //取出删除的值 + Object oldValue = elementData[index]; + + //做删除操作同样是复制数组 + //计算要删除的数量 + int numMoved = size - index - 1; + if ( numMoved > 0){ + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + } + + //将数组最后一个元素置为空,让GC回收 + elementData[size - 1] = null; + return oldValue; + + } + + @Override + public int size() { + return elementData.length; + } + + + //判断是否扩容 + public void ensureCapacity(int minCapacity) { + //取得当前数组容量 + int currentCapacity = elementData.length; + //如果最小需要的容量小于当前数组容量则需要扩容 + if (minCapacity < currentCapacity) { + int newCapacity = currentCapacity + (currentCapacity >> 1); + //如果扩容后的长度还是小于最小需要的长度,则直接以最小需要的长度作为当前数组的长度 + if (newCapacity < minCapacity) { + newCapacity = minCapacity; + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + } + +} diff --git a/group01/553624797/code2017/src/com/xxt/DataStructure/MyLinkedList.java b/group01/553624797/code2017/src/com/xxt/DataStructure/MyLinkedList.java new file mode 100644 index 0000000000..0c40956197 --- /dev/null +++ b/group01/553624797/code2017/src/com/xxt/DataStructure/MyLinkedList.java @@ -0,0 +1,141 @@ +package com.xxt.DataStructure; + +import java.util.Iterator; + +import java.util.LinkedList; +import java.util.function.Consumer; + +/** + * Created by star on 2017/2/25. + */ +public class MyLinkedList implements List{ + + public static class Node{ + E elementData; + Node prerious; + Node next; + + public Node(Node prerious , E elementData, Node next) { + this.prerious = prerious; + this.elementData= elementData; + this.next = next; + } + } + + + + private Node header; + private Node last; + + private int size = 0; + + + //往最后一个节点添加元素 + @Override + public void add(Object elementData) { + addBefore((E) elementData, last); + } + + @Override + public void add(int index, Object elementData) { + addBefore((E) elementData, (index == size ? last : node(index))); + } + + @Override + public Object get(int index) { + return node(index).elementData; + + } + + //获取index位置的节点 + private Node node(int index){ + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("数组下标越界"+size); + } + + if(index < (size >> 1)){ + Node e = header; + for(int i = 0; i < index; i ++){ + e = header.next; + return e; + } + }else { + Node e = last; + for(int i = size - 1; i > index; i--){ + e = last.prerious; + return e; + } + } + return null; + } + + + @Override + public Object remove(int index) { + + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("inde :" + index); + } + + return remove(node(index)); + } + + + + //返回被删除的节点 + private Object remove(Node e){ + E movedElementData = e.elementData; + + //被删除节点的上个节点指向该节点的下个节点 + e.prerious.next = e.next; + + //被删除节点的下个节点指向该节点的上个节点 + e.next.prerious = e.prerious; + + + //将该节点置为空,让GC能够回收 + e.next = e.prerious = null; + e.elementData = null; + //长度-1 + size--; + return movedElementData; + } + + + @Override + public int size() { + return size; + } + + public Object removeFirst(){ + return remove(header.next); + + + } + + public Object removeLast(){ + return remove(last.prerious); + } + + public Iterator iterator(){ + return null; + } + + + + //插入一个新的节点 + private Node addBefore(E e, Node node){ + + Node newNode = new Node(node.prerious, e, node.next); + + //将上个节点的next指向自己 + newNode.prerious.next = newNode; + + //将下个节点的previous指向自己 + newNode.next.prerious = newNode; + + size++; + return newNode; + } + +} diff --git a/group01/553624797/code2017/src/com/xxt/DataStructure/MyQueue.java b/group01/553624797/code2017/src/com/xxt/DataStructure/MyQueue.java new file mode 100644 index 0000000000..75d878af3b --- /dev/null +++ b/group01/553624797/code2017/src/com/xxt/DataStructure/MyQueue.java @@ -0,0 +1,88 @@ +package com.xxt.DataStructure; + +import java.util.Iterator; + +/** + * Created by star on 2017/2/26. + */ +public class MyQueue{ + + + public static class Node{ + Object elementData; + Node prerious; + Node next; + + public Node(Object elementData, Node prerious, Node next) { + this.elementData = elementData; + this.prerious = prerious; + this.next = next; + } + + } + + + private MyQueue.Node header; + private MyQueue.Node last; + + private int size = 0; + + + //入队操作.在链表的头节点插入元素 + public void enQueue(Object o){ + addBefore(o, header); + } + + + //出队操作,返回链表的尾节点的元素 + public Object deQueue(){ + return node(size); + } + + + public boolean isEmpty(){ + return header == last; + } + + + + public int size(){ + return size; + } + + private Node addBefore(Object o , Node node){ + Node newNode = new Node(o, node.prerious, node.next); + + newNode.prerious.next = newNode; + newNode.next.prerious = newNode; + + size++; + return newNode; + + } + + + private Node node(int index){ + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("数组下标越界"+size); + } + + if(index < (size >> 1)){ + Node e = header; + for(int i = 0; i < index; i ++){ + e = header.next; + return e; + } + }else { + Node e = last; + for(int i = size - 1; i > index; i--){ + e = last.prerious; + return e; + } + } + return null; + } + + + +} diff --git a/group01/553624797/code2017/src/com/xxt/DataStructure/MyStack.java b/group01/553624797/code2017/src/com/xxt/DataStructure/MyStack.java new file mode 100644 index 0000000000..4ac0cf3698 --- /dev/null +++ b/group01/553624797/code2017/src/com/xxt/DataStructure/MyStack.java @@ -0,0 +1,52 @@ +package com.xxt.DataStructure; + +import java.util.EmptyStackException; + +/** + * Created by star on 2017/2/26. + */ +public class MyStack { + + + //采用数组实现; + private Object[] array; + //栈顶指针 + private int top; + private final static int size = 100; + + public MyStack(Object[] array, int top) { + this.array = array; + //空栈 + top = -1 ; + } + + public void push(Object elementData){ + //栈满 + if(top == size - 1){ + throw new StackOverflowError(); + }else { + array[++top] = elementData; + } + } + + //弹栈 + public Object pop(){ + if( top == -1){ + throw new EmptyStackException(); + }else { + return array[top--]; + } + } + + public boolean isEmpty(){ + return top == -1; + } + + public Object peek(){ + if(top == -1){ + throw new EmptyStackException(); + }else { + return array[top]; + } + } +} diff --git a/group01/553624797/code2017/src/com/xxt/DataStructure/base/ArrayList.java b/group01/553624797/code2017/src/com/xxt/DataStructure/base/ArrayList.java new file mode 100644 index 0000000000..635c22fa88 --- /dev/null +++ b/group01/553624797/code2017/src/com/xxt/DataStructure/base/ArrayList.java @@ -0,0 +1,36 @@ +package com.xxt.DataStructure.base; + +import com.xxt.DataStructure.base.List; + +import java.util.Iterator; + +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/group01/553624797/code2017/src/com/xxt/DataStructure/base/BinaryTreeNode.java b/group01/553624797/code2017/src/com/xxt/DataStructure/base/BinaryTreeNode.java new file mode 100644 index 0000000000..ada0b84c78 --- /dev/null +++ b/group01/553624797/code2017/src/com/xxt/DataStructure/base/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.xxt.DataStructure.base; + +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/group01/553624797/code2017/src/com/xxt/DataStructure/base/Iterator.java b/group01/553624797/code2017/src/com/xxt/DataStructure/base/Iterator.java new file mode 100644 index 0000000000..4c8b3ae747 --- /dev/null +++ b/group01/553624797/code2017/src/com/xxt/DataStructure/base/Iterator.java @@ -0,0 +1,7 @@ +package com.xxt.DataStructure.base; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group01/553624797/code2017/src/com/xxt/DataStructure/base/LinkedList.java b/group01/553624797/code2017/src/com/xxt/DataStructure/base/LinkedList.java new file mode 100644 index 0000000000..924cf10ff0 --- /dev/null +++ b/group01/553624797/code2017/src/com/xxt/DataStructure/base/LinkedList.java @@ -0,0 +1,48 @@ +package com.xxt.DataStructure.base; + +import com.xxt.DataStructure.base.List; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + + } + public void add(int index , Object o){ + + } + public Object get(int index){ + return null; + } + public Object remove(int index){ + return null; + } + + public int size(){ + return -1; + } + + public void addFirst(Object o){ + + } + public void addLast(Object o){ + + } + public Object removeFirst(){ + return null; + } + public Object removeLast(){ + return null; + } + public com.xxt.DataStructure.base.Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } +} diff --git a/group01/553624797/code2017/src/com/xxt/DataStructure/base/List.java b/group01/553624797/code2017/src/com/xxt/DataStructure/base/List.java new file mode 100644 index 0000000000..2148154c46 --- /dev/null +++ b/group01/553624797/code2017/src/com/xxt/DataStructure/base/List.java @@ -0,0 +1,12 @@ +package com.xxt.DataStructure.base; + +/** + * Created by star on 2017/2/26. + */ +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(); +} \ No newline at end of file diff --git a/group01/553624797/code2017/src/com/xxt/DataStructure/base/Queue.java b/group01/553624797/code2017/src/com/xxt/DataStructure/base/Queue.java new file mode 100644 index 0000000000..5427579179 --- /dev/null +++ b/group01/553624797/code2017/src/com/xxt/DataStructure/base/Queue.java @@ -0,0 +1,18 @@ +package com.xxt.DataStructure.base; + +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/group01/553624797/code2017/src/com/xxt/DataStructure/base/Stack.java b/group01/553624797/code2017/src/com/xxt/DataStructure/base/Stack.java new file mode 100644 index 0000000000..9c62bcdceb --- /dev/null +++ b/group01/553624797/code2017/src/com/xxt/DataStructure/base/Stack.java @@ -0,0 +1,24 @@ +package com.xxt.DataStructure.base; + +import com.xxt.DataStructure.base.ArrayList; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + } + + public Object pop(){ + return null; + } + + public Object peek(){ + return null; + } + public boolean isEmpty(){ + return false; + } + public int size(){ + return -1; + } +} diff --git a/group01/751425278/.classpath b/group01/751425278/.classpath index fb5011632c..c03982172c 100644 --- a/group01/751425278/.classpath +++ b/group01/751425278/.classpath @@ -2,5 +2,7 @@ + + diff --git a/group01/751425278/.gitignore b/group01/751425278/.gitignore index ae3c172604..f5f89256ae 100644 --- a/group01/751425278/.gitignore +++ b/group01/751425278/.gitignore @@ -1 +1,32 @@ +*.class +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +*.iml +*.idea + + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders + + +#macOS +.DS_Store + +.idea/ +*.iml +rebel.* +.rebel.* + +target + /bin/ diff --git a/group01/751425278/src/com/sanmubird/array/ArrayUtil.java b/group01/751425278/src/com/sanmubird/array/ArrayUtil.java new file mode 100644 index 0000000000..e6101d3fca --- /dev/null +++ b/group01/751425278/src/com/sanmubird/array/ArrayUtil.java @@ -0,0 +1,287 @@ +package com.sanmubird.array; + +import java.util.Arrays; + +import com.sanmubird.basicDataStructure.ArrayList; +import com.sanmubird.basicDataStructure.Iterator; + +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[] result = new int[origin.length]; + for(int i = 0 ; i < origin.length ; i++){ + result[i] = origin[origin.length -1 -i]; + System.out.println(result[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 countZero = 0 ; + for(int i = 0 ; i < oldArray.length ; i++){ + if(oldArray[i] == 0 ) { + countZero++ ; + } + } + int[] newArray = new int[ oldArray.length - countZero]; + int index = 0 ; + for(int i = 0 ; i < oldArray.length ; i++){ + if(oldArray[i] != 0 ){ + newArray[index] = oldArray[i] ; + System.out.println(newArray[index]); + index++; + } + } + 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 newLength = array1.length + array2.length; + int[] array3 = Arrays.copyOf(array1, newLength); + for(int i = array1.length ; i < newLength ; i++ ){ + array3[i] = array2[newLength - i -1]; + } + // 对这个数组进行冒泡排序; + int temp ; + for(int i = 0 ; i < array3.length ; i++){ + for(int j = 0 ; j < array3.length - i -1 ; j++){ + if(array3[j+1] < array3[j]){ + temp = array3[j+1]; + array3[j+1] = array3[j]; + array3[j] = temp ; + } + } + } + //计算出排序后的数组中重复值的个数; + int count = 0 ; + for(int i = 0 ; i < array3.length -1 ; i++){ + if(array3[i] == array3[i+1]){ + count++; + } + } + //创建一个新的数组; + int[] array = new int[array3.length - count]; + //把合并后的数组复制到新的数组中去,在复制的过程中,去除重复的元素; + int size = 0 ; + for(int i = 0 ; i < array3.length ; i++){ + if(i < array3.length - 1){ + if(array3[i] == array3[i+1]){ + array[size] = array3[i+1] ; + }else{ + array[size] = array3[i]; + size++; + } + }else{ + array[size] = array3[i]; + } + } + return array; + } + /** + * 把一个已经存满数据的数组 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 < oldArray.length ; i++){ + newArray[i] = oldArray[i]; + } + for(int i = 0 ; i < newArray.length ; i++){ + System.out.print(newArray[i]+","); + } + 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){ + int count1 = 0 ; + int result1 ; + for(int i = 1 ; i <= max ;i++ ){ + result1 = getFiBo(i); + if(max >= result1){ + count1++; + }else{ + break; + } + } + System.out.println(count1); + int[] array = new int[count1]; + int count = 0 ; + int result ; + for(int i = 1 ; i <= max ;i++ ){ + result = getFiBo(i); + if(max >= result){ + array[count] = result ; + count++; + }else{ + break; + } + } + + for(int i = 0 ; i < array.length ; i++){ + System.out.println("array["+i+"]:"+array[i]+","); + } + return array; + } + + public static int getFiBo(int i){ + if(i == 1 || i == 2){ + return 1 ; + }else{ + return getFiBo(i-1) + getFiBo(i -2) ; + } + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + int count1 = 0 ; + for(int i = 1 ; i < max ; i++ ){ + if( isPrime(i )){ + count1++; + } + } + int[] array = new int[count1]; + int count = 0 ; + for(int i = 1 ; i < max ; i++ ){ + if( isPrime(i )){ + array[count] = i ; + count++; + } + } + for(int i = 0 ; i < array.length ; i++){ + System.out.println("array["+i+"]:"+array[i]+","); + } + + return array; + } + + public static boolean isPrime(int a ){ + int count = 0 ; + for(int i = 1 ; i <= a ; i++){ + if(a % i == 0 ){ + count++; + } + } + if(count == 2 ){ + return true ; + } + return false ; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + ArrayList al = new ArrayList(); + for(int i = 1 ; i <= max ; i++){ + if(isPerfectNumber(i)){ + al.add(i); + } + } + int[] array = arrayListToArray(al); + for(int i = 0 ; i < array.length ; i++){ + System.out.println("array["+i+"]:"+array[i]+","); + } + return array ; + } + + public static int[] arrayListToArray(ArrayList al){ + int size = al.size(); + int[] array = new int[size]; + for(int i = 0 ; i < size ; i++){ + array[i] = (int) al.get(i); + } + return array ; + } + + public static boolean isPerfectNumber(int a){ + ArrayList al = new ArrayList(); + for(int i = 1 ; i <= a/2 ; i++ ){ + if(a%i == 0){ + al.add(i); + } + } + int sum = 0 ; + Iterator it=al.iterator(); + while(it.hasNext()){ + sum += (int) it.next(); + } + if(a == sum ){ + return true ; + } + return false ; + } + + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + String s = ""; + String ss = ""; + String sss =""; + for(int i = 0 ; i < array.length ; i++){ + if(i == array.length-1){ + sss = array[i]+""; + s += sss; + }else{ + ss = array[i]+seperator; + s += ss; + } + } + return s; + } +} diff --git a/group01/751425278/src/com/sanmubird/litestruts/LoginAction.java b/group01/751425278/src/com/sanmubird/litestruts/LoginAction.java new file mode 100644 index 0000000000..63d91a4bac --- /dev/null +++ b/group01/751425278/src/com/sanmubird/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.sanmubird.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/group01/751425278/src/com/sanmubird/litestruts/Struts.java b/group01/751425278/src/com/sanmubird/litestruts/Struts.java new file mode 100644 index 0000000000..3334beda30 --- /dev/null +++ b/group01/751425278/src/com/sanmubird/litestruts/Struts.java @@ -0,0 +1,125 @@ +package com.sanmubird.litestruts; + +import java.io.InputStream; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.parsers.ParserConfigurationException; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.xml.sax.SAXException; + + + +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字段中。 + + */ + View view = new View(); + String actionClassPath = null ; + Class actionClass; + String resultName = null; + List actionElement = parseXML(); + try { + for(Element e : actionElement){ + if("action".equals(e.getName())){ + if(e.attributeValue("name").equals(actionName)){ + actionClassPath = e.attributeValue("class"); + actionClass = Class.forName(actionClassPath); + Object actionClassInstance = actionClass.newInstance(); //通过反射 得到类的对象 + + if(parameters != null){ + for(Map.Entry entry : parameters.entrySet()){ + String filedName = entry.getKey(); + String methodName = "set"+toUpperFirstLetter(filedName); + Class filedType = actionClass.getDeclaredField(filedName).getType(); + Method method = actionClass.getDeclaredMethod(methodName, filedType); + method.invoke(actionClassInstance, entry.getValue());//对当前对象的实例化; + } + } + + Method execute = actionClass.getDeclaredMethod("execute"); + String result = (String) execute.invoke(actionClassInstance); //得到execute执行的结果 + + Method[] methods = actionClass.getDeclaredMethods(); + Map param = new HashMap(); + for(Method method : methods){ + String methodName = method.getName(); + if(method.getName().startsWith("get")){ + String filedName = methodName.substring(3,4).toLowerCase()+methodName.substring(4); + Object filedValue = method.invoke(actionClassInstance); + param.put(filedName, filedValue); + } + } + + view.setParameters(param); + List resultElement = e.elements(); + for(Element e1 : resultElement ){ + if("result".equals(e1.getName())){ + resultName = e1.attributeValue("name"); + if(resultName.equals(result)){ + view.setJsp(e1.getText()); + break; + } + } + } + } + } + } + }catch (Exception e1) { + e1.printStackTrace(); + } + return view ; + } + + public static List parseXML(){ + SAXReader reader = new SAXReader(); + List firstList = null; + try { + + InputStream in = Struts.class.getClassLoader().getResourceAsStream("com/sanmubird/litestruts/struts.xml");// 要加载的文件和.class文件在同一个目录下; + // Class.getClassLoader.getResourceAsStream(String path); 默认是从ClassPath根下获取的; + // Class.getResourceAsStream(String path) path(/dirName/fileName)是从ClassPath根下获取的,没有/则是从本.class文件同目录中获取的; + Document doc = reader.read(in); + Element root = doc.getRootElement(); + firstList = root.elements(); + } catch (Exception e) { + e.printStackTrace(); + } + return firstList; + } + + public static String toUpperFirstLetter(String s){ + if(s != "" ){ + String s1 = s.substring(0,1).toUpperCase(); + String other = s.substring(1); + return s1+other; + }else{ + throw new RuntimeException("传入的参数不能是空 或 '' "); + } + } + +} diff --git a/group01/751425278/src/com/sanmubird/litestruts/StrutsTest.java b/group01/751425278/src/com/sanmubird/litestruts/StrutsTest.java new file mode 100644 index 0000000000..75e642de87 --- /dev/null +++ b/group01/751425278/src/com/sanmubird/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.sanmubird.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/group01/751425278/src/com/sanmubird/litestruts/View.java b/group01/751425278/src/com/sanmubird/litestruts/View.java new file mode 100644 index 0000000000..840fcb1ef3 --- /dev/null +++ b/group01/751425278/src/com/sanmubird/litestruts/View.java @@ -0,0 +1,23 @@ +package com.sanmubird.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/group01/751425278/src/com/sanmubird/litestruts/struts.xml b/group01/751425278/src/com/sanmubird/litestruts/struts.xml new file mode 100644 index 0000000000..8d05fbd92d --- /dev/null +++ b/group01/751425278/src/com/sanmubird/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/group01/765324639/src/zavier/week02/coderising/array/ArrayUtil.java b/group01/765324639/src/zavier/week02/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..96bff301c7 --- /dev/null +++ b/group01/765324639/src/zavier/week02/coderising/array/ArrayUtil.java @@ -0,0 +1,263 @@ +package zavier.week02.coderising.array; + +import zavier.week01.basic.ArrayList; +import zavier.week01.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 (origin == null) { + throw new IllegalArgumentException(); + } + + int temp; + 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 int[] removeZero(int[] oldArray) { + if (oldArray == null) { + throw new IllegalArgumentException(); + } + + int[] noZeroArray = new int[oldArray.length]; + int index = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + noZeroArray[index++] = oldArray[i]; + } + } + + return copyOf(noZeroArray, index); + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 = [3, 5, 7,8] a2 = [4, + * 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + if (array1 == null || array2 == null) { + throw new IllegalArgumentException(); + } + + int indexOfArray1 = 0; + int indexOfArray2 = 0; + int totalLength = array1.length + array2.length; + int[] destArr = new int[totalLength]; + + for (int i = 0; i < totalLength; i++) { + if (indexOfArray1 >= array1.length) { + // array1填充完毕,将array2填充剩下的元素 + arrayCopy(array2, indexOfArray2, destArr, i, array2.length - indexOfArray2); + int actualSize = i + array2.length - indexOfArray2; + return copyOf(destArr, actualSize); + } + + if (indexOfArray2 >= array2.length) { + arrayCopy(array1, indexOfArray1, destArr, i, array1.length - indexOfArray1); + int actualSize = i + array1.length - indexOfArray1; + return copyOf(destArr, actualSize); + } + + if (array1[indexOfArray1] < array2[indexOfArray2]) { + destArr[i] = array1[indexOfArray1]; + indexOfArray1++; + } else if (array1[indexOfArray1] == array2[indexOfArray2]) { + destArr[i] = array1[indexOfArray1]; + indexOfArray1++; + indexOfArray2++; // 去除重复元素 + } else { + destArr[i] = array2[indexOfArray2]; + indexOfArray2++; + } + + } + // array1.length、array2.length均为0的情况 + return new int[0]; + } + + private void arrayCopy(int[] src, int srcPos, int[] dest, int destPos, int length) { + for (int i = 0; i < length; i++) { + dest[destPos++] = src[srcPos++]; + } + } + + private int[] copyOf(int[] original, int newLength) { + int[] dest = new int[newLength]; + for (int i = 0; i < newLength; i++) { + dest[i] = original[i]; + } + return dest; + } + + /** + * 把一个已经存满数据的数组 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) { + if (oldArray == null || size < 0) { + throw new IllegalArgumentException(); + } + + int newSize = oldArray.length + size; + int[] newArray = new int[newSize]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + if (max < 0) { + throw new IllegalArgumentException(); + } + if (max == 1) { + return new int[] {}; + } + + ArrayList list = new ArrayList(); + list.add(1); + list.add(1); + + int i = 0; + while (true) { + int num = (int) list.get(i) + (int) list.get(i + 1); + if (num < max) { + list.add(num); + } else { + break; + } + i++; + } + + return intListToArray(list); + } + + private int[] intListToArray(List list) { + int[] array = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + array[i] = (int) list.get(i); + } + return array; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + if (max < 0) { + throw new IllegalArgumentException(); + } + + ArrayList list = new ArrayList(); + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + list.add(i); + } + } + return intListToArray(list); + } + + private boolean isPrime(int num) { + for (int i = 2; i <= num / 2; i++) { + if (num % i == 0) { + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + if (max < 0) { + throw new IllegalArgumentException(); + } + + ArrayList list = new ArrayList(); + for (int i = 2; i < max; i++) { + if (isPerfectNumber(i)) { + list.add(i); + } + } + return intListToArray(list); + } + + private boolean isPerfectNumber(int num) { + int sumOfFactor = 1; + for (int i = 2; i <= num / 2; i++) { + if (num % i == 0) { + sumOfFactor += i; + } + } + if (sumOfFactor == num) { + return true; + } + return false; + } + + /** + * 用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) { + throw new IllegalArgumentException(); + } + if (array.length == 0) { + return ""; + } + + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + builder.append(array[i]).append(seperator); + } + return builder.substring(0, builder.length() - seperator.length()); + } + + +} diff --git a/group01/765324639/src/zavier/week02/coderising/array/ArrayUtilTest.java b/group01/765324639/src/zavier/week02/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..d0e6fa11d8 --- /dev/null +++ b/group01/765324639/src/zavier/week02/coderising/array/ArrayUtilTest.java @@ -0,0 +1,133 @@ +package zavier.week02.coderising.array; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ArrayUtilTest { + + private ArrayUtil arrayUtil; + + @Before + public void setUp() { + arrayUtil = new ArrayUtil(); + } + + @Test + public void testReverseArray() { + int[] a = {}; + arrayUtil.reverseArray(a); + Assert.assertArrayEquals(new int[] {}, a); + + int[] b = {1, 2, 3}; + arrayUtil.reverseArray(b); + Assert.assertArrayEquals(new int[] {3, 2, 1}, b); + + int[] c = {1, 2, 3, 4}; + arrayUtil.reverseArray(c); + Assert.assertArrayEquals(new int[] {4, 3, 2, 1}, c); + + int[] d = new int[5000]; + int[] d1 = new int[5000]; + for (int i = 0; i < 5000; i++) { + d[i] = i; + d1[i] = 4999 - i; + } + arrayUtil.reverseArray(d); + Assert.assertArrayEquals(d1, d); + } + + @Test(expected = IllegalArgumentException.class) + public void testRemoveZero() { + int oldArr1[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + int newArr1[] = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}; + Assert.assertArrayEquals(newArr1, arrayUtil.removeZero(oldArr1)); + + int oldArr2[] = {0, 0, 0, 0, 0}; + int newArr2[] = {}; + Assert.assertArrayEquals(newArr2, arrayUtil.removeZero(oldArr2)); + + int oldArr3[] = {0, 0, 0, 0, 0, 1, 2, 3}; + int newArr3[] = {1, 2, 3}; + Assert.assertArrayEquals(newArr3, arrayUtil.removeZero(oldArr3)); + + int oldArr4[] = {1, 2, 3, 0, 0, 0, 0, 0}; + int newArr4[] = {1, 2, 3}; + Assert.assertArrayEquals(newArr4, arrayUtil.removeZero(oldArr4)); + + int oldArr5[] = {0, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 0}; + int newArr5[] = {1, 2, 3, 4}; + Assert.assertArrayEquals(newArr5, arrayUtil.removeZero(oldArr5)); + + int oldArr6[] = {}; + int newArr6[] = {}; + Assert.assertArrayEquals(newArr6, oldArr6); + + arrayUtil.removeZero(null); + } + + @Test + public void testMerge() { + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + Assert.assertArrayEquals(new int[] {3, 4, 5, 6, 7, 8}, arrayUtil.merge(a1, a2)); + + int[] b1 = {1, 2, 9, 100}; + int[] b2 = {3, 4, 5, 90, 100}; + Assert.assertArrayEquals(new int[] {1, 2, 3, 4, 5, 9, 90, 100}, arrayUtil.merge(b1, b2)); + + int[] c1 = {}; + int[] c2 = {1, 2, 3}; + Assert.assertArrayEquals(new int[] {1, 2, 3}, arrayUtil.merge(c1, c2)); + + int[] d1 = {}; + int[] d2 = {}; + Assert.assertArrayEquals(new int[] {}, arrayUtil.merge(d1, d2)); + } + + @Test + public void testGrow() { + int[] a1 = {2, 3, 6}; + Assert.assertArrayEquals(new int[] {2, 3, 6, 0, 0, 0}, arrayUtil.grow(a1, 3)); + Assert.assertArrayEquals(new int[] {2, 3, 6}, arrayUtil.grow(a1, 0)); + } + + @Test + public void testFibonacci() { + Assert.assertArrayEquals(new int[] {}, arrayUtil.fibonacci(1)); + Assert.assertArrayEquals(new int[] {1, 1, 2, 3, 5, 8, 13}, arrayUtil.fibonacci(15)); + + int[] a = new int[] {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, + 2584, 4181, 6765}; + Assert.assertArrayEquals(a, arrayUtil.fibonacci(6766)); + } + + @Test + public void testGetPrimes() { + Assert.assertArrayEquals(new int[] {}, arrayUtil.getPrimes(1)); + Assert.assertArrayEquals(new int[] {}, arrayUtil.getPrimes(2)); + Assert.assertArrayEquals(new int[] {2, 3, 5, 7, 11, 13, 17, 19}, arrayUtil.getPrimes(23)); + Assert.assertArrayEquals(new int[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, + 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, + 149, 151, 157, 163, 167, 173, 179}, arrayUtil.getPrimes(181)); + } + + @Test + public void testGetPerfectNumbers() { + Assert.assertArrayEquals(new int[] {6}, arrayUtil.getPerfectNumbers(7)); + Assert.assertArrayEquals(new int[] {6, 28}, arrayUtil.getPerfectNumbers(496)); + Assert.assertArrayEquals(new int[] {6, 28, 496}, arrayUtil.getPerfectNumbers(497)); + Assert.assertArrayEquals(new int[] {6, 28, 496, 8128}, arrayUtil.getPerfectNumbers(8129)); + + } + + @Test + public void testJoin() { + Assert.assertEquals("", arrayUtil.join(new int[] {}, "-")); + Assert.assertEquals("1", arrayUtil.join(new int[] {1}, "-")); + Assert.assertEquals("1-2-8-3-4-5", arrayUtil.join(new int[] {1, 2, 8, 3, 4, 5}, "-")); + Assert.assertEquals("1*-*2*-*8*-*3*-*4*-*5", + arrayUtil.join(new int[] {1, 2, 8, 3, 4, 5}, "*-*")); + } + +} diff --git a/group01/765324639/src/zavier/week02/coderising/litestruts/LoginAction.java b/group01/765324639/src/zavier/week02/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..617b5f115b --- /dev/null +++ b/group01/765324639/src/zavier/week02/coderising/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +package zavier.week02.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/group01/765324639/src/zavier/week02/coderising/litestruts/Struts.java b/group01/765324639/src/zavier/week02/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..d30beaafb3 --- /dev/null +++ b/group01/765324639/src/zavier/week02/coderising/litestruts/Struts.java @@ -0,0 +1,117 @@ +package zavier.week02.coderising.litestruts; + +import java.io.InputStream; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +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字段中。 + * + */ + + try { + + SAXReader reader = new SAXReader(); + InputStream struts = Struts.class.getResourceAsStream("struts.xml"); + Document document = null; + try { + document = reader.read(struts); + } catch (DocumentException e) { + e.printStackTrace(); + } + + String className = ""; // actionName对应的类名 + Element rootElement = document.getRootElement(); + Iterator iterator = rootElement.elementIterator("action"); + Element targetAction = null; // actionName对应的action + while (iterator.hasNext()) { + Element element = (Element) iterator.next(); + String name = element.attributeValue("name"); + if (name.equals(actionName)) { + className = element.attributeValue("class"); + targetAction = element; + break; + } + } + + + Class class1 = Class.forName(className); + Object instance = class1.newInstance(); + + Set keySet = parameters.keySet(); + for (String key : keySet) { + // 将变量名拼成对应的set方法名 + String methodName = "set" + key.substring(0, 1).toUpperCase() + key.substring(1); + Class type = class1.getDeclaredField(key).getType(); + Method method = class1.getDeclaredMethod(methodName, type); + // 依次调用对应的set方法 + method.invoke(instance, parameters.get(key)); + } + + String result = (String) class1.getDeclaredMethod("execute").invoke(instance); + + Method[] declaredMethods = class1.getDeclaredMethods(); + HashMap map = new HashMap<>(); + for (int i = 0; i < declaredMethods.length; i++) { + if (declaredMethods[i].getName().startsWith("get")) { + String fieldValue = (String) declaredMethods[i].invoke(instance); + String fieldName = methodNameToFieldName(declaredMethods[i].getName()); + map.put(fieldName, fieldValue); + } + } + + View view = new View(); + view.setParameters(map); + + Iterator elementIterator = targetAction.elementIterator("result"); + while (elementIterator.hasNext()) { + Element element = (Element) elementIterator.next(); + if (result.equals(element.attributeValue("name"))) { + view.setJsp(element.getText()); + } + } + + return view; + + + } catch (Exception e1) { + e1.printStackTrace(); + } + + return null; + } + + private static String methodNameToFieldName(String methodName) { + if (!methodName.startsWith("get") && !methodName.startsWith("set")) { + throw new IllegalArgumentException(); + } + + return methodName.substring(3, 4).toLowerCase() + methodName.substring(4); + } + +} diff --git a/group01/765324639/src/zavier/week02/coderising/litestruts/StrutsTest.java b/group01/765324639/src/zavier/week02/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..ba800c2fdc --- /dev/null +++ b/group01/765324639/src/zavier/week02/coderising/litestruts/StrutsTest.java @@ -0,0 +1,42 @@ +package zavier.week02.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/group01/765324639/src/zavier/week02/coderising/litestruts/View.java b/group01/765324639/src/zavier/week02/coderising/litestruts/View.java new file mode 100644 index 0000000000..f164c4bfc5 --- /dev/null +++ b/group01/765324639/src/zavier/week02/coderising/litestruts/View.java @@ -0,0 +1,26 @@ +package zavier.week02.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/group01/765324639/src/zavier/week02/coderising/litestruts/struts.xml b/group01/765324639/src/zavier/week02/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..ffe9110788 --- /dev/null +++ b/group01/765324639/src/zavier/week02/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/group01/819048836/lvxg2017/src/com/coderising/ArrayUtil.java b/group01/819048836/lvxg2017/src/com/coderising/ArrayUtil.java new file mode 100644 index 0000000000..dd25201471 --- /dev/null +++ b/group01/819048836/lvxg2017/src/com/coderising/ArrayUtil.java @@ -0,0 +1,226 @@ +package com.coderising; + +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 static void reverseArray(int[] origin) { + for (int i = 0; i < origin.length / 2; i++) { + int temp = origin[i]; + origin[i] = origin[origin.length - i - 1]; + origin[origin.length - i - 1] = 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 static int[] removeZero(int[] oldArray) { + int size = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + size++; + } + } + int[] newArray = new int[size]; + int j = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArray[j++] = oldArray[i]; + } + } + 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 static int[] merge(int[] array1, int[] array2) { + int[] newArr = new int[array1.length + array2.length]; + int index1 = 0; + int index2 = 0; + int z = 0; + for (int i = 0; i < array2.length; i++) { + if (array1[index1] - array2[index2] > 0) { + newArr[z++] = array2[index2]; + } + } + + return null; + } + + public static int[] merge1(int[] array1, int[] array2) { + int size = 0; + for (int i = 0; i < array1.length; i++) { + for (int j = 0; j < array2.length; j++) { + if (array1[i] == array2[j]) { + size++; + } + } + } + int[] newArr = new int[array1.length + array2.length - size]; + int z = 0; + for (int i = 0; i < array1.length; i++) { + for (int j = 0; j < array2.length; j++) { + if (array1[i] < array2[j]) { + newArr[z++] = array1[i]; + break; + } else if (array1[i] > array2[j]) { + newArr[z++] = array2[j]; + break; + } else if (array1[i] == array2[j]) { + newArr[z++] = array1[i]; + break; + } + + } + } + System.out.println(size); + return null; + } + + /** + * 把一个已存满数据的数组oldArray的容量进行扩展,扩展后的新数据大小为OldArray,length+size + * 注意,老数组的元素在新数组中需要保持 + * + * @param args + */ + public static int[] grow(int[] oldArray, int size) { + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, size); + return newArray; + } + + /** + * 裴波那契数列为:1,1,2,3,5,8,13,21。。。 例如max = 15,则返回的数组应该为[1,1,2,3,5,8,13] max =1 + * ,则返回空数组[] + * + * @param args + */ + public static int[] fibonacci(int max) { + if (max == 1) { + int[] arr = new int[0]; + return arr; + } + int x = 1; + int y = 1; + int temp = 0; + int size = 0; + while (true) { + temp = y; + y = x + y; + x = temp; + if (y >= max) { + break; + } + size++; + + } + int[] array = new int[size + 2]; + array[0] = 1; + array[1] = 1; + int j = 2; + int m = 1; + int n = 1; + int temp1 = 1; + while (true) { + temp1 = n; + n = m + n; + m = temp1; + if (n >= max) { + break; + } + size++; + array[j++] = n; + } + return array; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + */ + public static int[] getPrimes(int max) { + int i = 0; + for (int j = 2; j < max; j++) { + boolean b = false; + for (int j2 = 2; j2 < j; j2++) { + if (j % j2 == 0) { + b = true; + break; + } + } + if (b == false) { + i++; + } + } + int[] arr = new int[i]; + int z = 0; + for (int j = 2; j < max; j++) { + boolean b = false; + for (int j2 = 2; j2 < j; j2++) { + if (j % j2 == 0) { + b = true; + break; + } + } + if (b == false) { + arr[z++] = j; + } + } + return arr; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator) { + for (int i = 0; i < array.length; i++) { + if (i == 0) { + seperator = array[i] + ""; + } else { + seperator = seperator + "-" + "" + array[i] + ""; + } + } + return seperator; + } + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + return null; + } + + public static void main(String[] args) { + int[] a = { 1, 3, 5, 6 }; + int[] b = { 2, 3, 4, 5, 7 }; + int[] ab = ArrayUtil.fibonacci(30); + int array[] = { 1, 2, 3, 4 }; + String s = ArrayUtil.join(array, new String()); + System.out.println(s); + } +} diff --git a/group01/819048836/lvxg2017/src/com/coderising/action/LoginAction.java b/group01/819048836/lvxg2017/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..ccb1acce9a --- /dev/null +++ b/group01/819048836/lvxg2017/src/com/coderising/action/LoginAction.java @@ -0,0 +1,36 @@ +package com.coderising.action; + +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/group01/819048836/lvxg2017/src/com/coderising/action/Struts.java b/group01/819048836/lvxg2017/src/com/coderising/action/Struts.java new file mode 100644 index 0000000000..c6362cb87a --- /dev/null +++ b/group01/819048836/lvxg2017/src/com/coderising/action/Struts.java @@ -0,0 +1,102 @@ +package com.coderising.action; + +import java.io.InputStream; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +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字段中。 + * + */ + try { + SAXReader reader = new SAXReader(); + InputStream in = Struts.class.getResourceAsStream("struts.xml"); + Document document = reader.read(in); + Element root = document.getRootElement(); + System.out.println(root); + System.out.println(document); + + // 与actionName匹配的Element + Element actionElement = null; + String className = null; + + for (Iterator iterator = root.elementIterator("action"); iterator.hasNext();) { + Element e = iterator.next(); + if (e.attributeValue("name").equals(actionName)) { + actionElement = e; + className = e.attributeValue("class"); + break; + } + } + Class clazz = Class.forName(className); + Object instance = clazz.newInstance(); + Set keySet = parameters.keySet(); + for (String key : keySet) { + String methodName = "set" + key.substring(0, 1).toUpperCase() + key.substring(1); + Class type = clazz.getDeclaredField(key).getType(); + Method method = clazz.getDeclaredMethod(methodName, type); + // 依次调用对应的set方法 + method.invoke(instance, parameters.get(key)); + } + String result = (String) clazz.getDeclaredMethod("execute").invoke(instance); + // 反射调用getter方法 + Method[] methods =clazz.getDeclaredMethods(); + Map param = new HashMap<>(); + for (Method method : methods) { + String methodname = method.getName(); + if(method.getName().startsWith("get")){ + String fieldName = methodname.substring(3, 4).toLowerCase() + methodname.substring(4); + Object fieldValue = method.invoke(instance); + param.put(fieldName, fieldValue); + } + } + View view = new View(); + view.setParameters(param); + for (Iterator iterator = actionElement.elementIterator("result"); iterator.hasNext();) { + Element resultElement = iterator.next(); + if (resultElement.attributeValue("name").equals(result)) { + view.setJsp(resultElement.getText()); + break; + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + + public static void main(String[] args) { + Map map = new HashMap<>(); + map.put("name", "test"); + map.put("password", "1234"); + Struts.runAction("login", map); + + } +} diff --git a/group01/819048836/lvxg2017/src/com/coderising/action/StrutsTest.java b/group01/819048836/lvxg2017/src/com/coderising/action/StrutsTest.java new file mode 100644 index 0000000000..7f72a992a0 --- /dev/null +++ b/group01/819048836/lvxg2017/src/com/coderising/action/StrutsTest.java @@ -0,0 +1,24 @@ +package com.coderising.action; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + + @Test + public void testLoginActionSuccess() { + // File f = new + + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + + } +} diff --git a/group01/819048836/lvxg2017/src/com/coderising/action/View.java b/group01/819048836/lvxg2017/src/com/coderising/action/View.java new file mode 100644 index 0000000000..e79c9beb3f --- /dev/null +++ b/group01/819048836/lvxg2017/src/com/coderising/action/View.java @@ -0,0 +1,23 @@ +package com.coderising.action; + +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/group01/819048836/lvxg2017/src/com/coderising/action/struts.xml b/group01/819048836/lvxg2017/src/com/coderising/action/struts.xml new file mode 100644 index 0000000000..a087b48337 --- /dev/null +++ b/group01/819048836/lvxg2017/src/com/coderising/action/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/group01/895457260/code/src/algorithm/ArrayUtil.java b/group01/895457260/code/src/algorithm/ArrayUtil.java new file mode 100644 index 0000000000..bb5a25a93a --- /dev/null +++ b/group01/895457260/code/src/algorithm/ArrayUtil.java @@ -0,0 +1,239 @@ +package algorithm; + +import datastructure.basic.ArrayList; + +import java.util.stream.Stream; + +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 head = 0; + int rear = origin.length - 1; + for (; head < rear; ++head, --rear) { + if (origin[head] != origin[rear]) { + int t = origin[head]; + origin[head] = origin[rear]; + origin[rear] = t; + } + } + } + + /** + * 现在有如下的一个数组: 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 count = 0; + int[] tempArray = new int[oldArray.length]; + for (int i : oldArray) { + if (i != 0) { + tempArray[count++] = i; + } + } + int[] newArray = new int[count]; + System.arraycopy(tempArray, 0, newArray, 0, count); + 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[] tempArray = new int[array1.length + array2.length]; + int count = 0; + int p1 = 0, p2 = 0; + + while (p1 < array1.length && p2 < array2.length) { + if (array1[p1] < array2[p2]) { + if (count == 0 || array1[p1] != tempArray[count - 1]) { + tempArray[count++] = array1[p1]; + } + p1++; + } else if (array1[p1] > array2[p2]) { + if (count == 0 || array2[p2] != tempArray[count - 1]) { + tempArray[count++] = array2[p2++]; + } + p2++; + } else { + if (count == 0 || array1[p1] != tempArray[count - 1]) { + tempArray[count++] = array1[p1++]; + } + p1++; + p2++; + } + } + while (p1 < array1.length) { + if (count == 0 || array1[p1] != tempArray[count - 1]) { + tempArray[count++] = array1[p1]; + } + p1++; + } + while (p2 < array2.length) { + if (count == 0 || array2[p2] != tempArray[count - 1]) { + tempArray[count++] = array2[p2]; + } + p2++; + } + + int[] newArray = new int[count]; + System.arraycopy(tempArray, 0, newArray, 0, count); + return newArray; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public int[] grow(int [] oldArray, int size) { + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public int[] fibonacci(int max) { + if (max <= 1) { + return new int[0]; + } + + ArrayList list = new ArrayList(); + list.add(1); + list.add(1); + while (true) { + int a = (int) (list.get(list.size() - 1)); + int b = (int) (list.get(list.size() - 2)); + if (a + b < max) { + list.add(a + b); + } else { + break; + } + } + int[] result = new int[list.size()]; + for (int i = 0; i < list.size(); ++i) { + result[i] = (int) list.get(i); + } + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max) { + if (max <= 2) { + return new int[0]; + } + + ArrayList list = new ArrayList(); + for (int i = 2; true; ++i) { + if (i >= max) { + break; + } else if (isPrime(i)) { + list.add(i); + } + } + + int[] result = new int[list.size()]; + for (int i = 0; i < list.size(); ++i) { + result[i] = (int) list.get(i); + } + return result; + } + + private boolean isPrime(int n) { + for (int i = 2; i <= Math.sqrt(n); ++i) { + if (n % i == 0) { + return false; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + if (max <= 6) { + return new int[0]; + } + + ArrayList list = new ArrayList(); + for (int i = 2; true; ++i) { + if (i >= max) { + break; + } else if (isPerfect(i)) { + list.add(i); + } + } + + int[] result = new int[list.size()]; + for (int i = 0; i < list.size(); ++i) { + result[i] = (int) list.get(i); + } + return result; + } + + private boolean isPerfect(int n) { + int sum = 1; + for (int i = 2; i <= n / 2; ++i) { + if (sum > n) { + return false; + } + if (n % i == 0) { + sum += i; + } + } + return sum == n; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + if (array.length == 0) { + return ""; + } + StringBuilder builder = new StringBuilder(); + builder.append(array[0]); + for (int i = 1; i < array.length; ++i) { + builder.append(seperator).append(array[i]); + } + return builder.toString(); + } +} diff --git a/group01/895457260/code/src/algorithm/test/ArrayUtilTest.java b/group01/895457260/code/src/algorithm/test/ArrayUtilTest.java new file mode 100644 index 0000000000..85f04f3b31 --- /dev/null +++ b/group01/895457260/code/src/algorithm/test/ArrayUtilTest.java @@ -0,0 +1,234 @@ +package algorithm.test; + +import algorithm.ArrayUtil; +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +/** + * ArrayUtil Tester. + * + * @author + * @version 1.0 + * @since
二月 27, 2017
+ */ +public class ArrayUtilTest { + ArrayUtil util = new ArrayUtil(); + + /** + * Method: reverseArray(int[] origin) + */ + @Test + public void testReverseArray() throws Exception { +//TODO: Test goes here... + int[][] arrays = { + {7, 9, 30, 3}, + {7, 9, 30, 3, 4}, + {5}, + {} + }; + for (int[] a : arrays) { + util.reverseArray(a); + } + + int[][] result = { + {3, 30, 9, 7}, + {4, 3, 30, 9, 7}, + {5}, + {} + }; + Assert.assertArrayEquals(arrays, result); + } + + /** + * Method: removeZero(int[] oldArray) + */ + @Test + public void testRemoveZero() throws Exception { +//TODO: Test goes here... + int[][] arrays = { + {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}, + {6, 6, 3, 5, 4}, + {0, 0, 0}, + {} + }; + + for (int i = 0; i < arrays.length; ++i) { + arrays[i] = util.removeZero(arrays[i]); + } + + int[][] result = { + {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}, + {6, 6, 3, 5, 4}, + {}, + {} + }; + Assert.assertArrayEquals(arrays, result); + } + + /** + * Method: merge(int[] array1, int[] array2) + */ + @Test + public void testMerge() throws Exception { +//TODO: Test goes here... + int[][] arrays1 = { + {3, 5, 7, 8}, + {2, 3, 4}, + {1, 2, 3, 3, 4, 5}, + {1, 2, 2}, + {}, + {} + }; + int[][] arrays2 = { + {4, 5, 6, 7}, + {6, 7, 8, 9, 9}, + {4, 4, 5, 7}, + {}, + {2, 2, 3}, + {} + }; + + int[][] merged = new int[arrays1.length][]; + for (int i = 0; i < arrays1.length; ++i) { + merged[i] = util.merge(arrays1[i], arrays2[i]); + } + + int[][] result = { + {3, 4, 5, 6, 7, 8}, + {2, 3, 4, 6, 7, 8, 9}, + {1, 2, 3, 4, 5, 7}, + {1, 2}, + {2, 3}, + {} + }; + Assert.assertArrayEquals(merged, result); + } + + /** + * Method: grow(int [] oldArray, int size) + */ + @Test + public void testGrow() throws Exception { +//TODO: Test goes here... + int[][] arrays = { + {2, 3, 6}, + {}, + {1} + }; + + int[] size = {3, 3, 0}; + + for (int i = 0; i < arrays.length; ++i) { + arrays[i] = util.grow(arrays[i], size[i]); + } + + int[][] result = { + {2, 3, 6, 0, 0, 0}, + {0, 0, 0}, + {1} + }; + Assert.assertArrayEquals(arrays, result); + } + + /** + * Method: fibonacci(int max) + */ + @Test + public void testFibonacci() throws Exception { +//TODO: Test goes here... + int[] max = {0, 1, 2, 15}; + + int[][] arrays = new int[max.length][]; + + for (int i = 0; i < arrays.length; ++i) { + arrays[i] = util.fibonacci(max[i]); + } + + int[][] result = { + {}, + {}, + {1, 1}, + {1, 1, 2, 3, 5, 8, 13} + }; + Assert.assertArrayEquals(arrays, result); + } + + /** + * Method: getPrimes(int max) + */ + @Test + public void testGetPrimes() throws Exception { +//TODO: Test goes here... + int[] max = {0, 1, 2, 3, 11}; + + int[][] arrays = new int[max.length][]; + + for (int i = 0; i < arrays.length; ++i) { + arrays[i] = util.getPrimes(max[i]); + } + + int[][] result = { + {}, + {}, + {}, + {2}, + {2, 3, 5, 7} + }; + Assert.assertArrayEquals(arrays, result); + } + + /** + * Method: getPerfectNumbers(int max) + */ + @Test + public void testGetPerfectNumbers() throws Exception { +//TODO: Test goes here... + int[] max = {0, 6, 7, 496, 497}; + + int[][] arrays = new int[max.length][]; + + for (int i = 0; i < arrays.length; ++i) { + arrays[i] = util.getPerfectNumbers(max[i]); + } + + int[][] result = { + {}, + {}, + {6}, + {6, 28}, + {6, 28, 496} + }; + Assert.assertArrayEquals(arrays, result); + } + + /** + * Method: join(int[] array, String seperator) + */ + @Test + public void testJoin() throws Exception { +//TODO: Test goes here... + int[][] arrays = { + {3, 8, 9}, + {5}, + {6, 6}, + {} + }; + + String[] sep = {"-", "**", "", "---"}; + String[] joined = new String[arrays.length]; + + for (int i = 0; i < arrays.length; ++i) { + joined[i] = util.join(arrays[i], sep[i]); + } + + String[] result = { + "3-8-9", + "5", + "66", + "" + }; + Assert.assertArrayEquals(joined, result); + } +} diff --git a/group01/895457260/code/src/test/datastructure/basic/ArrayListTest.java b/group01/895457260/code/src/datastructure/test/ArrayListTest.java similarity index 99% rename from group01/895457260/code/src/test/datastructure/basic/ArrayListTest.java rename to group01/895457260/code/src/datastructure/test/ArrayListTest.java index 42b9144d38..3a3f742634 100644 --- a/group01/895457260/code/src/test/datastructure/basic/ArrayListTest.java +++ b/group01/895457260/code/src/datastructure/test/ArrayListTest.java @@ -1,4 +1,4 @@ -package test.datastructure.basic; +package datastructure.test; import datastructure.basic.ArrayList; import datastructure.basic.Iterator; diff --git a/group01/895457260/code/src/test/datastructure/basic/BinarySortedTreeTest.java b/group01/895457260/code/src/datastructure/test/BinarySortedTreeTest.java similarity index 98% rename from group01/895457260/code/src/test/datastructure/basic/BinarySortedTreeTest.java rename to group01/895457260/code/src/datastructure/test/BinarySortedTreeTest.java index ce15d5622c..f0374b2700 100644 --- a/group01/895457260/code/src/test/datastructure/basic/BinarySortedTreeTest.java +++ b/group01/895457260/code/src/datastructure/test/BinarySortedTreeTest.java @@ -1,4 +1,4 @@ -package test.datastructure.basic; +package datastructure.test; import datastructure.basic.BinarySortedTree; import datastructure.basic.BinaryTreeNode; diff --git a/group01/895457260/code/src/test/datastructure/basic/LinkedListTest.java b/group01/895457260/code/src/datastructure/test/LinkedListTest.java similarity index 98% rename from group01/895457260/code/src/test/datastructure/basic/LinkedListTest.java rename to group01/895457260/code/src/datastructure/test/LinkedListTest.java index 0ab03eb589..43e080258c 100644 --- a/group01/895457260/code/src/test/datastructure/basic/LinkedListTest.java +++ b/group01/895457260/code/src/datastructure/test/LinkedListTest.java @@ -1,4 +1,4 @@ -package test.datastructure.basic; +package datastructure.test; import datastructure.exception.EmptyListException; import datastructure.basic.LinkedList; diff --git a/group01/895457260/code/src/test/datastructure/basic/QueueTest.java b/group01/895457260/code/src/datastructure/test/QueueTest.java similarity index 98% rename from group01/895457260/code/src/test/datastructure/basic/QueueTest.java rename to group01/895457260/code/src/datastructure/test/QueueTest.java index df7587bd3c..5a47f764a9 100644 --- a/group01/895457260/code/src/test/datastructure/basic/QueueTest.java +++ b/group01/895457260/code/src/datastructure/test/QueueTest.java @@ -1,4 +1,4 @@ -package test.datastructure.basic; +package datastructure.test; import datastructure.exception.EmptyQueueException; import datastructure.basic.Queue; diff --git a/group01/895457260/code/src/test/datastructure/basic/StackTest.java b/group01/895457260/code/src/datastructure/test/StackTest.java similarity index 98% rename from group01/895457260/code/src/test/datastructure/basic/StackTest.java rename to group01/895457260/code/src/datastructure/test/StackTest.java index ac2bd31a22..24d7db58d8 100644 --- a/group01/895457260/code/src/test/datastructure/basic/StackTest.java +++ b/group01/895457260/code/src/datastructure/test/StackTest.java @@ -1,4 +1,4 @@ -package test.datastructure.basic; +package datastructure.test; import datastructure.basic.*; import org.junit.Assert; diff --git a/group01/895457260/code/src/test/datastructure/basic/TestSuite.java b/group01/895457260/code/src/datastructure/test/TestSuite.java similarity index 89% rename from group01/895457260/code/src/test/datastructure/basic/TestSuite.java rename to group01/895457260/code/src/datastructure/test/TestSuite.java index f7c5868383..d13bff3e5d 100644 --- a/group01/895457260/code/src/test/datastructure/basic/TestSuite.java +++ b/group01/895457260/code/src/datastructure/test/TestSuite.java @@ -1,4 +1,4 @@ -package test.datastructure.basic; +package datastructure.test; import org.junit.runner.RunWith; import org.junit.runners.Suite; diff --git a/group01/895457260/code/src/litestruts/Struts.java b/group01/895457260/code/src/litestruts/Struts.java new file mode 100644 index 0000000000..a84581b93c --- /dev/null +++ b/group01/895457260/code/src/litestruts/Struts.java @@ -0,0 +1,188 @@ +package litestruts; + +import datastructure.basic.ArrayList; +import litestruts.exception.XmlElementNotFoundException; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Hashtable; +import java.util.Map; + +public class Struts { + + /* + 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字段中。 + */ + public static View runAction(String actionName, Map parameters) { + //读取xml + Document document; + try { + document = loadXml(); + } catch (ParserConfigurationException | IOException | SAXException e) { + e.printStackTrace(); + return null; + } + + //获取所有action + Element struts = document.getDocumentElement(); + NodeList actionList = struts.getElementsByTagName("action"); + + //根据actionName找到相应action,创建View + try { + Element action = getAction(actionList, actionName); + return createView(action, parameters); + } catch (InvocationTargetException | XmlElementNotFoundException e) { + e.printStackTrace(); + return null; + } + } + + private static Element getAction(NodeList actionList, String name) throws XmlElementNotFoundException { + //根据actionName找到相应action + for (int i = 0; i < actionList.getLength(); ++i) { + Element action = (Element) actionList.item(i); + if (action.getAttribute("name").equals(name)) { + return action; + } + } + throw new XmlElementNotFoundException(elementNotFoundMessage("action", name)); + } + + private static View createView(Element action, Map parameters) + throws InvocationTargetException { + + String className = action.getAttribute("class"); + Class clazz; + Object object; + String executeResult; + try { + clazz = Class.forName(className); + object = clazz.newInstance(); + setParameters(object, parameters); + executeResult = execute(object); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | + InvocationTargetException | NoSuchMethodException e) { + e.printStackTrace(); + return null; + } + + NodeList resultList = action.getElementsByTagName("result"); + String jsp; + Map parameterMap; + try { + jsp = getJsp(resultList, executeResult); + parameterMap = createParameterMap(clazz, object); + } catch (XmlElementNotFoundException | IllegalAccessException e) { + e.printStackTrace(); + return null; + } + + View view = new View(); + view.setJsp(jsp); + view.setParameters(parameterMap); + return view; + } + + private static Map createParameterMap(Class clazz, Object object) + throws InvocationTargetException, IllegalAccessException { + //获取所有getter + Method[] getters = getAllGetters(clazz); + //创建parameter Map + Map parameterMap = new Hashtable<>(); + for (Method getter : getters) { + parameterMap.put(getAttributeName(getter.getName()), (String) getter.invoke(object)); + } + return parameterMap; + } + + private static String getJsp(NodeList resultList, String executeResult) throws XmlElementNotFoundException { + return getResult(resultList, executeResult).getTextContent(); + } + + private static Element getResult(NodeList resultList, String name) throws XmlElementNotFoundException { + for (int j = 0; j < resultList.getLength(); ++j) { + Element result = (Element) resultList.item(j); + if (result.getAttribute("name").equals(name)) { + return result; + } + } + throw new XmlElementNotFoundException(elementNotFoundMessage("result", name)); + } + + private static String elementNotFoundMessage(String elementName, String nameAttribute) { + return "\"" + elementName + "\" XML element not found: " + "attribute \"name\": " + nameAttribute; + } + + private static Method[] getAllGetters(Class clazz) { + Method[] methods = clazz.getDeclaredMethods(); + ArrayList list = new ArrayList(); + for (Method method : methods) { + if (method.getName().startsWith("get")) { + list.add(method); + } + } + + Method[] getters = new Method[list.size()]; + for (int i = 0; i < list.size(); ++i) { + getters[i] = (Method) list.get(i); + } + return getters; + } + + private static Document loadXml() throws ParserConfigurationException, IOException, SAXException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder; + Document document; + builder = factory.newDocumentBuilder(); + document = builder.parse(new File("src/litestruts/struts.xml")); + return document; + } + + private static String execute(Object object) + throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { + Method execute = object.getClass().getDeclaredMethod("execute"); + return (String) execute.invoke(object); + } + + private static void setParameters(Object object, Map parameters) + throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { + for (Map.Entry entry : parameters.entrySet()) { + Method setter = object.getClass().getDeclaredMethod(getSetterName(entry.getKey()), String.class); + if (setter != null) { + setter.invoke(object, entry.getValue()); + } + } + } + + private static String getSetterName(String attributeName) { + return "set" + Character.toUpperCase(attributeName.charAt(0)) + attributeName.substring(1); + } + + private static String getAttributeName(String getterOrSetterName) { + String sub = getterOrSetterName.substring(3); + return Character.toLowerCase(sub.charAt(0)) + sub.substring(1); + } +} diff --git a/group01/895457260/code/src/litestruts/View.java b/group01/895457260/code/src/litestruts/View.java new file mode 100644 index 0000000000..1eed614744 --- /dev/null +++ b/group01/895457260/code/src/litestruts/View.java @@ -0,0 +1,23 @@ +package 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/group01/895457260/code/src/litestruts/action/LoginAction.java b/group01/895457260/code/src/litestruts/action/LoginAction.java new file mode 100644 index 0000000000..9478c6931b --- /dev/null +++ b/group01/895457260/code/src/litestruts/action/LoginAction.java @@ -0,0 +1,39 @@ +package litestruts.action; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @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/group01/895457260/code/src/litestruts/exception/XmlElementNotFoundException.java b/group01/895457260/code/src/litestruts/exception/XmlElementNotFoundException.java new file mode 100644 index 0000000000..f69309f7d0 --- /dev/null +++ b/group01/895457260/code/src/litestruts/exception/XmlElementNotFoundException.java @@ -0,0 +1,14 @@ +package litestruts.exception; + +/** + * Created by Haochen on 2017/2/27. + * TODO: + */ +public class XmlElementNotFoundException extends RuntimeException { + public XmlElementNotFoundException() { + } + + public XmlElementNotFoundException(String message) { + super(message); + } +} diff --git a/group01/895457260/code/src/litestruts/struts.xml b/group01/895457260/code/src/litestruts/struts.xml new file mode 100644 index 0000000000..aad7e3f7fc --- /dev/null +++ b/group01/895457260/code/src/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/group01/895457260/code/src/litestruts/test/StrutsTest.java b/group01/895457260/code/src/litestruts/test/StrutsTest.java new file mode 100644 index 0000000000..0129a8ad31 --- /dev/null +++ b/group01/895457260/code/src/litestruts/test/StrutsTest.java @@ -0,0 +1,41 @@ +package litestruts.test; + +import litestruts.Struts; +import litestruts.View; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +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")); + } +} \ No newline at end of file diff --git a/group01/895457260/journal/week1.md b/group01/895457260/journal/week1.md new file mode 100644 index 0000000000..30e96532ab --- /dev/null +++ b/group01/895457260/journal/week1.md @@ -0,0 +1,34 @@ +# 简略描述CPU的工作方式 +  电脑,这个用法丰富而且功能异常强大的机器,也叫做计算机。计算机这个词听起来似乎并不能概括这种机器的功能,但它就是叫计算机。究其原因,无论多么复杂,多么神奇的功能,都能归结为大量的数据计算。加,减,乘,除,与,或,非……这些简单的计算经过组合,实现了令人眼花缭乱的效果。 +  那么计算机如何计算?这是第一个问题。 +  众所周知,计算机内部使用二进制,也就是0和1。而利用继电器(或晶体管)可以做成能处理二进制数的逻辑门(与/或/非门),用逻辑门可以组成加法器等各种器件(当然这些也是针对二进制的)。这些器件接受外部的二进制输入,再经过一系列变换,得到二进制的结果,最后传出外部。大学的“数字逻辑”这门课里已经花了大篇幅进行说明。 +## 如何进行计算:ALU +  首先要有一个东西,能从外部接受2个数据,然后把它们加减乘除与或非,再传出一个结果,而且还能随意控制到底做加法还是减法,还是别的。这个东西叫“ALU”,就是Arithmetic Logic Unit,算术逻辑单元。它实际上除了能接受2个数据进行计算以外,还能接受一堆别的数据(信号),用来控制做哪种计算,这些额外的输入就叫做“控制信号”。 +``` +    ALU +    输入:进行计算的数据2个 + 控制信号若干 +    输出:计算结果 +``` +  有了ALU,就可以做各种各样的计算。 +## 数据的来源和去处:内存 +  有一些数据来自你的内存条。内存实际上是存储器,可以保存二进制数,用于给ALU计算。这块存储器分为很多小格子,每个格子都有唯一编号称为“地址”,格子里能放一个数据。 +  存储器能接受至少3个输入:1地址、2数据、3读写控制端。有至少1个输出:1读出的数据。 +  举个例子:输入1表示了一个小格子,如果输入3为0表示读,那么就会输出这个格子里的数据,输入2会被忽略。如果输入3为1表示写,那么就会用输入2的数据覆盖掉格子里原有的数据,输出会被忽略。 +## ALU和内存交互:组成CPU +  要有某块逻辑电路把地址、数据、读写控制送给内存,然后获得内存里的数据传给ALU计算,再把结果送回内存。但ALU只能计算,这块逻辑电路不属于ALU,于是我们可以把ALU和这块逻辑电路放在一起,叫做CPU,Central Processing Unit,中央处理器。 +## 控制信号:指令译码 +  控制信号有很多个(每个控制信号都是1位二进制),如果把需要的控制信号一个一个并排放到内存里,那么势必导致内存空间的浪费(因为内存里一个格子能存放很多位二进制数),为此,我们另拿一块逻辑电路,让它接受一个内存格子的二进制输入,产生若干输出(就是那些控制信号)。这个逻辑电路就叫做“指令译码器”,把它放进CPU里。应该放进指令译码器的数据,称为“指令”。 +  于是整个计算过程就变成了: +``` +    1、从内存取出一个格子(是指令),放进指令译码器,得到若干组控制信号。 +    2、再从内存取出2个格子(是数据),传给ALU进行计算。 +    3、计算结果写回内存。 +``` +    (这个过程假设所有数据都来自内存,实际上并非这样) +## 区分指令和数据 +  这时又有新的问题产生了:1和2中同样是从内存取出格子,如何判断它是指令还是数据?CPU里还有一个东西叫“PC”,Program Counter,程序计数器。它能保存1个数,表示内存地址。从内存取指令,会把PC的地址送到内存,这时会控制内存的输出转向指令译码器。从内存取计算所用的数据,这个操作不使用PC的地址,那时内存的输出就不会指向指令译码器,而是直接传给ALU。 +## 更大的存储空间:硬盘 +  计算机通过某种方式不断重复上面的计算过程,只要内存里有正确排列的指令和数据,计算机就能自动工作,但是内存的容量比较小,不能存放过量的数据,于是就用到了硬盘。 +  硬盘的容量大,可以先把指令和数据放到硬盘里,需要时再由内存来取,读写硬盘和读写内存相似。 +  双击硬盘里的一个exe,exe中的指令和数据就会自动进入内存,然后由CPU做运算,结果还可以写回硬盘。 +(这篇文章忽略了寄存器、缓存、总线等构造,假设所有数据都必须来自内存) diff --git a/group01/932573198/20170227/.classpath b/group01/932573198/20170227/.classpath new file mode 100644 index 0000000000..cf68920d35 --- /dev/null +++ b/group01/932573198/20170227/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group01/932573198/20170227/.gitignore b/group01/932573198/20170227/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group01/932573198/20170227/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group01/932573198/20170227/.project b/group01/932573198/20170227/.project new file mode 100644 index 0000000000..62c904d144 --- /dev/null +++ b/group01/932573198/20170227/.project @@ -0,0 +1,17 @@ + + + 20170227 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group01/932573198/20170227/.settings/org.eclipse.jdt.core.prefs b/group01/932573198/20170227/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bb35fa0a87 --- /dev/null +++ b/group01/932573198/20170227/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/group01/932573198/20170227/src/com/coderising/array/ArrayUtil.java b/group01/932573198/20170227/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..a67618838a --- /dev/null +++ b/group01/932573198/20170227/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,225 @@ +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 l = origin.length, n; + for (int i = 0; i < l / 2; i++) { + n = origin[i]; + origin[i] = origin[l - 1 - i]; + origin[l - 1 - i] = n; + } + } + + /** + * 现在有如下的一个数组: 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 n = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) + n++; + } + int[] newArray = new int[oldArray.length - n]; + for (int i = 0, j = 0; i < oldArray.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 n = 0; // 重复的整数个数 + for (int i = 0, j = 0; i < array1.length && j < array2.length;) { + int z = array1[i] - array2[j]; + if (z < 0) + i++; + else if (z > 0) + j++; + else { + i++; + j++; + n++; + } + } + int[] newArray = new int[array1.length + array2.length - n]; + + for (int i = 0, j = 0, k = 0; i < array1.length && j < array2.length;) { + int z = array1[i] - array2[j]; + if (z < 0) { + newArray[k] = array1[i]; + k++; + i++; + } else if (z > 0) { + newArray[k] = array2[j]; + k++; + j++; + } else { + newArray[k] = array1[i]; + k++; + i++; + j++; + } + // 判断循环是否即将结束,若结束则将另外一个数组未比较的元素放入新数组 + if (i >= array1.length) { + for (int a = j; a < array2.length; a++, k++) { + newArray[k] = array2[a]; + } + } + if (j >= array2.length) { + for (int a = i; a < array1.length; a++, k++) { + newArray[k] = array1[a]; + } + } + } + return newArray; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 , + * 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + int n = 0; + for (int i = 1, j = 1, k; i < max; n++) { + k = i + j; + i = j; + j = k; + } + int[] newArray = new int[n]; + if (n > 1) { + newArray[0] = 1; + newArray[1] = 1; + } + for (int i = 2; i < n; i++) { + newArray[i] = newArray[i - 1] + newArray[i - 2]; + } + return newArray; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int n = 0; + for (int i = 2; i < max; i++) { + if (primeNumber(i)) + n++; + } + + int[] newArray = new int[n]; + for (int i = 2, j = 0; i < max; i++) { + if (primeNumber(i)) { + newArray[j++] = i; + } + } + return newArray; + } + + // 判断是否为素数 + public boolean primeNumber(int number) { + for (int i = 2; i < number; i++) { + if (number % i == 0) + return false; + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int n = 0; + for (int i = 6; i < max; i++) { + if (perfectNumber(i)) + n++; + } + + int[] newArray = new int[n]; + for (int i = 6, j = 0; i < max; i++) { + if (perfectNumber(i)) { + newArray[j++] = i; + } + } + return newArray; + } + + // 判断是否为完数 + public boolean perfectNumber(int number) { + int n = 0; + for (int i = 1; i <= number / 2; i++) { + if (number % i == 0) + n += i; + } + if (number != n) + return false; + return true; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + StringBuffer s = new StringBuffer(); + for (int i = 0; i < array.length; i++) { + s.append(String.valueOf(array[i])); + s.append("-"); + } + String str = s.substring(0, s.length() - 1); + return str; + } + +} diff --git a/group01/932573198/20170227/src/com/coderising/array/ArrayUtilTest.java b/group01/932573198/20170227/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..9f3a9ded22 --- /dev/null +++ b/group01/932573198/20170227/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,83 @@ +package com.coderising.array; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ArrayUtilTest { + + ArrayUtil util = null; + + @Before + public void setUp() throws Exception { + util = new ArrayUtil(); + } + + @Test + public void testReverseArray() { + int[] a = {1,2,3,4}; + int[] b = {4,3,2,1}; + util.reverseArray(a); + Assert.assertArrayEquals(b, a); + int[] c = {1,2,3,4,5}; + int[] d = {5,4,3,2,1}; + util.reverseArray(c); + Assert.assertArrayEquals(d, c); + } + + @Test + public void testRemoveZero() { + int[] oldArr = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] newArray = {1,3,4,5,6,6,5,4,7,6,7,5}; + int[] newArr = util.removeZero(oldArr); + Assert.assertArrayEquals(newArray, newArr); + } + + @Test + public void testMerge() { + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + int[] newArray = {3,4,5,6,7,8}; + int[] newArr = util.merge(a1, a2); + Assert.assertArrayEquals(newArray, newArr); + } + + @Test + public void testGrow() { + int[] oldArray = {2,3,6}; + int size = 3; + int[] newArray = {2,3,6,0,0,0}; + int[] newArr = util.grow(oldArray, size); + Assert.assertArrayEquals(newArray, newArr); + } + + @Test + public void testFibonacci() { + int[] arr1 = {1,1,2,3,5,8,13}; + Assert.assertArrayEquals(arr1, util.fibonacci(15)); + int[] arr2 = new int[0]; + Assert.assertArrayEquals(arr2, util.fibonacci(1)); + } + + @Test + public void testGetPrimes() { + int[] arr1 = {2,3,5,7,11,13,17,19}; + Assert.assertArrayEquals(arr1, util.getPrimes(23)); + int[] arr2 = new int[0]; + Assert.assertArrayEquals(arr2, util.getPrimes(2)); + } + + @Test + public void testGetPerfectNumbers() { + int[] arr = {6, 28, 496}; + Assert.assertArrayEquals(arr, util.getPerfectNumbers(497)); + } + + @Test + public void testJoin() { + int[] arr = {1,2,3,4,5}; + String s = "1-2-3-4-5"; + Assert.assertEquals(s, util.join(arr, "-")); + } + +} diff --git a/group01/932573198/20170227/src/com/coderising/litestruts/LoginAction.java b/group01/932573198/20170227/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..5512045571 --- /dev/null +++ b/group01/932573198/20170227/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,48 @@ +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; + } + + @Override + public String toString() { + return "LoginAction [name=" + name + ", password=" + password + ", message=" + message + "]"; + } + +} diff --git a/group01/932573198/20170227/src/com/coderising/litestruts/Struts.java b/group01/932573198/20170227/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..c69a3f6c3a --- /dev/null +++ b/group01/932573198/20170227/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,175 @@ +package com.coderising.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +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字段中。 + * + */ + + View view = new View(); + /* + * 0、读取xml文件 + */ + String path = "src/com/coderising/litestruts/struts.xml"; + Document document = null; + try { + document = new SAXReader().read(path); + } catch (DocumentException e) { + e.printStackTrace(); + } + + /* + * 1、 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , + * "password"="1234") , 那就应该调用 setName和setPassword方法 + */ + // 获取根节点 + Element root = document.getRootElement(); + // 遍历根节点下面的节点 + Iterator actionIt = root.elementIterator("action"); + while (actionIt.hasNext()) { + Element action = (Element) actionIt.next(); + if (action.attribute("name").getValue().equals(actionName)) { + String className = action.attribute("class").getValue(); + Class clazz = null; + try { + // 通过actionName找到className,并得到类 + clazz = Class.forName(className); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + Object clazzObject = null; + try { + // 创建类的一个对象 + clazzObject = clazz.newInstance(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + + // 遍历放参数的map + Set keySet = parameters.keySet(); + for (String key : keySet) { + // 拼接set方法的name + String methodName = "set" + key.substring(0, 1).toUpperCase() + key.substring(1, key.length()); + // 得到set方法对象 + Method setMethod = null; + try { + setMethod = clazz.getMethod(methodName, String.class); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } + try { + // 调用set方法,参数为parameters中对应的value + setMethod.invoke(clazzObject, parameters.get(key)); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + + /* + * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + */ + + Method executeMethod = null; + try { + // 得到exectue方法 + executeMethod = clazz.getMethod("execute"); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } + String string = null; + try { + // 调用exectue方法 + string = (String) executeMethod.invoke(clazzObject); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + /* + * 3、通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, + * 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + * 放到View对象的parameters + */ + + Map map = new HashMap<>(); + Method[] methods = clazz.getMethods(); + for (Method method : methods) { + if (method.getName().substring(0, 3).equals("get") && !method.getName().equals("getClass")) { + Object str = null; + try { + // 调用get方法 + str = method.invoke(clazzObject); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + map.put(method.getName().substring(3).toLowerCase(), str); + } + } + view.setParameters(map); + + /* + * 4、根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + */ + @SuppressWarnings("unchecked") + List elements = action.elements(); + for (Element element : elements) { + if (element.attribute("name").getValue().equals(string)) { + String jspName = element.getText(); + view.setJsp(jspName); + } + } + } + } + return view; + } + +} diff --git a/group01/932573198/20170227/src/com/coderising/litestruts/StrutsTest.java b/group01/932573198/20170227/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a45f9c968c --- /dev/null +++ b/group01/932573198/20170227/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,38 @@ +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/group01/932573198/20170227/src/com/coderising/litestruts/View.java b/group01/932573198/20170227/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..2183aca2bd --- /dev/null +++ b/group01/932573198/20170227/src/com/coderising/litestruts/View.java @@ -0,0 +1,29 @@ +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; + } + + @Override + public String toString() { + return "View [jsp=" + jsp + ", parameters=" + parameters + "]"; + } + +} diff --git a/group01/932573198/20170227/src/com/coderising/litestruts/struts.xml b/group01/932573198/20170227/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..1370971a4b --- /dev/null +++ b/group01/932573198/20170227/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/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/ArrayListTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/ArrayListTest.java index dcc45d792e..156eb638c7 100644 --- a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/ArrayListTest.java +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/ArrayListTest.java @@ -23,29 +23,29 @@ public void init() { @Test public void testAdd() { - Assert.assertEquals(arrayList.get(0), 1); - Assert.assertEquals(arrayList.get(1), 2); - Assert.assertEquals(arrayList.get(2), 3); - Assert.assertEquals(arrayList.size(), 3); + Assert.assertEquals(1, arrayList.get(0)); + Assert.assertEquals(2, arrayList.get(1)); + Assert.assertEquals(3, arrayList.get(2)); + Assert.assertEquals(3, arrayList.size()); } @Test public void testAddIndex() { arrayList.add(1, 4); arrayList.add(2, 5); - Assert.assertArrayEquals(arrayList.toArray(), new Object[]{1, 4, 5, 2, 3}); + Assert.assertArrayEquals(new Object[]{1, 4, 5, 2, 3}, arrayList.toArray()); } @Test public void testToArray() { - Assert.assertArrayEquals(arrayList.toArray(), new Object[]{1, 2, 3}); + Assert.assertArrayEquals(new Object[]{1, 2, 3}, arrayList.toArray()); } @Test public void testGet() { - Assert.assertEquals(arrayList.get(2), 3); - Assert.assertEquals(arrayList.get(0), 1); - Assert.assertEquals(arrayList.get(1), 2); + Assert.assertEquals(3, arrayList.get(2)); + Assert.assertEquals(1, arrayList.get(0)); + Assert.assertEquals(2, arrayList.get(1)); } @Test @@ -54,7 +54,7 @@ public void testRemove() { arrayList.remove(2); arrayList.add(4, 10); arrayList.add(3, 9); - Assert.assertArrayEquals(arrayList.toArray(), new Object[]{1, 4, 2, 9, 3, 10}); + Assert.assertArrayEquals(new Object[]{1, 4, 2, 9, 3, 10}, arrayList.toArray()); } @Test @@ -64,6 +64,6 @@ public void testIterator() { iterator.next(); iterator.remove(); } - Assert.assertArrayEquals(arrayList.toArray(), new Object[]{}); + Assert.assertArrayEquals(new Object[]{}, arrayList.toArray()); } } diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/BinaryTreeTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/BinaryTreeTest.java index 11fb7ad66b..ebc8a9193b 100644 --- a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/BinaryTreeTest.java +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/BinaryTreeTest.java @@ -26,13 +26,13 @@ public void init() { @Test public void testAdd() { int[] preorderDatas = binaryTree.traversal(BinaryTree.PREORDER); - Assert.assertArrayEquals(preorderDatas, new int[]{9, 4, 1, 2, 3, 5, 7, 10, 9, 17}); + Assert.assertArrayEquals(new int[]{9, 4, 1, 2, 3, 5, 7, 10, 9, 17}, preorderDatas); int[] inorderDatas = binaryTree.traversal(BinaryTree.INORDER); - Assert.assertArrayEquals(inorderDatas, new int[]{1, 2, 3, 4, 5, 7, 9, 9, 10, 17}); + Assert.assertArrayEquals(new int[]{1, 2, 3, 4, 5, 7, 9, 9, 10, 17}, inorderDatas); int[] postorderDatas = binaryTree.traversal(BinaryTree.POSTORDER); - Assert.assertArrayEquals(postorderDatas, new int[]{3, 2, 1, 7, 5, 4, 9, 17, 10, 9}); + Assert.assertArrayEquals(new int[]{3, 2, 1, 7, 5, 4, 9, 17, 10, 9}, postorderDatas); int[] hierarchicalDatas = binaryTree.traversal(BinaryTree.HIERARCHICAL); - Assert.assertArrayEquals(hierarchicalDatas, new int[]{9, 4, 10, 1, 5, 9, 17, 2, 7, 3}); + Assert.assertArrayEquals(new int[]{9, 4, 10, 1, 5, 9, 17, 2, 7, 3}, hierarchicalDatas); } @Test @@ -63,26 +63,26 @@ public void testDelete() { // 删除叶子节点 binaryTree.delete(11); int[] preOrderDatas = binaryTree.traversal(); - Assert.assertArrayEquals(preOrderDatas, new int[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82, 88}); + Assert.assertArrayEquals(new int[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82, 88}, preOrderDatas); binaryTree.delete(88); preOrderDatas = binaryTree.traversal(); - Assert.assertArrayEquals(preOrderDatas, new int[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82}); + Assert.assertArrayEquals(new int[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82}, preOrderDatas); // 删除一个子节点的节点 binaryTree.delete(70); preOrderDatas = binaryTree.traversal(); - Assert.assertArrayEquals(preOrderDatas, new int[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 60, 65, 63, 61, 90, 80, 85, 82}); + Assert.assertArrayEquals(new int[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 60, 65, 63, 61, 90, 80, 85, 82}, preOrderDatas); binaryTree.delete(80); preOrderDatas = binaryTree.traversal(); - Assert.assertArrayEquals(preOrderDatas, new int[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 60, 65, 63, 61, 90, 85, 82}); + Assert.assertArrayEquals(new int[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 60, 65, 63, 61, 90, 85, 82}, preOrderDatas); // 删除两个子节点的节点 binaryTree.delete(40); preOrderDatas = binaryTree.traversal(); - Assert.assertArrayEquals(preOrderDatas, new int[]{50, 25, 12, 14, 42, 35, 45, 44, 43, 75, 55, 60, 65, 63, 61, 90, 85, 82}); + Assert.assertArrayEquals(new int[]{50, 25, 12, 14, 42, 35, 45, 44, 43, 75, 55, 60, 65, 63, 61, 90, 85, 82}, preOrderDatas); binaryTree.delete(50); preOrderDatas = binaryTree.traversal(); - Assert.assertArrayEquals(preOrderDatas, new int[]{55, 25, 12, 14, 42, 35, 45, 44, 43, 75, 60, 65, 63, 61, 90, 85, 82}); + Assert.assertArrayEquals(new int[]{55, 25, 12, 14, 42, 35, 45, 44, 43, 75, 60, 65, 63, 61, 90, 85, 82}, preOrderDatas); } private void buildTree(int[] datas) { diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/LinkListTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/LinkListTest.java index b690c69c94..b1bfc6f1b8 100644 --- a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/LinkListTest.java +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/LinkListTest.java @@ -23,26 +23,26 @@ public void init() { @Test public void testAdd() { - Assert.assertArrayEquals(linkedList.toArray(), new Object[]{1, 2, 3}); + Assert.assertArrayEquals(new Object[]{1, 2, 3}, linkedList.toArray()); } @Test public void testAddIndex() { linkedList.add(1, 10); linkedList.add(0, 8); - Assert.assertArrayEquals(linkedList.toArray(), new Object[]{8, 1, 10, 2, 3}); + Assert.assertArrayEquals(new Object[]{8, 1, 10, 2, 3}, linkedList.toArray()); } @Test public void testAddFirst() { linkedList.addFirst(-1); - Assert.assertArrayEquals(linkedList.toArray(), new Object[]{-1, 1, 2, 3}); + Assert.assertArrayEquals(new Object[]{-1, 1, 2, 3}, linkedList.toArray()); } @Test public void testAddLast() { linkedList.addLast(99); - Assert.assertArrayEquals(linkedList.toArray(), new Object[]{1, 2, 3, 99}); + Assert.assertArrayEquals(new Object[]{1, 2, 3, 99}, linkedList.toArray()); } @Test @@ -52,21 +52,21 @@ public void testRemove() { linkedList.remove(2); linkedList.add(3, 3); linkedList.add(1, 2); - Assert.assertArrayEquals(linkedList.toArray(), new Object[]{8, 2, 10, 3, 3}); + Assert.assertArrayEquals(new Object[]{8, 2, 10, 3, 3}, linkedList.toArray()); } @Test public void testRemoveFirst() { linkedList.removeFirst(); linkedList.removeFirst(); - Assert.assertArrayEquals(linkedList.toArray(), new Object[]{3}); + Assert.assertArrayEquals(new Object[]{3}, linkedList.toArray()); } @Test public void testRemoveLast() { linkedList.removeLast(); linkedList.removeLast(); - Assert.assertArrayEquals(linkedList.toArray(), new Object[]{1}); + Assert.assertArrayEquals(new Object[]{1}, linkedList.toArray()); } @Test @@ -76,6 +76,6 @@ public void testIterator() { iterator.next(); iterator.remove(); } - Assert.assertArrayEquals(linkedList.toArray(), new Object[]{}); + Assert.assertArrayEquals(new Object[]{}, linkedList.toArray()); } } diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/QueueTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/QueueTest.java index 0035a353ec..273e1b9685 100644 --- a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/QueueTest.java +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/QueueTest.java @@ -21,13 +21,13 @@ public void init() { @Test public void testEnqueue() { - Assert.assertArrayEquals(queue.toArray(), new Object[]{1, 2, 3}); + Assert.assertArrayEquals(new Object[]{1, 2, 3}, queue.toArray()); } @Test public void testDequeue() { queue.deQueue(); queue.deQueue(); - Assert.assertArrayEquals(queue.toArray(), new Object[]{3}); + Assert.assertArrayEquals(new Object[]{3}, queue.toArray()); } } diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/StackTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/StackTest.java index 3add6bcfdf..0fe5ca06b1 100644 --- a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/StackTest.java +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/basic/StackTest.java @@ -22,25 +22,25 @@ public void init() { @Test public void testPush() { - Assert.assertArrayEquals(stack.toArray(), new Object[]{1, 2, 3}); + Assert.assertArrayEquals(new Object[]{1, 2, 3}, stack.toArray()); } @Test public void testPop() { Object element1 = stack.pop(); - Assert.assertEquals(element1, 3); + Assert.assertEquals(3, element1); Object element2 = stack.pop(); - Assert.assertEquals(element2, 2); - Assert.assertArrayEquals(stack.toArray(), new Object[]{1}); + Assert.assertEquals(2, element2); + Assert.assertArrayEquals(new Object[]{1}, stack.toArray()); } @Test public void testPeek() { Object element1 = stack.peek(); - Assert.assertEquals(element1, 3); + Assert.assertEquals(3, element1); Object element2 = stack.peek(); - Assert.assertEquals(element2, 3); - Assert.assertArrayEquals(stack.toArray(), new Object[]{1, 2, 3}); + Assert.assertEquals(3, element2); + Assert.assertArrayEquals(new Object[]{1, 2, 3}, stack.toArray()); } } diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericArrayListTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericArrayListTest.java index 8f97cbd3ea..a6376ce1dd 100644 --- a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericArrayListTest.java +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericArrayListTest.java @@ -24,34 +24,34 @@ public void init() { @Test public void testAdd() { - Assert.assertEquals(arrayList.get(0), "1"); - Assert.assertEquals(arrayList.get(1), "2"); - Assert.assertEquals(arrayList.get(2), "3"); - Assert.assertEquals(arrayList.size(), 3); + Assert.assertEquals("1", arrayList.get(0)); + Assert.assertEquals("2", arrayList.get(1)); + Assert.assertEquals("3", arrayList.get(2)); + Assert.assertEquals(3, arrayList.size()); } @Test public void testAddIndex() { arrayList.add(1, "4"); arrayList.add(2, "5"); - Assert.assertArrayEquals(arrayList.toArray(), new String[]{"1", "4", "5", "2", "3"}); + Assert.assertArrayEquals(new String[]{"1", "4", "5", "2", "3" }, arrayList.toArray()); } @Test public void testToArray() { - Assert.assertArrayEquals(arrayList.toArray(), new String[]{"1", "2", "3"}); + Assert.assertArrayEquals(new String[]{"1", "2", "3" }, arrayList.toArray()); } @Test public void testToGenericArray() { - Assert.assertArrayEquals(arrayList.toArray(new String[0]), new String[]{"1", "2", "3"}); + Assert.assertArrayEquals(new String[]{"1", "2", "3" }, arrayList.toArray(new String[0])); } @Test public void testGet() { - Assert.assertEquals(arrayList.get(2), "3"); - Assert.assertEquals(arrayList.get(0), "1"); - Assert.assertEquals(arrayList.get(1), "2"); + Assert.assertEquals("3", arrayList.get(2)); + Assert.assertEquals("1", arrayList.get(0)); + Assert.assertEquals("2", arrayList.get(1)); } @Test @@ -60,7 +60,7 @@ public void testRemove() { arrayList.remove(2); arrayList.add(4, "10"); arrayList.add(3, "9"); - Assert.assertArrayEquals(arrayList.toArray(), new String[]{"1", "4", "2", "9", "3", "10"}); + Assert.assertArrayEquals(new String[]{"1", "4", "2", "9", "3", "10" }, arrayList.toArray()); } @Test @@ -70,7 +70,7 @@ public void testIterator() { genericIterator.next(); genericIterator.remove(); } - Assert.assertArrayEquals(arrayList.toArray(), new String[]{}); + Assert.assertArrayEquals(new String[]{}, arrayList.toArray()); } } diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericBinaryTreeTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericBinaryTreeTest.java index 41adbf6706..6c39ab07e7 100644 --- a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericBinaryTreeTest.java +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericBinaryTreeTest.java @@ -13,7 +13,7 @@ public class GenericBinaryTreeTest { @Before public void init() { - String[] datas = new String[]{"9", "4", "5", "7", "1", "2", "3", "10", "17", "9"}; + String[] datas = new String[]{"9", "4", "5", "7", "1", "2", "3", "10", "17", "9" }; GenericBinaryTree binaryTree = new GenericBinaryTree<>(); for (String data : datas) { binaryTree.add(data); @@ -22,19 +22,19 @@ public void init() { @Test public void testAdd() { - String[] datas = new String[]{"9", "4", "5", "7", "1", "2", "3", "10", "17", "9"}; + String[] datas = new String[]{"9", "4", "5", "7", "1", "2", "3", "10", "17", "9" }; GenericBinaryTree binaryTree = new GenericBinaryTree<>(); for (String data : datas) { binaryTree.add(data); } String[] preorderDatas = binaryTree.traversal(GenericBinaryTree.PREORDER, new String[0]); - Assert.assertArrayEquals(preorderDatas, new String[]{"9", "4", "1", "2", "10", "17", "3", "5", "7", "9" }); + Assert.assertArrayEquals(new String[]{"9", "4", "1", "2", "10", "17", "3", "5", "7", "9" }, preorderDatas); String[] inorderDatas = binaryTree.traversal(GenericBinaryTree.INORDER, new String[0]); - Assert.assertArrayEquals(inorderDatas, new String[]{"1", "10", "17", "2", "3", "4", "5", "7", "9", "9" }); + Assert.assertArrayEquals(new String[]{"1", "10", "17", "2", "3", "4", "5", "7", "9", "9" }, inorderDatas); String[] postorderDatas = binaryTree.traversal(GenericBinaryTree.POSTORDER, new String[0]); - Assert.assertArrayEquals(postorderDatas, new String[]{"17", "10", "3", "2", "1", "7", "5", "4", "9", "9" }); + Assert.assertArrayEquals(new String[]{"17", "10", "3", "2", "1", "7", "5", "4", "9", "9" }, postorderDatas); String[] hierarchicalDatas = binaryTree.traversal(GenericBinaryTree.HIERARCHICAL, new String[0]); - Assert.assertArrayEquals(hierarchicalDatas, new String[]{"9", "4", "9", "1", "5", "2", "7", "10", "3", "17" }); + Assert.assertArrayEquals(new String[]{"9", "4", "9", "1", "5", "2", "7", "10", "3", "17" }, hierarchicalDatas); } @Test @@ -43,26 +43,26 @@ public void testDelete() { // 删除叶子节点 binaryTree.delete(11); Object[] preOrderDatas = binaryTree.traversal(); - Assert.assertArrayEquals(preOrderDatas, new Object[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82, 88}); + Assert.assertArrayEquals(new Object[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82, 88}, preOrderDatas); binaryTree.delete(88); preOrderDatas = binaryTree.traversal(); - Assert.assertArrayEquals(preOrderDatas, new Object[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82}); + Assert.assertArrayEquals(new Object[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 70, 60, 65, 63, 61, 90, 80, 85, 82}, preOrderDatas); // 删除一个子节点的节点 binaryTree.delete(70); preOrderDatas = binaryTree.traversal(); - Assert.assertArrayEquals(preOrderDatas, new Object[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 60, 65, 63, 61, 90, 80, 85, 82}); + Assert.assertArrayEquals(new Object[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 60, 65, 63, 61, 90, 80, 85, 82}, preOrderDatas); binaryTree.delete(80); preOrderDatas = binaryTree.traversal(); - Assert.assertArrayEquals(preOrderDatas, new Object[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 60, 65, 63, 61, 90, 85, 82}); + Assert.assertArrayEquals(new Object[]{50, 25, 12, 14, 40, 35, 45, 44, 43, 42, 75, 55, 60, 65, 63, 61, 90, 85, 82}, preOrderDatas); // 删除两个子节点的节点 binaryTree.delete(40); preOrderDatas = binaryTree.traversal(); - Assert.assertArrayEquals(preOrderDatas, new Object[]{50, 25, 12, 14, 42, 35, 45, 44, 43, 75, 55, 60, 65, 63, 61, 90, 85, 82}); + Assert.assertArrayEquals(new Object[]{50, 25, 12, 14, 42, 35, 45, 44, 43, 75, 55, 60, 65, 63, 61, 90, 85, 82}, preOrderDatas); binaryTree.delete(50); preOrderDatas = binaryTree.traversal(); - Assert.assertArrayEquals(preOrderDatas, new Object[]{55, 25, 12, 14, 42, 35, 45, 44, 43, 75, 60, 65, 63, 61, 90, 85, 82}); + Assert.assertArrayEquals(new Object[]{55, 25, 12, 14, 42, 35, 45, 44, 43, 75, 60, 65, 63, 61, 90, 85, 82}, preOrderDatas); } private GenericBinaryTree buildTree(int[] datas) { diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericLinkedListTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericLinkedListTest.java index 513119fa6e..50aaf13527 100644 --- a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericLinkedListTest.java +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericLinkedListTest.java @@ -22,26 +22,26 @@ public void init() { @Test public void testAdd() { - Assert.assertArrayEquals(linkedList.toArray(), new String[]{"1", "2", "3"}); + Assert.assertArrayEquals(new String[]{"1", "2", "3" }, linkedList.toArray()); } @Test public void testAddIndex() { linkedList.add(1, "10"); linkedList.add(0, "8"); - Assert.assertArrayEquals(linkedList.toArray(), new String[]{"8", "1", "10", "2", "3"}); + Assert.assertArrayEquals(new String[]{"8", "1", "10", "2", "3" }, linkedList.toArray()); } @Test public void testAddFirst() { linkedList.addFirst("-1"); - Assert.assertArrayEquals(linkedList.toArray(), new String[]{"-1", "1", "2", "3"}); + Assert.assertArrayEquals(new String[]{"-1", "1", "2", "3" }, linkedList.toArray()); } @Test public void testAddLast() { linkedList.addLast("99"); - Assert.assertArrayEquals(linkedList.toArray(), new String[]{"1", "2", "3", "99"}); + Assert.assertArrayEquals(new String[]{"1", "2", "3", "99" }, linkedList.toArray()); } @Test @@ -51,31 +51,31 @@ public void testRemove() { linkedList.remove(2); linkedList.add(3, "3"); linkedList.add(1, "2"); - Assert.assertArrayEquals(linkedList.toArray(), new String[]{"8", "2", "10", "3", "3"}); + Assert.assertArrayEquals(new String[]{"8", "2", "10", "3", "3" }, linkedList.toArray()); } @Test public void testRemoveFirst() { linkedList.removeFirst(); linkedList.removeFirst(); - Assert.assertArrayEquals(linkedList.toArray(), new String[]{"3"}); + Assert.assertArrayEquals(new String[]{"3" }, linkedList.toArray()); } @Test public void testRemoveLast() { linkedList.removeLast(); linkedList.removeLast(); - Assert.assertArrayEquals(linkedList.toArray(), new String[]{"1"}); + Assert.assertArrayEquals(new String[]{"1" }, linkedList.toArray()); } @Test public void testToArray() { - Assert.assertArrayEquals(linkedList.toArray(), new String[]{"1", "2", "3"}); + Assert.assertArrayEquals(new String[]{"1", "2", "3" }, linkedList.toArray()); } @Test public void testToGenericArray() { - Assert.assertArrayEquals(linkedList.toArray(new String[0]), new String[]{"1", "2", "3"}); + Assert.assertArrayEquals(new String[]{"1", "2", "3" }, linkedList.toArray(new String[0])); } @Test @@ -85,6 +85,6 @@ public void testIterator() { genericIterator.next(); genericIterator.remove(); } - Assert.assertArrayEquals(linkedList.toArray(), new String[]{}); + Assert.assertArrayEquals(new String[]{}, linkedList.toArray()); } } diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericQueueTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericQueueTest.java index 6b33a4b3e0..234cff5a02 100644 --- a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericQueueTest.java +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericQueueTest.java @@ -21,13 +21,13 @@ public void init() { @Test public void testEnqueue() { - Assert.assertArrayEquals(queue.toArray(), new String[]{"1", "2", "3"}); + Assert.assertArrayEquals(new String[]{"1", "2", "3" }, queue.toArray()); } @Test public void testDequeue() { queue.deQueue(); queue.deQueue(); - Assert.assertArrayEquals(queue.toArray(), new String[]{"3"}); + Assert.assertArrayEquals(new String[]{"3" }, queue.toArray()); } } diff --git a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericStackTest.java b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericStackTest.java index 0b4b587704..58ca230766 100644 --- a/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericStackTest.java +++ b/group01/954958168/class01/BasicDataStructure/src/test/java/com/aaront/execrise/generic/GenericStackTest.java @@ -22,25 +22,25 @@ public void init() { @Test public void testPush() { - Assert.assertArrayEquals(stack.toArray(), new String[]{"1", "2", "3"}); + Assert.assertArrayEquals(new String[]{"1", "2", "3" }, stack.toArray()); } @Test public void testPop() { String element1 = stack.pop(); - Assert.assertEquals(element1, "3"); + Assert.assertEquals("3", element1); String element2 = stack.pop(); - Assert.assertEquals(element2, "2"); - Assert.assertArrayEquals(stack.toArray(), new String[]{"1"}); + Assert.assertEquals("2", element2); + Assert.assertArrayEquals(new String[]{"1" }, stack.toArray()); } @Test public void testPeek() { String element1 = stack.peek(); - Assert.assertEquals(element1, "3"); + Assert.assertEquals("3", element1); String element2 = stack.peek(); - Assert.assertEquals(element2, "3"); - Assert.assertArrayEquals(stack.toArray(), new String[]{"1", "2", "3"}); + Assert.assertEquals("3", element2); + Assert.assertArrayEquals(new String[]{"1", "2", "3" }, stack.toArray()); } } diff --git a/group01/954958168/class02/ArrayUtil/pom.xml b/group01/954958168/class02/ArrayUtil/pom.xml new file mode 100644 index 0000000000..4ef1e0c611 --- /dev/null +++ b/group01/954958168/class02/ArrayUtil/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + com.aaront.exercise + array-util + 1.0.0-SNAPSHOT + + + UTF-8 + 1.8 + + + + + junit + junit + 4.12 + + + + \ No newline at end of file diff --git a/group01/954958168/class02/ArrayUtil/src/main/java/com/aaront/exercise/ArrayUtil.java b/group01/954958168/class02/ArrayUtil/src/main/java/com/aaront/exercise/ArrayUtil.java new file mode 100644 index 0000000000..8e3112cd06 --- /dev/null +++ b/group01/954958168/class02/ArrayUtil/src/main/java/com/aaront/exercise/ArrayUtil.java @@ -0,0 +1,240 @@ +package com.aaront.exercise; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + if (origin == null || origin.length == 0) return; + int head = 0; + int tail = origin.length - 1; + while (head < tail) { + origin[head] = origin[head] ^ origin[tail]; + origin[tail] = origin[head] ^ origin[tail]; + origin[head] = origin[head] ^ origin[tail]; + head++; + tail--; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + if (oldArray == null) return new int[0]; + int size = oldArray.length; + for (int i = 0; i < size; i++) { + if (oldArray[i] != 0) continue; + int j = i + 1; + for (; j < size; j++) { + if (oldArray[j] != 0) break; + } + size -= (j - i); + move(oldArray, i, j - i, size); + } + + int[] newArray = new int[size]; + for (int i = 0; i < size; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + private void move(int[] array, int start, int moveLen, int size) { + for (int i = start; i < size; i++) { + array[i] = array[i + moveLen]; + } + } + + /** + * 给定两个已经排序好的整形数组, 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 len1 = array1.length; + int len2 = array2.length; + int[] mergeArray = new int[len1 + len2]; + int i = 0; + int j = 0; + int k = 0; + for (; i < len1 && j < len2; ) { + if (array1[i] < array2[j]) { + mergeArray[k] = array1[i]; + i++; + } else if (array1[i] > array2[j]) { + mergeArray[k] = array2[j]; + j++; + } else { + mergeArray[k] = array1[i]; + i++; + j++; + } + k++; + } + while (i < len1) { + mergeArray[k++] = array1[i++]; + } + while (j < len2) { + mergeArray[k++] = array2[j++]; + } + return resize(mergeArray, k); + } + + /** + * 把一个已经存满数据的数组 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) { + if (oldArray == null) return new int[0]; + int[] newArray = new int[oldArray.length + size]; + int index = 0; + for (int len = oldArray.length; index < len; index++) { + newArray[index] = oldArray[index]; + } + for (int len = newArray.length; index < len; index++) { + newArray[index] = 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) { + if (max <= 1) return new int[0]; + int[] elements = new int[10]; + elements[0] = 1; + elements[1] = 1; + int index = 1; + int sum; + while ((sum = elements[index] + elements[index - 1]) < max) { + if (index + 1 >= elements.length) elements = dilatation(elements); + elements[++index] = sum; + } + if (index < elements.length - 1) { + elements = resize(elements, index + 1); + } + return elements; + } + + private int[] dilatation(int[] origin) { + int len = origin.length; + int[] newArray = new int[len * 2]; + for (int i = 0; i < len; i++) { + newArray[i] = origin[i]; + } + return newArray; + } + + private int[] resize(int[] origin, int size) { + int[] newArray = new int[size]; + for (int i = 0; i < size; i++) { + newArray[i] = origin[i]; + } + return newArray; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int[] primes = new int[max]; + int index = 0; + for (int i = 2; i < max; i++) { + if (isPrimes(i)) { + primes[index++] = i; + } + } + return resize(primes, index); + } + + private boolean isPrimes(int num) { + for (int i = 2; i < num; i++) { + if (num % i == 0) return false; + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int[] perfectNumbers = new int[max]; + int index = 0; + for (int i = 1; i < max; i++) { + int[] factors = getFactors(i); + int sum = 0; + for (int factor : factors) { + sum += factor; + } + if (sum == i) perfectNumbers[index++] = i; + } + return resize(perfectNumbers, index); + } + + private int[] getFactors(int num) { + int[] factors = new int[num]; + int index = 0; + for (int i = 1; i < num; i++) { + if (num % i == 0) { + factors[index++] = i; + } + } + return resize(factors, index); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + if (array == null) return ""; + StringBuilder sb = new StringBuilder(""); + for(int i = 0, len = array.length;i + + 4.0.0 + + com.aaront.exercise + lite-struts + 1.0.0-SNAPSHOT + + + UTF-8 + 1.8 + + + + + junit + junit + 4.12 + + + commons-digester + commons-digester + 2.1 + + + org.apache.commons + commons-lang3 + 3.5 + + + + \ No newline at end of file diff --git a/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/LiteStruts.java b/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/LiteStruts.java new file mode 100644 index 0000000000..61bb4c1107 --- /dev/null +++ b/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/LiteStruts.java @@ -0,0 +1,122 @@ +package com.aaront.exercise; + +import com.aaront.exercise.pojo.Action; +import com.aaront.exercise.pojo.Result; +import com.aaront.exercise.pojo.Structs; +import org.apache.commons.digester.Digester; +import org.apache.commons.lang3.StringUtils; +import org.xml.sax.SAXException; + +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URL; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + + +public class LiteStruts { + + private DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); + + public View runAction(String actionName, Map parameters) { + + /* + + 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字段中。 + + */ + URL resource = readProfiles("struts.xml"); + Structs structs = parseXML(resource); + Action action = structs.getActions().stream().filter(a -> StringUtils.equals(a.getName(), actionName)).findAny().orElseThrow(() -> new RuntimeException("Action不存在")); + + try { + Class actionClass = Class.forName(action.getClazz()); + Object instance = actionClass.newInstance(); + parameters.entrySet().stream().filter(entry -> StringUtils.isNotBlank(entry.getKey())).forEach(entry -> { + String propertyName = entry.getKey(); + String propertyValue = entry.getValue(); + String setterMethodName = StringUtils.prependIfMissing(StringUtils.capitalize(propertyName), "set"); + try { + Class propertyType = actionClass.getDeclaredField(propertyName).getType(); + Method method = actionClass.getMethod(setterMethodName, propertyType); + method.invoke(instance, propertyValue); + } catch (NoSuchFieldException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + }); + Method execute = actionClass.getMethod("execute"); + + Object methodReturnValue = execute.invoke(instance); + String viewPath = action.getResults().stream().filter(result -> result.getName().equals(methodReturnValue)).map(Result::getValue).findAny().orElseThrow(() -> new RuntimeException("没有对应的view")); + Map map = new HashMap(); + Arrays.stream(actionClass.getDeclaredMethods()).filter(method -> method.getName().startsWith("get")).forEach(method -> { + try { + Object result = method.invoke(instance); + String propertyName = StringUtils.uncapitalize(method.getName().substring(3)); + map.put(propertyName, result); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + }); + View view = new View(); + view.setJsp(viewPath); + view.setParameters(map); + return view; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + throw new RuntimeException("处理Action的类不存在"); + } catch (InstantiationException | IllegalAccessException e) { + // TODO: 17/2/27 这里可以优化成遍历所有的构造函数之后再抛错 + e.printStackTrace(); + throw new RuntimeException("处理Action的类没有默认的构造函数"); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + throw new RuntimeException("处理Action的类没有execute函数"); + } catch (InvocationTargetException e) { + e.printStackTrace(); + throw new RuntimeException("调用execute方法出错"); + } + } + + private URL readProfiles(String filePath) { + ClassLoader classLoader = LiteStruts.class.getClassLoader(); + URL resource = classLoader.getResource(filePath); + if (resource == null) throw new RuntimeException("文件不存在"); + return resource; + } + + private Structs parseXML(URL resource) { + Digester digester = new Digester(); + digester.addObjectCreate("struts", Structs.class); + digester.addObjectCreate("struts/action", Action.class); + digester.addSetProperties("struts/action", new String[]{"name", "class" }, new String[]{"name", "clazz" }); + digester.addSetNext("struts/action", "addAction"); + digester.addObjectCreate("struts/action/result", Result.class); + digester.addSetProperties("struts/action/result"); + digester.addBeanPropertySetter("struts/action/result", "value"); + digester.addSetNext("struts/action/result", "addResult"); + try { + return (Structs) digester.parse(resource); + } catch (IOException | SAXException e) { + e.printStackTrace(); + throw new RuntimeException("解析XML文件出错"); + } + } +} diff --git a/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/LoginAction.java b/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/LoginAction.java new file mode 100644 index 0000000000..050df9b79c --- /dev/null +++ b/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/LoginAction.java @@ -0,0 +1,39 @@ +package com.aaront.exercise; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + */ +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/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/View.java b/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/View.java new file mode 100644 index 0000000000..f23425e5bd --- /dev/null +++ b/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/View.java @@ -0,0 +1,26 @@ +package com.aaront.exercise; + +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/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/pojo/Action.java b/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/pojo/Action.java new file mode 100644 index 0000000000..cc945a6614 --- /dev/null +++ b/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/pojo/Action.java @@ -0,0 +1,38 @@ +package com.aaront.exercise.pojo; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author tonyhui + * @since 17/2/27 + */ +public class Action { + private String name; + private String clazz; + private List results = new ArrayList<>(); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClazz() { + return clazz; + } + + public void setClazz(String clazz) { + this.clazz = clazz; + } + + public void addResult(Result result) { + this.results.add(result); + } + + public List getResults() { + return results; + } +} diff --git a/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/pojo/Result.java b/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/pojo/Result.java new file mode 100644 index 0000000000..bdc78b0037 --- /dev/null +++ b/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/pojo/Result.java @@ -0,0 +1,26 @@ +package com.aaront.exercise.pojo; + +/** + * @author tonyhui + * @since 17/2/27 + */ +public class Result { + private String name; + private String value; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/pojo/Structs.java b/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/pojo/Structs.java new file mode 100644 index 0000000000..253a22d3e7 --- /dev/null +++ b/group01/954958168/class02/LiteStruts/src/main/java/com/aaront/exercise/pojo/Structs.java @@ -0,0 +1,20 @@ +package com.aaront.exercise.pojo; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author tonyhui + * @since 17/2/27 + */ +public class Structs { + private List actions = new ArrayList<>(); + + public void addAction(Action action) { + actions.add(action); + } + + public List getActions() { + return actions; + } +} diff --git a/group01/954958168/class02/LiteStruts/src/main/resources/struts.xml b/group01/954958168/class02/LiteStruts/src/main/resources/struts.xml new file mode 100644 index 0000000000..dcc7f41b61 --- /dev/null +++ b/group01/954958168/class02/LiteStruts/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group01/954958168/class02/LiteStruts/src/test/java/com/aaront/exercise/StrutsTest.java b/group01/954958168/class02/LiteStruts/src/test/java/com/aaront/exercise/StrutsTest.java new file mode 100644 index 0000000000..12318b16c8 --- /dev/null +++ b/group01/954958168/class02/LiteStruts/src/test/java/com/aaront/exercise/StrutsTest.java @@ -0,0 +1,48 @@ +package com.aaront.exercise; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + +public class StrutsTest { + + private LiteStruts liteStruts; + + @Before + public void setUp() { + liteStruts = new LiteStruts(); + } + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + + View view = liteStruts.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 = liteStruts.runAction(actionName, params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +}