diff --git a/group18/1159828430/20170226/.classpath b/group18/1159828430/20170226/.classpath new file mode 100644 index 0000000000..2dcf9f7eab --- /dev/null +++ b/group18/1159828430/20170226/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/group18/1159828430/20170226/.gitignore b/group18/1159828430/20170226/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group18/1159828430/20170226/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group18/1159828430/20170226/.project b/group18/1159828430/20170226/.project new file mode 100644 index 0000000000..30be1e8fb2 --- /dev/null +++ b/group18/1159828430/20170226/.project @@ -0,0 +1,17 @@ + + + 20170226 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group18/1159828430/20170226/src/com/coding/array/ArrayUtil.java b/group18/1159828430/20170226/src/com/coding/array/ArrayUtil.java new file mode 100644 index 0000000000..3440cbbb71 --- /dev/null +++ b/group18/1159828430/20170226/src/com/coding/array/ArrayUtil.java @@ -0,0 +1,232 @@ +package com.coding.array; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +/** + * @author Scholar + * @Time:2017年2月27日 下午8:46:07 + * @version 1.0 + */ +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 + * @return + */ + public static int[] 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; + } + return origin; + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + + public static int[] removeZero(int[] oldArray){ + //正常方式 + /*List list = new ArrayList(); + for (int i : oldArray) + if (i != 0) + list.add(i); + + int[] newArray = new int[list.size()]; + for (int i = 0; i < newArray.length; i++) { + newArray[i] = list.get(i); + }*/ + //jdk1.8 + IntStream intStream = Arrays.stream(oldArray); + int[] newArray = intStream.filter(i -> i != 0).toArray(); + 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= new int[array1.length + array2.length]; + System.arraycopy(array1, 0, newArray, 0, array1.length); + System.arraycopy(array2, 0, newArray, array1.length, array2.length); + + //Arrays.sort(newArray);//快速排序 + + //冒泡排序 + /*for(int i=0;i newArray[j]){ + int temp = newArray[i]; + newArray[i] = newArray[j]; + newArray[j] = temp; + } + } + }*/ + + //选择排序 + /*for (int i = 0; i < newArray.length - 1; i++) { + int min = i; + for (int j = i + 1; j < newArray.length; j++) { + if (newArray[min] > newArray[j]) { + min = j; + } + } + if (min != i) { + int temp = newArray[i]; + newArray[i] = newArray[min]; + newArray[min] = temp; + } + }*/ + + //插入排序 + for (int i = 1; i < newArray.length; i++) { + for (int j = i; j > 0; j--) { + if (newArray[j] < newArray[j - 1]) { + int temp = newArray[j - 1]; + newArray[j - 1] = newArray[j]; + newArray[j] = temp; + } else + break; + } + } + 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){ + oldArray = Arrays.copyOf(oldArray, oldArray.length + size); + return oldArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + @SuppressWarnings({ "unchecked", "rawtypes" }) + public static int[] fibonacci(int max){ + List list = new ArrayList(); + if (max == 1) { + int[] newArr = null; + return newArr; + } else { + for (int i = 0; i <= max; i++) { + int value = fibonacciSequence(i); + if (value < max) { + list.add(value); + } else { + break; + } + } + } + int[] newArray = list.stream().mapToInt(i -> i).toArray(); + return newArray; + } + + private static int fibonacciSequence(int n){ + if(n < 2){ + return 1; + }else{ + return fibonacciSequence(n-1) + fibonacciSequence(n-2); + } + } + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + @SuppressWarnings("unused") + public static int[] getPrimes(int max){ + List list = new ArrayList(); + for (int i = 2; i < max; i++) { + list.add(i); + for (int j = 2; j <= Math.sqrt(i); j++) { + if (i % j == 0){ + Integer temp = i; + list.remove(temp); + break; + } + } + } + + int[] newArray = list.stream().mapToInt(i -> i).toArray(); + return newArray; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + List list = new ArrayList(); + int[] newArray = null; + if (!(max < 6)) {//第一个完数为6 + for (int i = 6; i < max; i++) { + int sum = 0; + for (int j = 1; j <= i/2; j++) { + if (i % j == 0) { + sum += j; + } + } + if (sum == i) { + list.add(i); + } + System.out.println(i); + } + newArray = list.stream().mapToInt(i -> i).toArray(); + } + return newArray; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + StringBuilder str = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + str.append(array[i]); + if(i != array.length - 1){ + str.append(seperator); + } + } + return str.toString(); + } + +} + diff --git a/group18/1159828430/20170226/src/com/coding/array/ArrayUtilTest.java b/group18/1159828430/20170226/src/com/coding/array/ArrayUtilTest.java new file mode 100644 index 0000000000..a07658d0ca --- /dev/null +++ b/group18/1159828430/20170226/src/com/coding/array/ArrayUtilTest.java @@ -0,0 +1,80 @@ +package com.coding.array; + +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +import org.junit.Test; + +/** + * @author Scholar + * @Time:2017年3月4日 上午9:12:27 + * @version 1.0 + */ +public class ArrayUtilTest { + + @Test + public void testReverseArray(){ + int[] oldArr = {7, 9 , 30, 3}; + int[] newArr = ArrayUtil.reverseArray(oldArr); + int[] resultArr = {3, 30 , 9, 7}; + assertArrayEquals(resultArr, newArr); + + } + + @Test + public void testRemoveZero() { + int[] oldArr = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] newArr = ArrayUtil.removeZero(oldArr); + int[] resultArr = {1,3,4,5,6,6,5,4,7,6,7,5}; + assertArrayEquals(resultArr, newArr); + } + + @Test + public void testMerge() { + int[] a1 = {3,5,7,8}; + int[] a2 = {4, 5, 6,7}; + int[] newArr = ArrayUtil.merge(a1, a2); + Arrays.stream(newArr).forEach(i -> System.out.println(i)); + } + + @Test + public void testGrow() { + int[] oldArr = {2,3,6}; + int[] newArr = ArrayUtil.grow(oldArr, 3); + int[] resultArr = {2,3,6,0,0,0}; + assertArrayEquals(resultArr, newArr); + } + + @Test + public void testFibonacci() { + int[] newArr = ArrayUtil.fibonacci(15); + int[] resultArr = {1, 1, 2, 3, 5, 8, 13}; + assertArrayEquals(resultArr, newArr); + } + + @Test + public void testGetPrimes() { + int[] newArr = ArrayUtil.getPrimes(23); + int[] resultArr = {2,3,5,7,11,13,17,19}; + assertArrayEquals(resultArr, newArr); + } + + @Test + public void testGetPerfectNumbers() { + int[] newArr = ArrayUtil.getPerfectNumbers(500); + int[] resultArr = {6, 28, 496}; + assertArrayEquals(resultArr, newArr); + } + + @Test + public void testJoin() { + int[] oldArr = {3,8,9}; + String resultStr = ArrayUtil.join(oldArr, "-"); + String str = "3-8-9"; + assertEquals(str, resultStr); + } + +} diff --git a/group18/1159828430/20170226/src/com/coding/litestruts/LoginAction.java b/group18/1159828430/20170226/src/com/coding/litestruts/LoginAction.java new file mode 100644 index 0000000000..308a0c17fd --- /dev/null +++ b/group18/1159828430/20170226/src/com/coding/litestruts/LoginAction.java @@ -0,0 +1,38 @@ +package com.coding.litestruts; +/** + * @author Scholar + * @Time:2017年2月27日 下午8:50:23 + * @version 1.0 + */ +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; + } +} \ No newline at end of file diff --git a/group18/1159828430/20170226/src/com/coding/litestruts/Struts.java b/group18/1159828430/20170226/src/com/coding/litestruts/Struts.java new file mode 100644 index 0000000000..92a485db1b --- /dev/null +++ b/group18/1159828430/20170226/src/com/coding/litestruts/Struts.java @@ -0,0 +1,84 @@ +package com.coding.litestruts; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +/** + * @author Scholar + * @Time:2017年2月27日 下午8:49:47 + * @version 1.0 + */ +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + try {//多行代码需要try catch时,如何合理使用try cath和throw Exception? + Element actionElement = getElement("action", actionName); + //实例化action并设置参数 + String actionAdress = actionElement.attribute("class").getText(); + Class actionClass = Class.forName(actionAdress); + Object actionObj = actionClass.newInstance(); + for (Entry entry : parameters.entrySet()) { + Method setMethod = actionClass.getMethod("set"+initStr(entry.getKey()), String.class); + setMethod.invoke(actionObj, entry.getValue()); + } + + //执行execute + Method executeMethod = actionClass.getMethod("execute"); + //Type returnType = m.getGenericReturnType();//获取返回值类型 + String returnValue = (String)executeMethod.invoke(actionObj);//这里如何根据返回类型来动态接收方法的返回值? + + //获取返回值放到view + Map params = new HashMap(); + Field[] fields = actionClass.getDeclaredFields(); + for (Field field : fields) { + Method m = actionClass.getMethod("get"+initStr(field.getName())); + params.put(field.getName(), (String) m.invoke(actionObj)); + } + Class viewClass = View.class; + Object viewObj = viewClass.newInstance(); + Field paramField = viewClass.getDeclaredField("parameters"); + paramField.setAccessible(true); + paramField.set(viewObj, params); + + //将对应result的值赋值给view并返回view对象 + Element resultElement = getElement("result", returnValue); + String jsp = resultElement.getStringValue(); + Method setJsp = viewClass.getMethod("setJsp", String.class); + return (View)setJsp.invoke(viewObj, jsp); + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + + //DOM4J结合XPATH解析XML,取得相应的节点 + private static Element getElement(String node, String name){ + Document document; + Element element = null; + try { + File file = new File("src/com/coding/litestruts/struts.xml"); + SAXReader reader = new SAXReader(); + document = reader.read(file); + element = (Element) document.selectSingleNode("//"+ node + "[@name='" + name + "']"); + } catch (Exception e) { + System.out.println("读取XML失败"); + } + return element; + } + + // 将单词的首字母大写 + public static String initStr(String old){ + String str = old.substring(0,1).toUpperCase() + old.substring(1) ; + return str ; + } +} \ No newline at end of file diff --git a/group18/1159828430/20170226/src/com/coding/litestruts/StrutsTest.java b/group18/1159828430/20170226/src/com/coding/litestruts/StrutsTest.java new file mode 100644 index 0000000000..4bf614bbbc --- /dev/null +++ b/group18/1159828430/20170226/src/com/coding/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coding.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Scholar + * @Time:2017年2月27日 下午8:51:23 + * @version 1.0 + */ +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/group18/1159828430/20170226/src/com/coding/litestruts/View.java b/group18/1159828430/20170226/src/com/coding/litestruts/View.java new file mode 100644 index 0000000000..3c7175350a --- /dev/null +++ b/group18/1159828430/20170226/src/com/coding/litestruts/View.java @@ -0,0 +1,27 @@ +package com.coding.litestruts; + +import java.util.Map; +/** + * @author Scholar + * @Time:2017年2月27日 下午8:49:10 + * @version 1.0 + */ +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; + } +} \ No newline at end of file diff --git a/group18/1159828430/20170226/src/com/coding/litestruts/struts.xml b/group18/1159828430/20170226/src/com/coding/litestruts/struts.xml new file mode 100644 index 0000000000..60f2bc6c41 --- /dev/null +++ b/group18/1159828430/20170226/src/com/coding/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