From 1ba1b858c3fc46df7206a197714ad0072c66f346 Mon Sep 17 00:00:00 2001 From: jiangshaojie Date: Sat, 4 Mar 2017 22:24:56 +0800 Subject: [PATCH] 3.5 --- .../src/com/coderising/array/ArrayUtil.java | 283 ++++++++++++++++++ .../com/coderising/array/ArrayUtilTest.java | 163 ++++++++++ .../coderising/litestruts/LoginAction.java | 39 +++ .../src/com/coderising/litestruts/Struts.java | 108 +++++++ .../com/coderising/litestruts/StrutsTest.java | 44 +++ .../src/com/coderising/litestruts/View.java | 23 ++ .../src/com/coderising/litestruts/struts.xml | 11 + 7 files changed, 671 insertions(+) create mode 100644 group15/1502_1617273078/src/com/coderising/array/ArrayUtil.java create mode 100644 group15/1502_1617273078/src/com/coderising/array/ArrayUtilTest.java create mode 100644 group15/1502_1617273078/src/com/coderising/litestruts/LoginAction.java create mode 100644 group15/1502_1617273078/src/com/coderising/litestruts/Struts.java create mode 100644 group15/1502_1617273078/src/com/coderising/litestruts/StrutsTest.java create mode 100644 group15/1502_1617273078/src/com/coderising/litestruts/View.java create mode 100644 group15/1502_1617273078/src/com/coderising/litestruts/struts.xml diff --git a/group15/1502_1617273078/src/com/coderising/array/ArrayUtil.java b/group15/1502_1617273078/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..de36e61726 --- /dev/null +++ b/group15/1502_1617273078/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,283 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + * 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + * 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public void reverseArray(int[] origin) { + int[] a = new int[origin.length]; + for (int i = 0; i < origin.length; i++) { + a[i] = origin[origin.length - 1 - i]; + } + for (int i = 0; i < a.length; i++) { + System.out.print(a[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) { + int count = 0; + int index = 0; + //int[] brige = new int[oldArray.length]; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + + count++; + } + } + int[] result = new int[count]; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + result[index++] = oldArray[i]; + } + + + } + 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] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + int alength = array1.length; + int blength = array2.length; + int[] newint = new int[alength + blength]; + for (int i = 0; i < alength; i++) { + newint[i] = array1[i]; + } + int index = alength; + //有相同项为true,没有为false + boolean flag = false; + for (int c = 0; c < blength; c++) { + for (int j = 0; j < alength; j++) { + if (array1[j] == array2[c]) { + + flag = true; + break; + } + + } + if (flag) { + + flag = false; + } else { + newint[index] = array2[c]; + index++; + } + + } + // 去零 + newint = removeZero(newint); + //排序 + + quickSort(newint); + return newint; + } + + /** + * 把一个已经存满数据的数组 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[] newarry = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newarry[i] = oldArray[i]; + } + return newarry; + } + + /** + * 斐波那契数列为: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 count = 0; + for (int i = 0; ; i++) { + if (createfibonacci(i + 1) < max) { + count++; + } else { + break; + + } + } + int[] arry = new int[count]; + for (int a = 0; a < count; a++) { + arry[a] = createfibonacci(a + 1); + } + return arry; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int count = 0; + for (int i = 0; i < max; i++) { + if (isprime(i)) { + count++; + } + } + int[] arry = new int[count]; + int sign = 0; + for (int i = 0; i < max; i++) { + if (isprime(i)) { + arry[sign] = i; + sign++; + } + } + return arry; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int count = 0; + for (int i = 0; i < max; i++) { + if (isperfectnmber(i)) { + count++; + } + } + int[] arry = new int[count]; + int sign = 0; + for (int i = 0; i < max; i++) { + if (isperfectnmber(i)) { + arry[sign] = i; + sign++; + } + } + return arry; + + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param + * @return + */ + public String join(int[] array, String seperator) { + String stringBuilder=new String(String.valueOf(array[0])); + for (int i = 1; i = pivot) --high; + arr[low] = arr[high]; //交换比枢轴小的记录到左端 + while (low < high && arr[low] <= pivot) ++low; + arr[high] = arr[low]; //交换比枢轴小的记录到右端 + } + //扫描完成,枢轴到位 + arr[low] = pivot; + //返回的是枢轴的位置 + return low; + } + + //生成斐波那契数列 + public static int createfibonacci(int n) { + if (n <= 2) { + return 1; + } else { + return createfibonacci(n - 1) + createfibonacci(n - 2); + } + } + + //判断是否是素数 + public static boolean isprime(int a) { + boolean flag = true; + if (a < 2) { + return false; + } else { + for (int i = 2; i <= Math.sqrt(a); i++) { + if (a % i == 0) { + flag = false; + break; + } + } + } + return flag; + } + + //判断是否是完数 + public static boolean isperfectnmber(int a) { + boolean flag = true; + int temp = 0;// 定义因子之和变量 + + for (int n = 1; n < a / 2 + 1; n++) { + if (a % n == 0) { + temp += n;// 能被整除的除数则被加到temp中 + } + } + if (temp == a) {// 如果因子之和与原数相等的话,说明是完数 + //System.out.print(a + " ");// 输出完数 + flag = true; + } else { + flag = false; + } + return flag; + } + +} diff --git a/group15/1502_1617273078/src/com/coderising/array/ArrayUtilTest.java b/group15/1502_1617273078/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..17f810bc0f --- /dev/null +++ b/group15/1502_1617273078/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,163 @@ +package com.coderising.array; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** +* ArrayUtil Tester. +* +* @author +* @since
 27, 2017
+* @version 1.0 +*/ +public class ArrayUtilTest { + +@Before +public void before() throws Exception { + ArrayUtil arrayUtil = new ArrayUtil(); + int[] a = new int[10]; + a[0] = 1; + a[1] = 2; + a[2] = 3; + a[3] = 4; + a[4] = 5; + a[5] = 6; +} + +@After +public void after() throws Exception { +} + +/** +* +* Method: reverseArray(int[] origin) +* +*/ +@Test +public void testReverseArray() throws Exception { +//TODO: Test goes here... + int[] a = new int[10]; + a[0] = 1; + a[1] = 2; + a[2] = 3; + a[3] = 4; + a[4] = 5; + a[5] = 6; + ArrayUtil arrayUtil = new ArrayUtil(); + for (int i = 0; i parameters) throws DocumentException { + + /* + + 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字段中。 + + */ + HashMap Parameters = new HashMap<>(); + View view = new View(); + SAXReader reader = new SAXReader(); + Document document = reader.read(new File("./src/com/coderising/litestruts/struts.xml")); + Element root = document.getRootElement(); + Element thiselement = null; + List list = root.elements(); + String classpath = null; + for (Element element : list) { + //System.out.println(element.attribute("name").getValue()); + if (element.attribute("name").getValue().equals(actionName)) { + thiselement = element; + classpath = element.attribute("class").getValue(); + break; + } + } + Class ojbect; + + try { + ojbect=Class.forName(classpath); + Object acionclass = ojbect.newInstance(); + for (String string : parameters.keySet()) { + Method method = ojbect.getMethod("set"+upperCase(string),String.class); + method.invoke(acionclass,parameters.get(string)); + } + Method execute = ojbect.getMethod("execute"); + String result= (String) execute.invoke(acionclass); + //判断返回的jsp + List elementresult = thiselement.elements(); + for (Element element:elementresult){ + if (element.attribute("name").getValue().equals(result)) { + view.setJsp(element.getStringValue()); + break; + } + } + // 获得getter方法,并设置Parameters + Method[] methods = ojbect.getDeclaredMethods(); + + for (Method method : methods) { + if (method.getName().substring(0, 3).equals("get")) { + String attribute= (String) method.invoke(acionclass); + Parameters.put(method.getName().substring(3).toLowerCase(), attribute); + } + } + view.setParameters(Parameters); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + + return view; + } + //首字母大写 + public static String upperCase(String str) { + char[] ch = str.toCharArray(); + if (ch[0] >= 'a' && ch[0] <= 'z') { + ch[0] = (char) (ch[0] - 32); + } + return new String(ch); + } + +} diff --git a/group15/1502_1617273078/src/com/coderising/litestruts/StrutsTest.java b/group15/1502_1617273078/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..05b7a00aa8 --- /dev/null +++ b/group15/1502_1617273078/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,44 @@ +package com.coderising.litestruts; + +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 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 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/group15/1502_1617273078/src/com/coderising/litestruts/View.java b/group15/1502_1617273078/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group15/1502_1617273078/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/group15/1502_1617273078/src/com/coderising/litestruts/struts.xml b/group15/1502_1617273078/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..07f80b6476 --- /dev/null +++ b/group15/1502_1617273078/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