diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/array/ArrayUtil.java b/group22/1335499238/coding2017/src/main/java/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..a9e5e8bde0 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/array/ArrayUtil.java @@ -0,0 +1,239 @@ +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(checkArrayIsNull(origin)){ + return; + } + int size = origin.length; + for (int i = 0; i < size/2; i++) { + int swap = origin[i]; + origin[i] = origin[size - 1 - i]; + origin[size - 1 - i] = swap; + } + } + + /** + * 现在有如下的一个数组: 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(checkArrayIsNull(oldArray)){ + return null; + } + int[] swap = new int [oldArray.length]; + int size = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i] == 0){ + continue; + } + swap[size] = oldArray[i]; + ++size; + + } + int[] newArray = new int[size]; + System.arraycopy(swap, 0, newArray, 0, 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 int[] merge(int[] array1, int[] array2){ + if(checkArrayIsNull(array1) && checkArrayIsNull(array2)){ + return null; + } + int[] swap = new int[array1.length + array2.length]; + int index1 = 0,index2 = 0,size = 0; + while(index1 < array1.length && index2 < array2.length){ + if(array1[index1] == array2[index2]){ + swap[size++] = array1[index1]; + ++index1; + ++index2; + }else if(array1[index1] < array2[index2]){ + if(size > 0 && swap[size-1] == array1[index1]){ + ++index1; + continue; + } + swap[size++] = array1[index1++]; + }else{ + if(size > 0 && swap[size-1] == array2[index2]){ + ++index2; + continue; + } + swap[size++] = array2[index2++]; + } + + } + while(index1 < array1.length){ + swap[size++] = array1[index1]; + ++index1; + } + while(index2 < array2.length){ + swap[size++] = array2[index2]; + ++index2; + } + int[] newArray = new int [size]; + System.arraycopy(swap, 0, newArray, 0, 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 int[] grow(int[] oldArray, int size){ + if(checkArrayIsNull(oldArray)){ + return null; + } + int length = oldArray.length; + int[] newArray = new int [length + size]; + System.arraycopy(oldArray, 0, newArray, 0, 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 <= 0){ + return null; + } + if(max == 1){ + return new int[0]; + } + int array[] = null; + if(max < 20 ){ + array = new int [max]; + }else{ + array =new int [max/2]; + } + array[0] = array[1] = 1; + int index = 1; + while(array[index-1] + array[index] < max){ + array[index+1] = array[index-1] + array[index]; + ++index; + } + int[] newArray = new int [index+1]; + System.arraycopy(array, 0, newArray, 0, index+1); + return newArray; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max < 2){ + return null; + } + int init = 2; + int size = 0; + int array[] = new int[max]; + boolean flag = true; + while(init < max){ + flag = true; + for (int i = 2; i < init; i++) { + if(init % i == 0){ + flag = false; + break; + } + } + if(flag){ + array[size++] = init; + } + ++init; + } + int[] newArray = new int [size]; + System.arraycopy(array, 0, newArray, 0, size); + return newArray; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if(max < 6){ + return null; + } + int array[] = new int[max]; + int init = 6; + int size = 0,sum = 0; + while(init < max){ + sum = 0; + for (int i = 1; i <= init/2; i++) { + if(init % i == 0){ + sum += i; + } + } + if(sum == init){ + array[size++] = init; + } + ++init; + } + int[] newArray = new int [size]; + System.arraycopy(array, 0, newArray, 0, size); + return newArray; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + if(checkArrayIsNull(array) || seperator == null){ + return null; + } + StringBuilder stringBuilder = new StringBuilder(); + int size = array.length; + for (int i = 0; i < size; i++) { + if(i != size - 1){ + stringBuilder.append(array[i]).append(seperator); + continue; + } + stringBuilder.append(array[i]); + } + return stringBuilder.toString(); + } + + private boolean checkArrayIsNull(int[] array){ + if(array == null){ + return true; + } + return false; + } + +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/array/ArrayUtilTest.java b/group22/1335499238/coding2017/src/main/java/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..d2f4d2b668 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,84 @@ +package com.coderising.array; + +import java.util.Arrays; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ArrayUtilTest { + + public static ArrayUtil arrayUtil; + + @BeforeClass + public static void arrayUtil() throws Exception { + arrayUtil = new ArrayUtil(); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Test + public void reverseArrayTest() { + int[] array = {1,2,3,4,5,6,7}; + int[] swapAfter = {7,6,5,4,3,2,1}; + arrayUtil.reverseArray(array); + Assert.assertEquals(Arrays.toString(swapAfter), Arrays.toString(array)); + + } + + @Test + public void removeZeroTest(){ + int[] array = {0,1,0,0,2,3,4,5,6,0,0,7,0}; + int[] array2 = {1,2,3,4,5,6,7}; + int[] removeZero = arrayUtil.removeZero(array); + Assert.assertEquals(Arrays.toString(array2), Arrays.toString(removeZero)); + } + + @Test + public void mergeTest(){ + int[] array1 = {1,2,3,4,5}; + int[] array2 = {4,5,7,8,13,15}; + int[] array3 = {1,2,3,4,5,7,8,13,15}; + int[] merge = arrayUtil.merge(array1, array2); + Assert.assertEquals(Arrays.toString(array3), Arrays.toString(merge)); + } + + @Test + public void growTest(){ + int[] array = {1,2,3,4,5,6,7}; + int[] array2 = {1,2,3,4,5,6,7,0,0,0}; + int[] grow = arrayUtil.grow( array, 3); + Assert.assertEquals(Arrays.toString(array2), Arrays.toString(grow)); + } + + @Test + public void fibonacciTest(){ + int[] fibonacci = arrayUtil.fibonacci(1000); + int[] array2 = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987}; + Assert.assertEquals(Arrays.toString(array2), Arrays.toString(fibonacci)); + } + + @Test + public void getPrimesTest(){ + int[] primes = arrayUtil.getPrimes(20); + int[] array2 = {2, 3, 5, 7, 11, 13, 17, 19}; + Assert.assertEquals(Arrays.toString(array2), Arrays.toString(primes)); + } + + @Test + public void getPerfectNumbersTest(){ + int[] perfectNumbers = arrayUtil.getPerfectNumbers(1000); + int[] array2 = {6, 28, 496}; + Assert.assertEquals(Arrays.toString(array2), Arrays.toString(perfectNumbers)); + } + + @Test + public void joinTest(){ + int[] join = {2,4,5,6}; + String join2 = arrayUtil.join(join, "-"); + Assert.assertEquals("2-4-5-6", join2); + } +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/LoginAction.java b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + } + public void setPassword(String password){ + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/Struts.java b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..d231ca1634 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,154 @@ +package com.coderising.litestruts; + +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +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 = null; + SAXReader reader = new SAXReader(); + InputStream resourceAsStream = Struts.class.getResourceAsStream("struts.xml"); + try { + Document document = reader.read(resourceAsStream); + Element rootElement = document.getRootElement(); + + Element element = findElement(rootElement, actionName); + if(element == null){ + throw new RuntimeException("指定节点不存在"); + } + //子节点数据信息 + Map elementData = getElementData(element); + Class forName = Class.forName(element.attribute("class").getValue()); + Object classInstance = forName.newInstance(); + PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(forName).getPropertyDescriptors(); + for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { + String name = propertyDescriptor.getName(); + Method readMethod = propertyDescriptor.getWriteMethod(); + if(parameters != null && parameters.size() > 0 && parameters.containsKey(name) && readMethod != null){ + readMethod.setAccessible(true); + readMethod.invoke(classInstance, parameters.get(name)); + } + } + Method method = forName.getDeclaredMethod("execute"); + method.setAccessible(true); + Object o = method.invoke(classInstance); + Map viewMap = new HashMap(); + for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { + String name = propertyDescriptor.getName(); + Method readMethod = propertyDescriptor.getReadMethod(); + if(readMethod != null && !"class".equals(name)){ + viewMap.put(name, readMethod.invoke(classInstance)); + } + } + view = new View(); + view.setParameters(viewMap); + if(elementData != null && elementData.size() > 0 && elementData.containsKey(o)){ + view.setJsp(elementData.get(o)); + }else{ + view.setJsp(null); + } + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IntrospectionException e) { + e.printStackTrace(); + } + return view; + } + + /** + * 根据名称获取指定节点 + * @param rootElement 根节点 + * @param actionName action名称 + * @return + */ + private static Element findElement(Element rootElement,String actionName){ + @SuppressWarnings("unchecked") + List elements = rootElement.elements("action"); + for (Element element : elements) { + Attribute name = element.attribute("name"); + if(name == null){ + return null; + } + String value = name.getValue(); + if(value == null || !value.equals(actionName)){ + continue; + } + Attribute actionClass = element.attribute("class"); + if(actionClass == null || actionClass.getValue() == null){ + return null; + } + return element; + } + return null; + } + + /** + * 获取action下的result节点信息 + * @param element action节点 + * @return + */ + private static Map getElementData(Element element){ + Map map = null; + @SuppressWarnings("unchecked") + List elements = element.elements("result"); + for (Element elementSub : elements) { + if(map == null){ + map = new HashMap(); + } + Attribute attribute = elementSub.attribute("name"); + if(attribute!=null){ + map.put(attribute.getValue(), elementSub.getTextTrim()); + } + } + return map; + } +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/StrutsTest.java b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/View.java b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/struts.xml b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..4c6eeabbd4 --- /dev/null +++ b/group22/1335499238/coding2017/src/main/java/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file