diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/ArrayList.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/ArrayList.java index c93b6c76e0..6fde63911f 100644 --- a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/ArrayList.java +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0312/basic/ArrayList.java @@ -12,13 +12,14 @@ public class ArrayList implements List { public void add(Object o) { - if (elementData.length < size + 1) { - Object[] target = new Object[size + 1]; - System.arraycopy(elementData, 0, target, 0, elementData.length); - elementData = target; - } - elementData[size] = o; - size = size + 1; +// if (elementData.length < size + 1) { +// Object[] target = new Object[size + 1]; +// System.arraycopy(elementData, 0, target, 0, elementData.length); +// elementData = target; +// } +// elementData[size] = o; +// size = size + 1; + add(size, o); } @@ -29,7 +30,7 @@ public void add(int index, Object o) throws IndexOutOfBoundsException { int leftSize = index; int rightSize = size - index; - Object[] target = new Object[elementData.length + 1]; + Object[] target = new Object[size + 1]; System.arraycopy(elementData, 0, target, 0, leftSize); target[index] = o; System.arraycopy(elementData, index, target, index + 1, rightSize); diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/array/ArrayUtil.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/array/ArrayUtil.java new file mode 100644 index 0000000000..94f7b60c64 --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/array/ArrayUtil.java @@ -0,0 +1,337 @@ +package me.lzb.homework0319.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int[] target = new int[origin.length]; + for (int i = 0; i < origin.length; i++) { + target[i] = origin[origin.length - 1 - i]; + } + origin = 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 l = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + l = l + 1; + } + } + + int[] target = new int[l]; + + int a = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + target[a] = oldArray[i]; + a = a + 1; + } + } + + return target; + } + + /** + * 给定两个已经排序好的整形数组, 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[] tmp = new int[array1.length + array2.length]; + + + int mini = 0; + int a1 = array1[0]; + int a2 = array2[0]; + + if(a1 < a2){ + mini = a1; + }else { + mini = a2; + } + + tmp[0] = mini; + + int l3 = 0; + + for (int i = 1; i < array1.length + array2.length; i++) { + +// if(mini >= array1[l1 - 1] && mini >= array2[l2 - 1]){ +// l3 = i; +// break; +// } + + int oldMin = mini; + + + + int aa1 = mini; + if(mini < array1[array1.length - 1] ){ + for (int j = 0; j < array1.length; j++) { + if(array1[j] > mini){ + aa1 = array1[j]; + break; + } + } + + } + + int aa2 = mini; + if(mini < array2[array2.length - 1] ){ + for (int j = 0; j < array2.length; j++) { + if(array2[j] > mini){ + aa2 = array2[j]; + break; + } + } + } + + + if(aa1 != oldMin && aa2 != oldMin){ + if(aa1 < aa2){ + mini = aa1; + }else { + mini = aa2; + } + }else if(aa1 != oldMin){ + mini = aa1; + }else { + mini = aa2; + } + + + if(oldMin == mini){ + l3 = i; + break; + } + + tmp[i] = mini; + } + + int[] result = new int[l3]; + + System.arraycopy(tmp, 0, result, 0, l3); + + + + return result; + } + + + + + /** + * 把一个已经存满数据的数组 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[] result = {1, 1}; + + + int i = 2; + + int n = 0; + + while (n < max){ + int[] t = new int[result.length + 1]; + System.arraycopy(result, 0, t, 0, result.length); + n = t[i-1] + t[i - 2]; + + if(n >= max){ + return result; + } + + t[i] = n; + + result = t; + i = i + 1; + } + + return null; + } + + + /** + * 返回小于给定最大值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]; + } + + if (max == 3){ + return new int[]{2}; + } + + + int[] primes = new int[max+1]; + primes[0] = 2; + int count = 1; + for (int i = 3; i < max; i = i + 2) { + + boolean isPrime = true; + for (int j = 3; j < i; j++) { + if(i % j == 0){ + isPrime = false; + break; + } + } + + if(isPrime){ + primes[count] = i; + count = count + 1; + } + } + + int[] result = new int[count]; + System.arraycopy(primes, 0, result, 0, count); + + return result; + + } + + private boolean isPrime(int a){ + if (a < 2) { + return false; + } + + if (a == 2) { + return true; + } + + if(a % 2 == 0){ + return false; + } + + + for (int i = 3; i < a; i = i + 2) { + if(a % i == 0){ + return false; + } + } + + return true; + } + + + + /** + * 所谓“完数”, 是指这个数恰好等于它的真因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + if (max < 6){ + return new int[0]; + } + + + int[] pns = new int[max]; + + int count = 0; + for (int i = 6; i < max; i++) { + if (isPerfectNumber(i)){ + pns[count] = i; + count = count + 1; + } + } + + + + int[] result = new int[count]; + System.arraycopy(pns, 0, result, 0, count); + return result; + } + + + private boolean isPerfectNumber(int a){ + if(a < 6){ + return false; + } + + int sum = 0; + for (int i = 1; i < a; i++) { + if(a % i == 0){ + sum = sum + i; + } + } + + return sum == a; + } + + + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @return + */ + public static String join(int[] array, String seperator) { + String result = ""; + for (int i = 0; i < array.length; i++) { + result = result + array[i] + seperator ; + } + + result = result.substring(0, result.length() - 1); + return result; + } + +} diff --git a/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/LoginAction.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/LoginAction.java new file mode 100644 index 0000000000..4c4ca22187 --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package me.lzb.homework0319.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/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/Struts.java b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/Struts.java new file mode 100644 index 0000000000..aea8d1105d --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/main/java/me/lzb/homework0319/litestruts/Struts.java @@ -0,0 +1,113 @@ +package me.lzb.homework0319.litestruts; + +import org.apache.commons.lang3.StringUtils; +import org.dom4j.DocumentException; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + +/* + +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 class Struts { + + private static final String XML = "struts.xml"; + + private static final XmlUtil resource = createResource(XML); + + private static XmlUtil createResource(String xml){ + try { + return new XmlUtil(xml); + } catch (DocumentException e) { + e.printStackTrace(); + } + return null; + } + + + public static View runAction(String actionName, Map parameters) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException { + + Object loginAction = getAuctionByName(actionName); + + invokeSetMethods(loginAction, parameters); + + String resultName = invokeExecute(loginAction).toString(); + + View view = new View(); + view.setJsp(resource.getResultJsp(actionName, resultName)); + view.setParameters(invokeGetMethods(loginAction)); + + return view; + } + + private static Object getAuctionByName(String auctionName) throws ClassNotFoundException, IllegalAccessException, InstantiationException { + Class c = Class.forName(resource.getAuctionPathByName(auctionName)); + return c.newInstance(); + } + + + private static Object invokeExecute(Object o) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Class c = o.getClass(); + Method mExectue = c.getDeclaredMethod("execute"); + return mExectue.invoke(o); + } + + + private static void invokeSetMethods(Object o, Map parameteMap) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Class c = o.getClass(); + Method[] methods = c.getDeclaredMethods(); + for (int i = 0; i< methods.length; i++) { + String name = methods[i].getName(); + if(StringUtils.startsWith(name, "set")){ + String key = name.replaceAll("^set", "").toLowerCase(); + if(parameteMap.containsKey(key)){ + methods[i].invoke(o, parameteMap.get(key)); + } + } + } + +// //这样参数类型不固定的话不好搞 +// for (Map.Entry entry : parameteMap.entrySet()) { +// Method mSetter = c.getDeclaredMethod("set" + StringUtils.capitalize(entry.getKey()), String.class); +// mSetter.invoke(o, entry.getValue()); +// } + } + + + private static Map invokeGetMethods(Object o) throws InvocationTargetException, IllegalAccessException { + Map resultMap = new HashMap(); + Class c = o.getClass(); + Method[] methods = c.getDeclaredMethods(); + for(int i =0 ;i + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/ArrayListTest.java b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/ArrayListTest.java index c35ed8d2b4..4938e6a8ac 100644 --- a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/ArrayListTest.java +++ b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/ArrayListTest.java @@ -1,5 +1,7 @@ package me.lzb.homework0312.basic; +import me.lzb.homework0312.basic.ArrayList; +import me.lzb.homework0312.basic.Iterator; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; @@ -68,29 +70,21 @@ public void addTest() { @Test public void removeTest() throws IndexOutOfBoundsException { - - ArrayList list = new ArrayList(); - list.add("a"); - list.add("b"); - list.add("c"); - list.add("d"); - - - String r1 = list.remove(1).toString(); + String r1 = arrayList.remove(1).toString(); Assert.assertEquals("b", r1); - Assert.assertEquals(3, list.size()); + Assert.assertEquals(3, arrayList.size()); - String r0 = list.remove(0).toString(); + String r0 = arrayList.remove(0).toString(); Assert.assertEquals("a", r0); - Assert.assertEquals(2, list.size()); + Assert.assertEquals(2, arrayList.size()); - String rs = list.remove(list.size() - 1).toString(); + String rs = arrayList.remove(arrayList.size() - 1).toString(); Assert.assertEquals("d", rs); - Assert.assertEquals(1, list.size()); + Assert.assertEquals(1, arrayList.size()); thrown.expect(IndexOutOfBoundsException.class); thrown.expectMessage("index boom"); - list.remove(100); + arrayList.remove(100); } diff --git a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/LinkedListTest.java b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/LinkedListTest.java index 740d8768c7..0d48c290f1 100644 --- a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/LinkedListTest.java +++ b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0312/basic/LinkedListTest.java @@ -1,5 +1,7 @@ package me.lzb.homework0312.basic; +import me.lzb.homework0312.basic.Iterator; +import me.lzb.homework0312.basic.LinkedList; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; @@ -69,30 +71,22 @@ public void addTest() { @Test public void removeTest() throws IndexOutOfBoundsException { - - LinkedList list = new LinkedList(); - list.add("a"); - list.add("b"); - list.add("c"); - list.add("d"); - - - String r1 = list.remove(1).toString(); + String r1 = linkedList.remove(1).toString(); Assert.assertEquals("b", r1); - Assert.assertEquals(3, list.size()); + Assert.assertEquals(3, linkedList.size()); - String r0 = list.remove(0).toString(); + String r0 = linkedList.remove(0).toString(); Assert.assertEquals("a", r0); - Assert.assertEquals(2, list.size()); + Assert.assertEquals(2, linkedList.size()); - String rs = list.remove(list.size() - 1).toString(); + String rs = linkedList.remove(linkedList.size() - 1).toString(); Assert.assertEquals("d", rs); - Assert.assertEquals(1, list.size()); + Assert.assertEquals(1, linkedList.size()); thrown.expect(IndexOutOfBoundsException.class); thrown.expectMessage("index boom"); - list.remove(100); - list.remove(-1); + linkedList.remove(100); + linkedList.remove(-1); } diff --git a/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0319/litestruts/StrutsTest.java b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0319/litestruts/StrutsTest.java new file mode 100644 index 0000000000..027c33eba9 --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/test/java/me/lzb/homework0319/litestruts/StrutsTest.java @@ -0,0 +1,41 @@ +package me.lzb.homework0319.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws Exception{ + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + Assert.assertEquals("login successful", view.getParameters().get("message")); + } + + @Test + public void testLoginActionFailed() throws Exception{ + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //密码和预设的不一致 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group24/1148285693/learning2017/learning-basic/src/test/resources/litestruts/struts.xml b/group24/1148285693/learning2017/learning-basic/src/test/resources/litestruts/struts.xml new file mode 100644 index 0000000000..81c153757c --- /dev/null +++ b/group24/1148285693/learning2017/learning-basic/src/test/resources/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group24/1148285693/learning2017/pom.xml b/group24/1148285693/learning2017/pom.xml index 6493774794..4951c1ff42 100644 --- a/group24/1148285693/learning2017/pom.xml +++ b/group24/1148285693/learning2017/pom.xml @@ -65,13 +65,43 @@ - + junit junit 4.12 + + + dom4j + dom4j + 1.6.1 + + + + jaxen + jaxen + 1.1.6 + + + + + commons-io + commons-io + 2.5 + + + org.apache.commons + commons-lang3 + 3.5 + + + commons-codec + commons-codec + 1.10 + +