From b2e5179772db753a09f05167d7e4b1b042a46f56 Mon Sep 17 00:00:00 2001 From: kai Date: Sun, 5 Mar 2017 21:57:42 +0800 Subject: [PATCH] second homework --- group06/1378560653/.classpath | 1 + .../src/com/coderising/array/ArrayUtil.java | 208 ++++++++++++++++++ .../coderising/litestruts/LoginAction.java | 39 ++++ .../coderising/litestruts/StructsTest.java | 40 ++++ .../src/com/coderising/litestruts/Struts.java | 56 +++++ .../src/com/coderising/litestruts/View.java | 23 ++ .../src/com/coderising/litestruts/structs.xml | 11 + 7 files changed, 378 insertions(+) create mode 100644 group06/1378560653/src/com/coderising/array/ArrayUtil.java create mode 100644 group06/1378560653/src/com/coderising/litestruts/LoginAction.java create mode 100644 group06/1378560653/src/com/coderising/litestruts/StructsTest.java create mode 100644 group06/1378560653/src/com/coderising/litestruts/Struts.java create mode 100644 group06/1378560653/src/com/coderising/litestruts/View.java create mode 100644 group06/1378560653/src/com/coderising/litestruts/structs.xml diff --git a/group06/1378560653/.classpath b/group06/1378560653/.classpath index fb5011632c..3e0fb272a8 100644 --- a/group06/1378560653/.classpath +++ b/group06/1378560653/.classpath @@ -2,5 +2,6 @@ + diff --git a/group06/1378560653/src/com/coderising/array/ArrayUtil.java b/group06/1378560653/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..11fefff510 --- /dev/null +++ b/group06/1378560653/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,208 @@ +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 N = origin.length; + for(int i = 0; i < N/2; i++){ + int temp = origin[i]; + origin[i] = origin[N-i-1]; + origin[N-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 N = oldArray.length; + int[] newArray = new int[N]; + int j = 0; + for(int i = 0; i < N; i++){ + if(oldArray[i] != 0){ + newArray[j] = oldArray[i]; + j++; + } + } + 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 N = array1.length + array2.length; + int[] array3 = new int[N]; + System.arraycopy(array1, 0, array3, 0, array1.length); + for(int i = 0; i < N; i++){ + for(int j = 0; j < array2.length; j++){ + if(array3[i] > array2[j]){ + System.arraycopy(array3, i , array3, i+1, N-i-1); + array3[i] = array2[j]; + } + } + } + return array3; + } + /** + * 把一个已经存满数据的数组 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[] zero = new int[0]; + int[] array = new int[100]; + array[0] = 1; + array[1] = 1; + int i = 1; + + if(max == 1){ + return zero; + }else{ + while(array[i] <= max){ + i++; + if(i > array.length){ + grow(array, i*2); + } + array[i+1] = array[i] + array[i-1]; + } + return array; + } + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + boolean[] prime = new boolean[max + 1]; + int[] zero = new int[0]; + int[] array = new int[max]; + int q = 1; + if(max == 1 || max ==2){ + return zero; + }else{ + for(int i = 3; i < max; i++) + if(i % 2 == 0){ + prime[i] = false; + }else{ + prime[i] = true; + } + + for(int i = 3; i < Math.sqrt(max); i++){//因子;若n是合数,则其所有因子都不超过sqrt(n) + if(prime[i]) + for(int j = 2 * i; j<= max; j += i){//其倍数 + prime[j] = false; + } + } + array[0] = 2; + for(int p = 0; p < max; p++){ + if(prime[p] == true){ + array[q] = p; + q++; + } + } + return array; + } + } + /*int[] zero = new int[0]; + int[] array = new int[100]; + int k = 0; + + if(max == 1 || max == 2){ + return zero; + }else{ + for(int n = 2; n <= max; n++){ + int isPrime = 1; + for(int i = 2; i < n; i++){ + if(n % i == 0){ + isPrime = 0; + break; + } + if(isPrime == 1){ + array[k] = n; + k++; + } + } + } + return array; + }*/ + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] perfectNumbers = new int[max]; + int n = 0; + for(int i = 1; i < max; i++){//i:要判断的数 + int sum = 0; + for(int j = 1; j < i; j++){//j:可能因子 + if(i % j == 0){ + sum += j; + } + } + if(sum == i){ + perfectNumbers[n] = i; + n++; + } + } + return perfectNumbers; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + String s = ""; + for(int i = 0; i < array.length; i++){ + s = s + array[i] + seperator; + } + return s; + } + + +} \ No newline at end of file diff --git a/group06/1378560653/src/com/coderising/litestruts/LoginAction.java b/group06/1378560653/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..76547ac3b3 --- /dev/null +++ b/group06/1378560653/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/group06/1378560653/src/com/coderising/litestruts/StructsTest.java b/group06/1378560653/src/com/coderising/litestruts/StructsTest.java new file mode 100644 index 0000000000..4e761b0b2d --- /dev/null +++ b/group06/1378560653/src/com/coderising/litestruts/StructsTest.java @@ -0,0 +1,40 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + +public class StructsTest { + + @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/group06/1378560653/src/com/coderising/litestruts/Struts.java b/group06/1378560653/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..0e73017f99 --- /dev/null +++ b/group06/1378560653/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,56 @@ +package com.coderising.litestruts; + +import java.io.IOException; +import java.util.Map; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + + +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字段中。 + + */ + + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + try { + DocumentBuilder db = dbf.newDocumentBuilder(); + Document document = db.parse("structs.xml"); + NodeList actionList = document.getElementsByTagName("action"); + + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } + +} \ No newline at end of file diff --git a/group06/1378560653/src/com/coderising/litestruts/View.java b/group06/1378560653/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..f68a8c438b --- /dev/null +++ b/group06/1378560653/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/group06/1378560653/src/com/coderising/litestruts/structs.xml b/group06/1378560653/src/com/coderising/litestruts/structs.xml new file mode 100644 index 0000000000..dd598a3664 --- /dev/null +++ b/group06/1378560653/src/com/coderising/litestruts/structs.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file