diff --git "a/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/ArrayUtil.java" "b/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/ArrayUtil.java" new file mode 100644 index 0000000000..cd1fb6810c --- /dev/null +++ "b/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/ArrayUtil.java" @@ -0,0 +1,203 @@ +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 tmp ; + int length = origin.length; + for (int i = 0 ; i < length / 2 ; i++) { + tmp = origin[i]; + origin[i] = origin[length - i - 1]; + origin[length - i - 1] = tmp; + } + } + + /** + * 现在有如下的一个数组: 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 size = oldArray.length; + int[] newArray = new int[size]; + int repeatTime = 0; + int count = 0; + for (int i = 0 ; i < oldArray.length;i++) { + if (oldArray[i] != 0) { + newArray[count++] = oldArray[i]; + }else{ + repeatTime++; + } + } + return Arrays.copyOf(newArray,newArray.length - repeatTime); + } + + /** + * 给定两个已经排序好的整形数组, 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[] newArray = new int[array1.length + array2.length]; + int count = 0; + int cursor = 0; + int last = 0; + int repeatTime = 0; + for (int i = 0 ; i < array1.length;i++) { + last = i; + int value1 = array1[i]; + if (value1 < array2[cursor]) { + newArray[count++] = value1; + }else{ + newArray[count++] = array2[cursor]; + if (value1 != array2[cursor]) { + i--; + }else{ + repeatTime++; + } + cursor++; + if (cursor == array2.length) { + break; + } + } + } + for (int i = cursor ; i < array2.length ;i++) { + if (newArray[count - 1] == array2[i]) { + continue; + } + newArray[count++] = array2[i]; + } + + for (int i = last;i < array1.length;i++) { + if (newArray[count - 1] == array1[i]) { + continue; + } + newArray[count++] = array1[i]; + } + return Arrays.copyOf(newArray,newArray.length - repeatTime); + } + /** + * 把一个已经存满数据的数组 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[]{}; + } + int num1 = 1; + int num2 = 1; + int next = 2; + int [] array = new int[max + 1]; + array[0] = num1; + int count = 1; + while (next <= max) { + array[count++] = num2; + next = num1 + num2; + num1 = num2; + num2 = next; + } + return Arrays.copyOf(array,count); + } + + /** + * 返回小于给定最大值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 count = 0; + boolean isPrime = true; + for (int i = 2 ; i < max ;i ++) { + for (int j = 2 ; j < Math.sqrt(i);j++) { + if (i % j == 0){ + isPrime = false; + break; + } + } + + if (isPrime) { + primes[count++] = i; + } + + isPrime = true; + } + return Arrays.copyOf(primes,count); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] array = new int[max]; + int count = 0; + for (int i = 1 ; i < max ; i++) { + int sum = 0; + for (int j = 1 ; j < i;j++) { + if (i % j == 0) { + sum += j; + } + } + if (sum == i) { + array[count++] = i; + } + } + return Arrays.copyOf(array, count); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator){ + StringBuffer sb = new StringBuffer(); + 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/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/liteStructs/LoginAction.java" "b/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/liteStructs/LoginAction.java" new file mode 100644 index 0000000000..cc9a7dcf4e --- /dev/null +++ "b/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/liteStructs/LoginAction.java" @@ -0,0 +1,38 @@ + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @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/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/liteStructs/Struts.java" "b/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/liteStructs/Struts.java" new file mode 100644 index 0000000000..05ee388d2b --- /dev/null +++ "b/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/liteStructs/Struts.java" @@ -0,0 +1,161 @@ + +import com.byhieg.utils.bexception.UncheckedException; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; + + +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字段中。 + + */ + + //创建文件对象,ClassName,view对象等必备的对象以及得到params参数中所有的key-value + String fileName = "/Users/byhieg/IdeaProjects/learnjava/src/com/byhieg/coding2017/homework305/struts.xml"; + String className; + List keys = new ArrayList<>(); + List values = new ArrayList<>(); + Iterator iterator = parameters.entrySet().iterator(); + String[] methodNames = new String[parameters.size()]; + int i = 0; + + View view = new View(); + Map map = new HashMap(); + // 得到set方法的方法名 + while (iterator.hasNext()) { + Map.Entry entry = (Map.Entry) iterator.next(); + String key = entry.getKey(); + keys.add(key); + values.add(entry.getValue()); + methodNames[i++] = "set" + key.substring(0, 1).toUpperCase() + key.substring(1); + } + + try { + //反射得到对象 + Element element = getTargetElement(actionName, fileName); + className = getClassName(element); + Class clz = Class.forName(className); + Object obj = clz.newInstance(); + + //根据上面的set方法名的数组,依次调用, + for (int j = 0; j < methodNames.length; j++) { + Method method = clz.getMethod(methodNames[j], String.class); + method.invoke(obj, values.get(j)); + } + + //执行execute方法 + Method method = clz.getMethod("execute"); + String result = (String) method.invoke(obj); + + //得到所有方法,判断哪些是get方法,是的话,生成需要的map + Method[] methods = clz.getMethods(); + for (Method item : methods) { + if (item.getName().contains("get")) { + String key = item.getName().substring(3).toLowerCase(); + Object value = item.invoke(obj); + map.put(key, value); + } + } + + view.setParameters(map); + + //根据result得到jsp,放入view对象中 + String jsp = getJsp(element, result); + view.setJsp(jsp); + + return view; + + } catch (DocumentException e) { + System.out.println("文件找不到"); + } catch (ClassNotFoundException e) { + System.out.println("找不到指定的类"); + } catch (InstantiationException | IllegalAccessException e) { + System.out.println("创建对象失败"); + } catch (NoSuchMethodException e) { + System.out.println("方法创建失败"); + } catch (InvocationTargetException e) { + System.out.println("方法执行失败"); + } + + + return view; + } + + + /** + * + * @param element 指定的的节点 + * @return 返回该节点对应的ClassName的值 + * @throws DocumentException 文件异常 + */ + + private static String getClassName(Element element) throws DocumentException { + return element.attribute(1).getValue(); + + + } + + /** + * + * @param actionName actionName + * @param fileName xml文件路径 + * @return 指定actionName对应的节点元素 + * @throws DocumentException 文件异常 + */ + private static Element getTargetElement(String actionName, String fileName) throws DocumentException { + SAXReader reader = new SAXReader(); + Document document = reader.read(new File(fileName)); + Element rootNode = document.getRootElement(); + List elements = rootNode.elements(); + for (Element item : elements) { + if (actionName.equals(item.attribute(0).getValue())) { + return item; + } + } + return null; + } + + /** + * 通过result,得到指定的JSP + * @param element actionName对象的节点 + * @param result result标签的name + * @return result标签的值 + */ + private static String getJsp(Element element,String result) { + List elements = element.elements(); + for (Element e : elements) { + if (result.equals(e.attribute(0).getValue())){ + return e.getTextTrim(); + } + } + + return null; + } + + +} diff --git "a/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/liteStructs/StrutsTest.java" "b/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/liteStructs/StrutsTest.java" new file mode 100644 index 0000000000..beca61aa9a --- /dev/null +++ "b/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/liteStructs/StrutsTest.java" @@ -0,0 +1,42 @@ + +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/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/liteStructs/View.java" "b/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/liteStructs/View.java" new file mode 100644 index 0000000000..12ae000ab0 --- /dev/null +++ "b/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/liteStructs/View.java" @@ -0,0 +1,22 @@ + +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/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/liteStructs/struts.xml" "b/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/liteStructs/struts.xml" new file mode 100644 index 0000000000..ae0ce37fd8 --- /dev/null +++ "b/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/src/liteStructs/struts.xml" @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git "a/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/ArrayUtilTest.java" "b/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/ArrayUtilTest.java" new file mode 100644 index 0000000000..df9ab466b1 --- /dev/null +++ "b/group03/1196051822/3\346\234\21005\346\227\245\344\275\234\344\270\232\347\232\204\344\273\243\347\240\201/test/ArrayUtilTest.java" @@ -0,0 +1,81 @@ +package com.byhieg.coding2017.homework305; + +import com.byhieg.utils.bprint.FullPrint; +import junit.framework.Assert; +import junit.framework.TestCase; + +/** + * Created by byhieg on 17/3/1. + * Mail to byhieg@gmail.com + */ +public class ArrayUtilTest extends TestCase { + public void testReverseArray() throws Exception { + int[] array = new int[]{1, 2, 4, 5}; + ArrayUtil util = new ArrayUtil(); + util.reverseArray(array); + for (int i = 0 ; i < array.length;i++) { + System.out.print(array[i]); + } + + } + + public void testRemoveZero() throws Exception { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + ArrayUtil util = new ArrayUtil(); + int [] newArray = util.removeZero(oldArr); + for (int i = 0 ; i < newArray.length;i++) { + System.out.print(newArray[i] + " "); + } + } + + public void testMerge() throws Exception { + int [] a1 = new int[]{3, 5,7,8,10,11}; + int [] a2 = new int[]{4, 5, 6,7}; + int[] newArray = new ArrayUtil().merge(a1,a2); + for (int i = 0 ; i < newArray.length;i++) { + System.out.print(newArray[i] + " "); + } + + } + + public void testGrow() throws Exception { + int[] a = new int[]{1, 2, 3, 4, 5}; + int [] newArray = new ArrayUtil().grow(a,3); + for (int i = 0 ; i < newArray.length;i++) { + System.out.print(newArray[i] + " "); + } + + } + + + public void testFibonacci()throws Exception { + int[] newArray = new ArrayUtil().fibonacci(2); + for (int i = 0 ; i < newArray.length;i++) { + System.out.print(newArray[i] + " "); + } + + } + + public void testgetPrimes() throws Exception { + int[] newArray = new ArrayUtil().getPrimes(23); + for (int i = 0 ; i < newArray.length;i++) { + System.out.print(newArray[i] + " "); + } + + } + + public void testgetPerfectNumbers() throws Exception { + int [] newArray = new ArrayUtil().getPerfectNumbers(100); + for (int i = 0 ; i < newArray.length;i++) { + System.out.print(newArray[i] + " "); + } + + } + + public void testJoin() throws Exception { + int[] a = new int[]{3, 8, 9}; + System.out.println(new ArrayUtil().join(a,"-")); + + } + +} \ No newline at end of file diff --git a/group03/1196051822/README b/group03/1196051822/README deleted file mode 100644 index c7b21c2ef0..0000000000 --- a/group03/1196051822/README +++ /dev/null @@ -1,2 +0,0 @@ -# 作业文件夹说明 -src文件夹存放是的作业源码的文件,test文件夹存放的是源码相应的测试文件 diff --git a/group03/1196051822/readme.md b/group03/1196051822/readme.md new file mode 100644 index 0000000000..fa4ad63f02 --- /dev/null +++ b/group03/1196051822/readme.md @@ -0,0 +1,5 @@ +# 说明 +这个文件夹是QQ:119601822 学员的代码文件夹。 +在每个作业代码包含src文件夹和test文件夹 +src文件夹内容均是Java代码,无执行的class文件。 +test文件夹内容时src文件夹代码的测试类的文件夹。 \ No newline at end of file diff --git a/group03/1360464792/pom.xml b/group03/1360464792/pom.xml index 5fed7b15ab..fc16664c23 100644 --- a/group03/1360464792/pom.xml +++ b/group03/1360464792/pom.xml @@ -20,6 +20,13 @@ junit junit 4.12 + test + + + + dom4j + dom4j + 1.6.1 diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/coderising/array/ArrayUtil.java b/group03/1360464792/src/main/java/rui/study/coding2017/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..031b14fcef --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/coderising/array/ArrayUtil.java @@ -0,0 +1,230 @@ +package rui.study.coding2017.coderising.array; + +import java.util.Arrays; + +/** + * 创建于 2017-03-01. + * + * @author 赵睿 + */ +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 int[] reverseArray(int[] origin){ + int length=origin.length; + + int[] result=new int[length]; + int j=0; + for (int i = length-1; i >=0 ; i--) { + result[j]=origin[i]; + j++; + } + return result; + } + + /** + * 现在有如下的一个数组: 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 length=oldArray.length; + int notZero=0; + int[] temArray=new int[length]; + for (int i = 0; i array2[j]){ + tempArr[tempSize]=array2[j]; + j++; + }else if(array1[i]==array2[j]){ + tempArr[tempSize]=array1[i]; + i++;j++; + }else { + tempArr[tempSize]=array1[i]; + i++; + } + tempSize++; + flag=j>array2.length-1||i>array1.length-1; + } + if(i<=j){ + for (; i < array1.length ; i++) { + tempArr[tempSize]=array1[i]; + tempSize++; + } + }else{ + for (; j < array2.length ; i++) { + tempArr[tempSize]=array2[i]; + tempSize++; + } + } + return Arrays.copyOf(tempArr,tempSize); + } + + + /** + * 把一个已经存满数据的数组 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[] empty={}; + int tempSize=0; + int temp[]=new int[max]; + + if(max<=1){ + return empty; + }else if(max==2){ + temp[0]=1; + temp[1]=1; + return temp; + }else{ + temp[0]=1; + temp[1]=1; + tempSize=2; + } + boolean flag=false; + while(!flag){ + int tempMax=temp[tempSize-2]+temp[tempSize-1]; + if(tempMax>max){ + flag=true; + }else{ + temp[tempSize++]=tempMax; + } + } + return Arrays.copyOf(temp,tempSize); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + int temp[]=new int[max]; + int tempSize=0; + for (int i = 0; i < max; i++) { + if(isPrimes(i)){ + temp[tempSize++]=i; + } + } + return Arrays.copyOf(temp,tempSize); + } + + private boolean isPrimes(int num){ + if(num<2) return false; + if(num==2) return true; + if(num==3) return true; + for (int i = 2; i *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 temp[] =new int[max]; + int tempSize=0; + + for (int i = 0; i < max; i++) { + if(isPerfectNumber(i)){ + temp[tempSize++]=i; + } + } + return Arrays.copyOf(temp,tempSize); + } + + private boolean isPerfectNumber(int num){ + if(num<1)return false; + if(num==1)return true; + int temp[] =new int[num]; + int tempSize=0; + temp[tempSize++]=1; + for (int i = 2; i < num; i++) { + if(num%i==0){ + temp[tempSize++]=i; + } + } + + int add=0; + for (int i = 0; i < tempSize; i++) { + add+=temp[i]; + } + if(add==num)return true; + return false; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder stringBuilder=new StringBuilder(); + + for (int i = 0; i < array.length-1; i++) { + stringBuilder.append(array[i]) + .append(seperator); + } + stringBuilder.append(array[array.length-1]); + return stringBuilder.toString(); + } +} \ No newline at end of file diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/coderising/liteststruts/LoginAction.java b/group03/1360464792/src/main/java/rui/study/coding2017/coderising/liteststruts/LoginAction.java new file mode 100644 index 0000000000..2feac1cd83 --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/coderising/liteststruts/LoginAction.java @@ -0,0 +1,39 @@ +package rui.study.coding2017.coderising.liteststruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @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/group03/1360464792/src/main/java/rui/study/coding2017/coderising/liteststruts/LogoutAction.java b/group03/1360464792/src/main/java/rui/study/coding2017/coderising/liteststruts/LogoutAction.java new file mode 100644 index 0000000000..c74632c2b6 --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/coderising/liteststruts/LogoutAction.java @@ -0,0 +1,39 @@ +package rui.study.coding2017.coderising.liteststruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @author liuxin + * + */ +public class LogoutAction { + 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/group03/1360464792/src/main/java/rui/study/coding2017/coderising/liteststruts/Struts.java b/group03/1360464792/src/main/java/rui/study/coding2017/coderising/liteststruts/Struts.java new file mode 100644 index 0000000000..0225b9ac36 --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/coderising/liteststruts/Struts.java @@ -0,0 +1,106 @@ +package rui.study.coding2017.coderising.liteststruts; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + View view=new View(); + try { + StrutsAction strutsAction= StrutsAction.getFormStrutsMap(actionName); + + Class clazz=strutsAction.getaClass(); + Object object=clazz.newInstance(); + + setActionValue(parameters,object); + String resultStr = execute(object); + getVlaue(view, object); + getJspPath(view, strutsAction, resultStr); + } catch (IOException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return view; + } + + /** + * + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + * @param parameters 请求参数 + * @param object 对象 + */ + private static void setActionValue(Map parameters, Object object) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + for (Map.Entry entry: parameters.entrySet()) { + String key=entry.getKey(); + String methodName="set"+key.substring(0,1).toUpperCase()+key.substring(1,key.length()); + Method method=object.getClass().getMethod(methodName,String.class); + method.invoke(object,entry.getValue()); + } + } + + /** + * 2. 通过反射调用对象的execute 方法, 并获得返回值,例如"success" + * @param object 反射生成的对象 + * @return 执行结果表示 + */ + private static String execute(Object object) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, IOException { + Method executeMethod=object.getClass().getMethod("execute"); + Object returnObj=executeMethod.invoke(object); + if(returnObj.getClass()!=String.class) throw new IOException("不支持非页面跳转类型"); + return (String) returnObj; + } + + + /** + * + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + * @param view 视图 + * @param object 反射之后的action类 + */ + private static void getVlaue(View view, Object object) throws IllegalAccessException, InvocationTargetException { + Map resultMap=new HashMap(); + Method[] methods=object.getClass().getDeclaredMethods(); + for (Method getMethod:methods) { + String methodName=getMethod.getName(); + if(methodName.contains("get")){ + Object o=getMethod.invoke(object); + String fileName=methodName.replace("get",""); + fileName=fileName.substring(0,1).toLowerCase()+fileName.substring(1,fileName.length()); + resultMap.put(fileName,o); + } + } + view.setParameters(resultMap); + } + + /** + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + * @param view 视图 + * @param strutsAction xml解析后的存储类 + * @param resultStr 结果表示 + */ + private static void getJspPath(View view, StrutsAction strutsAction, String resultStr) { + for (StrutsAction.Result result:strutsAction.getResults()) { + if(result.name.equals(resultStr))view.setJsp(result.jspPath); + } + } +} diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/coderising/liteststruts/StrutsAction.java b/group03/1360464792/src/main/java/rui/study/coding2017/coderising/liteststruts/StrutsAction.java new file mode 100644 index 0000000000..3f41707dcb --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/coderising/liteststruts/StrutsAction.java @@ -0,0 +1,127 @@ +package rui.study.coding2017.coderising.liteststruts; + + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 读取struct.xml.解析xml + * Created by 赵睿 on 2017/3/4. + */ +public class StrutsAction { + private String name; + private Class aClass; + private List results; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Class getaClass() { + return aClass; + } + + public void setaClass(Class aClass) { + this.aClass = aClass; + } + + class Result{ + String name; + String jspPath; + } + + public List getResults() { + return results; + } + + public void setResults(List results) { + this.results = results; + } + public void setResult(Result result) { + this.results.add(result); + } + + private static volatile Map strutsMap=new HashMap(); + + static void putStrutsMap(String key,Object obj){ + if(strutsMap.get(key)==null){ + synchronized (key){ + if(strutsMap.get(key)==null){ + synchronized (key){ + strutsMap.put(key,obj); + } + } + } + } + } + + static StrutsAction getFormStrutsMap(String key){ + return (StrutsAction) strutsMap.get(key); + } + + //0. 读取配置文件struts.xml + static { + try { + File strutsFile=new File(Struts.class.getResource("/struts.xml").getPath()); + Document document= new SAXReader().read(strutsFile); + Element element=document.getRootElement(); + if(!element.getName().equals("struts")) throw new IOException("不是一个struts.xml的配置文件"); + StrutsAction.listNodes(element,null); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + + + static void listNodes(Element element, Object object) throws ClassNotFoundException { + List elements=element.elements(); + for (Element childEle:elements) { + if(childEle.getName().equals("action")) dealAction(childEle); + if(childEle.getName().equals("result")) dealResult(childEle, (StrutsAction) object); + } + + } + + static void dealAction(Element action) throws ClassNotFoundException { + StrutsAction strutsAction=new StrutsAction(); + List attributes=action.attributes(); + for (Attribute attribute:attributes) { + if(attribute.getName().equals("name")) strutsAction.setName(attribute.getValue()); + if(attribute.getName().equals("class")) strutsAction.setaClass(Class.forName(attribute.getValue())); + } + List results=new ArrayList(action.elements().size()); + strutsAction.setResults(results); + listNodes(action,strutsAction); + putStrutsMap(strutsAction.getName(),strutsAction); + } + + static void dealResult(Element element,StrutsAction strutsAction) { + StrutsAction.Result result=strutsAction.new Result(); + List attributes=element.attributes(); + for (Attribute attribute:attributes) { + if(attribute.getName().equals("name")) result.name=attribute.getValue(); + } + result.jspPath=element.getStringValue(); + strutsAction.setResult(result); + } +} + + diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/coderising/liteststruts/View.java b/group03/1360464792/src/main/java/rui/study/coding2017/coderising/liteststruts/View.java new file mode 100644 index 0000000000..cb09758177 --- /dev/null +++ b/group03/1360464792/src/main/java/rui/study/coding2017/coderising/liteststruts/View.java @@ -0,0 +1,23 @@ +package rui.study.coding2017.coderising.liteststruts; + +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/group03/1360464792/src/main/java/rui/study/coding2017/ArrayList.java b/group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/ArrayList.java similarity index 98% rename from group03/1360464792/src/main/java/rui/study/coding2017/ArrayList.java rename to group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/ArrayList.java index 4040049ed8..ddf5e49be2 100644 --- a/group03/1360464792/src/main/java/rui/study/coding2017/ArrayList.java +++ b/group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/ArrayList.java @@ -1,4 +1,4 @@ -package rui.study.coding2017; +package rui.study.coding2017.coding.basic; import java.util.Arrays; diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTree.java b/group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/BinaryTree.java similarity index 98% rename from group03/1360464792/src/main/java/rui/study/coding2017/BinaryTree.java rename to group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/BinaryTree.java index 7d63153ffb..f306da9cf0 100644 --- a/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTree.java +++ b/group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/BinaryTree.java @@ -1,4 +1,4 @@ -package rui.study.coding2017; +package rui.study.coding2017.coding.basic; /** * 二叉树 diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTreeNode.java b/group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/BinaryTreeNode.java similarity index 95% rename from group03/1360464792/src/main/java/rui/study/coding2017/BinaryTreeNode.java rename to group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/BinaryTreeNode.java index 3895133f17..1d535da5ee 100644 --- a/group03/1360464792/src/main/java/rui/study/coding2017/BinaryTreeNode.java +++ b/group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/BinaryTreeNode.java @@ -1,4 +1,4 @@ -package rui.study.coding2017; +package rui.study.coding2017.coding.basic; public class BinaryTreeNode { diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/Iterator.java b/group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/Iterator.java similarity index 67% rename from group03/1360464792/src/main/java/rui/study/coding2017/Iterator.java rename to group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/Iterator.java index dddde983c6..ee7dbc7683 100644 --- a/group03/1360464792/src/main/java/rui/study/coding2017/Iterator.java +++ b/group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/Iterator.java @@ -1,4 +1,4 @@ -package rui.study.coding2017; +package rui.study.coding2017.coding.basic; public interface Iterator { diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/LinkedList.java b/group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/LinkedList.java similarity index 98% rename from group03/1360464792/src/main/java/rui/study/coding2017/LinkedList.java rename to group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/LinkedList.java index f0a04cd32f..67cd9d5b91 100644 --- a/group03/1360464792/src/main/java/rui/study/coding2017/LinkedList.java +++ b/group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/LinkedList.java @@ -1,4 +1,4 @@ -package rui.study.coding2017; +package rui.study.coding2017.coding.basic; /** * 单向链表 diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/List.java b/group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/List.java similarity index 81% rename from group03/1360464792/src/main/java/rui/study/coding2017/List.java rename to group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/List.java index ef68407c78..efdbab7a77 100644 --- a/group03/1360464792/src/main/java/rui/study/coding2017/List.java +++ b/group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/List.java @@ -1,4 +1,4 @@ -package rui.study.coding2017; +package rui.study.coding2017.coding.basic; public interface List { public void add(Object o); diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/Queue.java b/group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/Queue.java similarity index 89% rename from group03/1360464792/src/main/java/rui/study/coding2017/Queue.java rename to group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/Queue.java index 4ab5d7e5c1..e40782e26e 100644 --- a/group03/1360464792/src/main/java/rui/study/coding2017/Queue.java +++ b/group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/Queue.java @@ -1,4 +1,4 @@ -package rui.study.coding2017; +package rui.study.coding2017.coding.basic; public class Queue { LinkedList linkedList=new LinkedList(); diff --git a/group03/1360464792/src/main/java/rui/study/coding2017/Stack.java b/group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/Stack.java similarity index 91% rename from group03/1360464792/src/main/java/rui/study/coding2017/Stack.java rename to group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/Stack.java index 9ff78d6dca..00bb8b5881 100644 --- a/group03/1360464792/src/main/java/rui/study/coding2017/Stack.java +++ b/group03/1360464792/src/main/java/rui/study/coding2017/coding/basic/Stack.java @@ -1,4 +1,4 @@ -package rui.study.coding2017; +package rui.study.coding2017.coding.basic; public class Stack { private ArrayList elementData = new ArrayList(); diff --git a/group03/1360464792/src/main/resources/struts.xml b/group03/1360464792/src/main/resources/struts.xml new file mode 100644 index 0000000000..dd6152a234 --- /dev/null +++ b/group03/1360464792/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/group03/1360464792/src/test/java/rui/study/coding2017/coderising/array/ArrayUtilTest.java b/group03/1360464792/src/test/java/rui/study/coding2017/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..9b5f3d8175 --- /dev/null +++ b/group03/1360464792/src/test/java/rui/study/coding2017/coderising/array/ArrayUtilTest.java @@ -0,0 +1,91 @@ +package rui.study.coding2017.coderising.array; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * 创建于 2017-03-01. + * + * @author 赵睿 + */ +public class ArrayUtilTest { + ArrayUtil arrayUtil=new ArrayUtil(); + + @Test + public void reverseArray() throws Exception { + int[] origin={7, 9 , 30, 3}; + origin=arrayUtil.reverseArray(origin); + for (Integer i:origin) { + System.out.println(i); + } + } + + @Test + public void removeZero() throws Exception { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + oldArr=arrayUtil.removeZero(oldArr); + for (Integer i:oldArr) { + System.out.println(i); + } + } + + @Test + public void merge() throws Exception { + int arr1[]={3, 5, 7,8}; + int arr2[]={4, 5, 6,7}; + + int oldArr[]=arrayUtil.merge(arr1,arr2); + for (Integer i:oldArr) { + System.out.println(i); + } + } + + @Test + public void grow() throws Exception { + int oldArr[]={2,3,6}; + oldArr=arrayUtil.grow(oldArr,3); + for (Integer i:oldArr) { + System.out.println(i); + } + } + + @Test + public void fibonacci() throws Exception { + for (Integer i:arrayUtil.fibonacci(0)) { + System.out.println(i); + } + for (Integer i:arrayUtil.fibonacci(1)) { + System.out.println(i); + } + System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>"); + for (Integer i:arrayUtil.fibonacci(2)) { + System.out.println(i); + } + System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>"); + for (Integer i:arrayUtil.fibonacci(15)) { + System.out.println(i); + } + } + + @Test + public void getPrimes() throws Exception { + for (Integer i:arrayUtil.getPrimes(100)) { + System.out.println(i); + } + } + + @Test + public void getPerfectNumbers() throws Exception { + for (Integer i:arrayUtil.getPerfectNumbers(1000)) { + System.out.println(i); + } + } + + @Test + public void join() throws Exception { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + System.out.println(arrayUtil.join(oldArr,"````")); + } + +} \ No newline at end of file diff --git a/group03/1360464792/src/test/java/rui/study/coding2017/coderising/liteststruts/StrutsTest.java b/group03/1360464792/src/test/java/rui/study/coding2017/coderising/liteststruts/StrutsTest.java new file mode 100644 index 0000000000..0f41c2ba4c --- /dev/null +++ b/group03/1360464792/src/test/java/rui/study/coding2017/coderising/liteststruts/StrutsTest.java @@ -0,0 +1,47 @@ +package rui.study.coding2017.coderising.liteststruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; + +/** + * 测试struts功能 + * Created by 赵睿 on 2017/3/4. + */ + +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/group03/1360464792/src/test/java/rui/study/coding2017/ArrayListTest.java b/group03/1360464792/src/test/java/rui/study/coding2017/coding/basic/ArrayListTest.java similarity index 97% rename from group03/1360464792/src/test/java/rui/study/coding2017/ArrayListTest.java rename to group03/1360464792/src/test/java/rui/study/coding2017/coding/basic/ArrayListTest.java index e81d34d1c0..7a753e14ce 100644 --- a/group03/1360464792/src/test/java/rui/study/coding2017/ArrayListTest.java +++ b/group03/1360464792/src/test/java/rui/study/coding2017/coding/basic/ArrayListTest.java @@ -1,9 +1,7 @@ -package rui.study.coding2017; +package rui.study.coding2017.coding.basic; import org.junit.Test; -import static org.junit.Assert.*; - /** * 测试我的数组如何 * Created by 赵睿 on 2017/2/24. diff --git a/group03/1360464792/src/test/java/rui/study/coding2017/BinaryTreeTest.java b/group03/1360464792/src/test/java/rui/study/coding2017/coding/basic/BinaryTreeTest.java similarity index 91% rename from group03/1360464792/src/test/java/rui/study/coding2017/BinaryTreeTest.java rename to group03/1360464792/src/test/java/rui/study/coding2017/coding/basic/BinaryTreeTest.java index b2e4f76e24..3c74302e47 100644 --- a/group03/1360464792/src/test/java/rui/study/coding2017/BinaryTreeTest.java +++ b/group03/1360464792/src/test/java/rui/study/coding2017/coding/basic/BinaryTreeTest.java @@ -1,9 +1,7 @@ -package rui.study.coding2017; +package rui.study.coding2017.coding.basic; import org.junit.Test; -import static org.junit.Assert.*; - /** * 二叉树测试 * Created by 赵睿 on 2017/2/25. diff --git a/group03/1360464792/src/test/java/rui/study/coding2017/LinkedListTest.java b/group03/1360464792/src/test/java/rui/study/coding2017/coding/basic/LinkedListTest.java similarity index 98% rename from group03/1360464792/src/test/java/rui/study/coding2017/LinkedListTest.java rename to group03/1360464792/src/test/java/rui/study/coding2017/coding/basic/LinkedListTest.java index 625582e67c..5c78c9618f 100644 --- a/group03/1360464792/src/test/java/rui/study/coding2017/LinkedListTest.java +++ b/group03/1360464792/src/test/java/rui/study/coding2017/coding/basic/LinkedListTest.java @@ -1,4 +1,4 @@ -package rui.study.coding2017; +package rui.study.coding2017.coding.basic; import org.junit.Test; diff --git a/group03/1360464792/src/test/java/rui/study/coding2017/QueueTest.java b/group03/1360464792/src/test/java/rui/study/coding2017/coding/basic/QueueTest.java similarity index 92% rename from group03/1360464792/src/test/java/rui/study/coding2017/QueueTest.java rename to group03/1360464792/src/test/java/rui/study/coding2017/coding/basic/QueueTest.java index baff49411c..2b3631ad86 100644 --- a/group03/1360464792/src/test/java/rui/study/coding2017/QueueTest.java +++ b/group03/1360464792/src/test/java/rui/study/coding2017/coding/basic/QueueTest.java @@ -1,9 +1,7 @@ -package rui.study.coding2017; +package rui.study.coding2017.coding.basic; import org.junit.Test; -import static org.junit.Assert.*; - /** * 测试队列 * Created by 赵睿 on 2017/2/25. diff --git a/group03/1360464792/src/test/java/rui/study/coding2017/StackTest.java b/group03/1360464792/src/test/java/rui/study/coding2017/coding/basic/StackTest.java similarity index 94% rename from group03/1360464792/src/test/java/rui/study/coding2017/StackTest.java rename to group03/1360464792/src/test/java/rui/study/coding2017/coding/basic/StackTest.java index 115a16f1fc..804510e85b 100644 --- a/group03/1360464792/src/test/java/rui/study/coding2017/StackTest.java +++ b/group03/1360464792/src/test/java/rui/study/coding2017/coding/basic/StackTest.java @@ -1,9 +1,7 @@ -package rui.study.coding2017; +package rui.study.coding2017.coding.basic; import org.junit.Test; -import static org.junit.Assert.*; - /** * 测试栈 * Created by 赵睿 on 2017/2/25. diff --git a/group03/345943980/lite-struts-0226/pom.xml b/group03/345943980/lite-struts-0226/pom.xml new file mode 100644 index 0000000000..92af8d801e --- /dev/null +++ b/group03/345943980/lite-struts-0226/pom.xml @@ -0,0 +1,50 @@ + + 4.0.0 + + org.lemon.cm + lite-struts-0226 + 0.0.1-SNAPSHOT + jar + + lite-struts-0226 + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 4.10 + test + + + + commons-digester + commons-digester + 2.1 + + + + dom4j + dom4j + 1.6.1 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.7 + 1.7 + + + + + diff --git a/group03/345943980/lite-struts-0226/src/main/java/com/coderising/array/ArrayUtil.java b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..a2fc003e17 --- /dev/null +++ b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/array/ArrayUtil.java @@ -0,0 +1,258 @@ +package com.coderising.array; + +import java.util.Arrays; + +public class ArrayUtil { + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int[] target = new int[origin.length]; + for (int i = origin.length - 1, j = 0; i >= 0; i--, j++) { + target[j] = origin[i]; + } + System.out.println(Arrays.toString(target)); + } + + /** + * 现在有如下的一个数组: 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[] newArr = new int[5]; // 数组初始化,默认给10个位置 + int size = 0; + + for (int i = 0; i < oldArray.length; i++) { + if (size >= newArr.length) { // 大于初始长度,对新数组进行扩容 + newArr = this.grow(newArr, 5); + } + if (oldArray[i] != 0) { + newArr[size] = oldArray[i]; + size++; + } + } + // 对结果数组进行排0处理 + int[] newArrary = new int[size]; + for (int i = 0, j = 0; i < newArr.length; i++) { + if (newArr[i] != 0) { + newArrary[j] = newArr[i]; + j++; + } + } + return newArrary; + } + + /** + * 给定两个已经排序好的整形数组, 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[] array3 = new int[array1.length + array2.length]; + System.arraycopy(array1, 0, array3, 0, array1.length); + System.arraycopy(array2, 0, array3, array1.length, array2.length); + int temp = 0; + // 冒泡排序 + for (int i = array3.length - 1; i >= 0; i--) { + for (int j = 0; j < i; j++) { + if (array3[j] > array3[j + 1]) { + temp = array3[j + 1]; + array3[j + 1] = array3[j]; + array3[j] = temp; + } + } + } + // Arrays.sort(array3); + // System.out.println(Arrays.toString(array3)); + // 消除重复 + for (int i = array3.length - 1; i >= 0; i--) { + for (int j = 0; j < i; j++) { + if (array3[j] == array3[j + 1]) { + array3[j + 1] = 0; + } + } + } + // 去掉0值 + return removeZero(array3); + } + + /** + * 方法实现二 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] mergeMethod(int[] array1, int[] array2) { + int[] array3 = new int[array1.length + array2.length]; + System.arraycopy(array1, 0, array3, 0, array1.length); + System.arraycopy(array2, 0, array3, array1.length, array2.length); + int counts = 0; // 找出重复元素个数 + for (int i = 0; i < array3.length - 1; i++) { + for (int j = i + 1; j < array3.length; j++) { + if (array3[i] == array3[j]) { + counts++; + break; + } + } + } + // 消除重复 + int[] newArr = new int[array3.length - counts]; + int index = 0; + for (int i = 0; i < array3.length; i++) { + boolean flag = false; + for (int j = 0; j < newArr.length; j++) { + if (array3[i] == newArr[j]) { + flag = true; + break; + } + } + if (!flag) { + newArr[index++] = array3[i]; + } + } + // 排序 + Arrays.sort(newArr); + return newArr; + } + + /** + * 把一个已经存满数据的数组 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) { + int a = 1, b = 1, c = 0, size = 2; + int[] array = new int[0]; + if (max <= 1) + return array; + array = new int[] { a, b }; + while (true) { + c = a + b; + a = b; + b = c; + if (c > max) + break; + // 对数组进行扩容 + array = ensureCapacity(size + 1, array); + array[size] = c; + size++; + } + return removeZero(array); + } + + private int[] ensureCapacity(int minCapacity, int[] array) { + if (minCapacity > array.length) { + int newCapacity = Math.max(minCapacity, array.length * 2); + int[] newDataArray = new int[newCapacity]; + System.arraycopy(array, 0, newDataArray, 0, array.length); + return newDataArray; + } + return array; + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int size = 0; + int[] array = new int[0]; + if (max < 2) { + return array; + } + for (int i = 2; i < max; i++) { + for (int j = 2; j <= i; j++) { + if (i % j == 0 && i != j) { + break; + } + if (i % j == 0 && i == j) { + array = this.ensureCapacity(size + 1, array); + array[size] = i; + size++; + } + } + } + return this.removeZero(array); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int size = 0; + int[] array = new int[0]; + if (max < 1) { + return array; + } + for (int i = 1; i <= max; i++) { + int sum = 0; + for (int j = 1; j < i / 2 + 1; j++) { + if (i % j == 0) { + sum += j; + } + } + if (i == sum) { + array = this.ensureCapacity(size + 1, array); + array[size] = i; + size++; + } + } + return this.removeZero(array); + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + StringBuilder sb = new StringBuilder(); + if (array == null || array.length == 0) { + return null; + } + for (int i = 0; i < array.length; i++) { + if (i == array.length - 1) { + sb.append(array[i]); + } else { + sb.append(array[i]).append(seperator); + } + } + return sb.toString(); + } + +} diff --git a/group03/345943980/lite-struts-0226/src/main/java/com/coderising/litestruts/LoginAction.java b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..614f4053f0 --- /dev/null +++ b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +package com.coderising.litestruts; + +public class LoginAction { + + private String name; + private String password; + private String mssage; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMssage() { + return mssage; + } + + public void setMssage(String mssage) { + this.mssage = mssage; + } + + public String execute() { + if (this.getName().equals("test") && this.getPassword().equals("1234")) { + this.setMssage("login successful"); + return "success"; + } else { + this.setMssage("login failed,please check your user/pwd"); + return "fail"; + } + } +} diff --git a/group03/345943980/lite-struts-0226/src/main/java/com/coderising/litestruts/Struts.java b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..f4cc579b96 --- /dev/null +++ b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,139 @@ +package com.coderising.litestruts; + +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.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class Struts { + + + @SuppressWarnings("unchecked") + public static View runAction(String actionName, Map parameters) { + + /* + * + * 0. 读取配置文件struts.xml + * + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + * ("name"="test" , "password"="1234") , 那就应该调用 setName和setPassword方法 + * + * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + * + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + * 放到View对象的parameters + * + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, 放到View对象的jsp字段中。 + */ + View view = new View(); + Map map = new HashMap(); + SAXReader reader = new SAXReader(); + try { + Object obj = null; + Document document = reader.read(Struts.class.getResourceAsStream("/struts.xml")); + Element root = document.getRootElement(); + List elements = root.elements(); + Class clz = null; + for (Element element : elements) { + if (element.attributeValue("name").trim().equalsIgnoreCase(actionName)) { + String classStr = element.attributeValue("class"); + clz = Class.forName(classStr); + obj = clz.newInstance(); + for (Map.Entry entry : parameters.entrySet()) { + String key = entry.getKey(); + String setMethod = "set" + key.substring(0, 1).toUpperCase() + + key.substring(1); + Method method = clz.getDeclaredMethod(setMethod, String.class); + method.invoke(obj, entry.getValue()); + } + List listElement = element.elements("result"); + for (Element subElement : listElement) { + map.put(subElement.attribute("name").getText(), subElement.getTextTrim()); + } + } + } + Method execMethod = clz.getDeclaredMethod("execute"); + String result = execMethod.invoke(obj).toString(); + Method msgMethod = clz.getDeclaredMethod("getMssage"); + String message = msgMethod.invoke(obj).toString(); + Map msgs = new HashMap(); + msgs.put("message", message); + view.setJsp(map.get(result)); + view.setParameters(msgs); + } catch (Exception e) { + e.printStackTrace(); + } + + return view; + } + + public static void main(String[] args) { + // Digester digester = new Digester(); + // // 指定它不要用DTD验证XML文档的合法性——这是因为我们没有为XML文档定义DTD + // digester.setValidating(false); + // digester.addObjectCreate("struts", Struts.class); + // digester.addObjectCreate("struts/action", LoginAction.class); + // digester.addBeanPropertySetter("struts/action/name"); + // //digester.addBeanPropertySetter("struts/action/class"); + // digester.addObjectCreate("struts/action/result", Result.class); + // digester.addBeanPropertySetter("struts/action/result/name"); + // digester.addBeanPropertySetter("struts/action/result/value"); + // Struts struts = null; + // try { + // struts = (Struts)digester.parse(Struts.class.getResourceAsStream("/struts.xml")); + // System.out.println(struts.actions.get(0).getName()); + // // /System.out.println(struts.actions.get(0).getActionClass()); + // } catch (IOException e) { + // e.printStackTrace(); + // } catch (SAXException e) { + // e.printStackTrace(); + // } + SAXReader reader = new SAXReader(); + try { + Document document = reader.read(Struts.class.getResourceAsStream("/struts.xml")); + Element root = document.getRootElement(); + // 获取某个节点的子节点 + Element element = root.element("action"); + String classStr = element.attributeValue("class"); + Class clz = Class.forName(classStr); + Object obj = clz.newInstance(); + Method medName = clz.getDeclaredMethod("setName", String.class); + medName.invoke(obj, "test"); + Method medPwd = clz.getDeclaredMethod("setPassword", String.class); + medPwd.invoke(obj, "123456"); + + Method medGetName = clz.getDeclaredMethod("getName"); + String username = medGetName.invoke(obj).toString(); + Method medGetPwd = clz.getDeclaredMethod("getPassword"); + String password = medGetPwd.invoke(obj).toString(); + System.out.println(username + "\t" + password); + + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/group03/345943980/lite-struts-0226/src/main/java/com/coderising/litestruts/View.java b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..3ddc1be52c --- /dev/null +++ b/group03/345943980/lite-struts-0226/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,27 @@ +package com.coderising.litestruts; + +import java.util.Map; + +@SuppressWarnings("rawtypes") +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/group03/345943980/lite-struts-0226/src/main/resources/struts.xml b/group03/345943980/lite-struts-0226/src/main/resources/struts.xml new file mode 100644 index 0000000000..c3a3756f71 --- /dev/null +++ b/group03/345943980/lite-struts-0226/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/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/ArrayUtilTest.java b/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/ArrayUtilTest.java new file mode 100644 index 0000000000..c24d4147e9 --- /dev/null +++ b/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/ArrayUtilTest.java @@ -0,0 +1,79 @@ +package com.coderising.litestruts; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; + +import com.coderising.array.ArrayUtil; + +public class ArrayUtilTest { + + private ArrayUtil arrayUtil = null; + + @Before + public void init() { + arrayUtil = new ArrayUtil(); + } + + @Test + public void testReverseArray() { + int[] origin = { 7, 9, 30, 3 }; + arrayUtil.reverseArray(origin); + origin = new int[] { 7, 9, 30, 3, 4 }; + arrayUtil.reverseArray(origin); + } + + @Test + public void testRemoveZero() { + int oldArr[] = { 1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 }; + assertArrayEquals(new int[] { 1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5 }, + arrayUtil.removeZero(oldArr)); + } + + @Test + public void testMerge() { + int[] array1 = { 3, 5, 7, 8 }; + int[] array2 = { 4, 5, 6, 7 }; + int[] array3 = { 3, 4, 5, 6, 7, 8 }; + assertArrayEquals(array3, arrayUtil.merge(array1, array2)); + assertArrayEquals(array3, arrayUtil.mergeMethod(array1, array2)); + } + + @Test + public void testGrow() { + int[] oldArray = { 2, 3, 6 }; + int[] newArray = arrayUtil.grow(oldArray, 3); + assertArrayEquals(new int[] { 2, 3, 6, 0, 0, 0 }, newArray); + } + + @Test + public void testFibonacci() { + assertArrayEquals(new int[] { 1, 1, 2, 3, 5, 8, 13 }, arrayUtil.fibonacci(15)); + assertArrayEquals(new int[0], arrayUtil.fibonacci(1)); + } + + @Test + public void testGetPrimes() { + System.out.println(Arrays.toString(arrayUtil.getPrimes(23))); + } + + @Test + public void testGetPerfectNumbers() { + assertArrayEquals(new int[]{6}, arrayUtil.getPerfectNumbers(6)); + } + + @Test + public void testJoin() { + int[] array = { 3, 8, 9 }; + Assert.assertEquals("3-8-9", arrayUtil.join(array, "-")); + } + @Test + public void main(){ + System.out.println(3 / 2 + 1); + } +} diff --git a/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/StrutsTest.java b/group03/345943980/lite-struts-0226/src/test/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..505e2afbfa --- /dev/null +++ b/group03/345943980/lite-struts-0226/src/test/java/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/group03/569045298/pom.xml b/group03/569045298/pom.xml index 338f3870aa..c2a9dd870e 100644 --- a/group03/569045298/pom.xml +++ b/group03/569045298/pom.xml @@ -1,5 +1,5 @@ - 4.0.0 com.ztc @@ -10,12 +10,6 @@ http://maven.apache.org - - junit - junit - 3.8.1 - test - org.junit.jupiter junit-jupiter-api @@ -25,11 +19,27 @@ junit junit 4.12 + test + + + dom4j + dom4j + 1.6.1 JavaLevelUp + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + diff --git a/group03/569045298/src/main/com/coderising/array/ArrayUtil.java b/group03/569045298/src/main/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..992e0652fb --- /dev/null +++ b/group03/569045298/src/main/com/coderising/array/ArrayUtil.java @@ -0,0 +1,175 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + */ + public 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} + */ + public int[] removeZero(int[] oldArray) { + int[] newArray = new int[oldArray.length]; + int index = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArray[index] = oldArray[i]; + index++; + } + } + int[] result = new int[index]; + System.arraycopy(newArray, 0, result, 0, index); + return result; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + */ + public int[] merge(int[] array1, int[] array2) { + Set set = new TreeSet<>(); + for (int i = 0; i < array1.length; i++) { + set.add(array1[i]); + } + for (int i = 0; i < array2.length; i++) { + set.add(array2[i]); + } + return set2Array(set); + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + */ + 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, 则返回空数组 [] + */ + public int[] fibonacci(int max) { + List list = new ArrayList<>(); + for (int i = 1; i < max; i++) { + int f = fibonacci2(i); + if (f >= max) { + break; + } else { + list.add(f); + } + } + return list2Array(list); + } + + public int fibonacci2(int n) { + if (n == 0) { + return 0; + } else if (n == 1) { + return 1; + } + return fibonacci2(n - 1) + fibonacci2(n - 2); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + */ + public int[] getPrimes(int max) { + List list = new ArrayList<>(); + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + list.add(i); + } + } + return list2Array(list); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + */ + public int[] getPerfectNumbers(int max) { + List list = new ArrayList<>(); + for (int i = 1; i <= max; i++) { + int temp = 0; + for (int n = 1; n < i / 2 + 1; n++) { + if (i % n == 0) { + temp += n; + } + } + if (temp == i) { + list.add(i); + } + } + return list2Array(list); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + */ + public String join(int[] array, String seperator) { + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + if (i != 0) { + stringBuilder.append(seperator); + } + stringBuilder.append(array[i]); + } + return stringBuilder.toString(); + } + + private int[] list2Array(List list) { + int[] result = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + result[i] = list.get(i); + } + return result; + } + + private int[] set2Array(Set set) { + int[] result = new int[set.size()]; + Iterator iterator = set.iterator(); + int index = 0; + while (iterator.hasNext()) { + result[index++] = iterator.next(); + } + return result; + } + + private boolean isPrime(int num) { + for (int i = 2; i < num; i++) { + if (num % i == 0) { + return false; + } + } + return true; + } + +} diff --git a/group03/569045298/src/main/com/coderising/litestruts/LoginAction.java b/group03/569045298/src/main/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..3c3de8ba91 --- /dev/null +++ b/group03/569045298/src/main/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,47 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * + */ +public class LoginAction { + + private String name; + + private String password; + + private String message; + + 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 String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return this.message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/group03/569045298/src/main/com/coderising/litestruts/LogoutAction.java b/group03/569045298/src/main/com/coderising/litestruts/LogoutAction.java new file mode 100644 index 0000000000..3cee1f3824 --- /dev/null +++ b/group03/569045298/src/main/com/coderising/litestruts/LogoutAction.java @@ -0,0 +1,11 @@ +package com.coderising.litestruts; + +/** + * Created by zt on 2017/3/1. + */ +public class LogoutAction { + + public String execute() { + return "success"; + } +} diff --git a/group03/569045298/src/main/com/coderising/litestruts/Struts.java b/group03/569045298/src/main/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..7ec84c9a2d --- /dev/null +++ b/group03/569045298/src/main/com/coderising/litestruts/Struts.java @@ -0,0 +1,156 @@ +package com.coderising.litestruts; + +import com.coderising.litestruts.StrutsBean.Action; +import com.coderising.litestruts.StrutsBean.Result; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +/** + * 模拟struts执行 + * 读取类似struts.xml文件,根据xml的定义创建相关的Action类来执行 + */ +public class Struts { + + private static final String FILE_PATH = "src/main/com/coderising/litestruts/struts.xml"; + + public View runAction(String actionName, Map parameters) { + // 视图 + View view = new View(); + // 读取配置文件struts.xml + Map actions = this.xmlToList(); + if (null == actions || actions.size() == 0) { + return null; + } + try { + // 根据actionName找到相对应的class + Action action = actions.get(actionName); + Class clazz = Class.forName(action.getClassName()); + // 通过反射实例化 + Object newInstance = clazz.newInstance(); + // 获得所有方法 + Map methodMap = new HashMap<>(); + Method[] methods = clazz.getDeclaredMethods(); + for (Method method : methods) { + methodMap.put(method.getName(), method); + } + // 根据parameters中的数据,调用对象的setter方法 + for (Map.Entry entry : parameters.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + Method setterMethod = methodMap.get(settterMethodName(key)); + setterMethod.invoke(newInstance, value); + } + // 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + Method executeMethod = clazz.getMethod("execute"); + Object object = executeMethod.invoke(newInstance); + // 通过反射找到对象的所有getter方法 + Map map = new HashMap<>(); + Field[] fields = clazz.getDeclaredFields(); + if (fields != null && fields.length > 0) { + // 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + for (int i = 0; i < fields.length; i++) { + Field field = fields[i]; + String fieldName = field.getName(); + Method getterMethod = methodMap.get(gettterMethodName(fieldName)); + Object value = getterMethod.invoke(newInstance); + map.put(fieldName, value); + } + } + // 放到View对象的parameters中 + view.setParameters(map); + // 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp,放到View对象的jsp字段中 + List resultList = action.getResult(); + for (Result result : resultList) { + if (result.getName().equals(object)) { + view.setJsp(result.getValue()); + } + } + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } + return view; + } + + private Map xmlToList() { + Map map = new HashMap<>(); + SAXReader saxReader = new SAXReader(); + Document document; + try { + document = saxReader.read(new File(FILE_PATH)); + Element rootElement = document.getRootElement(); + Iterator iterator = rootElement.elementIterator("action"); + while (iterator.hasNext()) { + Action action = new Action(); + List results = new ArrayList<>(); + action.setResult(results); + Element element = iterator.next(); + List attributes = element.attributes(); + for (Attribute attribute : attributes) { + String attributeName = attribute.getName(); + if (attributeName.equals("name")) { + action.setName(attribute.getStringValue()); + } else if (attributeName.equals("class")) { + action.setClassName(attribute.getStringValue()); + } + } + Iterator iterator1 = element.elementIterator(); + while (iterator1.hasNext()) { + Result result = new Result(); + Element element1 = iterator1.next(); + List attributes1 = element1.attributes(); + for (Attribute attribute : attributes1) { + String attributeName = attribute.getName(); + if (attributeName.equals("name")) { + result.setName(attribute.getStringValue()); + } + } + result.setValue(element1.getStringValue()); + results.add(result); + } + map.put(action.getName(), action); + } + } catch (DocumentException e) { + e.printStackTrace(); + } + return map; + } + + private String gettterMethodName(String fieldName) { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("get"); + stringBuilder.append(fieldName.substring(0, 1).toUpperCase()); + stringBuilder.append(fieldName.substring(1)); + return stringBuilder.toString(); + } + + private String settterMethodName(String fieldName) { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("set"); + stringBuilder.append(fieldName.substring(0, 1).toUpperCase()); + stringBuilder.append(fieldName.substring(1)); + return stringBuilder.toString(); + } + +} diff --git a/group03/569045298/src/main/com/coderising/litestruts/StrutsBean/Action.java b/group03/569045298/src/main/com/coderising/litestruts/StrutsBean/Action.java new file mode 100644 index 0000000000..1f7e5eea05 --- /dev/null +++ b/group03/569045298/src/main/com/coderising/litestruts/StrutsBean/Action.java @@ -0,0 +1,40 @@ +package com.coderising.litestruts.StrutsBean; + +import java.io.Serializable; +import java.util.List; + +/** + * Created by zt on 2017/3/1. + */ +public class Action implements Serializable { + + private String name; + + private String className; + + private List result; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public List getResult() { + return result; + } + + public void setResult(List result) { + this.result = result; + } +} diff --git a/group03/569045298/src/main/com/coderising/litestruts/StrutsBean/Result.java b/group03/569045298/src/main/com/coderising/litestruts/StrutsBean/Result.java new file mode 100644 index 0000000000..ef4c73b2bd --- /dev/null +++ b/group03/569045298/src/main/com/coderising/litestruts/StrutsBean/Result.java @@ -0,0 +1,29 @@ +package com.coderising.litestruts.StrutsBean; + +import java.io.Serializable; + +/** + * Created by zt on 2017/3/1. + */ +public class Result implements Serializable { + + 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/group03/569045298/src/main/com/coderising/litestruts/View.java b/group03/569045298/src/main/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..258285e4f3 --- /dev/null +++ b/group03/569045298/src/main/com/coderising/litestruts/View.java @@ -0,0 +1,28 @@ +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/group03/569045298/src/main/com/coderising/litestruts/struts.xml b/group03/569045298/src/main/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..561e9693c1 --- /dev/null +++ b/group03/569045298/src/main/com/coderising/litestruts/struts.xml @@ -0,0 +1,14 @@ + + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + + /jsp/welcome.jsp + /jsp/error.jsp + + + \ No newline at end of file diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/ArrayList.java b/group03/569045298/src/main/com/coding/basic/datastructure/ArrayList.java index dee00342d2..8d3de85e34 100644 --- a/group03/569045298/src/main/com/coding/basic/datastructure/ArrayList.java +++ b/group03/569045298/src/main/com/coding/basic/datastructure/ArrayList.java @@ -56,7 +56,7 @@ public int size() { } private void checkRange(int index) { - if (index < 0 || index > elementData.length) { + if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(); } } @@ -76,8 +76,8 @@ public Iterator iterator() { private class ArrayListIterator implements Iterator { - ArrayList arrayList = null; - int pos = 0; + private ArrayList arrayList = null; + private int position = 0; private ArrayListIterator(ArrayList arrayList) { this.arrayList = arrayList; @@ -85,24 +85,18 @@ private ArrayListIterator(ArrayList arrayList) { @Override public boolean hasNext() { - // TODO - pos++; - if (pos > size) { - return false; - } - return true; + return position < size(); } @Override public Object next() { - // TODO - return elementData[pos]; + return get(position++); } @Override public Object remove() { // TODO - return null; + return this.arrayList.remove(position--); } } } diff --git a/group03/569045298/src/main/com/coding/basic/datastructure/LinkedList.java b/group03/569045298/src/main/com/coding/basic/datastructure/LinkedList.java index 8814ea82e8..0f4ac3231b 100644 --- a/group03/569045298/src/main/com/coding/basic/datastructure/LinkedList.java +++ b/group03/569045298/src/main/com/coding/basic/datastructure/LinkedList.java @@ -62,14 +62,16 @@ public Object get(int index) { public Object remove(int index) { checkRange(index); checkNodeNotNull(); + Object object; if (index == 0) { - removeFirst(); - return head; + object = removeFirst(); + return object; } Node pre = node(index - 1); + object = pre.next.data; pre.next = pre.next.next; size--; - return head; + return object; } @Override @@ -92,20 +94,25 @@ public void addFirst(Object object) { public Object removeFirst() { checkNodeNotNull(); + Object oldValue = head.data; head = head.next; size--; - return head; + return oldValue; } public Object removeLast() { checkNodeNotNull(); + Object oldValue; if (size == 1) { + oldValue = head.data; head = null; + return oldValue; } Node pre = node(size() - 2); + oldValue = pre.next.data; pre.next = null; size--; - return head; + return oldValue; } private void checkRange(int index) { diff --git a/group03/569045298/src/test/coderising/array/ArrayUtilTest.java b/group03/569045298/src/test/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..52638781aa --- /dev/null +++ b/group03/569045298/src/test/coderising/array/ArrayUtilTest.java @@ -0,0 +1,86 @@ +package coderising.array; + +import com.coderising.array.ArrayUtil; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by zt on 2017/2/27. + */ +public class ArrayUtilTest { + + private ArrayUtil arrayUtil = null; + + @Before + public void setUp() { + arrayUtil = new ArrayUtil(); + } + + @Test + public void testReverseArray() { + int[] a = {7, 9, 30, 3}; + int[] expectedReversedA = {3, 30, 9, 7}; + Assert.assertArrayEquals(expectedReversedA, arrayUtil.reverseArray(a)); + int[] b = {7, 9, 30, 3, 4}; + int[] expectedReversedB = {4, 3, 30, 9, 7}; + Assert.assertArrayEquals(expectedReversedB, arrayUtil.reverseArray(b)); + } + + @Test + public void testRemoveZero() { + int oldArr[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + int[] expected = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}; + Assert.assertArrayEquals(expected, arrayUtil.removeZero(oldArr)); + } + + @Test + public void testFibonacci() { + int max = 1; + int[] expected = {}; + Assert.assertArrayEquals(expected, arrayUtil.fibonacci(max)); + int max2 = 15; + int[] expected2 = {1, 1, 2, 3, 5, 8, 13}; + Assert.assertArrayEquals(expected2, arrayUtil.fibonacci(max2)); + } + + @Test + public void testGetPrimes() { + int[] expected = {2, 3, 5, 7, 11, 13, 17, 19}; + int max = 23; + Assert.assertArrayEquals(expected, arrayUtil.getPrimes(max)); + } + + @Test + public void testMerge() { + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + int[] expected = {3, 4, 5, 6, 7, 8}; + Assert.assertArrayEquals(expected, arrayUtil.merge(a1, a2)); + } + + @Test + public void testGetPerfectNumbers() { + int max = 6; + arrayUtil.getPerfectNumbers(max); + } + + @Test + public void testGrow() { + int[] oldArray = {2, 3, 6}; + int size = 3; + int[] newArray = arrayUtil.grow(oldArray, size); + int[] expected = {2, 3, 6, 0, 0, 0}; + Assert.assertArrayEquals(expected, newArray); + } + + @Test + public void testJoin() { + int[] array = {3, 8, 9}; + String seperator = "-"; + String joinedString = arrayUtil.join(array, seperator); + Assert.assertEquals("3-8-9", joinedString); + } + +} diff --git a/group03/569045298/src/test/coderising/litestruts/StrutsTest.java b/group03/569045298/src/test/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..61c25c3648 --- /dev/null +++ b/group03/569045298/src/test/coderising/litestruts/StrutsTest.java @@ -0,0 +1,46 @@ +package coderising.litestruts; + +import com.coderising.litestruts.Struts; +import com.coderising.litestruts.View; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + +public class StrutsTest { + + private Struts struts; + + @Before + public void setUp() { + struts = new Struts(); + } + + @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/group03/58555264/.idea/compiler.xml b/group03/58555264/.idea/compiler.xml new file mode 100644 index 0000000000..d4d178c429 --- /dev/null +++ b/group03/58555264/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group03/58555264/.idea/encodings.xml b/group03/58555264/.idea/encodings.xml new file mode 100644 index 0000000000..b26911bd02 --- /dev/null +++ b/group03/58555264/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/group03/58555264/.idea/inspectionProfiles/Project_Default.xml b/group03/58555264/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000000..8d66637cb9 --- /dev/null +++ b/group03/58555264/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/group03/58555264/.idea/libraries/Maven__junit_junit_4_11.xml b/group03/58555264/.idea/libraries/Maven__junit_junit_4_11.xml new file mode 100644 index 0000000000..f33320d8e7 --- /dev/null +++ b/group03/58555264/.idea/libraries/Maven__junit_junit_4_11.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/group03/58555264/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/group03/58555264/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000000..f58bbc1127 --- /dev/null +++ b/group03/58555264/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/group03/58555264/.idea/misc.xml b/group03/58555264/.idea/misc.xml new file mode 100644 index 0000000000..e199196115 --- /dev/null +++ b/group03/58555264/.idea/misc.xml @@ -0,0 +1,109 @@ + + + + + + + + + + + + + + + + Android > Lint > Correctness + + + Android > Lint > Performance + + + Android > Lint > Security + + + Android > Lint > Usability > Icons + + + Android Lint for Kotlin + + + Code style issuesJava + + + Compiler issuesJava + + + Finalization issuesJava + + + GeneralJavaScript + + + Groovy + + + Inheritance issuesJava + + + J2ME issuesJava + + + JSP Inspections + + + Java + + + Java language level migration aidsJava + + + JavaBeans issuesJava + + + JavaScript + + + Kotlin + + + Numeric issuesJava + + + OtherGroovy + + + Performance issuesJava + + + Probable bugsJava + + + Security issuesJava + + + Serialization issuesJava + + + Verbose or redundant code constructsJava + + + + + Android + + + + + + + + + \ No newline at end of file diff --git a/group03/58555264/.idea/modules.xml b/group03/58555264/.idea/modules.xml new file mode 100644 index 0000000000..3acbd95495 --- /dev/null +++ b/group03/58555264/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/group03/58555264/.idea/sonarIssues.xml b/group03/58555264/.idea/sonarIssues.xml new file mode 100644 index 0000000000..766ed3fe03 --- /dev/null +++ b/group03/58555264/.idea/sonarIssues.xml @@ -0,0 +1,79 @@ + + + + + + \ No newline at end of file diff --git a/group03/58555264/.idea/sonarlint/issuestore/4/4/442292b8a7efeabbe4cc176709b833b1792140ec b/group03/58555264/.idea/sonarlint/issuestore/4/4/442292b8a7efeabbe4cc176709b833b1792140ec new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group03/58555264/.idea/sonarlint/issuestore/7/5/75b2bc528306e8fb4b899cc781e70fe6a351737c b/group03/58555264/.idea/sonarlint/issuestore/7/5/75b2bc528306e8fb4b899cc781e70fe6a351737c new file mode 100644 index 0000000000..bc41e6c43e --- /dev/null +++ b/group03/58555264/.idea/sonarlint/issuestore/7/5/75b2bc528306e8fb4b899cc781e70fe6a351737c @@ -0,0 +1,2 @@ + +W squid:S2094"BRemove this empty class, write its code or make it an "interface".( \ No newline at end of file diff --git a/group03/58555264/.idea/sonarlint/issuestore/index.pb b/group03/58555264/.idea/sonarlint/issuestore/index.pb new file mode 100644 index 0000000000..279a4cb36d --- /dev/null +++ b/group03/58555264/.idea/sonarlint/issuestore/index.pb @@ -0,0 +1,5 @@ + +7 +pom.xml,4/4/442292b8a7efeabbe4cc176709b833b1792140ec +b +2src/main/java/com/circle/collection/ArrayList.java,7/5/75b2bc528306e8fb4b899cc781e70fe6a351737c \ No newline at end of file diff --git a/group03/58555264/.idea/workspace.xml b/group03/58555264/.idea/workspace.xml new file mode 100644 index 0000000000..bd847f163f --- /dev/null +++ b/group03/58555264/.idea/workspace.xml @@ -0,0 +1,1175 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + add + peek + removeFirst + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + project + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1487924915857 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + file://$PROJECT_DIR$/src/main/java/com/circle/collection/ArrayList.java + 37 + + + + file://$PROJECT_DIR$/src/test/java/com/circle/collection/ArrayListTest.java + 34 + + + + file://$PROJECT_DIR$/src/test/java/com/circle/collection/LinkedListTest.java + 87 + + + + file://$PROJECT_DIR$/src/main/java/com/circle/collection/Stack.java + 47 + + + + file://$PROJECT_DIR$/src/main/java/com/circle/collection/BinaryTree.java + 147 + + + + file://$PROJECT_DIR$/src/test/java/com/circle/collection/BinaryTreeTest.java + 69 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No facets are configured + + + + + + + + + + + + + + + 1.7 + + + + + + + + 58555264 + + + + + + + + 1.7 + + + + + + + + Maven: junit:junit:4.11 + + + + + + + + \ No newline at end of file diff --git a/group03/58555264/58555264.iml b/group03/58555264/58555264.iml new file mode 100644 index 0000000000..6f2092a3ac --- /dev/null +++ b/group03/58555264/58555264.iml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group03/58555264/pom.xml b/group03/58555264/pom.xml new file mode 100644 index 0000000000..ede0dc4997 --- /dev/null +++ b/group03/58555264/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.circle + 58555264 + 1.0-SNAPSHOT + jar + + 58555264 + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + + + junit + junit + 4.11 + + + diff --git a/group03/58555264/src/main/java/com/circle/algorithm/ArrayUtil.java b/group03/58555264/src/main/java/com/circle/algorithm/ArrayUtil.java new file mode 100644 index 0000000000..3bfae82039 --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/algorithm/ArrayUtil.java @@ -0,0 +1,151 @@ +package com.circle.algorithm; + +import java.util.Arrays; + +/** + * Created by keweiyang on 2017/3/1. + */ +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 + */ + public void reverseArray(int[] origin) { + int first = 0; + int last = origin.length - 1; + + while (first < last) { + swap(origin, first, last); + first++; + last--; + } + + } + + private void swap(int[] origin, int first, int last) { + int temp = origin[last]; + origin[last] = origin[first]; + origin[first] = 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 size = 0; + for (int i : oldArray) { + if (i != 0) { + + size++; + } + } + int[] newArray = new int[size]; + + int currentIndex = 0; + for (int i : oldArray) { + if (i != 0) { + + newArray[currentIndex++] = 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[] array = new int[max]; + int i = 1; + while (max > fun(i)) { + + array[i] = fun(i); + i++; + } + + + return removeZero(array); + } + + private int fun(int i) { + if (i == 1 || i == 2) { + return 1; + } + return fun(i - 1) + fun(i - 2); + } + + /** + * 返回小于给定最大值max的所有素数(质数)数组 + * 例如max=23,返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int[] arr = new int[max]; + int k = 0; + if (max < 2) { + return null; + } else { + for (int i = 2; i < max; i++) { + boolean flag = false; + int j = i-1; + while (j > 1) { + if (i % j == 0) { + flag = true; + break; + } + j--; + + } + if (!flag) { + arr[k++] = i; + } + + + } + } + /* for (int i : arr) { + System.out.println(i); + }*/ + return removeZero(arr); + } + + /** + * 用seperator把数组array给连接起来 + * 例如array=[3,8,9],seperator="-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + StringBuilder builder = new StringBuilder(); + + for (int i = 0; i < array.length; i++) { + builder.append(array[i]); + builder.append(seperator); + } + builder.deleteCharAt(builder.length() - 1); + return builder.toString(); + } + + +} diff --git a/group03/58555264/src/main/java/com/circle/algorithm/Reverse.java b/group03/58555264/src/main/java/com/circle/algorithm/Reverse.java new file mode 100644 index 0000000000..387fd863b5 --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/algorithm/Reverse.java @@ -0,0 +1,30 @@ +package com.circle.algorithm; + +/** + * Created by keweiyang on 2017/3/1. + * Problem: + Given an input string, reverse the string word by word. + For example, + Given s = “the sky is blue”, + return “blue is sky the”. + */ +public class Reverse { + + public void reverse(String string) { + + char[] cs = string.toCharArray(); + char[] newChar = new char[cs.length]; + for(int i=0;i> list = new ArrayList<>(); + + for (int i = 0; i < as.length; i++) { + for (int j = i + 1; j < as.length; j++) { + int k = as.length - 1; + + + while (target != as[i] + as[j] + as[k]) { + if (target < as[i] + as[j] + as[k]) { + k--; + if (j > k) { + break; + } + } else if (target > as[i] + as[j] + as[k]) { + break; + } + } + + if (target == as[i] + as[j] + as[k]) { + Integer[] arr = new Integer[3]; + arr[0] = as[i]; + arr[1] = as[j]; + arr[2] = as[k]; + list.add(Arrays.asList(arr)); + } + + + + } + } + + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + + } + } + + public static void main(String[] args) { + Sum sum = new Sum(); + int[] as = new int[]{-1, 0, 1, 2, -1, -4}; + Arrays.sort(as); + + sum.sum(as, 0); + + } +} diff --git a/group03/58555264/src/main/java/com/circle/collection/ArrayList.java b/group03/58555264/src/main/java/com/circle/collection/ArrayList.java new file mode 100644 index 0000000000..26ac64b863 --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/collection/ArrayList.java @@ -0,0 +1,168 @@ +package com.circle.collection; + +import java.util.Arrays; + +/** + * Created by keweiyang on 2017/2/25. + * 自定义ArrayList + */ +public class ArrayList implements List { + java.util.ArrayList list; + private Object[] elementData = null; + private int currentIndex = -1; + + public ArrayList(int length) { + if (length < 0) { + throw new RuntimeException("数组初始化大小必须大于0"); + } + elementData = new Object[length]; + } + + /** + * 在数组最后一位插入数据 + * + * @param object + */ + public void add(Object object) { + //1:先判断数组是否越界,由于其他函数也会用到,所以提取为一个函数 + ensureCapacity(); + //2:执行插入操作 + currentIndex++; + elementData[currentIndex] = object; + } + + + /** + * 给数组动态扩容 + */ + public void ensureCapacity() { + if (currentIndex + 2 > elementData.length) { +// Object[] newObjects = new Object[elementData.length * 2 + 2]; + elementData = Arrays.copyOf(elementData, elementData.length * 2 + 2); + //System.arraycopy(elementData, 0, newObjects, 0, newObjects.length); + } + } + + /** + * 在数组指定位置(任意位置)插入数据 + * + * @param index + * @param object + */ + public void add(int index, Object object) { + + rangeCheck(index); + ensureCapacity(); + //如果index>currentIndex,并且在数组范围内,则插入 + if (index > currentIndex && index <= elementData.length) { + currentIndex = index; + elementData[currentIndex] = object; + currentIndex++; + + } else if (index >= 0 && index < currentIndex) { + //如果0<=index< currentIndex,则将index和之后的数据往后面移动 + + for (int i = currentIndex; i >= index; i--) { + elementData[i + 1] = elementData[i]; + } + elementData[index] = object; + currentIndex++; + } else { + //如果index=currentIndex,则调用add(Object o) + add(object); + } + + + } + + public Object[] toArray() { + Object[] array = new Object[currentIndex]; + System.arraycopy(elementData, 0, array, 0, currentIndex); + return array; + } + + private void rangeCheck(int index) { + if (index < 0 || index > elementData.length - 1) { + throw new ArrayIndexOutOfBoundsException("输入索引位置越界"); + } + } + + /** + * 获取指定位置数据 + * + * @param index + * @return + */ + public Object get(int index) { + rangeCheck(index); + return elementData[index]; + } + + /** + * 删除指定位置数据 + * + * @param index + * @return 返回要删除的数据 + */ + public Object remove(int index) { + + rangeCheck(index); + Object temp = elementData[index]; + for (int i = index + 1; i <= currentIndex; i++) { + elementData[i - 1] = elementData[i]; + } + + elementData[currentIndex] = null; + currentIndex--; + return temp; + } + + /** + * 防止内存泄露 + */ + public void clear() { + for(int i=0;i<=currentIndex;i++) { + elementData[i] = null; + } + currentIndex = -1; + } + + /** + * 返回数组长度 + * + * @return + */ + public int size() { + return currentIndex + 1; + } + + /** + * 遍历ArrayList + * + * @return + */ + public Iterator iterator() { + + return new Iterator() { + + int pos = -1; + + public boolean hasNext() { + if (pos + 1 <= currentIndex) { + return true; + } + return false; + } + + public Object next() { + if (hasNext()) { + pos++; + return elementData[pos]; + } + return null; + } + }; + } + + +} diff --git a/group03/58555264/src/main/java/com/circle/collection/BinaryTree.java b/group03/58555264/src/main/java/com/circle/collection/BinaryTree.java new file mode 100644 index 0000000000..ccb05b965f --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/collection/BinaryTree.java @@ -0,0 +1,280 @@ +package com.circle.collection; + +/** + * Created by keweiyang on 2017/2/25. + * 自定义二叉树 + */ +public class BinaryTree { + + private Node root; + + /** + * 查找某个节点 + * + * @param key + * @return + */ + public Node get(int key) { + Node currentNode = root; + + if (currentNode == null) { + throw new RuntimeException("这棵二叉树为空二叉树"); + } else { + while (currentNode.getId()!= key) { + if (currentNode.getId() > key) { + currentNode = currentNode.getLeftChild(); + } else { + currentNode.getRightChild(); + } + + if (currentNode == null) { + return null; + } + } + } + + return currentNode; + } + + /** + * 插入一个节点 + * + * @param id + * @param data + */ + public void insert(int id, Object data) { + Node newNode = new Node(id, data); + //1:先找到插入位置 + Node parentNode = null; + Node currentNode = root; + if (root == null) { + root = newNode; + } else { + while (true) { + parentNode = currentNode; + if (currentNode.getId() > id) { + currentNode = currentNode.getLeftChild(); + if (currentNode == null) { + //如果没有左子节点,则插入 + parentNode.setLeftChild(newNode); + return; + } + } else { + currentNode = currentNode.getRightChild(); + if (currentNode == null) { + //如果没有右子节点,则插入 + parentNode.setRightChild(newNode); + return; + } + + } + } + } + } + + /** + * 删除节点 + * @param key + */ + public boolean delete(int key) { + //根据key找到对应的节点 + // 1、如果该节点不存在就抛出异常 + Node parentNode = null; + Node currentNode = root; + boolean isLeftNode = false; + if (currentNode == null) { + throw new RuntimeException("二叉树为空"); + }else{ + while (currentNode.getId() != key) { + parentNode = currentNode; + if (currentNode.getId() < key) { + currentNode = currentNode.getRightChild(); + isLeftNode = false; + }else{ + currentNode = currentNode.getLeftChild(); + isLeftNode = true; + } + + if (currentNode == null) { + return false; + } + } + } + + + //2、下面讨论该节点存在 + // 2.1如果该节点的左右子节点都不存在,则直接删除该节点 + if (currentNode.getRightChild() == null && currentNode.getLeftChild() == null) { + this.noChild(currentNode, parentNode, isLeftNode); + } + // 2.2如果该节点的只存在一个子节点 + //2.2.1 如果存在左节点 + if (currentNode.getRightChild() == null) { + this.oneLeftChild(currentNode,parentNode,isLeftNode); + } + //2.2.2 如果存在右节点 + if (currentNode.getLeftChild() == null) { + this.oneRightChild(currentNode, parentNode, isLeftNode); + } + //2.3 如果左右孩子都存在,则直接拿右孩子中最小的节点替换该节点 + if (currentNode.getLeftChild() != null && currentNode.getRightChild() != null) { + this.bothChild(currentNode, parentNode, isLeftNode); + } + return false; + + } + + private void bothChild(Node currentNode, Node parentNode, boolean flag) { + //找到中序后继节点 + if (flag) { + Node node = this.successor(currentNode); + node.setLeftChild(currentNode.getLeftChild()); + + parentNode.setLeftChild(node); + + + }else{ + Node node = this.successor(currentNode); + node.setRightChild(currentNode.getRightChild()); + + parentNode.setRightChild(node); + + } + } + + private Node successor(Node currentNode) { + //由于当前节点的左右子节点都存在,所以current一定存在 + Node parent = currentNode; + Node current = currentNode.getRightChild(); + + while (current != null) { + parent = current; + current = current.getLeftChild(); + } + + return parent; + + } + + private void oneRightChild(Node currentNode, Node parentNode, boolean flag) { + if (flag) { + parentNode.setRightChild(currentNode.getLeftChild()); + + }else{ + parentNode.setRightChild(currentNode.getRightChild()); + + } + } + + private void oneLeftChild(Node currentNode, Node parentNode, boolean flag) { + if (flag) { + parentNode.setLeftChild(currentNode.getLeftChild()); + + }else{ + parentNode.setLeftChild(currentNode.getRightChild()); + + } + } + + private void noChild(Node currentNode, Node parentNode, boolean flag) { + if (flag) { + parentNode.setLeftChild(null); + + }else{ + parentNode.setRightChild(null); + + } + } + + /** + * 前序 + * + * @param node + */ + public void preOrder(Node node) { + if (node != null) { + System.out.println(node.getId() + "--- "); + preOrder(node.getLeftChild()); + preOrder(node.getRightChild()); + + } + } + + /** + * 中序 + * + * @param node + */ + public void inOrder(Node node) { + if (node != null) { + inOrder(node.getLeftChild()); + System.out.println(node.getId() + "--"); + inOrder(node.getRightChild()); + + } + } + + /** + * 后序 + * + * @param node + */ + public void postOrder(Node node) { + if (node != null) { + postOrder(node.getLeftChild()); + postOrder(node.getRightChild()); + System.out.println(node.getId() + "---"); + } + } + + private class Node { + private int id; + private Object data; + private Node leftChild; + private Node rightChild; + + + public Node(int id, Object data) { + this.id = id; + this.data = data; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public Node getLeftChild() { + return leftChild; + } + + public void setLeftChild(Node leftChild) { + this.leftChild = leftChild; + } + + public Node getRightChild() { + return rightChild; + } + + public void setRightChild(Node rightChild) { + this.rightChild = rightChild; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + @Override + public String toString() { + + return "id= " + id + " , data= " + data; + } + } +} diff --git a/group03/58555264/src/main/java/com/circle/collection/Iterator.java b/group03/58555264/src/main/java/com/circle/collection/Iterator.java new file mode 100644 index 0000000000..52616dfc27 --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/collection/Iterator.java @@ -0,0 +1,11 @@ +package com.circle.collection; + +/** + * Created by keweiyang on 2017/2/25. + * Iterator对外暴露2个接口,隐藏了具体实现(数组or链表) + */ +public interface Iterator { + boolean hasNext(); + + Object next(); +} diff --git a/group03/58555264/src/main/java/com/circle/collection/LinkedList.java b/group03/58555264/src/main/java/com/circle/collection/LinkedList.java new file mode 100644 index 0000000000..328414aa88 --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/collection/LinkedList.java @@ -0,0 +1,218 @@ +package com.circle.collection; + +/** + * Created by keweiyang on 2017/2/25. + */ +public class LinkedList implements List{ + java.util.LinkedList list; + + private Node first = null; + + private int currentIndex = -1;//主要用于统计链表长度 + + //注意Node是静态内部类 + private static class Node { + + Object data; + Node nextNode; + + public Node(Object obj) { + this.data = obj; + } + + public Node getNextNode() { + return nextNode; + } + + public void setNextNode(Node nextNode) { + this.nextNode = nextNode; + } + + @Override + public String toString() { + return "data= " + data; + } + } + + /** + * 插入 + * + * @param o + */ + public void add(Object o) { + //1:生成一个Node + Node newNode = new Node(o); + Node currentNode = first; + //2:插入Node + if (currentNode == null) { + first = newNode; + } else { + while (currentNode.getNextNode() != null) { + currentNode = currentNode.getNextNode(); + } + currentNode.setNextNode(newNode); + + } + currentIndex++; + } + + /** + * 在指定位置插入节点 + * + * @param index + * @param o + */ + public void add(int index, Object o) { + + Node newNode = new Node(o); + + if (index < 0 || index > currentIndex + 1) { + throw new RuntimeException("索引不正确"); + } + + Node currentNode = first; + Node previousNode = currentNode; + int pos = 0; + while (currentNode != null && pos != index) { + previousNode = currentNode; + currentNode = currentNode.getNextNode(); + pos++; + } + + previousNode.setNextNode(newNode); + newNode.setNextNode(currentNode); + currentIndex++; + + } + + public Object get(int index) { + + rangeCheck(index); + Node node = first; + int pos = 0; + + while (node != null && pos != index) { + + node = node.getNextNode(); + pos++; + } + return node; + } + + private void rangeCheck(int index) { + if (index < 0 || index > currentIndex) { + throw new RuntimeException("索引不正确"); + } + } + + public Object remove(int index) { + + //1:获取要删除的节点 + rangeCheck(index); + Node currentNode = first; + + + if (index == 0) { + + currentNode = first; + first = first.getNextNode(); + }else{ + Node previousNode = first; + int pos = 0; + while (currentNode != null && pos != index) { + previousNode = currentNode; + currentNode = currentNode.getNextNode(); + pos++; + } + + //2:执行删除操作 + previousNode.setNextNode(currentNode.getNextNode()); + } + + currentIndex--; + + return currentNode; + } + + public int size() { + return currentIndex + 1; + } + + public void addFirst(Object o) { + Node newNode = new Node(o); + if (first == null) { + first = newNode; + } else { + newNode.setNextNode(first); + first = newNode; + } + currentIndex++; + } + + public void addLast(Object o) { + + Node newNode = new Node(o); + Node currentNode = first; + if (currentNode == null) { + first=newNode; + } else { + while (currentNode.getNextNode() != null) { + currentNode = currentNode.getNextNode(); + } + currentNode.setNextNode(newNode); + + } + currentIndex++; + + } + + public Object removeFirst() { + + Node node = first; + if (first == null) { + throw new RuntimeException("链表长度为0,不能执行这步操作"); + } else { + first = first.getNextNode(); + } + currentIndex--; + return node; + } + + public Object removeLast() { + Node currentNode = first; + Node previousNode = currentNode; + if (currentNode == null) { + throw new RuntimeException("链表长度为0,不能执行这步操作"); + } else { + while (currentNode.getNextNode() != null) { + previousNode = currentNode; + currentNode = currentNode.getNextNode(); + } + previousNode.setNextNode(null); + + } + currentIndex--; + return currentNode; + } + + public Iterator iterator() { + return new Iterator() { + int pos = -1; + + public boolean hasNext() { + if (pos + 1 <= currentIndex) { + return true; + } + return false; + } + + public Object next() { + if (hasNext()) { + pos++; + return get(pos); + } + return null; + } + }; + } +} diff --git a/group03/58555264/src/main/java/com/circle/collection/List.java b/group03/58555264/src/main/java/com/circle/collection/List.java new file mode 100644 index 0000000000..3c15767b48 --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/collection/List.java @@ -0,0 +1,17 @@ +package com.circle.collection; + +/** + * Created by keweiyang on 2017/2/25. + */ +public interface List { + void add(Object o); + + void add(int index, Object o); + + Object get(int index); + + Object remove(int index); + + int size(); + +} diff --git a/group03/58555264/src/main/java/com/circle/collection/Queue.java b/group03/58555264/src/main/java/com/circle/collection/Queue.java new file mode 100644 index 0000000000..6e67a1c524 --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/collection/Queue.java @@ -0,0 +1,25 @@ +package com.circle.collection; + +/** + * Created by keweiyang on 2017/2/25. + */ +public class Queue { + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o) { + + elementData.addLast(o); + } + + public Object deQueue() { + return elementData.removeFirst(); + } + + public boolean isEmpty() { + return elementData.size() <= 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group03/58555264/src/main/java/com/circle/collection/Stack.java b/group03/58555264/src/main/java/com/circle/collection/Stack.java new file mode 100644 index 0000000000..d57bac0841 --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/collection/Stack.java @@ -0,0 +1,58 @@ +package com.circle.collection; + +/** + * Created by keweiyang on 2017/2/25. + * 自定义Stack,此Stack是基于ArrayList实现的 + */ +public class Stack { + + private ArrayList elementData = new ArrayList(4); + + /** + * 入栈 + * + * @param o + */ + public void push(Object o) { + + elementData.add(o); + } + + /** + * 出栈 + * + * @return + */ + public Object pop() { + Object ret = null; + if (elementData.size() > 0) { + ret = elementData.remove(elementData.size() - 1); + }else{ + throw new RuntimeException("栈中没有元素"); + } + return ret; + } + + /** + * 获取栈顶元素 + * + * @return + */ + public Object peek() { + Object ret = null; + if (elementData.size() > 0) { + ret = elementData.get(elementData.size() - 1); + } else { + throw new RuntimeException("栈中没有元素"); + } + return ret; + } + + public boolean isEmpty() { + return elementData.size() <= 0; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group03/58555264/src/main/java/com/circle/struts/ActionEntity.java b/group03/58555264/src/main/java/com/circle/struts/ActionEntity.java new file mode 100644 index 0000000000..8326e7c81a --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/struts/ActionEntity.java @@ -0,0 +1,59 @@ +package com.circle.struts; + +import java.util.*; + +/** + * Created by keweiyang on 2017/3/2. + */ +class ActionEntity { + + private String name; + private String className; + + private Map resultMap = new HashMap<>(); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public Map getResultMap() { + return resultMap; + } + + public void setResultMap(Map resultMap) { + this.resultMap = resultMap; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("name= "); + builder.append(this.name); + builder.append(" ,className= "); + builder.append(this.className); + + + for (Map.Entry entry : resultMap.entrySet()) { + builder.append(" , resultName= "); + builder.append(entry.getKey()); + builder.append(" , jsp= "); + builder.append(entry.getValue()); + } + + + return builder.toString(); + } + +} diff --git a/group03/58555264/src/main/java/com/circle/struts/LoginAction.java b/group03/58555264/src/main/java/com/circle/struts/LoginAction.java new file mode 100644 index 0000000000..8fb042c28a --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/struts/LoginAction.java @@ -0,0 +1,39 @@ +package com.circle.struts; + +/** + * Created by keweiyang on 2017/3/5. + */ +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/group03/58555264/src/main/java/com/circle/struts/Struts.java b/group03/58555264/src/main/java/com/circle/struts/Struts.java new file mode 100644 index 0000000000..7424a9bb6a --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/struts/Struts.java @@ -0,0 +1,233 @@ +package com.circle.struts; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by keweiyang on 2017/3/2. + */ +public class Struts { + + private final static String D = "1234"; + + private List actionEntityList = new ArrayList<>(); + + private ActionEntity actionEntity = null; + + /** + * @param actionName action方法名称 例如:LoginAction + * @param parameters 参数map,例如:name->liuxin,password->1234 + * @param pathName 文件路径 + * @return + */ + public View runAction(String actionName, Map parameters, String pathName) { + /** + * 0.读取配置文件struts.xml + * + * 1.根据actionName找到对应的class,例如LoginActon,通过反射实例化(创建对象),然后根据parameters中的数据,调用 + * 对象的setter方法,例如parameters中的数据是("name"="liuxin","password"="1234"),那就调用setName和setPassword方法 + * + * 2.通过反射调用对象的execute方法,并获得返回值,例如:"success" + * + * 3. 通过反射找到对象的所有getter方法(例如getMessage),通过反射调用,把值和属性形成一个HashMap,例如{"message":"登录成功"}, + * 放到View对象的parameters + * + * 4. 根据struts.xml中的配置,以及execute的返回值,确定哪一个jsp,放到View对象的jsp对象中 + * + */ + + try { + //1:读取struts.xml + this.getActionEntityList(pathName); + //2:获取对应的classpath + String classPath = initParameter(actionName, parameters); + //3:反射 + View view = createClass(classPath, parameters); + return view; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + + } + + /** + * 将map中的key值修改为setxxx,并且获取classpath + * + * @param actionName + * @param mapParameters + * @return + */ + private String initParameter(String actionName, Map mapParameters) { + if (actionName == null || actionName.trim().length() == 0) { + + throw new IllegalStateException("actionName为空"); + } + if (mapParameters == null || mapParameters.size() == 0) { + throw new IllegalStateException("参数为空"); + } + + String classpath = ""; + for (ActionEntity entity : this.actionEntityList) { + if (entity.getName().equals(actionName)) { + this.actionEntity = entity; + classpath = entity.getClassName(); + break; + + } + } + + + Map map = new HashMap<>(); + for (Map.Entry entry : mapParameters.entrySet()) { + String methodName = "set" + entry.getKey().substring(0, 1).toUpperCase() + entry.getKey().substring(1); + map.put(methodName, entry.getValue()); + } + + mapParameters.clear(); + for (Map.Entry entry : map.entrySet()) { + mapParameters.put(entry.getKey(), entry.getValue()); + } + + + return classpath; + } + + private View createClass(String classpath, Map mapParameters) throws Exception { + View view = null; + if (classpath.equals("")) { + + throw new IllegalStateException("classPath为空"); + } + + Class clazz = Class.forName(classpath); + Object obj = clazz.newInstance(); + Method[] methods = clazz.getDeclaredMethods(); + Field[] fields = clazz.getDeclaredFields(); + + // 先调用setter方法 + for (Map.Entry entry : mapParameters.entrySet()) { + Method method = clazz.getDeclaredMethod(entry.getKey(), String.class); + if (method != null) { + method.invoke(obj, entry.getValue()); + } + + } + + // 再调用execute方法 +// if (obj instanceof LoginAction) { + Method method = clazz.getDeclaredMethod("execute"); + String ret = (String) method.invoke(obj, null); + + Map retMap = this.actionEntity.getResultMap(); + for (Map.Entry result : retMap.entrySet()) { + if (ret.equals(result.getKey())) { + Map map = new HashMap<>(); + System.out.println(result.getValue()); + + this.showView(methods, fields, obj, clazz, map); + view = new View(); + view.setJsp(result.getValue()); + view.setParameters(map); + System.out.println(view.toString()); + break; + } +// } + + } + return view; + } + + private void showView(Method[] methods, Field[] fields, Object object, Class clazz, Map map) throws Exception { + + + String[] ss = new String[fields.length]; + Method[] ms = new Method[methods.length]; + for (int i = 0; i < fields.length; i++) { + ss[i] = fields[i].getName(); + } + + for (int i = 0; i < ss.length; i++) { + String str = ss[i]; + ss[i] = "get" + ss[i].substring(0, 1).toUpperCase() + ss[i].substring(1); + + + ms[i] = clazz.getDeclaredMethod(ss[i], null); + String returnType = (String) ms[i].invoke(object, null); + map.put(str, returnType); +// System.out.println(returnType.toString()); + } + + + } + + /** + * 读取指定路径 + * + * @param pathName 文件路径 + * @return + */ + private List getActionEntityList(String pathName) { + //读取struts.xml配置文件 + if (pathName == null || pathName.trim().length() == 0) { + throw new IllegalStateException("pathName为空"); + } + Document document = XmlUtil.getDocument(pathName); + Map resultMap = null; + + if (document.hasChildNodes()) { + NodeList nodeList = document.getElementsByTagName("struts"); + for (int i = 0; i < nodeList.getLength(); i++) { + Element rootEle = (Element) nodeList.item(i); + + if (rootEle.hasChildNodes()) { + NodeList childList = rootEle.getElementsByTagName("action"); + + for (int j = 0; j < childList.getLength(); j++) { + Element childEle = (Element) childList.item(j); + ActionEntity actionEntity = new ActionEntity(); + + String name = childEle.getAttribute("name"); + String className = childEle.getAttribute("class"); + actionEntity.setClassName(className); + actionEntity.setName(name); + + if (childEle.hasChildNodes()) { + NodeList grandSonList = childEle.getElementsByTagName("result"); + resultMap = new HashMap<>(); + for (int k = 0; k < grandSonList.getLength(); k++) { + Element resultEle = (Element) grandSonList.item(k); + String resultName = resultEle.getAttribute("name"); + String value = resultEle.getTextContent(); + resultMap.put(resultName, value); + } + } + actionEntity.setResultMap(resultMap); + + actionEntityList.add(actionEntity); + } + } + } + } + return actionEntityList; + } + + +} diff --git a/group03/58555264/src/main/java/com/circle/struts/View.java b/group03/58555264/src/main/java/com/circle/struts/View.java new file mode 100644 index 0000000000..d1bf62c405 --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/struts/View.java @@ -0,0 +1,44 @@ +package com.circle.struts; + +import java.util.Map; + +/** + * Created by keweiyang on 2017/3/2. + */ +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + + public void setJsp(String jsp) { + this.jsp = jsp; + } + + public Map getParameters() { + return parameters; + } + + public void setParameters(Map parameters) { + this.parameters = parameters; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("jsp= "); + builder.append(this.jsp); + + for (Map.Entry entry : parameters.entrySet()) { + builder.append(" , "); + builder.append(entry.getKey()); + builder.append(" = "); + builder.append(entry.getValue()); + } + + + return builder.toString(); + } +} diff --git a/group03/58555264/src/main/java/com/circle/struts/XmlUtil.java b/group03/58555264/src/main/java/com/circle/struts/XmlUtil.java new file mode 100644 index 0000000000..aeb43d5bb8 --- /dev/null +++ b/group03/58555264/src/main/java/com/circle/struts/XmlUtil.java @@ -0,0 +1,43 @@ +package com.circle.struts; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.IOException; + +/** + * Created by keweiyang on 2017/3/5. + * 解析XML文件,向外提供Docuemnt对象 + */ +public class XmlUtil { + + private XmlUtil() { + + } + + public static Document getDocument(String fileName) { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + try { + DocumentBuilder builder = factory.newDocumentBuilder(); + + //读当前文件路径下的 + Document doc = builder.parse(fileName); + + //去掉document的空格 + doc.normalize(); + return doc; + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } +} + + diff --git a/group03/58555264/src/test/java/com/circle/algorithm/ArrayUtilTest.java b/group03/58555264/src/test/java/com/circle/algorithm/ArrayUtilTest.java new file mode 100644 index 0000000000..8df45c15b6 --- /dev/null +++ b/group03/58555264/src/test/java/com/circle/algorithm/ArrayUtilTest.java @@ -0,0 +1,57 @@ +package com.circle.algorithm; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by keweiyang on 2017/3/5. + */ +public class ArrayUtilTest { + + ArrayUtil util = new ArrayUtil(); + + @Test + public void reverseArray() throws Exception { + int[] origin = new int[]{7, 9, 30, 3, 4}; + util.reverseArray(origin); + for (int i : origin) { + System.out.println(i); + } + } + + @Test + public void removeZero() throws Exception { + int[] origin = new int[]{1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] newArray = util.removeZero(origin); + for (int i : newArray) { + System.out.println(i); + + } + + } + + @Test + public void fibonacci() throws Exception { + int[] newArray = util.fibonacci(15); + for (int i : newArray) { + System.out.println(i); + } + } + + @Test + public void getPrimes() throws Exception { + int[] newArray = util.getPrimes(23); + for (int i : newArray) { + System.out.println(i); + } + + } + + @Test + public void join() throws Exception { + int[] origin = new int[]{3,8,9}; + System.out.println(util.join(origin, "-")); + } + +} \ No newline at end of file diff --git a/group03/58555264/src/test/java/com/circle/collection/ArrayListTest.java b/group03/58555264/src/test/java/com/circle/collection/ArrayListTest.java new file mode 100644 index 0000000000..7a31fc5590 --- /dev/null +++ b/group03/58555264/src/test/java/com/circle/collection/ArrayListTest.java @@ -0,0 +1,80 @@ +package com.circle.collection; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by keweiyang on 2017/2/25. + */ +public class ArrayListTest { + private ArrayList list = null; + +// @Test + public void add() throws Exception { + list = new ArrayList(3); + list.add(1); + list.add(2); + list.add(3); + //测试自动扩容 + list.add(4); + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + } + } + +// @Test + public void add1() throws Exception { + list = new ArrayList(10); + list.add(1); + list.add(2); + list.add(3); + //测试自动扩容 + list.add(4); + list.add(5, 5); + list.add(4, 6); + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + } + + } + +// @Test + public void get() throws Exception { + list = new ArrayList(10); + list.add(1); + list.add(2); + list.add(3); + + System.out.println(list.get(1)); + + } + +// @Test + public void remove() throws Exception { + list = new ArrayList(10); + list.add(1); + list.add(2); + list.add(3); + + System.out.println(list.remove(1)); + System.out.println("------------>"); + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + } + } + + @Test + public void size() throws Exception { + list = new ArrayList(10); + list.add(1); + list.add(2); + list.add(3); + + System.out.println("size= "+list.size()); + } + +} \ No newline at end of file diff --git a/group03/58555264/src/test/java/com/circle/collection/BinaryTreeTest.java b/group03/58555264/src/test/java/com/circle/collection/BinaryTreeTest.java new file mode 100644 index 0000000000..7db0f9dc96 --- /dev/null +++ b/group03/58555264/src/test/java/com/circle/collection/BinaryTreeTest.java @@ -0,0 +1,74 @@ +package com.circle.collection; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by keweiyang on 2017/2/25. + */ +public class BinaryTreeTest { + + private BinaryTree tree; + + @Test + public void get() throws Exception { + tree = new BinaryTree(); + tree.insert(5, 5); + tree.insert(6, 6); + System.out.println(tree.get(5)); + + } + + @Test + public void insert() throws Exception { + tree = new BinaryTree(); + tree.insert(5, 5); + tree.insert(6, 6); + + } + + @Test + public void preOrder() throws Exception { + tree = new BinaryTree(); + tree.insert(5, 5); + tree.insert(6, 6); + tree.preOrder(tree.get(5)); + + } + + @Test + public void inOrder() throws Exception { + tree = new BinaryTree(); + tree.insert(5, 5); + tree.insert(6, 6); + tree.insert(4, 4); + tree.inOrder(tree.get(5)); + } + + @Test + public void postOrder() throws Exception { + tree = new BinaryTree(); + tree.insert(5, 5); + tree.insert(6, 6); + tree.insert(4, 4); + tree.postOrder(tree.get(5)); + } + + @Test + public void delete() throws Exception { + tree = new BinaryTree(); + tree.insert(7, 7); + tree.insert(5, 5); + tree.insert(10, 10); + tree.insert(2, 2); + tree.insert(6, 6); + tree.insert(11, 11); + tree.insert(13, 13); + tree.inOrder(tree.get(7)); + + tree.delete(10); + tree.inOrder(tree.get(7)); + + } +} \ No newline at end of file diff --git a/group03/58555264/src/test/java/com/circle/collection/LinkedListTest.java b/group03/58555264/src/test/java/com/circle/collection/LinkedListTest.java new file mode 100644 index 0000000000..05d99641c1 --- /dev/null +++ b/group03/58555264/src/test/java/com/circle/collection/LinkedListTest.java @@ -0,0 +1,160 @@ +package com.circle.collection; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by keweiyang on 2017/2/25. + */ +public class LinkedListTest { + private LinkedList list = null; + +// @Test + public void add() throws Exception { + + list = new LinkedList(); + + list.add(1); + list.add(2); + + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + + } + } + + @Test + public void add1() throws Exception { + list = new LinkedList(); + list.add(1); + list.add(2); + list.add(1, 3); + + list.add(3, 4); + //索引不正确情况 + list.add(6,6); + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + + } + + } + + @Test + public void get() throws Exception { + list = new LinkedList(); + list.add(1); + list.add(2); + + System.out.println(list.get(0)); + System.out.println(list.get(1)); + + } + + @Test + public void remove() throws Exception { + list = new LinkedList(); + list.add(1); + list.add(2); + list.remove(1); + + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + + } + + + } + + @Test + public void size() throws Exception { + list = new LinkedList(); + list.add(1); + list.add(2); + System.out.println("size= "+ list.size()); + } + + @Test + public void addFirst() throws Exception { + list = new LinkedList(); + list.add(1); + list.add(2); + list.addFirst("123"); + + + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + + } + + list.remove(0); + System.out.println("---------------");; + it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + + } + } + + @Test + public void addLast() throws Exception { + list = new LinkedList(); + list.addFirst(1); + list.addLast(2); + list.add(3); + list.addLast(4); + list.addFirst(0); + + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + + } + + } + + @Test + public void removeFirst() throws Exception { + + list = new LinkedList(); + list.addFirst(1); + list.addLast(2); + list.add(3); + list.addLast(4); + list.addFirst(0); + list.removeFirst(); + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + } + } + + + + @Test + public void removeLast() throws Exception { + list = new LinkedList(); + list.addFirst(1); + list.addLast(2); + list.add(3); + list.addLast(4); + list.addFirst(0); + list.removeLast(); + list.removeLast(); + Iterator it = list.iterator(); + while (it.hasNext()) { + System.out.println(it.next()); + } + } + + @Test + public void iterator() throws Exception { + + } + +} \ No newline at end of file diff --git a/group03/58555264/src/test/java/com/circle/collection/QueueTest.java b/group03/58555264/src/test/java/com/circle/collection/QueueTest.java new file mode 100644 index 0000000000..6c35605e18 --- /dev/null +++ b/group03/58555264/src/test/java/com/circle/collection/QueueTest.java @@ -0,0 +1,49 @@ +package com.circle.collection; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by keweiyang on 2017/2/25. + */ +public class QueueTest { + + private Queue queue = null; + + @Test + public void enQueue() throws Exception { + queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + queue.enQueue(4); + System.out.println(queue.deQueue()); + } + + @Test + public void deQueue() throws Exception { + + queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + System.out.println(queue.deQueue()); + } + + @Test + public void isEmpty() throws Exception { + queue = new Queue(); +// queue.enQueue(1); +// queue.enQueue(2); + System.out.println(queue.isEmpty()); + } + + @Test + public void size() throws Exception { + + queue = new Queue(); + queue.enQueue(1); + queue.enQueue(2); + System.out.println(queue.size()); + } + +} \ No newline at end of file diff --git a/group03/58555264/src/test/java/com/circle/collection/StackTest.java b/group03/58555264/src/test/java/com/circle/collection/StackTest.java new file mode 100644 index 0000000000..6ec7a3c4ca --- /dev/null +++ b/group03/58555264/src/test/java/com/circle/collection/StackTest.java @@ -0,0 +1,63 @@ +package com.circle.collection; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by keweiyang on 2017/2/25. + */ +public class StackTest { + private Stack stack = null; + + + @Test + public void push() throws Exception { + stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + System.out.println(stack.pop()); + } + + @Test + public void pop() throws Exception { + stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + stack.pop(); + System.out.println(stack.pop()); + } + + @Test + public void peek() throws Exception { + + stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(4); + System.out.println(stack.peek()); + + } + + @Test + public void isEmpty() throws Exception { + + stack = new Stack(); + System.out.println(stack.isEmpty()); + + } + + @Test + public void size() throws Exception { + + stack = new Stack(); + stack.push(1); + System.out.println(stack.size()); + } + +} \ No newline at end of file diff --git a/group03/58555264/src/test/java/com/circle/struts/StrutsTest.java b/group03/58555264/src/test/java/com/circle/struts/StrutsTest.java new file mode 100644 index 0000000000..d5dcb972cd --- /dev/null +++ b/group03/58555264/src/test/java/com/circle/struts/StrutsTest.java @@ -0,0 +1,51 @@ +package com.circle.struts; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; + +/** + * Created by keweiyang on 2017/3/5. + */ +public class StrutsTest { + Map map = null; + Struts struts = null; + String pathName = null; + String actionName = null; + + + + @Test + public void runAction() throws Exception { + map = new HashMap<>(); + struts = new Struts(); + map.put("name", "test"); + map.put("password", "1234"); + actionName = "login"; + pathName = "src/main/resources/struts.xml"; + + View view = struts.runAction(actionName, map, pathName); + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws Exception { + map = new HashMap<>(); + struts = new Struts(); + map.put("name", "test"); + map.put("password", "12345"); + actionName = "login"; + pathName = "src/main/resources/struts.xml"; + + View view = struts.runAction(actionName, map, pathName); + 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/group03/617187912/Learning02/.classpath b/group03/617187912/Learning02/.classpath index fceb4801b5..07c9acc328 100644 --- a/group03/617187912/Learning02/.classpath +++ b/group03/617187912/Learning02/.classpath @@ -2,5 +2,11 @@ + + + + + + diff --git a/group03/617187912/Learning02/src/com/coderising/array/ArrayUtil.java b/group03/617187912/Learning02/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..28f7f0ccc0 --- /dev/null +++ b/group03/617187912/Learning02/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,197 @@ +package com.coderising.array; + +import java.awt.Dialog.ModalExclusionType; +import java.awt.Dimension; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Dictionary; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.StringJoiner; +import java.util.TreeSet; + +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 int[] reverseArray(int[] origin) { + int length = origin.length; + int[] nArr = new int[length]; + for (int i = 0; i < length; i++) { + nArr[i] = origin[length - i - 1]; + } + return nArr; + } + + /** + * 现在有如下的一个数组: 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 = new int[oldArray.length]; + int size = 0; + for (int i : oldArray) { + if (i != 0) { + newArray[size] = i; + size++; + } + } + return Arrays.copyOf(newArray, size); + } + + /** + * 给定两个已经排序好的整形数组, 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) { + Set set = new TreeSet(); + for (int i = 0; i < array1.length; i++) { + set.add(array1[i]); + } + for (int i = 0; i < array2.length; i++) { + set.add(array2[i]); + } + int[] newArray = new int[set.size()]; + int j = 0; + for (Integer i : set) { + newArray[j++] = i; + } + 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 = 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 static int[] fibonacci(int max) { + if (max <= 1) { + return null; + } + int[] arrFib = new int[max / 2 + 2]; + int current = 0; + int temp = 1; + int pre = 1; + int next = 2; + arrFib[current++] = 1; + arrFib[current++] = 1; + do { + arrFib[current++] = next; + temp = pre; + pre = next; + next = next + temp; + } while (next <= max); + return Arrays.copyOf(arrFib, current); + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + if (max < 2) { + return null; + } + int[] arrPrimes = new int[max]; + int current = 0; + arrPrimes[current++] = 2; + Boolean isPrime = true; + for (int i = 3; i < max; i++) { + isPrime = true; + for (Integer j : arrPrimes) { + if (j == 0) { + break; + } + if (i % j == 0) { + isPrime = false; + break; + } + } + if (isPrime == true) { + arrPrimes[current++] = i; + } + } + System.out.println(current); + return Arrays.copyOf(arrPrimes, current); + + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + if (max < 6) { + return null; + } + int[] arrPerNums = new int[max / 6 + 1]; + int current = 0; + arrPerNums[current++] = 6; + for (int i = 7; i < max; i++) { + int sum = 1; + for (int j = 2; j < i / 2+1; j++) { + if (i % j == 0) { + sum += j; + } + } + if (sum == i) { + arrPerNums[current++] = i; + } + } + return Arrays.copyOf(arrPerNums, current); + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + sb.append("-"); + sb.append(array[i]); + } + return sb.toString().substring(1); + } + +} diff --git a/group03/617187912/Learning02/src/com/coderising/array/ArrayUtilTest.java b/group03/617187912/Learning02/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..64a7bf9553 --- /dev/null +++ b/group03/617187912/Learning02/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,85 @@ +package com.coderising.array; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class ArrayUtilTest { + + @Before + public void setUp() throws Exception { + } + + @Test + public void testReverseArray() { + int[] a = { 7, 9, 30, 3 }; + int[] b = ArrayUtil.reverseArray(a); + int[] c = ArrayUtil.reverseArray(b); + assertArrayEquals(a, c); + } + + @Test + public void testRemoveZero() { + int[] arrOld={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] arrTarget={1,3,4,5,6,6,5,4,7,6,7,5}; + int[] arrResult =ArrayUtil.removeZero(arrOld); + assertArrayEquals(arrTarget,arrResult); + } + + @Test + public void testMerge() { + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + int[] arrTarget = {3,4,5,6,7,8}; + int[] arrResult = ArrayUtil.merge(a1, a2); + assertArrayEquals(arrTarget, arrResult); + } + + @Test + public void testGrow() { + int[] arrOld = {2,3,6}; + int[] arrTarget = {2,3,6,0,0,0}; + int[] arrResult = ArrayUtil.grow(arrOld, 3); + assertArrayEquals(arrTarget, arrResult); + } + + @Test + public void testFibonacci() { + int max = 1; + assertNull(ArrayUtil.fibonacci(max)); + max =15; + int[] arrTarget = {1,1,2,3,5,8,13}; + int[] arrResult = ArrayUtil.fibonacci(max); + assertArrayEquals(arrTarget,arrResult); + } + + @Test + public void testGetPrimes() { + int max = 1; + assertNull(ArrayUtil.getPrimes(max)); + max =23; + int[] arrTarget = {2,3,5,7,11,13,17,19}; + int[] arrResult = ArrayUtil.getPrimes(max); + assertArrayEquals(arrTarget,arrResult); + } + + @Test + public void testGetPerfectNumbers() { + int max = 5; + assertNull(ArrayUtil.getPerfectNumbers(max)); + max =8129; + int[] arrTarget = {6,28,496,8128}; + int[] arrResult = ArrayUtil.getPerfectNumbers(max); + assertArrayEquals(arrTarget,arrResult); + } + + @Test + public void testJoin() { + int[] arrOld ={3,8,9}; + String target = "3-8-9"; + String result = ArrayUtil.join(arrOld, "-"); + assertEquals(target, result); + } + +} diff --git a/group03/617187912/Learning02/src/com/coderising/litestruts/LoginAction.java b/group03/617187912/Learning02/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group03/617187912/Learning02/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/group03/617187912/Learning02/src/com/coderising/litestruts/Struts.java b/group03/617187912/Learning02/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..175bb2ecad --- /dev/null +++ b/group03/617187912/Learning02/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,119 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +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) + throws ClassNotFoundException, DocumentException, InstantiationException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException { + + /* + * + * 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字段中。 + * + */ + String[] methodNames = createSetMethodNames(parameters); + Struts.class.getResourceAsStream("/struts.xml"); + Element element = getTargetElement(actionName); + String className = element.attribute(1).getValue(); + Class clz = Class.forName(className); + Object obj = clz.newInstance(); + invokeObjectSetter(parameters, methodNames, clz, obj); + View view = new View(); + view.setParameters(createGetterMap(clz, obj)); + setViewJsp(view, element, clz, obj); + return view; + } + + private static String[] createSetMethodNames(Map parameters) { + String[] methodNames = new String[parameters.size()]; + int i = 0; + for (String key : parameters.keySet()) { + methodNames[i++] = "set" + key.substring(0, 1).toUpperCase() + key.substring(1); + } + return methodNames; + } + + private static void setViewJsp(View view, Element element, Class clz, Object obj) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + view.setJsp(getJsp(element, executeToGetResult(clz, obj))); + } + + private static Map createGetterMap(Class clz, Object obj) throws IllegalAccessException, InvocationTargetException { + Map map = new HashMap(); + Method[] methods = clz.getMethods(); + for (Method item : methods) { + if (item.getName().contains("get")) { + String key = item.getName().substring(3).toLowerCase(); + Object value = item.invoke(obj); + map.put(key, value); + } + } + return map; + } + + private static String executeToGetResult(Class clz, Object obj) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + Method method = clz.getMethod("execute"); + String result = (String) method.invoke(obj); + return result; + } + + private static void invokeObjectSetter(Map parameters, + String[] methodNames, Class clz, Object obj) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + for (String key : methodNames) { + Method method = clz.getMethod(key, String.class); + method.invoke(obj, parameters.get(key)); + } + } + + private static Element getTargetElement(String actionName) throws DocumentException { + SAXReader reader = new SAXReader(); + InputStream inputStream =Struts.class.getResourceAsStream("/struts.xml"); + Document document = reader.read(inputStream); + Element rootNode = document.getRootElement(); + List elements = rootNode.elements(); + for (Element item : elements) { + if (actionName.equals(item.attribute(0).getValue())) { + return item; + } + } + return null; + } + + private static String getJsp(Element element, String result) { + List elements = element.elements(); + for (Element e : elements) { + if (result.equals(e.attribute(0).getValue())) { + return e.getTextTrim(); + } + } + return null; + } +} diff --git a/group03/617187912/Learning02/src/com/coderising/litestruts/StrutsTest.java b/group03/617187912/Learning02/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..66dc565ed3 --- /dev/null +++ b/group03/617187912/Learning02/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,49 @@ +package com.coderising.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws ClassNotFoundException, + InstantiationException, IllegalAccessException, NoSuchMethodException, + InvocationTargetException, DocumentException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws ClassNotFoundException, + InstantiationException, IllegalAccessException, NoSuchMethodException, + InvocationTargetException, DocumentException { + 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/group03/617187912/Learning02/src/com/coderising/litestruts/View.java b/group03/617187912/Learning02/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group03/617187912/Learning02/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/group03/617187912/Learning02/src/com/coderising/litestruts/struts.xml b/group03/617187912/Learning02/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..dd598a3664 --- /dev/null +++ b/group03/617187912/Learning02/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/group03/619224754/.classpath b/group03/619224754/.classpath index 373dce4005..310c94b783 100644 --- a/group03/619224754/.classpath +++ b/group03/619224754/.classpath @@ -3,5 +3,7 @@ + + diff --git a/group03/619224754/src/com/coderising/array/ArrayUtil.java b/group03/619224754/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..a3f51130f9 --- /dev/null +++ b/group03/619224754/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,202 @@ +package com.coderising.array; + +import java.util.Arrays; + +import com.coding.basic.ArrayList; +import com.coding.basic.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[] copy = Arrays.copyOf(origin, origin.length); + for (int i = 0; i < copy.length; i++) { + origin[i] = copy[origin.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 list = new ArrayList(); + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + list.add(oldArray[i]); + } + } + + int[] newArray = new int[list.size()]; + Iterator it = list.iterator(); + for (int i = 0; i < newArray.length; i++) { + newArray[i] = Integer.parseInt(list.get(i).toString()); + } + + return null; + } + + /** + * Ѿõ飬 a1a2 , һµa3, ʹa3 a1a2 Ԫأ Ȼ a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] a3 Ϊ[3,4,5,6,7,8] , ע⣺ Ѿظ + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + ArrayList list = new ArrayList(); + int i = 0, j = 0; + + while (i < array1.length && j < array2.length) { + if (array1[i] < array2[j]) { + list.add(array1[i]); + i++; + } else { + list.add(array1[j]); + j++; + } + } + + if (i < array1.length) { + for (; i < array1.length; i++) { + list.add(array1[i]); + } + } else { + for (; j < array2.length; j++) { + list.add(array2[i]); + } + } + + int[] mergedArr = new int[list.size()]; + for (int m = 0; m < list.size(); m++) { + mergedArr[m] = Integer.parseInt(list.get(m).toString()); + } + + return null; + } + + /** + * һѾݵ oldArrayչ չݴСΪoldArray.length + size + * ע⣬ԪҪ oldArray = [2,3,6] , size = 3,򷵻صΪ + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + for (int i = oldArray.length - 1; i < newArray.length - 1; i++) { + newArray[i] = 0; + } + + return newArray; + } + + /** + * 쳲Ϊ1123581321...... һֵ Сڸֵ 磬 max = 15 , + * 򷵻صӦΪ [11235813] max = 1, 򷵻ؿ [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + if(max == 1) { + return new int[0]; + } + + int a = 1; + int b = 1; + ArrayList list = new ArrayList(); + list.add(a); + list.add(b); + int i = 2; + while (a < 15) { + int next = Integer.parseInt(list.get(i - 1).toString()) + + Integer.parseInt(list.get(i - 2).toString()); + list.add(next); + i++; + } + + int[] arrInt = new int[list.size()]; + for(int j = 0; j < list.size(); j++) { + arrInt[j] = Integer.parseInt(list.get(j).toString()); + } + + return arrInt; + } + + /** + * Сڸֵmax max = 23, صΪ[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + ArrayList list = new ArrayList(); + for(int i = 0; i < max; i++) { + if(isPrime(i)) { + list.add(i); + } + } + + + return null; + } + + + private static boolean isPrime(int n) { + if (n <= 1) { + return false; + } + int k = (int) Math.sqrt(n); + for (int i = 2; i <= k; i++) { + if(n % i == 0) { + return false; + } + } + return true; + } + + /** + * ν ָǡõ֮ͣ6=1+2+3 һֵmax һ飬 Сmax + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + return null; + } + + /** + * seperator array array= [3,8,9], seperator = "-" 򷵻ֵΪ"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + String retString = ""; + for (int i = 0; i < array.length; i++) { + retString += seperator + array[i]; + } + + retString = retString.substring(1); + + return retString; + } + +} \ No newline at end of file diff --git a/group03/619224754/src/com/coderising/litestruts/LoginAction.java b/group03/619224754/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..43674ac7c8 --- /dev/null +++ b/group03/619224754/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; + } +} \ No newline at end of file diff --git a/group03/619224754/src/com/coderising/litestruts/LogoutAction.java b/group03/619224754/src/com/coderising/litestruts/LogoutAction.java new file mode 100644 index 0000000000..5afc869c1c --- /dev/null +++ b/group03/619224754/src/com/coderising/litestruts/LogoutAction.java @@ -0,0 +1,5 @@ +package com.coderising.litestruts; + +public class LogoutAction { + +} diff --git a/group03/619224754/src/com/coderising/litestruts/Struts.java b/group03/619224754/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..378fa96871 --- /dev/null +++ b/group03/619224754/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,88 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.Node; +import org.dom4j.io.SAXReader; + +public class Struts { + + public static View runAction(String actionName, + Map parameters) throws Exception { + View view = new View(); + // InputStream inputStream = new FileInputStream(new + // File("D:/git/coding2017/group03/619224754/src/com/coderising/litestruts/struts.xml")); + SAXReader saxReader = new SAXReader(); + Document document = saxReader + .read(new File( + "D:/git/coding2017/group03/619224754/src/com/coderising/litestruts/struts.xml")); + + Element rootElement = document.getRootElement(); + List lstAction = rootElement.selectNodes("action"); + Iterator it = lstAction.iterator(); + while (it.hasNext()) { + Element actionElement = (Element) it.next(); + String name = actionElement.attributeValue("name"); + if (name.equals(actionName)) { + String actionClass = actionElement.attributeValue("class"); + Class classType = Class.forName(actionClass); + Object obj = classType.newInstance(); + for (String key : parameters.keySet()) { + String value = parameters.get(key); + String strMethod = getFiledSetMethod(key); + Method setMethod = classType.getMethod(strMethod, String.class); + setMethod.invoke(obj, value); + } + Method excuteMethod = classType.getMethod("execute"); + Object retValue = excuteMethod.invoke(obj); + Node resultNode = actionElement + .selectSingleNode("result[@name='" + retValue.toString() + "']"); + view.setJsp(resultNode.getText()); + Map result = new HashMap (); + + Method msessageMethod = classType.getMethod("getMessage"); + Object message = msessageMethod.invoke(obj); + result.put("message", message.toString()); + view.setParameters(result); + } + } + + /* + * + * 0. ȡļstruts.xml + * + * 1. actionNameҵӦclass LoginAction, ͨʵ + * parametersеݣösetter parametersе ("name"="test" , + * "password"="1234") , ǾӦõ setNamesetPassword + * + * 2. ͨöexectue ÷ֵ"success" + * + * 3. ͨҵgetter getMessage, ͨã ֵγһHashMap , + * {"message": "¼ɹ"} , ŵViewparameters + * + * 4. struts.xmlе ,Լexecuteķֵ ȷһjsp + * ŵViewjspֶС + */ + + return view; + } + + public static String getFiledSetMethod(String filedName) { + String methodName = ""; + methodName = "set" + filedName.toUpperCase().substring(0, 1) + + filedName.substring(1); + return methodName; + } +} \ No newline at end of file diff --git a/group03/619224754/src/com/coderising/litestruts/StrutsTest.java b/group03/619224754/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..319168afbd --- /dev/null +++ b/group03/619224754/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; +import com.coderising.litestruts.View; + + + + +public class StrutsTest { + +// @Test +// public void testLoginActionSuccess() throws Exception { +// +// String actionName = "login"; +// +// Map params = new HashMap(); +// params.put("name","test"); +// params.put("password","1234"); +// +// +// View view = Struts.runAction(actionName,params); +// +// Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); +// Assert.assertEquals("login successful", view.getParameters().get("message")); +// } + + @Test + public void testLoginActionFailed() throws Exception { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //ԤIJһ + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} \ No newline at end of file diff --git a/group03/619224754/src/com/coderising/litestruts/View.java b/group03/619224754/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..f68a8c438b --- /dev/null +++ b/group03/619224754/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; + } +} \ No newline at end of file diff --git a/group03/619224754/src/com/coderising/litestruts/struts.xml b/group03/619224754/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group03/619224754/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/group03/619224754/src/test/BinaryTreeNodeTest.java b/group03/619224754/src/test/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..24d76fa271 --- /dev/null +++ b/group03/619224754/src/test/BinaryTreeNodeTest.java @@ -0,0 +1,25 @@ +package test; + +import static org.junit.Assert.*; + +import org.junit.Assert; +import org.junit.Test; + +import com.coding.basic.BinaryTreeNode; + +public class BinaryTreeNodeTest { + + @Test + public void test() { + BinaryTreeNode rootNode = new BinaryTreeNode(); + rootNode.setData(6); + rootNode.insert(5); + rootNode.insert(9); + rootNode.insert(7); + + Assert.assertEquals("Shoule be the same", 5, rootNode.getLeft().getData()); + Assert.assertEquals("Shoule be the same", 9, rootNode.getRight().getData()); + Assert.assertEquals("Shoule be the same", 7, rootNode.getRight().getLeft().getData()); + } + +} diff --git a/group03/619224754/src/test/LinkedListTest.java b/group03/619224754/src/test/LinkedListTest.java new file mode 100644 index 0000000000..30727bbc1c --- /dev/null +++ b/group03/619224754/src/test/LinkedListTest.java @@ -0,0 +1,103 @@ +package test; + +import static org.junit.Assert.*; + +import org.junit.Assert; +import org.junit.Test; + +import com.coding.basic.Iterator; +import com.coding.basic.LinkedList; + +public class LinkedListTest { + + private LinkedList list = new LinkedList(); + + @Test + public void testAddObject() { + list.add(1); + list.add(2); + Assert.assertEquals("Test", 1, list.get(0)); + Assert.assertEquals("Test", 2, list.get(1)); + } + + @Test + public void testAddIntObject() { + list.add(1); + list.add(2); + list.add(1, 2); + list.add(1, 4); + Assert.assertEquals("Test", 4, list.get(1)); + Assert.assertEquals("Test", 2, list.get(2)); + } + + @Test + public void testGet() { + list.add(1); + list.add(2); + Assert.assertEquals("Test", 1, list.get(0)); + Assert.assertEquals("Test", 2, list.get(1)); + } + + @Test + public void testRemove() { + list.add(1); + list.add(2); + list.remove(0); + Assert.assertEquals("Test", 2, list.get(0)); + } + + @Test + public void testSize() { + list.add(1); + list.add(2); + list.remove(0); + Assert.assertEquals("Test", 1, list.size()); + } + + @Test + public void testAddFirst() { + list.add(1); + list.add(2); + list.addFirst(3); + Assert.assertEquals("Test", 3, list.get(0)); + } + + @Test + public void testAddLast() { + list.add(1); + list.add(2); + list.addLast(3); + Assert.assertEquals("Test", 3, list.get(2)); + } + + @Test + public void testRemoveFirst() { + list.add(1); + list.add(2); + list.removeFirst(); + Assert.assertEquals("Test", 2, list.get(0)); + } + + @Test + public void testRemoveLast() { + list.add(1); + list.add(2); + list.removeLast(); + Assert.assertEquals("Test", 1, list.size()); + } + + @Test + public void testIterator() { + int i = 0; + for (i = 0; i < 3; i++) { + list.add(i); + } + + Iterator iterator = list.iterator(); + i = 0; + while(iterator.hasNext()) { + Assert.assertEquals("Shoule be the same", i, iterator.next()); + } + } + +} diff --git a/group03/664269713/DataStructure/src/com/ace/coding/ArrayList.java b/group03/664269713/DataStructure/src/com/ace/coding/ArrayList.java index a529faef7b..0af37b9d4e 100644 --- a/group03/664269713/DataStructure/src/com/ace/coding/ArrayList.java +++ b/group03/664269713/DataStructure/src/com/ace/coding/ArrayList.java @@ -56,11 +56,28 @@ public int size(){ return size; } + public boolean contains(Object o){ + for (int i = 0; i < elementData.length; i++) { + if(elementData[i] == o){ + return true; + } + } + return false; + } + public Iterator iterator(){ return null; // return new ListIterator(); } + public int[] listToArray(ArrayList arrayList){ + int[] newArray = new int[arrayList.size()]; + for (int k = 0; k < newArray.length; k++) { + newArray[k] = (int)arrayList.get(k); + } + return newArray; + } + /*private class ListIterator implements Iterator{ private int index = 0; diff --git a/group03/664269713/DataStructure/src/com/ace/homework2/ArrayUtil.java b/group03/664269713/DataStructure/src/com/ace/homework2/ArrayUtil.java new file mode 100644 index 0000000000..63b2fb0866 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/homework2/ArrayUtil.java @@ -0,0 +1,223 @@ +package com.ace.homework2; + +import java.util.Arrays; + +import com.ace.coding.ArrayList; + +public class ArrayUtil { + /** + * һa , Ըֵû + 磺 a = [7, 9 , 30, 3] , ûΪ [3, 30, 9,7] + a = [7, 9, 30, 3, 4] , ûΪ [4,3, 30 , 9,7] + * @param origin + * @return + */ + public void reverseArray(int[] origin){ + for(int i = 0; i < origin.length/2; i++){ + int 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){ + int newLength = 0; + for(int i:oldArray){ + if(i != 0){ + newLength++; + } + } + int[] newArray = new int[newLength]; + int newArrayIndex = 0; + for(int j = 0; j < oldArray.length; j++){ + if(oldArray[j] != 0){ + newArray[newArrayIndex] = oldArray[j]; + newArrayIndex++; + } + } + return newArray; + } + + /** + * Ѿõ飬 a1a2 , һµa3, ʹa3 a1a2 Ԫأ Ȼ + * a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] a3 Ϊ[3,4,5,6,7,8] , ע⣺ Ѿظ + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + ArrayList arrayList = new ArrayList(); + for (int i = 0; i < array1.length; i++) { + arrayList.add(array1[i]); + } + for (int j = 0; j < array2.length; j++) { + if(!arrayList.contains(array2[j])){ + arrayList.add(array2[j]); + } + } + int[] newArray = new int[arrayList.size()]; + for (int k = 0; k < newArray.length; k++) { + newArray[k] = (int)arrayList.get(k); + } + Arrays.sort(newArray); + 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 newLength = oldArray.length + size; + int[] newArray = new int[newLength]; + for(int i = 0; i < newLength; i++){ + if(i < oldArray.length){ + newArray[i] = oldArray[i]; + } else { + newArray[i] = 0; + } + } + return newArray; + } + + /** + * 쳲Ϊ1123581321...... һֵ Сڸֵ + * 磬 max = 15 , 򷵻صӦΪ [11235813] + * max = 1, 򷵻ؿ [] + * @param max + * @return + */ + public int[] fibonacci(int max){ + int num = 1; + ArrayList arrayList = new ArrayList(); + while(getFibonacci(num) < max){ + arrayList.add(getFibonacci(num)); + num++; + } + int[] newArray = toArray(arrayList); + return newArray; + } + + private int getFibonacci(int num){ + if(num == 1){ + return 1; + } else if(num == 2){ + return 1; + } else { + return getFibonacci(num-1) + getFibonacci(num-2); + } + } + + + /** + * Сڸֵmax + * max = 23, صΪ[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + ArrayList arrayList = new ArrayList(); + arrayList.add(2); + for(int i = 3; i <= max; i++){ + int temp = (int)Math.sqrt(i)+1; + for(int j = 2; j <= temp; j++){ + if(i % j ==0){ + break; + } + if(j == temp){ + arrayList.add(i); + } + } + } + int[] newArray = toArray(arrayList); + return newArray; + } + + + + /** + * ν ָǡõ֮ͣ6=1+2+3 + * һֵmax һ飬 Сmax + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + ArrayList arrayList = new ArrayList(); + for (int i = 2; i <= max; i++) { + if(getPerfectNumber(i)){ + arrayList.add(i); + } + } + int[] newArray = toArray(arrayList); + return newArray; + } + + public boolean getPerfectNumber(int num){ + ArrayList arrayList = new ArrayList(); + for(int i = 1; i <= num/2 ; i++){ + if(num % i == 0){ + arrayList.add(i); + } + } + int sum = 0; + for(int j = 0; j < arrayList.size(); j++){ + sum = sum + (int)arrayList.get(j); + } + return sum == num; + } + + + /** + * seperator array + * array= [3,8,9], seperator = "-" + * 򷵻ֵΪ"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder stringBuilder = new StringBuilder(); + for(int i = 0; i < array.length; i++){ + if(i ,Լexecuteķֵ ȷһjsp ŵViewjspֶС + */ + private static final String STRUTS_XML = "struts.xml"; + private static final String NAME = "name"; + private static final String CLASS = "class"; + private static final String EXECUTE = "execute"; + private static final String GET = "get"; + private static final String SET = "set"; + + private static List getStrutsList() { + List strutsObjList = new ArrayList(); + URL path = Struts.class.getResource(STRUTS_XML); + File f = new File(path.getFile()); + SAXReader saxReader = new SAXReader(); + try { + Document document = saxReader.read(f); + Element strutsNode = document.getRootElement(); + Iterator actionIterator = strutsNode.elementIterator(); + while (actionIterator.hasNext()) { + Element actionNode = (Element) actionIterator.next(); + StrutsObj strutsObj = new StrutsObj(); + strutsObj.setName(actionNode.attributeValue(NAME)); + strutsObj.setClassName(actionNode.attributeValue(CLASS)); + + Iterator resultIterator = actionNode.elementIterator(); + Map map = new HashMap(); + while (resultIterator.hasNext()) { + Element resultNode = (Element) resultIterator.next(); + map.put(resultNode.attributeValue(NAME), resultNode.getStringValue()); + } + strutsObj.setMap(map); + strutsObjList.add(strutsObj); + } + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return strutsObjList; + }; + + public static View runAction(String actionName, Map parameters) { + List lists = getStrutsList(); + View view = new View(); + for (int i = 0; i < lists.size(); i++) { + StrutsObj strutsObj = lists.get(i); + + if (actionName.equals(strutsObj.getName())) { + + try { + Class clazz = Class.forName(lists.get(i).getClassName()); + Object obj = clazz.newInstance(); + for (Map.Entry entry : parameters.entrySet()) { + String methodName = SET + entry.getKey().substring(0, 1).toUpperCase() + + entry.getKey().substring(1); + clazz.getMethod(methodName, String.class).invoke(obj, entry.getValue()); + } + + String status = (String) clazz.getMethod(EXECUTE).invoke(obj); + Map strutsMap = strutsObj.getMap(); + for (Map.Entry entry : strutsMap.entrySet()) { + if (entry.getKey().equals(status)) { + view.setJsp(entry.getValue()); + break; + } + } + + Map map = new HashMap(); + Method[] methods = clazz.getDeclaredMethods(); + for (Method method : methods) { + if (method.getName().startsWith(GET)) { + String tempString = method.getName().substring(3); + String resultkey = tempString.substring(0, 1).toLowerCase() + tempString.substring(1); + String resultValue = (String) method.invoke(obj); + map.put(resultkey, resultValue); + break; + } + + } + view.setParameters(map); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + return view; + } + /*URL path = Struts.class.getResource(STRUTS_XML); + File f = new File(path.getFile()); + SAXReader saxReader = new SAXReader(); + View view = new View(); + try { + Document document = saxReader.read(f); + Element strutsNode = document.getRootElement(); + Iterator actionIterator = strutsNode.elementIterator(); + while(actionIterator.hasNext()){ + Element actionNode = (Element)actionIterator.next(); + + if(actionName.equals(actionNode.attributeValue("name"))){ + String className = actionNode.attributeValue("class"); + Class clazz = Class.forName(className); + LoginAction loginAction = (LoginAction) clazz.newInstance(); + for(Map.Entry entry:parameters.entrySet()){ + String methodName = "set" + entry.getKey().substring(0, 1).toUpperCase() + entry.getKey().substring(1); + clazz.getMethod(methodName, String.class).invoke(loginAction, entry.getValue()); + } + + String status = (String) clazz.getMethod("execute").invoke(loginAction); + + Map map = new HashMap(); + + Method[] methods = clazz.getDeclaredMethods(); + for(Method method: methods){ + if(method.getName().startsWith("get")){ + String tempString = method.getName().substring(3); + String resultkey = tempString.substring(0, 1).toLowerCase() + tempString.substring(1); + String resultValue = (String)method.invoke(loginAction); + map.put(resultkey, resultValue); + } + + } + + view.setParameters(map); + Iterator resultIterator = actionNode.elementIterator(); + while(resultIterator.hasNext()){ + Element resultNode = (Element) resultIterator.next(); + if(status.equals(resultNode.attributeValue("name"))){ + view.setJsp(resultNode.getStringValue()); + return view; + } + } + } + } + } catch (DocumentException e) { + System.out.println(e.getMessage()); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }*/ + +} \ No newline at end of file diff --git a/group03/664269713/DataStructure/src/com/ace/homework2/StrutsObj.java b/group03/664269713/DataStructure/src/com/ace/homework2/StrutsObj.java new file mode 100644 index 0000000000..f1db3ed2f8 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/homework2/StrutsObj.java @@ -0,0 +1,29 @@ +package com.ace.homework2; + +import java.util.Map; + +public class StrutsObj { + private String name; + private String className; + private Map map; + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getClassName() { + return className; + } + public void setClassName(String className) { + this.className = className; + } + public Map getMap() { + return map; + } + public void setMap(Map map) { + this.map = map; + } + +} diff --git a/group03/664269713/DataStructure/src/com/ace/homework2/View.java b/group03/664269713/DataStructure/src/com/ace/homework2/View.java new file mode 100644 index 0000000000..33304451d1 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/homework2/View.java @@ -0,0 +1,23 @@ +package com.ace.homework2; + +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; + } +} \ No newline at end of file diff --git a/group03/664269713/DataStructure/src/com/ace/homework2/struts.xml b/group03/664269713/DataStructure/src/com/ace/homework2/struts.xml new file mode 100644 index 0000000000..7f4ca75bb3 --- /dev/null +++ b/group03/664269713/DataStructure/src/com/ace/homework2/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/group03/894844916/coding2017-02/pom.xml b/group03/894844916/coding2017-02/pom.xml new file mode 100644 index 0000000000..48498ad304 --- /dev/null +++ b/group03/894844916/coding2017-02/pom.xml @@ -0,0 +1,45 @@ + + 4.0.0 + + nusubmarine + coding2017-02 + 0.0.1-SNAPSHOT + jar + + coding2017-02 + http://maven.apache.org + + + UTF-8 + 4.7 + 1.6.1 + + + + + dom4j + dom4j + ${dom4j.version} + + + junit + junit + ${junit.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + diff --git a/group03/894844916/coding2017-02/src/main/java/com/coderising/array/ArrayUntil.java b/group03/894844916/coding2017-02/src/main/java/com/coderising/array/ArrayUntil.java new file mode 100644 index 0000000000..7e5c0356be --- /dev/null +++ b/group03/894844916/coding2017-02/src/main/java/com/coderising/array/ArrayUntil.java @@ -0,0 +1,256 @@ +/** + * + */ +package com.coderising.array; + +/** + * @author patchouli + * + */ +public class ArrayUntil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a = + * [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int size = origin.length; + if (size == 0 || size == 1) { + return; + } + int temp = 0; + for (int i = 0; i < size / 2; i++) { + temp = origin[i]; + origin[i] = origin[size - 1 - i]; + origin[size - 1 - i] = temp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + int size = oldArray.length; + if (size == 0) { + return new int[0]; + } + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) { + size--; + } + } + if (size == 0) { + return new int[0]; + } + int[] noZero = new int[size]; + for (int i = 0, j = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + noZero[j] = oldArray[i]; + j++; + } + } + return noZero; + } + + /** + * 给定两个已经排序好的整形数组, 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.length == 0) { + return array2; + } + if (array2.length == 0) { + return array1; + } + int size = array1.length + array2.length; + int[] merged = new int[size]; + int i=0,j=0,k=0; + while (iarray2[j]) { + merged[k]=array2[j]; + j++; + } + else { + merged[k]=array1[i]; + i++; + j++; + } + k++; + } + if (i==array1.length-1) { + System.arraycopy(array2, j, merged, k, array2.length-j); + k=k+array2.length-j; + } + if (i==array2.length-1) { + System.arraycopy(array1, i, merged, k, array1.length-i); + k=k+array1.length-i; + } + size = k; + int[] newArray = new int[size]; + System.arraycopy(merged, 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) { + 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]; + } + int[] fib = { 1, 1 }; + int size = 2; + int capacity = 2; + int i = 1; + while (fib[i] < max) { + if (size == capacity) { + fib = grow(fib, capacity); + capacity = fib.length; + } + fib[i + 1] = fib[i - 1] + fib[i]; + i++; + size++; + } + int[] newFib = new int[size-1]; + System.arraycopy(fib, 0, newFib, 0, size-1); + return newFib; + } + + /** + * 返回小于给定最大值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[] primes = new int[] { 2 }; + if (max ==3) { + return primes; + } + int size = 1; + int capacity = 1; + int number = 3; + boolean prime = true; + while (primes[size - 1] < max) { + for (int i = 2; i < number; i++) { + if (number % i == 0) { + prime = false; + break; + } + } + if (prime == true) { + if (size == capacity) { + primes = grow(primes, capacity); + capacity = primes.length; + } + primes[size] = number; + size++; + } + number++; + prime = true; + } + int[] newPrimes = new int[size - 1]; + System.arraycopy(primes, 0, newPrimes, 0, size-1); + return newPrimes; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int number = 2; + int size = 0; + int capacity = 10; + int[] perfectNumbers = new int[capacity]; + while (number < max) { + int capa = 10; + int i = 0; + int[] factor = new int[capa]; + int sum = 0; + for (int j = 1; j <= number / 2; j++) { + if (number % j == 0) { + if (i == capa) { + factor = grow(factor, capa); + capa = factor.length; + } + factor[i] = j; + sum += j; + i++; + } + } + if (sum == number && number != max) { + if (size == capacity) { + perfectNumbers = grow(perfectNumbers, capacity); + capacity = perfectNumbers.length; + } + perfectNumbers[size] = number; + size++; + } + number++; + } + int[] newPerfactNumber = new int[size]; + System.arraycopy(perfectNumbers, 0, newPerfactNumber, 0, size); + return newPerfactNumber; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + StringBuffer stringBuffer = new StringBuffer(); + for (int i = 0; i < array.length; i++) { + stringBuffer.append(array[i]); + if (i != array.length - 1) { + stringBuffer.append(seperator); + } + } + return stringBuffer.toString(); + } +} diff --git a/group03/894844916/coding2017-02/src/main/java/com/coderising/litestruts/LoginAction.java b/group03/894844916/coding2017-02/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..5f41f42c62 --- /dev/null +++ b/group03/894844916/coding2017-02/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +package com.coderising.litestruts; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * + * @author liuxin + * + */ +public class LoginAction { + private String name; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public String execute() { + if ("test".equals(name) && "1234".equals(password)) { + this.message = "login successful"; + return "success"; + } + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name) { + this.name = name; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return this.message; + } +} diff --git a/group03/894844916/coding2017-02/src/main/java/com/coderising/litestruts/Struts.java b/group03/894844916/coding2017-02/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..ab39929f2a --- /dev/null +++ b/group03/894844916/coding2017-02/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,118 @@ +package com.coderising.litestruts; + +/** + * @author patchouli + */ +import java.io.File; +import java.io.FileNotFoundException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class Struts { + private static final String ELEMENT_ACTION = "action"; + private static final String ELEMENT_RESULT = "result"; + + /* + * + * 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) + throws FileNotFoundException, URISyntaxException, DocumentException, ClassNotFoundException { + SAXReader reader = new SAXReader(); + URL url = ClassLoader.getSystemResource("struts.xml"); + File file = new File(url.toURI()); + if (!file.exists()) { + throw new FileNotFoundException("struts.xml"); + } + Document document = reader.read(file); + Element strutsElement = document.getRootElement(); + Map viewParameters = new HashMap<>(); + String jsp = ""; + String result = ""; + for (Element actionElement : (List) strutsElement.elements(ELEMENT_ACTION)) { + String name = actionElement.attribute("name").getText(); + if (name.equals(actionName)) { + String className = actionElement.attribute("class").getText(); + Class cls = Class.forName(className); + Object actionObject = null; + try { + actionObject = cls.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + for (Map.Entry entry : parameters.entrySet()) { + + try { + String str = entry.getKey(); + str = str.replace(str.substring(0, 1), str.substring(0, 1).toUpperCase()); + Method method = cls.getDeclaredMethod("set" + str, String.class); + try { + method.invoke(actionObject, entry.getValue()); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } catch (NoSuchMethodException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + try { + Method method = cls.getDeclaredMethod("execute"); + try { + result = (String) method.invoke(actionObject); + Method[] methods = cls.getDeclaredMethods(); + for (int i = 0; i < methods.length; i++) { + String methodName = methods[i].getName(); + if (methodName.startsWith("get")) { + String value = (String) methods[i].invoke(actionObject); + String key = methodName.substring(3); + key = key.replace(key.substring(0, 1), key.substring(0, 1).toLowerCase()); + viewParameters.put(key, value); + } + } + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } catch (NoSuchMethodException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + for (Element resultElement : (List) actionElement.elements(ELEMENT_RESULT)) { + String resultName = resultElement.attribute("name").getText(); + if (resultName.equals(result)) { + jsp = resultElement.getText(); + } + } + } + } + View view = new View(); + view.setJsp(jsp); + view.setParameters(viewParameters); + return view; + } +} diff --git a/group03/894844916/coding2017-02/src/main/java/com/coderising/litestruts/View.java b/group03/894844916/coding2017-02/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..be50f467ed --- /dev/null +++ b/group03/894844916/coding2017-02/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,29 @@ +package com.coderising.litestruts; + +/** + * @author patchouli + */ +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/group03/894844916/coding2017-02/src/main/resources/struts.xml b/group03/894844916/coding2017-02/src/main/resources/struts.xml new file mode 100644 index 0000000000..ff7623e6e1 --- /dev/null +++ b/group03/894844916/coding2017-02/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/group03/894844916/coding2017-02/src/test/java/com/coderising/array/ArrayUntilTest.java b/group03/894844916/coding2017-02/src/test/java/com/coderising/array/ArrayUntilTest.java new file mode 100644 index 0000000000..46fd229487 --- /dev/null +++ b/group03/894844916/coding2017-02/src/test/java/com/coderising/array/ArrayUntilTest.java @@ -0,0 +1,124 @@ +package com.coderising.array; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ArrayUntilTest { + + ArrayUntil arrayUntil=new ArrayUntil(); + + @Test + public void testReverseArray() { + int[] arr1={7,9,30,3}; + int[] reArr1={3,30,9,7}; + int[] arr2={7,9,30,3,4}; + int[] reArr2={4,3,30,9,7}; + + arrayUntil.reverseArray(arr1); + arrayUntil.reverseArray(arr2); + assertArrayEquals(reArr1,arr1); + assertArrayEquals(reArr2,arr2); + } + + @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}; + int oldArr2[]={0,0,0,0,0,0}; + int newArr2[]={}; + + assertArrayEquals(newArr1, arrayUntil.removeZero(oldArr1)); + assertArrayEquals(newArr2, arrayUntil.removeZero(oldArr2)); + } + + @Test + public void testMerge() { + int[] a1={3,5,7,8}; + int[] b1={4,5,6,7}; + int[] c1={3,4,5,6,7,8}; + + int[] a2={}; + int[] b2={4,5,6,7}; + int[] c2={4,5,6,7}; + + int[] a3={3,5,7,8}; + int[] b3={}; + int[] c3={3,5,7,8}; + + int[] a4={}; + int[] b4={}; + int[] c4={}; + + assertArrayEquals(c1, arrayUntil.merge(a1, b1)); + assertArrayEquals(c2, arrayUntil.merge(a2, b2)); + assertArrayEquals(c3, arrayUntil.merge(a3, b3)); + assertArrayEquals(c4, arrayUntil.merge(a4, b4)); + } + + @Test + public void testGrow() { + int[] oldArray={2,3,6}; + int[] newArray={2,3,6,0,0,0}; + int size=3; + + assertArrayEquals(newArray, arrayUntil.grow(oldArray, size)); + } + + @Test + public void testFibonacci() { + int max1=1; + int max2=15; + int max3=21; + + int[] fib1={}; + int[] fib2={1,1,2,3,5,8,13}; + int[] fib3={1,1,2,3,5,8,13}; + + assertArrayEquals(fib1, arrayUntil.fibonacci(max1)); + assertArrayEquals(fib2, arrayUntil.fibonacci(max2)); + assertArrayEquals(fib3, arrayUntil.fibonacci(max3)); + } + + @Test + public void testGetPrimes() { + int max1=1; + int max2=3; + int max3=7; + int max4=23; + + int[] primes1={}; + int[] primes2={2}; + int[] primes3={2,3,5}; + int[] primes4={2,3,5,7,11,13,17,19}; + + assertArrayEquals(primes1, arrayUntil.getPrimes(max1)); + assertArrayEquals(primes2, arrayUntil.getPrimes(max2)); + assertArrayEquals(primes3, arrayUntil.getPrimes(max3)); + assertArrayEquals(primes4, arrayUntil.getPrimes(max4)); + } + + @Test + public void testGetPerfectNumbers() { + int max1=6; + int max2=28; + int max3=500; + int[] perfectNumbers1={}; + int[] perfectNumbers2={6}; + int[] perfectNumbers3={6,28,496}; + + assertArrayEquals(perfectNumbers1, arrayUntil.getPerfectNumbers(max1)); + assertArrayEquals(perfectNumbers2, arrayUntil.getPerfectNumbers(max2)); + assertArrayEquals(perfectNumbers3, arrayUntil.getPerfectNumbers(max3)); + } + + @Test + public void testJoin() { + String seperator="-"; + int[] arr={3,8,9}; + String string="3-8-9"; + + assertEquals(string,arrayUntil.join(arr, seperator)); + } + +} diff --git a/group03/894844916/coding2017-02/src/test/java/com/coderising/litestruts/StrutsTest.java b/group03/894844916/coding2017-02/src/test/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..47e41f8f89 --- /dev/null +++ b/group03/894844916/coding2017-02/src/test/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,41 @@ +package com.coderising.litestruts; + +import java.io.FileNotFoundException; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws FileNotFoundException, URISyntaxException, DocumentException, ClassNotFoundException { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + View view = Struts.runAction(actionName, params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws FileNotFoundException, URISyntaxException, DocumentException, ClassNotFoundException { + 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")); + } +}