From ff0984fe86088837601c5e41eeae03049670b3dc Mon Sep 17 00:00:00 2001 From: hejj Date: Mon, 6 Mar 2017 16:44:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- liuxin/.classpath | 7 - liuxin/.gitignore | 1 - liuxin/.project | 17 -- liuxin/.settings/org.eclipse.jdt.core.prefs | 11 - .../src/com/coderising/array/ArrayUtil.java | 213 ------------------ .../coderising/litestruts/LoginAction.java | 39 ---- .../src/com/coderising/litestruts/Struts.java | 167 -------------- .../com/coderising/litestruts/StrutsTest.java | 43 ---- .../src/com/coderising/litestruts/View.java | 23 -- .../src/com/coderising/litestruts/struts.xml | 11 - liuxin/src/com/coding/basic/ArrayList.java | 32 --- .../src/com/coding/basic/BinaryTreeNode.java | 32 --- liuxin/src/com/coding/basic/Iterator.java | 7 - liuxin/src/com/coding/basic/LinkedList.java | 46 ---- liuxin/src/com/coding/basic/List.java | 9 - liuxin/src/com/coding/basic/Queue.java | 19 -- liuxin/src/com/coding/basic/Stack.java | 22 -- liuxin/struts.xml | 11 - 18 files changed, 710 deletions(-) delete mode 100644 liuxin/.classpath delete mode 100644 liuxin/.gitignore delete mode 100644 liuxin/.project delete mode 100644 liuxin/.settings/org.eclipse.jdt.core.prefs delete mode 100644 liuxin/src/com/coderising/array/ArrayUtil.java delete mode 100644 liuxin/src/com/coderising/litestruts/LoginAction.java delete mode 100644 liuxin/src/com/coderising/litestruts/Struts.java delete mode 100644 liuxin/src/com/coderising/litestruts/StrutsTest.java delete mode 100644 liuxin/src/com/coderising/litestruts/View.java delete mode 100644 liuxin/src/com/coderising/litestruts/struts.xml delete mode 100644 liuxin/src/com/coding/basic/ArrayList.java delete mode 100644 liuxin/src/com/coding/basic/BinaryTreeNode.java delete mode 100644 liuxin/src/com/coding/basic/Iterator.java delete mode 100644 liuxin/src/com/coding/basic/LinkedList.java delete mode 100644 liuxin/src/com/coding/basic/List.java delete mode 100644 liuxin/src/com/coding/basic/Queue.java delete mode 100644 liuxin/src/com/coding/basic/Stack.java delete mode 100644 liuxin/struts.xml diff --git a/liuxin/.classpath b/liuxin/.classpath deleted file mode 100644 index b387714202..0000000000 --- a/liuxin/.classpath +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/liuxin/.gitignore b/liuxin/.gitignore deleted file mode 100644 index ae3c172604..0000000000 --- a/liuxin/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/bin/ diff --git a/liuxin/.project b/liuxin/.project deleted file mode 100644 index d4d12e546b..0000000000 --- a/liuxin/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - HW2 - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/liuxin/.settings/org.eclipse.jdt.core.prefs b/liuxin/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index 3a21537071..0000000000 --- a/liuxin/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,11 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.8 -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.8 diff --git a/liuxin/src/com/coderising/array/ArrayUtil.java b/liuxin/src/com/coderising/array/ArrayUtil.java deleted file mode 100644 index 4798b2796d..0000000000 --- a/liuxin/src/com/coderising/array/ArrayUtil.java +++ /dev/null @@ -1,213 +0,0 @@ -package com.coderising.array; - -import java.util.ArrayList; -import java.util.List; - -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 len = origin.length; - for (int i = 0; i < len/2; i++) { - int temp = origin[i]; - origin[i] = origin[len-1-i]; - origin[len-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 slow = 0; - for (int i = 0; i < oldArray.length; i++) { - - if (oldArray[i] != 0) { - oldArray[slow++] = oldArray[i]; - } - } - int[] newArray = new int[slow]; - for (int i = 0; i < slow; i++) { - newArray[i] = oldArray[i]; - } - - 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[] result = new int[array1.length + array2.length]; - int i1 = 0 , i2 = 0, i3 = 0; - while (i1 < array1.length && i2 < array2.length) { - if (array1[i1] == array2[i2]) { - result[i3++] = array1[i1++]; - i2++; - } else if (array1[i1] < array2[i2]) { - result[i3++] = array1[i1++]; - } else { - result[i3++] = array2[i2++]; - } - } - - while (i1 < array1.length) { - result[i3++] = array1[i1++]; - } - while (i2 < array2.length) { - result[i3++] = array2[i2++]; - } - - int[] finalResult = new int[i3]; - for (int i = 0; i < i3; i++) { - finalResult[i] = result[i]; - } - - return finalResult; - } - /** - * 把一个已经存满数据的数组 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[] result = new int[oldArray.length + size]; - for (int i = 0; i < oldArray.length; i++) { - result[i] = oldArray[i]; - } - - return result; - - } - - /** - * 斐波那契数列为: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){ - List result = new ArrayList<>(); - if (max <= 1) return new int[]{}; - if (max < 2) return new int[]{1,1}; - result.add(1); - //result.add(1); - int num = 1, index = 1; - - while (num < max) { - result.add(num); - num = result.get(index) + result.get(index - 1); - index++; - - } - - int[] resArray = new int[result.size()]; - for (int i = 0; i < result.size(); i++) { - resArray[i] = result.get(i); - } - - return resArray; - } - - /** - * 返回小于给定最大值max的所有素数数组 - * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] - * @param max - * @return - */ - public int[] getPrimes(int max){ - List result = new ArrayList<>(); - boolean[] notPrime = new boolean[max]; - for (int i = 2; i < max; i++) { - if (!notPrime[i]) result.add(i); - for (int j = 2; i*j < max; j++) { - notPrime[i*j] = true; - } - } - - int[] resArray = new int[result.size()]; - for (int i = 0; i < result.size();i++) { - resArray[i] = result.get(i); - } - - return resArray; - - } - - /** - * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 - * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 - * @param max - * @return - */ - public int[] getPerfectNumbers(int max){ - List result = new ArrayList<>(); - for (int i = 1; i < max; i++) { - int sum = 0; - for (int j = 1; j < i/2; j++) { - if (i % j == 0) { - sum += j; - } - } - if (sum == i) result.add(i); - } - - int[] resArray = new int[result.size()]; - for (int i = 0; i < result.size(); i++) { - resArray[i] = result.get(i); - } - return resArray; - } - - /** - * 用seperator 把数组 array给连接起来 - * 例如array= [3,8,9], seperator = "-" - * 则返回值为"3-8-9" - * @param array - * @param s - * @return - */ - public String join(int[] array, String seperator){ - if (array == null || array.length == 0) return null; - int len = array.length; - char[] result = new char[len + seperator.length()*(len - 1)]; - int indexA = 0, indexR = 0; - while (indexA < array.length && indexR < result.length) { - result[indexR++] = (char)array[indexA++]; - if (indexA < array.length - 1){ - for (int i = 0; i < seperator.length(); i++) { - result[indexR++] = seperator.charAt(i); - } - } - } - - return String.valueOf(result); - - - - - } - - -} diff --git a/liuxin/src/com/coderising/litestruts/LoginAction.java b/liuxin/src/com/coderising/litestruts/LoginAction.java deleted file mode 100644 index 1005f35a29..0000000000 --- a/liuxin/src/com/coderising/litestruts/LoginAction.java +++ /dev/null @@ -1,39 +0,0 @@ -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/liuxin/src/com/coderising/litestruts/Struts.java b/liuxin/src/com/coderising/litestruts/Struts.java deleted file mode 100644 index 77d4d1642f..0000000000 --- a/liuxin/src/com/coderising/litestruts/Struts.java +++ /dev/null @@ -1,167 +0,0 @@ -package com.coderising.litestruts; - -import java.io.File; -import java.io.IOException; -import java.lang.reflect.Method; -import java.util.HashMap; -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.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; - - - -public class Struts { - - public static View runAction(String actionName, Map parameters) throws ClassNotFoundException { - - /* - - 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字段中。 - - */ - - - Class targetClass = getTargetClass("struts.xml", actionName); - Object foo = null; - try { - foo = targetClass.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - Method method = null; - Method methodOne = null; - String result = null; - Map viewParas = new HashMap<>(); - try { - for (String key : parameters.keySet()) { - - method = foo.getClass().getMethod("set"+key.substring(0, 1).toUpperCase()+key.substring(1), String.class); - method.invoke(foo, parameters.get(key)); - } - - methodOne = foo.getClass().getMethod("execute"); - result = (String) methodOne.invoke(foo); - Method[] allMethods = foo.getClass().getDeclaredMethods(); - for (Method m : allMethods) { - if (m.getName().startsWith("get")) { - viewParas.put(lowerFirstLetter(m.getName().substring(3)), (String) m.invoke(foo)); - } - } - - } catch (Exception e) { - e.printStackTrace(); - } - - View view = new View(); - view.setParameters(viewParas); - String destination = getDestiLoca("struts.xml", actionName, result); - view.setJsp(destination); - - - return view; - } - - private static String lowerFirstLetter(String input) { - String result = input.substring(0,1).toLowerCase() + input.substring(1); - return result; - } - - private static Document readXML (String fileName) { - Document doc = null; - try { - File xmlFile = new File(fileName); - DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); - DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); - doc = dBuilder.parse(xmlFile); - doc.getDocumentElement().normalize(); - - } catch (ParserConfigurationException | SAXException | IOException e) { - e.printStackTrace(); - } - - return doc; - } - - private static Element getTargetElement(String fileName, String actionName) { - Document doc = readXML(fileName); - NodeList nList = doc.getElementsByTagName("action"); - Element target = null; - for (int i = 0; i < nList.getLength(); i++) { - Node nNode = nList.item(i); - - if (nNode.getNodeType() == Node.ELEMENT_NODE) { - Element element = (Element)nNode; - if (element.getAttribute("name").equals(actionName)){ - target = element; - break; - } - } - } - - return target; - } - private static String getDestiLoca(String fileName, String actionName, String result) { - Document doc = readXML(fileName); - NodeList nList = doc.getElementsByTagName("action"); - String resultDes = null; - for (int i = 0; i < nList.getLength(); i++) { - Node nNode = nList.item(i); - Element el = (Element)nNode; - - if (el.getAttribute("name").equals(actionName)){ - - NodeList nListSub = el.getElementsByTagName("result"); - for (int j = 0; j < nListSub.getLength(); j++) { - Node nNodeSub = nListSub.item(j); - Element eSub = (Element) nNodeSub; - if (eSub.getAttribute("name").equals(result)){ - resultDes = eSub.getTextContent(); - break; - } - } - } - - } - - return resultDes; - } - - private static Class getTargetClass(String fileName, String actionName) { - Element target = getTargetElement(fileName, actionName); - - String className = target.getAttribute("class"); - Class targetClass = null; - try { - targetClass = Class.forName(className); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } - - return targetClass; - } - -} diff --git a/liuxin/src/com/coderising/litestruts/StrutsTest.java b/liuxin/src/com/coderising/litestruts/StrutsTest.java deleted file mode 100644 index 3daefe6ceb..0000000000 --- a/liuxin/src/com/coderising/litestruts/StrutsTest.java +++ /dev/null @@ -1,43 +0,0 @@ -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() throws 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 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")); - } -} diff --git a/liuxin/src/com/coderising/litestruts/View.java b/liuxin/src/com/coderising/litestruts/View.java deleted file mode 100644 index 0194c681f6..0000000000 --- a/liuxin/src/com/coderising/litestruts/View.java +++ /dev/null @@ -1,23 +0,0 @@ -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/liuxin/src/com/coderising/litestruts/struts.xml b/liuxin/src/com/coderising/litestruts/struts.xml deleted file mode 100644 index 246b3595ad..0000000000 --- a/liuxin/src/com/coderising/litestruts/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - \ No newline at end of file diff --git a/liuxin/src/com/coding/basic/ArrayList.java b/liuxin/src/com/coding/basic/ArrayList.java deleted file mode 100644 index 1f185736f9..0000000000 --- a/liuxin/src/com/coding/basic/ArrayList.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.coding.basic; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o){ - - } - public void add(int index, Object o){ - - } - - public Object get(int index){ - return null; - } - - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public Iterator iterator(){ - return null; - } - -} diff --git a/liuxin/src/com/coding/basic/BinaryTreeNode.java b/liuxin/src/com/coding/basic/BinaryTreeNode.java deleted file mode 100644 index d7ac820192..0000000000 --- a/liuxin/src/com/coding/basic/BinaryTreeNode.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.coding.basic; - -public class BinaryTreeNode { - - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public Object getData() { - return data; - } - public void setData(Object data) { - this.data = data; - } - public BinaryTreeNode getLeft() { - return left; - } - public void setLeft(BinaryTreeNode left) { - this.left = left; - } - public BinaryTreeNode getRight() { - return right; - } - public void setRight(BinaryTreeNode right) { - this.right = right; - } - - public BinaryTreeNode insert(Object o){ - return null; - } - -} diff --git a/liuxin/src/com/coding/basic/Iterator.java b/liuxin/src/com/coding/basic/Iterator.java deleted file mode 100644 index 06ef6311b2..0000000000 --- a/liuxin/src/com/coding/basic/Iterator.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} diff --git a/liuxin/src/com/coding/basic/LinkedList.java b/liuxin/src/com/coding/basic/LinkedList.java deleted file mode 100644 index e2c4e5e795..0000000000 --- a/liuxin/src/com/coding/basic/LinkedList.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.coding.basic; - -public class LinkedList implements List { - - private Node head; - - public void add(Object o){ - - } - public void add(int index , Object o){ - - } - public Object get(int index){ - return null; - } - public Object remove(int index){ - return null; - } - - public int size(){ - return -1; - } - - public void addFirst(Object o){ - - } - public void addLast(Object o){ - - } - public Object removeFirst(){ - return null; - } - public Object removeLast(){ - return null; - } - public Iterator iterator(){ - return null; - } - - - private static class Node{ - Object data; - Node next; - - } -} diff --git a/liuxin/src/com/coding/basic/List.java b/liuxin/src/com/coding/basic/List.java deleted file mode 100644 index 10d13b5832..0000000000 --- a/liuxin/src/com/coding/basic/List.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.coding.basic; - -public interface List { - public void add(Object o); - public void add(int index, Object o); - public Object get(int index); - public Object remove(int index); - public int size(); -} diff --git a/liuxin/src/com/coding/basic/Queue.java b/liuxin/src/com/coding/basic/Queue.java deleted file mode 100644 index 36e516e266..0000000000 --- a/liuxin/src/com/coding/basic/Queue.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.coding.basic; - -public class Queue { - - public void enQueue(Object o){ - } - - public Object deQueue(){ - return null; - } - - public boolean isEmpty(){ - return false; - } - - public int size(){ - return -1; - } -} diff --git a/liuxin/src/com/coding/basic/Stack.java b/liuxin/src/com/coding/basic/Stack.java deleted file mode 100644 index a5a04de76d..0000000000 --- a/liuxin/src/com/coding/basic/Stack.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.coding.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o){ - } - - public Object pop(){ - return null; - } - - public Object peek(){ - return null; - } - public boolean isEmpty(){ - return false; - } - public int size(){ - return -1; - } -} diff --git a/liuxin/struts.xml b/liuxin/struts.xml deleted file mode 100644 index 07f80b6476..0000000000 --- a/liuxin/struts.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - /jsp/homepage.jsp - /jsp/showLogin.jsp - - - /jsp/welcome.jsp - /jsp/error.jsp - - \ No newline at end of file