diff --git a/group12/563253496/week2/out/production/week2/litestruts/struts.xml b/group12/563253496/week2/out/production/week2/litestruts/struts.xml new file mode 100644 index 0000000000..bfc639857c --- /dev/null +++ b/group12/563253496/week2/out/production/week2/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/group12/563253496/week2/src/array/ArrayUtil.java b/group12/563253496/week2/src/array/ArrayUtil.java new file mode 100644 index 0000000000..d9236e72d4 --- /dev/null +++ b/group12/563253496/week2/src/array/ArrayUtil.java @@ -0,0 +1,329 @@ +package 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 temp; + for (int i = 0; i < origin.length / 2; i++) { + temp = origin[i]; + origin[i] = origin[origin.length - i - 1]; + origin[origin.length - i - 1] = temp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * + * @param oldArray + * @return + */ + + public int[] removeZero(int[] oldArray) { + int count = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) { + count++; + } + } + int[] newArray = new int[oldArray.length - count]; + int flag = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArray[flag] = oldArray[i]; + flag++; + } else { + continue; + } + } + return newArray; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + + /* + int[] temp = new int[array1.length + array2.length]; + int count = array1.length; + int point = array1.length; + for (int i = 0; i < array1.length; i++) { + temp[i] = array1[i]; + } + boolean flag = true; + for (int i = 0; i < array2.length; i++) { + for (int j = 0; j < array1.length; j++) { + if (array1[j] == array2[i]) { + flag = false; + } + } + if (flag) { + temp[count]=array2[i]; + count++; + } + flag = true; + } + */ + if (array1.length == 0) { + return array2; + } + if (array2.length == 0) { + return array1; + } + + int[] temp = new int[array1.length + array2.length]; + int ap = 0; + int bp = 0; + int count = 0; + while (ap < array1.length && bp < array2.length) { + if (array1[ap] == array2[bp]) { + temp[count] = array1[ap]; + ap++; + bp++; + count++; + } else if (array1[ap] > array2[bp]) { + temp[count] = array2[bp]; + bp++; + count++; + } else { + temp[count] = array1[ap]; + ap++; + count++; + } + } + if (ap == array1.length) { + for (int i = bp; i < array2.length; i++) { + temp[count] = array2[i]; + count++; + } + } else if (bp == array2.length) { + for (int i = ap; i < array1.length; i++) { + temp[count] = array1[i]; + count++; + } + + + } + int array3[] = new int[count]; + System.arraycopy(temp, 0, array3, 0, count); + + return array3; + /*int[] temp = new int[array2.length]; + boolean flag = true; + int count = 0; + for (int i = 0; i < array2.length; i++) { + for (int j = 0; j < array1.length; j++) { + if (array2[j] == array1[i]) { + flag = false; + } + } + if (flag) { + temp[count] = array2[i]; + count++; + } + } + if (count == 0) { + return array1; + } + + int ap = 0; //数组1的指针 + int bp = 0; //数组2的指针 + int[] array3 = new int[count + array1.length]; + for (int i = 0; i < array3.length; i++) { + if (array1[ap] > array2[bp]) { + array3[i] = array2[bp]; + bp++; + }else { + array3[i] = array1[ap]; + ap++; + } + + } +*/ + + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public int[] grow(int[] oldArray, int size) { + int[] newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * + * @param max + * @return + */ + public int[] fibonacci(int max) { + + if (max == 1) { + return null; + } + int a = 1; + int b = 1; + int[] result = {1, 1}; + int[] temp; + while (b < max) { + + b = a + b; + a = b - a; + temp = result; + result = new int[result.length + 1]; + + for (int i = 0; i < temp.length; i++) { + result[i] = temp[i]; + } + result[result.length - 1] = b; + } + temp = result; + result = new int[result.length - 1]; + for (int i = 0; i < result.length; i++) { + result[i] = temp[i]; + } + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + /* int[] result = null; + int[] temp = null; + if (max < 2) { + return null; + } + boolean flag = true; + for (int i = 2; i < max; i++) { + for (int j = 2; j * j < i; j++) { + if (i % j == 0) { + flag = false; + } + } + if (flag) { + if (result == null) { + result = new int[1]; + result[0] = i; + } else { + temp = result; + result = new int[result.length + 1]; + for (int j = 0; j < temp.length; j++) { + result[j] = temp[j]; + } + result[result.length - 1] = i; + } + } + flag = true; + } + + return result;*/ + if (max < 2) { + return null; + } + int[] result = {2}; + int[] temp ; + boolean flag = true; + for (int i = 3; i < max; i++) { + for (int j = 2; j * j <= i; j++) { + if (i % j == 0){ + flag = false; + } + } + if (flag) { + temp=result; + result=new int[temp.length+1]; + System.arraycopy(temp,0,result,0,temp.length); + result[result.length-1]=i; + } + flag=true; + + } + return result; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int[] result = {}; + int[] temp = null; + int count = 0; + for (int i = 1; i < max; i++) { + for (int j = 1; j < i; j++) { + if (i % j == 0) { + count += j; + } + } + if (count == i) { + temp = result; + result = new int[temp.length + 1]; + for (int j = 0; j < temp.length; j++) { + result[j] = temp[j]; + } + result[result.length - 1] = i; + } + + } + return result; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < array.length - 1; i++) { + sb.append(array[i]); + sb.append(seperator); + } + sb.append(array[array.length - 1]); + return sb.toString(); + + } + + +} diff --git a/group12/563253496/week2/src/litestruts/LoginAction.java b/group12/563253496/week2/src/litestruts/LoginAction.java new file mode 100644 index 0000000000..bba4c11c9f --- /dev/null +++ b/group12/563253496/week2/src/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package 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/group12/563253496/week2/src/litestruts/Struts.java b/group12/563253496/week2/src/litestruts/Struts.java new file mode 100644 index 0000000000..4c478cae5e --- /dev/null +++ b/group12/563253496/week2/src/litestruts/Struts.java @@ -0,0 +1,128 @@ +package litestruts; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法,例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + + //读取配置文件 + + SAXReader reader = new SAXReader(); + try { + Document document = reader.read("src/litestruts/struts.xml"); + Element root = document.getRootElement(); + Element temp = null; + + /* + *保存root的子节点 + Map lables = new HashMap(); + + for (int i = 0; i < root.elements().size(); i++) { + temp = (Element) root.elements().get(i); + lables.put(temp.attribute(0).getText(), i); + } + */ + + Map viewResult = new HashMap<>(); + Map resultLable = new HashMap<>(); + Map firstLayorNode = new HashMap<>(); + + //保存根节点的子节点 + for (int i = 0; i < root.elements().size(); i++) { + temp = (Element)root.elements().get(i); + firstLayorNode.put(temp.attribute("name").getText(),i); + } + + View view = new View(); + + Element actionLable = (Element)root.elements().get(firstLayorNode.get(actionName)); + + if (actionName.equals("login")) { + + + for (int i = 0; i < actionLable.elements().size(); i++) { + temp = (Element) actionLable.elements().get(i); + resultLable.put(temp.attribute("name").getText(), temp.getText()); + } + + + Class clazz = Class.forName(actionLable.attribute("class").getText()); + + LoginAction la = (LoginAction) clazz.newInstance(); + la.setName(parameters.get("name")); + la.setPassword(parameters.get("password")); + + Method m = clazz.getMethod("execute"); + String result = (String) m.invoke(la); + + + view.setJsp(resultLable.get(result)); + + + m = clazz.getMethod("getMessage"); + result = (String) m.invoke(la); + viewResult.put("message", result); + + m = clazz.getMethod("getName"); + result = (String) m.invoke(la); + viewResult.put("name", result); + + m = clazz.getMethod("getPassword"); + result = (String)m.invoke(la); + viewResult.put("password", result); + + view.setParameters(viewResult); + return view; + } + + + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + + return null; + } + +} diff --git a/group12/563253496/week2/src/litestruts/StrutsTest.java b/group12/563253496/week2/src/litestruts/StrutsTest.java new file mode 100644 index 0000000000..d6be3a2d14 --- /dev/null +++ b/group12/563253496/week2/src/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package 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/group12/563253496/week2/src/litestruts/View.java b/group12/563253496/week2/src/litestruts/View.java new file mode 100644 index 0000000000..1eed614744 --- /dev/null +++ b/group12/563253496/week2/src/litestruts/View.java @@ -0,0 +1,23 @@ +package litestruts; + +import java.util.Map; + +public class View { + private String jsp; + private Map parameters; + + public String getJsp() { + return jsp; + } + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + public Map getParameters() { + return parameters; + } + public View setParameters(Map parameters) { + this.parameters = parameters; + return this; + } +} diff --git a/group12/563253496/week2/src/litestruts/struts.xml b/group12/563253496/week2/src/litestruts/struts.xml new file mode 100644 index 0000000000..bfc639857c --- /dev/null +++ b/group12/563253496/week2/src/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group12/563253496/week2/src/litestruts/test.java b/group12/563253496/week2/src/litestruts/test.java new file mode 100644 index 0000000000..f29f7ee366 --- /dev/null +++ b/group12/563253496/week2/src/litestruts/test.java @@ -0,0 +1,44 @@ +package litestruts; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +/** + * Created by bdl19 on 2017/3/2. + */ +public class test { + public static void main(String[] args) { + SAXReader reader = new SAXReader(); + try { + Document document = reader.read("src/litestruts/struts.xml"); + Element root = document.getRootElement(); + System.out.println(root.elements().size()); + + +/* + + String s = "login"; + String string = "login"; + System.out.println(root.getName()); + Element action = root.element("action"); + System.out.print(action.getName()); + System.out.print(action.attribute("name").getText()); + System.out.print(action.attribute("name").getText()); + String str = action.attribute("name").getText(); + if (str.equals(s)) { + System.out.println(123123); + System.out.println("123"); + // Class clazz = Class.forName(action.attribute("class")); + } + System.out.println(s); + System.out.println(str); + +*/ + + } catch (DocumentException e) { + e.printStackTrace(); + } + } +} diff --git a/group12/563253496/week2/src/test/array/ArrayUtilTest.java b/group12/563253496/week2/src/test/array/ArrayUtilTest.java new file mode 100644 index 0000000000..57a0c6d648 --- /dev/null +++ b/group12/563253496/week2/src/test/array/ArrayUtilTest.java @@ -0,0 +1,166 @@ +package test.array; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.Before; +import org.junit.After; + +import array.ArrayUtil; + +import java.lang.reflect.Array; + +/** + * ArrayUtil Tester. + * + * @author + * @version 1.0 + * @since
���� 2, 2017
+ */ +public class ArrayUtilTest { + + @Before + public void before() throws Exception { + + } + + @After + public void after() throws Exception { + + } + + /** + * Method: reverseArray(int[] origin) + */ + @Test + public void testReverseArray() throws Exception { +//TODO: Test goes here... + ArrayUtil au = new ArrayUtil(); + int[] a = {7, 9, 30, 3}; + int[] b = {3, 30, 9, 7}; + au.reverseArray(a); + for (int i = 0; i < a.length; i++) { + Assert.assertEquals(b[i], a[i]); + } + + a = new int[]{7, 9, 30, 3, 4}; + b = new int[]{4, 3, 30, 9, 7}; + au.reverseArray(a); + for (int i = 0; i < a.length; i++) { + Assert.assertEquals(b[i], a[i]); + } + + } + + /** + * Method: removeZero(int[] oldArray) + */ + @Test + public void testRemoveZero() throws Exception { +//TODO: Test goes here... + ArrayUtil au = new ArrayUtil(); + + int oldArr[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + int result[] = au.removeZero(oldArr); + int exResult[] = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}; + Assert.assertEquals(result.length,exResult.length); + for (int i = 0; i < result.length; i++) { + Assert.assertEquals(result[i], exResult[i]); + + } + } + + /** + * Method: merge(int[] array1, int[] array2) + */ + @Test + public void testMerge() throws Exception { +//TODO: Test goes here... + ArrayUtil au = new ArrayUtil(); + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + int[] a3 = {3, 4, 5, 6, 7, 8}; + int[] result = au.merge(a1,a2); + Assert.assertEquals(a3.length, result.length); + for (int i = 0; i < result.length; i++) { + Assert.assertEquals(a3[i],result[i]); + } + } + + /** + * Method: grow(int[] oldArray, int size) + */ + @Test + public void testGrow() throws Exception { +//TODO: Test goes here... + ArrayUtil au = new ArrayUtil(); + + int[] oldArray = {2,3,6}; + int size = 3; + + int[] newArray = au.grow(oldArray,size); + int[] exArray = {2,3,6,0,0,0}; + for (int i = 0; i < newArray.length; i++) { + Assert.assertEquals(exArray[i],newArray[i]); + } + + } + + /** + * Method: fibonacci(int max) + */ + @Test + public void testFibonacci() throws Exception { +//TODO: Test goes here... + ArrayUtil au = new ArrayUtil(); + int[] exArray = {1,1,2,3,5,8,13}; + int[] newArray = au.fibonacci(14); + for (int i = 0; i < newArray.length; i++) { + Assert.assertEquals(exArray[i],newArray[i]); + } + } + + /** + * Method: getPrimes(int max) + */ + @Test + public void testGetPrimes() throws Exception { +//TODO: Test goes here... + ArrayUtil au = new ArrayUtil(); + int[] exArray = {2,3,5,7,11,13,17,19}; + int[] newArray = au.getPrimes(23); + for (int i = 0; i < newArray.length; i++) { + Assert.assertEquals(exArray[i],newArray[i]); + } + } + + /** + * Method: getPerfectNumbers(int max) + */ + @Test + public void testGetPerfectNumbers() throws Exception { +//TODO: Test goes here... + ArrayUtil au = new ArrayUtil(); + int exArray[] = {6,28,496}; + int[] newArray = au.getPerfectNumbers(1000); + for (int i = 0; i < newArray.length; i++) { + Assert.assertEquals(exArray[i],newArray[i]); + } + + + } + + /** + * Method: join(int[] array, String seperator) + */ + @Test + public void testJoin() throws Exception { +//TODO: Test goes here... + ArrayUtil au = new ArrayUtil(); + String exS = "3-8-9"; + int[] array= {3,8,9}; + String result = au.join(array,"-"); + Assert.assertEquals(exS,result); + } + + +}