diff --git a/.gitignore b/.gitignore index 8b13789179..d749cfbd62 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,21 @@ - +*.class +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders +.idea/ +*.iml +rebel.* +.rebel.* + +target diff --git a/.project b/.project new file mode 100644 index 0000000000..6b4d50ed9a --- /dev/null +++ b/.project @@ -0,0 +1,11 @@ + + + coding2017 + + + + + + + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..4824b80263 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/array/ArrayUtil.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/array/ArrayUtil.java new file mode 100644 index 0000000000..38c0889f32 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/array/ArrayUtil.java @@ -0,0 +1,301 @@ +package com.github.Ven13.coding2017.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 originalLen = origin.length; + + int len = originalLen; + + int temp; + + for(int i = 0; i < (originalLen/2); i++){ + + temp = origin[len - i - 1]; + + origin[len - i - 1] = origin[i]; + + origin[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 newLength = 0; + // 获得新数组长度值 + for (int i = 0; i < oldArray.length; i++) { + + if(oldArray[i] != 0) { + + newLength++; + + } + + } + + // 定意新数组 + int[] newArray = new int[newLength]; + // 定意新数组下标变量 + int n = 0; + // 转存数组 + for (int i = 0; i < oldArray.length; i++) { + + if(oldArray[i] != 0) { + + newArray[n] = oldArray[i];// 转存数组 + n++;// 新数组下标偏移 + + } + + } + + //返回生成的新数组 + 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[] newArray = new int[array1.length + array2.length]; + + int k = 0; + + String inNum = ""; + + for(int i = 0; i < array1.length; i++) { + + for(int j = 0; j < array2.length; j++) { + + if (array1[i] < array2[j]) { + if (inNum.indexOf(array1[i] + "|") < 0) { + newArray[k++] = array1[i]; + inNum += array1[i] + "|"; + } + + } else if (array1[i] == array2[j]) { + if (inNum.indexOf(array1[i] + "|") < 0) { + newArray[k++] = array1[i]; + inNum += array1[i] + "|"; + } + } else { + if (i == array1.length - 1) { + if (inNum.indexOf(array1[i] + "|") < 0) { + newArray[k++] = array1[i]; + inNum += array1[i] + "|"; + } + } else { + if (inNum.indexOf(array2[j] + "|") < 0) { + newArray[k++] = array2[j]; + inNum += array2[j] + "|"; + } + } + + } + + } + + } + + return newArray; + } + /** + * 把一个已经存满数据的数组 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]; + 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){ + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + + int[] newArray = new int[max]; + + int k = 0; + + boolean isN = true; + + if (max > 2) { + + for (int i = 2; i < max; i++) { + + isN = true; + + for (int j = 2; j < max; j++) { + + if (i % j == 0 && i != j) { + + isN = false; + + } + } + + if (isN) { + + newArray[k++] = i; + + } + + } + + } else if (max == 2) { + + newArray[0] = 2; + k++; + } else { + + return null; + + } + + int[] newArray2 = new int[k]; + + for(int i = 0; i < k; i ++) { + + newArray2[i] = newArray[i]; + + } + + return newArray2; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + + int i, j, k; + + int sum; + + k = 0; + + for(i = 1; i <= max; i++) { + + + sum = 0; + + for(j = 1; j < i; j++) { + + if(i % j == 0) { + + sum += j; + + } + + } + + if(i == sum) + k++; + } + + int[] newArray = new int[k]; + + k = 0; + + for(i = 1; i <= max; i++) { + + + sum = 0; + + for(j = 1; j < i; j++) { + + if(i % j == 0) { + + sum += j; + + } + + } + + if(i == sum) { + + newArray[k] = i; + + k++; + + } + + } + + + return newArray; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + + String str = ""; + + for(int i = 0; i < array.length; i++) { + + str += array[i] + seperator; + + } + + str = str.substring(0, str.length() - 1); + + return str; + } + + + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/array/test/ArrayUtilTest.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/array/test/ArrayUtilTest.java new file mode 100644 index 0000000000..c9684fa326 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/array/test/ArrayUtilTest.java @@ -0,0 +1,46 @@ +package com.github.Ven13.coding2017.array.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.github.Ven13.coding2017.array.ArrayUtil; + +public class ArrayUtilTest { + + @Test + public final void testMerge() { + ArrayUtil arrayUtil = new ArrayUtil(); + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + int[] a3 = {}; + a3 = arrayUtil.merge(a1, a2); + assertEquals(3, a3[0]); + assertEquals(4, a3[1]); + assertEquals(5, a3[2]); + assertEquals(6, a3[3]); + assertEquals(7, a3[4]); + assertEquals(8, a3[5]); + + } + + @Test + public final void testgetPrimes() { + ArrayUtil arrayUtil = new ArrayUtil(); + int max = 23; + int[] a1 = {}; + a1 = arrayUtil.getPrimes(max); + assertEquals(3, a1.length); + assertEquals(1, a1[0]); + assertEquals(2, a1[1]); + assertEquals(3, a1[2]); + } + + @Test + public final void testgetPerfectNumbers() { + ArrayUtil arrayUtil = new ArrayUtil(); + int max = 6; + int[] a1 = {}; + a1 = arrayUtil.getPerfectNumbers(max); + } +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/LoginAction.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/LoginAction.java new file mode 100644 index 0000000000..d43bc2c452 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +package com.github.Ven13.coding2017.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 void setMessage(String message) { + this.message = message; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/Struts.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/Struts.java new file mode 100644 index 0000000000..a9040e257d --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/Struts.java @@ -0,0 +1,166 @@ +package com.github.Ven13.coding2017.litestruts; + +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; +import java.io.File; +import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + View view = new View(); + + Map map = testParseXmlData(actionName, "/struts.xml"); + + String className = (String)map.get(actionName); + + String resultName = ""; + System.out.println(className); + + try { + Class c = Class.forName(className); + Object obj = c.newInstance(); + Method method = null; + + for (Map.Entry entry : parameters.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + PropertyDescriptor pd = new PropertyDescriptor(key, c); + Method setMethod = pd.getWriteMethod(); + setMethod.invoke(obj, value); + } + + Method exec = c.getDeclaredMethod("execute"); + Object res = exec.invoke(obj); + if(res != null) { + resultName = (String)map.get(actionName + '|' + res); + view.setJsp(resultName); + } + Field[] fields = c.getDeclaredFields(); + for(Field f : fields){ + PropertyDescriptor descriptor = new PropertyDescriptor(f.getName(), c); + Method getMethod = descriptor.getReadMethod(); + Object value = getMethod.invoke(obj); + parameters.put(f.getName(), (String)value); + } + view.setParameters(parameters); + + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (IntrospectionException e) { + System.out.println(e.toString()); + e.printStackTrace(); + } + + + return view; + } + + public static Document parse2Document(String xmlFilePath){ + SAXReader reader = new SAXReader(); + Document doc = null; + try { + InputStream inputStream = Class.class.getResourceAsStream(xmlFilePath); + doc = reader.read(inputStream); + } catch (DocumentException e) { + e.printStackTrace(); + } + return doc; + } + + public static Map testParseXmlData(String actionName, String xmlFilePath){ + //鑾峰彇xml瑙f瀽鍣ㄥ璞 + //SAXReader reader = new SAXReader(); + //灏唜ml瑙f瀽涓篋ocument瀵硅薄 + Document doc = parse2Document(xmlFilePath); + //鑾峰彇鏂囨。鐨勬牴鍏冪礌 + Element root = doc.getRootElement(); + + //瀹氫箟淇濆瓨灞炴с佸肩殑map + Map map = new HashMap(); + + + //閬嶅巻褰撳墠鍏冪礌(鍦ㄦ鏄牴鍏冪礌)鐨勫瓙鍏冪礌 + + for (Iterator i_pe = root.elementIterator(); i_pe.hasNext();) { + + Element e_pe = (Element) i_pe.next(); + //鑾峰彇褰撳墠鍏冪礌鐨勫悕瀛 + String act = e_pe.getName(); + //鑾峰彇褰撳墠鍏冪礌鐨刵ame鍜宑lass灞炴х殑鍊煎苟鍒嗗埆璧嬬粰attName,attClass鍙橀噺 + System.out.println(act); + String attName = e_pe.attributeValue("name"); + String attClass = e_pe.attributeValue("class"); + if (attName.equals(actionName)) { + map.put(attName, attClass); + + //Element e_res = e_pe.element("result"); + //System.out.println(e_res.getName()); + for (Iterator i_res = e_pe.elementIterator(); i_res.hasNext();) { + + Element e_re = (Element) i_res.next(); + //鑾峰彇褰撳墠鍏冪礌鐨勫悕瀛 + String person_n = e_re.getName(); + //鑾峰彇褰撳墠鍏冪礌鐨刵ame鍜宑lass灞炴х殑鍊煎苟鍒嗗埆璧嬬粰attName,attClass鍙橀噺 + System.out.println(person_n); + String resName = e_re.attributeValue("name"); + String resClass = e_re.getStringValue(); + map.put(attName + '|' + resName, resClass); + + } + } + } + + return map; + + } + +} diff --git a/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/StrutsTest.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a5a777be1c --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.github.Ven13.coding2017.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/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/View.java b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/View.java new file mode 100644 index 0000000000..2b0997095c --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/View.java @@ -0,0 +1,23 @@ +package com.github.Ven13.coding2017.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/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/struts.xml b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/litestruts/struts.xml new file mode 100644 index 0000000000..59ec89bb69 --- /dev/null +++ b/group02/106614649/106614649Learnin/src/com/github/Ven13/coding2017/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/group05/1094051862/test01/src/com/coding/basic/.gitignore b/group02/106614649/106614649Learnin/src/struts.xml similarity index 100% rename from group05/1094051862/test01/src/com/coding/basic/.gitignore rename to group02/106614649/106614649Learnin/src/struts.xml diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/ArrayUtil.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..f60be05977 --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/ArrayUtil.java @@ -0,0 +1,228 @@ +package com.github.FelixCJF.coding2017.coderising.array; + +import java.util.ArrayList; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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 array[] = new int[origin.length]; + for (int i = 0; i newArr[i+1]) { + int temp = newArr[i]; + newArr[i] = newArr[i+1]; + newArr[i + 1] = temp; + } + } + return newArr; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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 newArr[] = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArr, 0, oldArray.length); + return newArr; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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[] newArr; + int f1 = 0; + int f2 = 1; + int f = 0; + if (max < 2) { + return newArr = new int[0]; + } + ArrayList list = new ArrayList(); + for (int i = 2; f < max; i++) { + list.add(f2); + f = f1 + f2; + f1 = f2; + f2 = f; + } + newArr = new int[list.size()]; + for (int i = 0; i < newArr.length; i++) { + newArr[i] = (int) list.get(i); + } + return newArr; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + + ArrayList list = new ArrayList(); + + for (int i = 1; i < max; i++) { + if (isPrime(i)) { + list.add(i); + } + } + int[] newArr = new int[list.size()]; + for (int i = 0; i < newArr.length; i++) { + newArr[i] = (int) list.get(i); + } + return newArr; + } + //鍒ゆ柇鏄惁涓虹礌鏁 + private boolean isPrime(int a) { + + boolean flag = true; + + if (a < 2) {// 绱犳暟涓嶅皬浜2 + return false; + } else { + + for (int i = 2; i <= Math.sqrt(a); i++) { + + if (a % i == 0) {// 鑻ヨ兘琚暣闄わ紝鍒欒鏄庝笉鏄礌鏁帮紝杩斿洖false + + flag = false; + break;// 璺冲嚭寰幆 + } + } + } + return flag; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] newArr; + if (max == 0) { + return newArr = new int[0]; + } + ArrayList list = new ArrayList(); + for (int i = 1; i < max; i++) { + if (isWanshu(i)) { + list.add(i); + } + } + newArr = new int[list.size()]; + for (int i = 0; i < newArr.length; i++) { + newArr[i] = (int) list.get(i); + } + return newArr; + } + //鍒ゆ柇涓涓暟鏄笉鏄畬鏁 + private boolean isWanshu(int n) + { + boolean flag=false; + int i,sum=0; + for(i=1;i<=n/2;i++) + { + if(n%i==0) + { + sum+=i; + } + } + if(sum==n) + { + flag=true; + } + return flag; + } + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + String string = ""; + for (int i = 0; i < array.length; i++) { + if (i == array.length - 1) { + string += array[i]; + } else { + string += array[i] + seperator; + } + } + return string; + } + + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/test/ArrayUtilTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/test/ArrayUtilTest.java new file mode 100644 index 0000000000..378e052164 --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/array/test/ArrayUtilTest.java @@ -0,0 +1,148 @@ +package com.github.FelixCJF.coding2017.coderising.array.test; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.github.FelixCJF.coding2017.coderising.array.ArrayUtil; + + +public class ArrayUtilTest +{ + private ArrayUtil myArray; + @Before + public void setUp() throws Exception + { + myArray = new ArrayUtil(); + } + + @Test + public void testReverseArray() + { + int[] a = {1, 2, 1, 3, 5, 6}; + int[] b = {6, 5, 3, 1, 2, 1}; + int[] c = new int[0]; + + myArray.reverseArray(a); + assertArrayEquals(a, b); + + //瀵圭┖鏁扮粍杩涜鍙嶈浆 + myArray.reverseArray(c); + assertArrayEquals(c, new int[0]); + } + + @Test + public void testRemoveZero() + { + int[] oldArr= {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 1, 2, 0, 5}; + int b[] = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 1, 2, 5}; + int[] c = myArray.removeZero(oldArr); + assertArrayEquals(b, c); + + int[] d = new int[0]; + assertArrayEquals(d, new int[0]); + } + + @Test + public void testMerge() + { + int a1[] = {1, 2, 3, 4, 5}; + int b1[] = {3, 4, 5, 6, 7, 8}; + int c1[] = {1, 2, 3, 4, 5, 6, 7, 8}; + int[] newArray1 = myArray.merge(a1, b1); + assertArrayEquals(c1, newArray1); + + int a2[] = new int[0]; + int b2[] = {0, 2, 3, 6, 7, 8}; + int c2[] = {0, 2, 3, 6, 7, 8}; + int[] newArray2 = myArray.merge(a2, b2); + assertArrayEquals(c2, newArray2); + + int a3[] = {0, 2, 3, 6, 7, 8}; + int b3[] = new int[0]; + int c3[] = {0, 2, 3, 6, 7, 8}; + int[] newArray3 = myArray.merge(a3, b3); + assertArrayEquals(c3, newArray3); + + int[] a4 = new int[0]; + int[] b4 = new int[0]; + int[] newArray4 = myArray.merge(a4, b4); + assertArrayEquals(new int[0], newArray4); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + @Test + public void testGrow() + { + int[] a = {3, 5, 7, 8, 9}; + int[] b = {3, 5, 7, 8, 9, 0, 0, 0}; + int[] newArray = myArray.grow(a, 3); + assertArrayEquals(b, newArray); + + // size < 0 鎶涘嚭寮傚父 + expectedEx.expect(Exception.class); + int[] newArray1 = myArray.grow(a, -3); + assertArrayEquals(b, newArray1); + + } + + @Test + public void testFibonacci() + { + //max == 1鏃惰繑鍥炵┖鏁扮粍 + int[] array1 = myArray.fibonacci(1); + int[] b = new int[0]; + assertArrayEquals(array1, b); + + + int[] array2= myArray.fibonacci(35); + int[] c = {1, 1, 2, 3, 5, 8, 13, 21, 34 }; + assertArrayEquals(c, array2); + } + + @Test + public void testGetPrimes() + { + int[] a = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; + int[] array1 = myArray.getPrimes(35); + assertArrayEquals(a, array1); + + //max <= 2鐨勬椂鍊欐病鏈夌礌鏁帮紝鏁扮粍涓虹┖鏁扮粍 new int[0] + int[] array2 = myArray.getPrimes(1); + int[] b = new int[0]; + assertArrayEquals(array2, b); + } + + @Test + public void testGetPerfectNumbers() + { + int[] array = myArray.getPerfectNumbers(10000); + int[] a = {6, 28, 496, 8128 }; + assertArrayEquals(a, array); + } + + @Test + public void testJoin() + { + int[] Array0 = {3, 5, 7, 8, 9}; + String s0 = myArray.join(Array0, "-"); + String s1 = "3-5-7-8-9"; + assertEquals(s1, s0); + + int[] Array1 = {3}; + String s2 = myArray.join(Array1, "-"); + String s3 = "3"; + assertEquals(s2, s3); + + //浼犻掔┖鏁扮粍鏃讹紝杩斿洖绌烘暟缁 + int[] Array2 = new int[0]; + String s4 = myArray.join(Array2, "-"); + String s5 = ""; + assertEquals(s4, s5); + } + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/LoginAction.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/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; + } +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/Struts.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + return null; + } + +} diff --git a/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/StrutsTest.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/View.java b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/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/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/struts.xml b/group02/1554421063/src/com/github/FelixCJF/coding2017/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..a6cfe43e6c --- /dev/null +++ b/group02/1554421063/src/com/github/FelixCJF/coding2017/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 diff --git a/group02/527705641/src/com/github/fei9009/coderising0226/array/ArrayUtil.java b/group02/527705641/src/com/github/fei9009/coderising0226/array/ArrayUtil.java new file mode 100644 index 0000000000..d4e62075d9 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coderising0226/array/ArrayUtil.java @@ -0,0 +1,218 @@ +package com.github.fei9009.coderising0226.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import com.sun.org.apache.bcel.internal.generic.ISTORE; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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 i = 0, j = origin.length-1; + while(i < j){ + int tmp = origin[i]; + origin[i] = origin[j]; + origin[j] = tmp; + i++; + j--; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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){ + if (oldArray == null || oldArray.length == 0){ + return oldArray; + } + int count = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + count++; + } + } + int[] newArray = new int[count]; + int j = 0; + for (int i = 0; i <= oldArray.length - 1; i++) { + if (oldArray[i] == 0) { + continue; + } + newArray[j++] = oldArray[i]; + } + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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){ + if (array1 == null && array2 == null) { + return null; + } + if (array1 == null) { + return array2; + } + if (array2 == null) { + return array1; + } + int i=0, j=0, k=0; + int len1 = array1.length; + int len2 = array2.length; + int[] mergeArr = new int[len1+len2]; + while(true){ + if(i == len1 || j == len2) + break; + if(array1[i]array2[j]){ + mergeArr[k++] = array2[j++]; + }else{ + mergeArr[k++] = array1[i++]; + j++; + } + } + + for(;i list = new ArrayList<>(); + int f1 = 1, f2 = 1, f3; + list.add(f1); + list.add(f2); + while (f1 +f2 < max) { + f3 = f1 + f2; + list.add(f3); + f1 = f2; + f2 = f3; + } + int[] result = new int[list.size()]; + int j = 0; + for(Integer i : list) { + result[j++] = i; + } + return result; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + int[] a = new int[max]; + int k=0; + for (int z = 1; ztype){ + try { + Method met = obj.getClass().getMethod("set" + initStr(att), type); + met.invoke(obj, value); + }catch (Exception e){ + e.printStackTrace(); + } + } + + private static String getter(Object obj, String att){ + try { + Method met = obj.getClass().getMethod("get" + initStr(att)); + return (String)met.invoke(obj); + }catch (Exception e){ + e.printStackTrace(); + } + return null; + } + private static String initStr(String name) { + name = name.substring(0, 1).toUpperCase() + name.substring(1); + return name; + } + + private static String toLowerString(String name) { + name = name.substring(0, 1).toLowerCase() + name.substring(1); + return name; + } + + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + SAXReader saxReader = new SAXReader(); + try { + Document document = saxReader.read(new File("struts.xml")); + Element root = document.getRootElement(); + + + boolean flag = false; + String className = ""; + Element action = null; + for (Iterator iter = root.elementIterator(); iter.hasNext();) + { + Element e = (Element) iter.next(); + if(e.attributeValue("name").equals(actionName)){ + flag = true; + className = e.attributeValue("class"); + action = e; + break; + } + } + if(!flag) + throw new Exception(actionName+"鏈畾涔"); + + Class clz = Class.forName(className); + Constructor c = clz.getConstructor(); + Object obj = c.newInstance(); + + for (String in : parameters.keySet()) { + setter(obj,in,parameters.get(in), String.class); + } + Method exc = clz.getDeclaredMethod("execute"); + String res = (String)exc.invoke(obj); + + //鑾峰彇鎵鏈塯etter鏂规硶 + //Get the methods + Method[] methods = clz.getDeclaredMethods(); + //Loop through the methods and print out their names + Map params = new HashMap(); + + for (Method method : methods) { + String name = method.getName(); + if(name.substring(0,3).equals("get")){ + params.put(toLowerString(name.substring(3)),getter(obj,toLowerString(name.substring(3)))); + } + } + View view = new View(); + view.setParameters(params); + //step 4 + flag = false; + Element result = null; + List actionChildList = action.elements("result"); + for (Iterator iter = action.elementIterator(); iter.hasNext();){ + Element e = (Element) iter.next(); + if(e.attributeValue("name").equals(res)){ + flag = true; + result = e; + break; + } + } + if(!flag) + throw new Exception(res+"undefined"); + view.setJsp(result.getText()); + return view; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/StrutsTest.java b/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/StrutsTest.java new file mode 100644 index 0000000000..c5ee5f10a9 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.github.fei9009.coderising0226.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/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/View.java b/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/View.java new file mode 100644 index 0000000000..9332493c42 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/View.java @@ -0,0 +1,23 @@ +package com.github.fei9009.coderising0226.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/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/struts.xml b/group02/527705641/src/com/github/fei9009/coderising0226/litestruts/struts.xml new file mode 100644 index 0000000000..68098b57e3 --- /dev/null +++ b/group02/527705641/src/com/github/fei9009/coderising0226/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/group02/562768642/bin/com/github/orajavac/coding2017/litestructs/struts.xml b/group02/562768642/bin/com/github/orajavac/coding2017/litestructs/struts.xml new file mode 100644 index 0000000000..6e5022fb64 --- /dev/null +++ b/group02/562768642/bin/com/github/orajavac/coding2017/litestructs/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/group02/562768642/src/com/github/orajavac/coding2017/array/ArrayUtil.java b/group02/562768642/src/com/github/orajavac/coding2017/array/ArrayUtil.java new file mode 100644 index 0000000000..62de048a68 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/array/ArrayUtil.java @@ -0,0 +1,234 @@ +package com.github.orajavac.coding2017.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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; + int c=origin.length; + for (int i=0,j=origin.length-1;itarget[j+1]){ + t=target[j]; + target[j]=target[j+1]; + target[j+1]=t; + } + } + return removeZero(target); + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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[] target = new int[size]; + for (int i=0;iindex){ if(i+1!=len){ elementData[i]=elementData[i+1]; - }else{ //我们假设数组索引 0-3,那么数组长度是4,3+1==4,elementData[i+1]会报错 + }else{ //鎴戜滑鍋囪鏁扮粍绱㈠紩 0-3锛岄偅涔堟暟缁勯暱搴︽槸4锛3+1==4锛宔lementData[i+1]浼氭姤閿 elementData[i]=null; } } diff --git a/group02/562768642/src/com/github/orajavac/coding2017/litestructs/LoginAction.java b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/LoginAction.java new file mode 100644 index 0000000000..7c16390ef1 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/LoginAction.java @@ -0,0 +1,43 @@ +package com.github.orajavac.coding2017.litestructs; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public void setMessage(String message) { + this.message = 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/group02/562768642/src/com/github/orajavac/coding2017/litestructs/Struts.java b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/Struts.java new file mode 100644 index 0000000000..a5ea0cd702 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/Struts.java @@ -0,0 +1,99 @@ +package com.github.orajavac.coding2017.litestructs; + +import java.beans.PropertyDescriptor; +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.List; +import java.util.Iterator; + +import org.dom4j.Attribute; +import org.dom4j.Element; +import org.dom4j.Document; +import org.dom4j.io.SAXReader; + + +public class Struts { + + private static Map xmlMap = new HashMap(); + + public static View runAction(String actionName, Map parameters) { + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + try{ + if(xmlMap.size()==0) + Struts.DOM4jParserXml(); + + StrutsXml sx = xmlMap.get(actionName); + Class clz = Class.forName(sx.getClassz()); + Object o = clz.newInstance(); + String key = null; + Method[] mets = clz.getDeclaredMethods(); + for (Method m : mets){ + if(m.getName().startsWith("set")){ + key = m.getName().substring(3,m.getName().length()).toLowerCase(); + m.invoke(o,parameters.get(key)); + } + } + Method m1 = clz.getDeclaredMethod("execute"); + String result = (String)m1.invoke(o); + Map param = new HashMap(); + Field[] fields = clz.getDeclaredFields(); + for(int i=0;i iterator = root.elementIterator(); + while(iterator.hasNext()){ + Element e = iterator.next(); + StrutsXml sx = new StrutsXml(); + sx.setName(e.attribute("name").getValue()); + sx.setClassz(e.attribute("class").getValue()); + xmlMap.put(e.attribute("name").getValue(), sx); + Iterator r = e.elementIterator("result"); + while(r.hasNext()){ + Element child = r.next(); + sx.getResult().put(child.attribute("name").getValue(), child.getTextTrim()); + } + } + }catch(Exception e){ + e.printStackTrace(); + } + } +} diff --git a/group02/562768642/src/com/github/orajavac/coding2017/litestructs/StrutsTest.java b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/StrutsTest.java new file mode 100644 index 0000000000..447dc3a24b --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/StrutsTest.java @@ -0,0 +1,43 @@ +package com.github.orajavac.coding2017.litestructs; + +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/group02/562768642/src/com/github/orajavac/coding2017/litestructs/StrutsXml.java b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/StrutsXml.java new file mode 100644 index 0000000000..b22eb3ba05 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/StrutsXml.java @@ -0,0 +1,28 @@ +package com.github.orajavac.coding2017.litestructs; + +import java.util.HashMap; +import java.util.Map; + +public class StrutsXml { + private String name; + private String classz; + private Map result = new HashMap(); + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getClassz() { + return classz; + } + public void setClassz(String classz) { + this.classz = classz; + } + public Map getResult() { + return result; + } + public void setResult(Map result) { + this.result = result; + } +} diff --git a/group02/562768642/src/com/github/orajavac/coding2017/litestructs/View.java b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/View.java new file mode 100644 index 0000000000..49c5feeba3 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/View.java @@ -0,0 +1,23 @@ +package com.github.orajavac.coding2017.litestructs; + +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/group02/562768642/src/com/github/orajavac/coding2017/litestructs/struts.xml b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/struts.xml new file mode 100644 index 0000000000..6e5022fb64 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/litestructs/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/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayUtil.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayUtil.java new file mode 100644 index 0000000000..2e9c09effc --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayUtil.java @@ -0,0 +1,294 @@ +package com.github.congcongcong250.coding2017.basic; + +import java.util.Arrays; +import java.util.ArrayList; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 渚嬪锛 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[] copy = origin.clone(); + int j, last = copy.length - 1; + + for (int i = last; i >= 0; i--) { + j = last - i; + origin[i] = copy[j]; + } + + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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) { + /* + * Method 1, traverse oldArray and count non-zero value, create new + * int[count], traverse again oldArray and insert to new one + */ + + /* + * Method 2, traverse olArray and insert non-zero value to an List, + * then List.toArray(). + */ + + // Method 3 + if(oldArray == null){ + return null; + } + int[] tmp = new int[oldArray.length]; + int count = 0; + + for (int v : oldArray) { + if (v != 0) { + tmp[count] = v; + count++; + } + } + + return Arrays.copyOf(tmp, count); + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 渚嬪 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) { + if (array1 == null) { + if (array2 == null) { + return null; + } + return array2; + } else if (array2 == null) { + return array1; + } + + int l1 = array1.length, l2 = array2.length; + int[] whole = new int[l1 + l2]; + + int dupValue = 0; + int dupCount = 0; + boolean isDup = false; + + int j = 0; + // Traverse array1 + for (int i = 0; i < l1; i++) { + + // Get rid of duplicate value in array1 + if (isDup) { + if (array1[i] == dupValue) { + dupCount++; + continue; + } else { + isDup = false; + } + } + + while (j < l2) { + if (array1[i] > array2[j]) { + // If int from array1 is larger, add int from array2 first + whole[i + j - dupCount] = array2[j]; + j++; + continue; + } else if (array1[i] == array2[j]) { + // If equals, skip int from array2, and set duplicate value + isDup = true; + dupValue = array1[i]; + dupCount++; + j++; + continue; + } else if (array1[i] < array2[j]) { + // If smaller, break and add from in array1 + break; + } + } + whole[i + j - dupCount] = array1[i]; + } + + // Deal with left over in array2 + while (j < l2) { + whole[l1 + j - dupCount] = array2[j]; + j++; + } + + return Arrays.copyOf(whole, l1 + l2 - dupCount); + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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) { + checkGrowSize(size); + if (oldArray == null) { + return null; + } + int newlength = oldArray.length + size; + int[] res = new int[newlength]; + res = Arrays.copyOf(oldArray, newlength); + + return res; + } + + private void checkGrowSize(int size) { + if (size < 0) { + throw new IndexOutOfBoundsException( + "Negative size is not allowed in grow()"); + } + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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]; + } + + // Fib(47) == 2971215073 > INT_MAX; + int[] tmp = new int[46]; + tmp[0] = 1; + tmp[1] = 1; + + int next = 1 + 1; + int i = 1; + while (next < max) { + i++; + tmp[i] = next; + next = tmp[i] + tmp[i - 1]; + } + + return Arrays.copyOf(tmp, i + 1); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 渚嬪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]; + } + + ArrayList primeList = new ArrayList(); + primeList.add(2); + + for (int candidate = 3; candidate < max; candidate += 2) { + // For every number smaller than max + int ceiling = (int) Math.floor(Math.sqrt(candidate)); + for (Integer prime : primeList) { + // Divided by every prime number smaller than sqrt(candidate) + if (candidate % prime == 0) { + // When reminder equals 0, candidate is not a prime + break; + } + if (prime > ceiling) { + // When every prime number <= sqrt(candidate), add candidate to primelist + primeList.add(candidate); + break; + } + } + } + + // Transfer primelist to int array + int[] res = arrayFromList(primeList); + + return res; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + if (max <= 2) { + return new int[0]; + } + + ArrayList perfectList = new ArrayList(); + + for (int candidate = 2; candidate < max; candidate++) { + int sum = 1; + int ceiling = (int) Math.floor(Math.sqrt(candidate)); + if (Math.sqrt(candidate) == ceiling) { + sum += ceiling; + } + // For each number smaller than max + for (int divisor = 2; divisor <= ceiling; divisor++) { + if (candidate % divisor == 0) { + sum += divisor; + sum += candidate / divisor; + } + if (sum > candidate) { + break; + } + } + if (sum == candidate) { + perfectList.add(candidate); + } + } + + // Transfer primelist to int array + int[] res = arrayFromList(perfectList); + + return res; + } + + private int[] arrayFromList(ArrayList list) { + int[] r = new int[list.size()]; + int j = 0; + for (Integer v : list) { + r[j++] = v; + } + return r; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 渚嬪array= [3,8,9], seperator = "-" 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + String res = ""; + if (array == null || array.length == 0) { + return res; + } + res += array[0]; + for (int i = 1; i < array.length; i++) { + res += seperator + array[i]; + } + return res; + } + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WArrayList.java similarity index 87% rename from group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java rename to group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WArrayList.java index af4588763e..4aa718017a 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WArrayList.java @@ -4,7 +4,7 @@ import java.util.InputMismatchException; import java.util.NoSuchElementException; -public class ArrayList implements List { +public class WArrayList implements WList { private int size = 0; @@ -63,7 +63,7 @@ public int size(){ return size; } - public Iterator iterator(){ + public WIterator wIterator(){ return new Itr(); } @@ -79,7 +79,7 @@ private void checkIndex(int index){ } - private class Itr implements Iterator{ + private class Itr implements WIterator{ //index for next element to visit private int cursor = 0; @@ -103,7 +103,7 @@ public void remove() { throw new NoSuchElementException(); } - ArrayList.this.remove(--cursor); + WArrayList.this.remove(--cursor); } } diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WBinaryTreeNode.java similarity index 59% rename from group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java rename to group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WBinaryTreeNode.java index 9e0e5aa10e..6601a5cd0a 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WBinaryTreeNode.java @@ -1,18 +1,18 @@ package com.github.congcongcong250.coding2017.basic; -public class BinaryTreeNode >{ +public class WBinaryTreeNode >{ private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; + private WBinaryTreeNode left; + private WBinaryTreeNode right; - public BinaryTreeNode(){ + public WBinaryTreeNode(){ data = null; left = null; right = null; } - public BinaryTreeNode(Object obj){ + public WBinaryTreeNode(Object obj){ data = obj; left = null; right = null; @@ -24,16 +24,16 @@ public Object getData() { public void setData(Object data) { this.data = data; } - public BinaryTreeNode getLeft() { + public WBinaryTreeNode getLeft() { return left; } - public void setLeft(BinaryTreeNode left) { + public void setLeft(WBinaryTreeNode left) { this.left = left; } - public BinaryTreeNode getRight() { + public WBinaryTreeNode getRight() { return right; } - public void setRight(BinaryTreeNode right) { + public void setRight(WBinaryTreeNode right) { this.right = right; } @@ -43,7 +43,7 @@ public void destroy(){ this.right = null; } - public BinaryTreeNode insert(Object o){ + public WBinaryTreeNode insert(Object o){ //If is empty root if(data == null){ data = o; @@ -51,18 +51,18 @@ public BinaryTreeNode insert(Object o){ } //If it is a normal root - BinaryTreeNode in; + WBinaryTreeNode in; if(o.compareTo(data) <= 0){ if(left == null){ - in = new BinaryTreeNode(o); + in = new WBinaryTreeNode(o); left = in; }else{ in = left.insert(o); } }else{ if(right == null){ - in = new BinaryTreeNode(o); + in = new WBinaryTreeNode(o); right = in; }else{ in = right.insert(o); diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Iterator.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WIterator.java similarity index 77% rename from group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Iterator.java rename to group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WIterator.java index 9033ad33be..747045754c 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Iterator.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WIterator.java @@ -1,6 +1,6 @@ package com.github.congcongcong250.coding2017.basic; -public interface Iterator { +public interface WIterator { public boolean hasNext(); public Object next(); public void remove(); diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WLinkedList.java similarity index 88% rename from group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java rename to group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WLinkedList.java index 640d9ec974..1c5728a63b 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WLinkedList.java @@ -2,12 +2,12 @@ import java.util.NoSuchElementException; -public class LinkedList implements List { +public class WLinkedList implements WList { private Node head; private int size; - public LinkedList(){ + public WLinkedList(){ head = new Node(); size = 0; } @@ -79,7 +79,7 @@ public Object removeFirst(){ public Object removeLast(){ return remove(size-1); } - public Iterator iterator(){ + public WIterator wIterator(){ return new ListItr(); } public void clear(){ @@ -134,7 +134,7 @@ public Node(Object obj,Node pre, Node nx){ } - private class ListItr implements Iterator{ + private class ListItr implements WIterator{ //Point to next node Node cursor; int nextIndex; @@ -169,7 +169,7 @@ public Object previous() { public void remove() { //Check bound checkBound(); - LinkedList.this.remove(--nextIndex); + WLinkedList.this.remove(--nextIndex); } diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/List.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WList.java similarity index 85% rename from group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/List.java rename to group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WList.java index fe05e60f37..a79f0f9ff1 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/List.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WList.java @@ -1,6 +1,6 @@ package com.github.congcongcong250.coding2017.basic; -public interface List { +public interface WList { public void add(Object o); public void add(int index, Object o); public Object get(int index); diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WQueue.java similarity index 76% rename from group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java rename to group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WQueue.java index 0381c65fd8..66a269ac2a 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WQueue.java @@ -1,10 +1,10 @@ package com.github.congcongcong250.coding2017.basic; -public class Queue { - private LinkedList elementData; +public class WQueue { + private WLinkedList elementData; - public Queue(){ - elementData = new LinkedList(); + public WQueue(){ + elementData = new WLinkedList(); } public void enQueue(Object o){ diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WStack.java similarity index 73% rename from group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java rename to group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WStack.java index 3e411cb0be..de14bf5200 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/WStack.java @@ -1,10 +1,10 @@ package com.github.congcongcong250.coding2017.basic; -public class Stack { - private LinkedList elementData = new LinkedList(); +public class WStack { + private WLinkedList elementData = new WLinkedList(); - public Stack(){ - elementData = new LinkedList(); + public WStack(){ + elementData = new WLinkedList(); } public void push(Object o){ diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/ArrayUtilTest.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/ArrayUtilTest.java new file mode 100644 index 0000000000..d8b583095a --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/ArrayUtilTest.java @@ -0,0 +1,140 @@ +package com.github.congcongcong250.coding2017.basicTest; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.github.congcongcong250.coding2017.basic.ArrayUtil; + +public class ArrayUtilTest { + private ArrayUtil myArray; + + @Before + public void setUp() throws Exception { + myArray = new ArrayUtil(); + } + + @Test + public void testReverseArray() { + int[] origin = { 1, 2, 1, 3, 5, 6 }; + int[] reverse = { 6, 5, 3, 1, 2, 1 }; + + myArray.reverseArray(origin); + assertArrayEquals(origin, reverse); + + int[] empty = new int[0]; + myArray.reverseArray(empty); + assertArrayEquals(empty, new int[0]); + } + + @Test + public void testRemoveZero() { + int[] oldArray = { 1, 5, 0, 0, 6, 6, 0, 5, 4, 0, 7, 6, 7, 1, 2, 0 }; + int[] newArray = { 1, 5, 6, 6, 5, 4, 7, 6, 7, 1, 2 }; + int[] res = myArray.removeZero(oldArray); + assertArrayEquals(newArray, res); + + int[] nl = null; + int[] nll = myArray.removeZero(nl); + assertNull(nll); + } + + @Test + public void testMerge() { + int a1[] = { 1, 2, 3, 4, 5 }; + int b1[] = { 3, 4, 5, 6, 7, 8 }; + int c1[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + int[] newArray1 = myArray.merge(a1, b1); + assertArrayEquals(c1, newArray1); + + int a2[] = new int[0]; + int b2[] = { 0, 2, 3, 6, 7, 8 }; + int c2[] = { 0, 2, 3, 6, 7, 8 }; + int[] newArray2 = myArray.merge(a2, b2); + assertArrayEquals(c2, newArray2); + + int a3[] = { 0, 2, 3, 6, 7, 8 }; + int b3[] = new int[0]; + int c3[] = { 0, 2, 3, 6, 7, 8 }; + int[] newArray3 = myArray.merge(a3, b3); + assertArrayEquals(c3, newArray3); + + int[] a4 = null; + int[] b4 = null; + int[] newArray4 = myArray.merge(a4, b4); + assertNull(newArray4); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testGrow() { + int[] a = { 3, 5, 7, 8, 9 }; + int[] b = { 3, 5, 7, 8, 9, 0, 0, 0 }; + int[] newArray = myArray.grow(a, 3); + assertArrayEquals(b, newArray); + + int[] c = null; + int[] newArray1 = myArray.grow(c, 3); + assertNull(newArray1); + + // size < 0 + expectedEx.expect(Exception.class); + myArray.grow(a, -3); + } + + @Test + public void testFibonacci() { + + int[] array1 = myArray.fibonacci(1); + int[] b = new int[0]; + assertArrayEquals(array1, b); + + int[] array2 = myArray.fibonacci(35); + int[] c = { 1, 1, 2, 3, 5, 8, 13, 21, 34 }; + assertArrayEquals(c, array2); + } + + @Test + public void testGetPrimes() { + int[] a = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; + int[] array1 = myArray.getPrimes(35); + assertArrayEquals(a, array1); + + int[] array2 = myArray.getPrimes(1); + int[] b = new int[0]; + assertArrayEquals(array2, b); + } + + @Test + public void testGetPerfectNumbers() { + int[] array = myArray.getPerfectNumbers(10000); + int[] a = { 6, 28, 496, 8128 }; + assertArrayEquals(a, array); + } + + @Test + public void testJoin() { + int[] a = { 3, 5, 7, 8, 9 }; + String s0 = myArray.join(a, "-"); + String s1 = "3-5-7-8-9"; + assertEquals(s1, s0); + + int[] a1 = { 3 }; + String s2 = myArray.join(a1, "-"); + String s3 = "3"; + assertEquals(s2, s3); + + int[] a0 = new int[0]; + String s4 = myArray.join(a0, "-"); + String s5 = ""; + assertEquals(s4, s5); + } + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/TestRunner.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/TestRunner.java index c9a623e402..31470679fd 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/TestRunner.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/TestRunner.java @@ -1,3 +1,6 @@ +/* + * An automatic runner for Junit test for DataStructure assignment + * */ package com.github.congcongcong250.coding2017.basicTest; @@ -8,19 +11,19 @@ public class TestRunner { public static void main(String[] args){ - ArrayListTest ALT = new ArrayListTest(); + WArrayListTest ALT = new WArrayListTest(); test(ALT); - LinkedListTest LLT = new LinkedListTest(); + WLinkedListTest LLT = new WLinkedListTest(); test(LLT); - StackTest STT = new StackTest(); + WStackTest STT = new WStackTest(); test(STT); - QueueTest QT = new QueueTest(); + WQueueTest QT = new WQueueTest(); test(QT); - BinaryTreeNodeTest BTNT = new BinaryTreeNodeTest(); + WBinaryTreeNodeTest BTNT = new WBinaryTreeNodeTest(); test(BTNT); } diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/ArrayListTest.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WArrayListTest.java similarity index 86% rename from group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/ArrayListTest.java rename to group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WArrayListTest.java index 647a0e9c4e..ee437f5494 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/ArrayListTest.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WArrayListTest.java @@ -8,12 +8,12 @@ import org.junit.Before; import org.junit.Test; -import com.github.congcongcong250.coding2017.basic.ArrayList; -import com.github.congcongcong250.coding2017.basic.Iterator; +import com.github.congcongcong250.coding2017.basic.WArrayList; +import com.github.congcongcong250.coding2017.basic.WIterator; -public class ArrayListTest implements testCase { +public class WArrayListTest implements testCase { - ArrayList testlist = new ArrayList(); + WArrayList testlist = new WArrayList(); @Override @Before @@ -66,7 +66,7 @@ public void testRemove() { @Test(expected=IndexOutOfBoundsException.class) public void testgetneg(){ - ArrayList emptyList = new ArrayList(); + WArrayList emptyList = new WArrayList(); Object o = emptyList.get(-1); } @@ -84,7 +84,7 @@ public void testremoveExp(){ @Override @Test public void testFunctional() { - Iterator itr = testlist.iterator(); + WIterator itr = testlist.wIterator(); assertTrue(itr.hasNext()); for(int i = 0; i < 20; i++){ assertEquals(i, itr.next()); diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/BinaryTreeNodeTest.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WBinaryTreeNodeTest.java similarity index 87% rename from group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/BinaryTreeNodeTest.java rename to group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WBinaryTreeNodeTest.java index f0e7e59ee3..4bcc7b0c8e 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/BinaryTreeNodeTest.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WBinaryTreeNodeTest.java @@ -8,11 +8,11 @@ import org.junit.Before; import org.junit.Test; -import com.github.congcongcong250.coding2017.basic.BinaryTreeNode; +import com.github.congcongcong250.coding2017.basic.WBinaryTreeNode; -public class BinaryTreeNodeTest implements testCase { +public class WBinaryTreeNodeTest implements testCase { - BinaryTreeNode node = new BinaryTreeNode(); + WBinaryTreeNode node = new WBinaryTreeNode(); @Override @Before diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/LinkedListTest.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WLinkedListTest.java similarity index 88% rename from group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/LinkedListTest.java rename to group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WLinkedListTest.java index 701bd54402..f45daa6f35 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/LinkedListTest.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WLinkedListTest.java @@ -8,13 +8,13 @@ import org.junit.Before; import org.junit.Test; -import com.github.congcongcong250.coding2017.basic.ArrayList; -import com.github.congcongcong250.coding2017.basic.LinkedList; -import com.github.congcongcong250.coding2017.basic.Iterator; +import com.github.congcongcong250.coding2017.basic.WArrayList; +import com.github.congcongcong250.coding2017.basic.WLinkedList; +import com.github.congcongcong250.coding2017.basic.WIterator; -public class LinkedListTest implements testCase { +public class WLinkedListTest implements testCase { - LinkedList testlist = new LinkedList(); + WLinkedList testlist = new WLinkedList(); @Override @Before @@ -91,7 +91,7 @@ public void testRemove() { @Test(expected=IndexOutOfBoundsException.class) public void testgetneg(){ - LinkedList emptyList = new LinkedList(); + WLinkedList emptyList = new WLinkedList(); Object o = emptyList.get(-2); } @@ -110,7 +110,7 @@ public void testremoveExp(){ @Override @Test public void testFunctional() { - Iterator itr = testlist.iterator(); + WIterator itr = testlist.wIterator(); assertTrue(itr.hasNext()); for(int i = 0; i < 12; i++){ diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/QueueTest.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WQueueTest.java similarity index 91% rename from group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/QueueTest.java rename to group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WQueueTest.java index a176a44f2c..48e84eb287 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/QueueTest.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WQueueTest.java @@ -8,11 +8,11 @@ import org.junit.Before; import org.junit.Test; -import com.github.congcongcong250.coding2017.basic.Queue; +import com.github.congcongcong250.coding2017.basic.WQueue; -public class QueueTest implements testCase { +public class WQueueTest implements testCase { - Queue testqueue = new Queue(); + WQueue testqueue = new WQueue(); @Override @Before diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/StackTest.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WStackTest.java similarity index 90% rename from group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/StackTest.java rename to group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WStackTest.java index 9d6085ab03..4dad0ec720 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/StackTest.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basicTest/WStackTest.java @@ -6,11 +6,11 @@ import org.junit.Before; import org.junit.Test; -import com.github.congcongcong250.coding2017.basic.Stack; +import com.github.congcongcong250.coding2017.basic.WStack; -public class StackTest implements testCase { +public class WStackTest implements testCase { - Stack teststack = new Stack(); + WStack teststack = new WStack(); @Override @Before diff --git a/group05/1377699408/.gitignore b/group02/609990377/LiteStruts/.gitignore similarity index 55% rename from group05/1377699408/.gitignore rename to group02/609990377/LiteStruts/.gitignore index 5deea7e781..fa968c2f2b 100644 --- a/group05/1377699408/.gitignore +++ b/group02/609990377/LiteStruts/.gitignore @@ -1,4 +1,3 @@ -/bin +/bin/ .classpath .project -.settings/ diff --git a/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/LoginAction.java b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/LoginAction.java new file mode 100644 index 0000000000..a146c5046b --- /dev/null +++ b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +package com.github.congcongcong250.coding2017.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 void setMessage(String message){ + this.message = message; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/Struts.java b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/Struts.java new file mode 100644 index 0000000000..8b6090e4a8 --- /dev/null +++ b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/Struts.java @@ -0,0 +1,120 @@ +package com.github.congcongcong250.coding2017.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.w3c.dom.*; +import org.xml.sax.SAXException; + +import javax.xml.parsers.*; + +import java.beans.PropertyDescriptor; +import java.io.*; +import java.lang.reflect.*; + +public class Struts { + + public static View runAction(String actionName, + Map parameters) { + + /* + * + * 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + * + * 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + * 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 ("name"="test" , + * "password"="1234") , 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + * + * 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + * + * 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 + * {"message": "鐧诲綍鎴愬姛"} , 鏀惧埌View瀵硅薄鐨刾arameters + * + * 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + * 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + + /* + * File f = new File("."); System.out.println(f.getAbsolutePath()); + */ + String filepath = "./src/com/github/congcongcong250/coding2017/litestruts/struts.xml"; + File inputFile = new File(filepath); + View view = new View(); + + try { + // Use DOM parser + Element e = getActionByName(actionName, inputFile); + + // Get class and entity by reflection + String clsName = e.getAttribute("class"); + Class clazz = Class.forName(clsName); + Object obj = clazz.newInstance(); + + // Set entity attributes from the parameters passed in + for (Map.Entry entry : parameters.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + PropertyDescriptor pd = new PropertyDescriptor(key,clazz); + Method setMethod = pd.getWriteMethod(); + setMethod.invoke(obj, value); + } + + // Execute Login class, get result message + Method exec = clazz.getDeclaredMethod("execute"); + Object res = exec.invoke(obj); + + // Get result jsp address according to struts config + NodeList list = e.getElementsByTagName("result"); + for(int i = 0; i params = new HashMap(); + Field[] fields = clazz.getDeclaredFields(); + for(Field f : fields){ + PropertyDescriptor descriptor = new PropertyDescriptor(f.getName(),clazz); + Method getMethod = descriptor.getReadMethod(); + Object value = getMethod.invoke(obj); + params.put(f.getName(), value); + + } + view.setParameters(params); + + + + } catch (Exception e) { + e.printStackTrace(); + } + + // Return view + return view; + } + + private static Element getActionByName(String actionName, File inputFile) + throws ParserConfigurationException, SAXException, IOException { + + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(inputFile); + doc.getDocumentElement().normalize(); + + NodeList nList = doc.getElementsByTagName("action"); + for (int i = 0; i < nList.getLength(); i++) { + Node nNode = nList.item(i); + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + Element eElement = (Element) nNode; + if (eElement.getAttribute("name").equalsIgnoreCase( + actionName.toLowerCase())) { + return eElement; + } + } + } + return null; + } + +} diff --git a/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/StrutsTest.java b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a06d8e57c3 --- /dev/null +++ b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.github.congcongcong250.coding2017.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/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/View.java b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/View.java new file mode 100644 index 0000000000..69b5c87885 --- /dev/null +++ b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/View.java @@ -0,0 +1,23 @@ +package com.github.congcongcong250.coding2017.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/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/struts.xml b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/struts.xml new file mode 100644 index 0000000000..d288f75839 --- /dev/null +++ b/group02/609990377/LiteStruts/src/com/github/congcongcong250/coding2017/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group02/609990377/LiteStruts/src/struts.xml b/group02/609990377/LiteStruts/src/struts.xml new file mode 100644 index 0000000000..d288f75839 --- /dev/null +++ b/group02/609990377/LiteStruts/src/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/array/ArrayUtil.java b/group02/727171008/src/com/github/HarryHook/coding2017/array/ArrayUtil.java new file mode 100644 index 0000000000..a29fcdd193 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/array/ArrayUtil.java @@ -0,0 +1,345 @@ +package com.github.HarryHook.coding2017.array; + +public class ArrayUtil +{ + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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) + { + for(int i=0, j = origin.length-1; i array2[j]) + { + newArray[count++] = array2[j++]; + } + else if(array1[i] == array2[j]) + { + newArray[count++] = array2[j++]; + i++; + } + } + while(i==array1.length && j 1) + { + s = s + seperator; + for(int i=1; i parameters) + { + return null; + } + public static void main(String[] args) throws Exception + { + /* + Class clz = Class.forName("com.github.HarryHook.coding2017.litestruts.LoginAction"); + //鍒涘缓绫荤殑瀹炰緥 + Object obj = clz.newInstance(); + //寰楀埌鏂规硶鐨勫紩鐢 + Method method = clz.getDeclaredMethod("setName", java.lang.String.class); + //璋冪敤璇ユ柟娉 + method.invoke(obj, "test"); + LoginAction b = (LoginAction) obj; + System.out.println(b.getName()); + + method = clz.getDeclaredMethod("setPassword", java.lang.String.class); + method.invoke(obj, "1234"); + b = (LoginAction) obj; + System.out.println(b.getPassword()); + */ + + //鑾峰緱dom瑙f瀽鍣ㄥ伐鍘傦紝鐢ㄤ簬鍒涘缓鍏蜂綋鐨勮В鏋愬櫒 + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + //鑾峰緱鍏蜂綋鐨刣om瑙f瀽鍣 + DocumentBuilder db = dbf.newDocumentBuilder(); + //瑙f瀽涓涓獂ml鏂囨。,鑾峰緱Document瀵硅薄(鏍硅妭鐐) + Document doc = db.parse(new File("src/com/github/HarryHook/coding2017/litestruts/struts.xml")); + //瀵规枃妗h繘琛岃В鏋 + Element root = doc.getDocumentElement(); + parseElement(root); + System.out.println(""); + + } + private static void parseElement(Element element) + { + String tagName = element.getNodeName(); + NodeList children = element.getChildNodes(); + System.out.print("<" + tagName); + + //element鍏冪礌鐨勬墍鏈夊睘鎬ф墍鏋勬垚鐨凬amedNodeMap闃熷舰锛岃瀵瑰叾杩涜鍒ゆ柇 + NamedNodeMap nnm = element.getAttributes(); + + //濡傛灉鍏冪礌瀛樺湪灞炴 + if(nnm != null) + { + for(int i=0; i"); + + for(int i=0; i"); + } + } + System.out.print(""); + + } + + + +} \ No newline at end of file diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/StrutsTest.java b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/StrutsTest.java new file mode 100644 index 0000000000..458f2b190c --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/StrutsTest.java @@ -0,0 +1,57 @@ +package com.github.HarryHook.coding2017.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; + + 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"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + try + { + View 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")); + + }catch(Exception e) + { + e.printStackTrace(); + } + + } +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/View.java b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/View.java new file mode 100644 index 0000000000..5d1977d887 --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/View.java @@ -0,0 +1,31 @@ +package com.github.HarryHook.coding2017.litestruts; + +import java.util.Map; + +public class View +{ + private String jsp; + private Map parameters; + + //瀵瑰簲action鑾峰彇Jsp + public String getJsp() + { + return jsp; + } + public View setJsp(String jsp) + { + this.jsp = jsp; + return this; + } + + //execute()鑾峰彇瀵瑰簲鍙傛暟 + public Map getParameters() + { + return parameters; + } + public View setParameters(Map parameters) + { + this.parameters = parameters; + return this; + } +} diff --git a/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/struts.xml b/group02/727171008/src/com/github/HarryHook/coding2017/litestruts/struts.xml new file mode 100644 index 0000000000..7d2a8ed4ab --- /dev/null +++ b/group02/727171008/src/com/github/HarryHook/coding2017/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/group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java index e6bd69b5d8..1c1b9a6df3 100644 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/ArrayList.java @@ -32,7 +32,8 @@ public Object remove(int index){ ensureIndex(index); Object temp = elementData[index]; System.arraycopy(elementData, index+1, elementData, index, - size - index); + size - index - 1); + elementData[size-1] = null; size--; return temp; } diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java index 57be8bbfef..698506e2b5 100644 --- a/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/basic/test/BinaryTreeNodeTest.java @@ -18,7 +18,7 @@ public class BinaryTreeNodeTest { // 1 5 // 2 3 */ - @Before + @Before public void setUpBinaryTreeNode() { binaryTreeNode = new BinaryTreeNode(4); binaryTreeNode.insert(1); diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/ArrayUtil.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..5d7ad8f4d3 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/ArrayUtil.java @@ -0,0 +1,279 @@ +package com.github.miniyk2012.coding2017.coderising.array; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.IntStream; +import java.util.Arrays; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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 size = origin.length; + if (size == 0) return; + int start = 0, end = size-1; + while (start < end) { + int temp = origin[start]; + origin[start++] = origin[end]; + origin[end--] = 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){ + if (oldArray==null) return null; + List list=new ArrayList<>(); + for (int e : oldArray) { + if (e != 0) { + list.add(e); + } + } + return list2Array(list); + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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){ + if (array1==null || array2==null) return null; + if (array1.length == 0) return Arrays.copyOf(array2, array2.length); + List list = array2List(array1); + int currentIndex = 0; + for (int e : array2) { + for (int index = currentIndex; index < list.size(); index++ ) { + currentIndex = index + 1; + if (list.get(index) == e) break; + if (list.get(index) > e) { + list.add(index, e); + break; + } + } + if (e > list.get(list.size()-1)) list.add(e); + } + return list2Array(list); + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + * @throws Exception + */ + public int[] grow(int [] oldArray, int size) throws Exception{ + if (oldArray==null) return null; + if (size < 0) throw new Exception(); + int oldSize = oldArray.length; + int[] newArray = new int[size+oldSize]; + System.arraycopy(oldArray, 0, newArray, 0, oldSize); + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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 i = 1, j = 1; + List list = new ArrayList<>(); + list.add(i); + list.add(j); + int next = i+j; + while (max > next) { + list.add(next); + i = j; + j = next; + next = i+j; + } + return list2Array(list); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + // TODO 浣跨敤绛涙硶,鍐欑殑涓嶅ソ锛屾湁寰呮敼鍠 + if (max <= 2) return new int[0]; + List list = new ArrayList<>(); + int i; + for (i=2; i= 0) + list.remove(index); + k += currentNum; + } + currentNum = list.get(++i); + } + return list2Array(list); + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + List list = new ArrayList<>(); + int[] factors; + for (int i=1; i list = new ArrayList<>(); + for (int i=1; i < num; i++) { + if(num % i == 0) list.add(i); + } + return list2Array(list); + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + if (array.length == 0) return ""; + if (array.length == 1) return "" + array[0]; + StringBuilder s = new StringBuilder(); + for (int i=0; i杞崲涓虹浉鍚岄『搴忓拰闀垮害鐨刬nt[] + * @param list + * @return + */ + private int[] list2Array(List list) { + int size = list.size(); + int[] newArray = new int[size]; + for (int i=0; i + * @param array + * @return + */ + private List array2List(int[] array) { + List list = new ArrayList<>(); + for (int e : array) { + list.add(e); + } + return list; + } + + public static void main(String []args) throws Exception { + ArrayUtil arrayUtil = new ArrayUtil(); + + // merge + int[] a1 = {1,2,3}, a2 = {-4,-2,2,3,4}; +// int[] a1 = {}, a2 = {}; +// int[] a1 = {1,2,3}, a2 = {}; +// int[] a1 = {}, a2 = {1,2,3}; +// int[] a1 = {4,5,6}, a2 = {1,2,3}; + int[] a3 = arrayUtil.merge(a1, a2); + System.out.println(Arrays.toString(a3)); + + // reverse +// a1 = new int[] {}; +// a1 = new int[] {4,}; +// a1 = new int[] {4,3,5,6,7,7,8}; + a1 = new int[] {4,3,5,6,7,7,8, 9}; + arrayUtil.reverseArray(a1); + System.out.println(Arrays.toString(a1)); + + // remove zero +// a1 = new int[] {}; +// a1 = new int[] {0,0}; + a1 = new int[] {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + a2 = arrayUtil.removeZero(a1); + System.out.println(Arrays.toString(a2)); + + // grow + a1 = new int[] {1,2,3}; + a2 = arrayUtil.grow(a1, 4); +// a2 = arrayUtil.grow(a1, 2); +// a2 = arrayUtil.grow(a1, 0); + System.out.println(Arrays.toString(a2)); + + // fibonacci + a1 = arrayUtil.fibonacci(15); +// a1 = arrayUtil.fibonacci(1); +// a1 = arrayUtil.fibonacci(2); +// a1 = arrayUtil.fibonacci(-2); + System.out.println(Arrays.toString(a1)); + + // prime + a1 = arrayUtil.getPrimes(2); +// a1 = arrayUtil.getPrimes(3); +// a1 = arrayUtil.getPrimes(8); +// a1 = arrayUtil.getPrimes(12); +// a1 = arrayUtil.getPrimes(23); +// a1 = arrayUtil.getPrimes(24); +// a1 = arrayUtil.getPrimes(50); + a1 = arrayUtil.getPrimes(100); + System.out.println(Arrays.toString(a1)); + + // perfectNumbers + a1 = arrayUtil.getPerfectNumbers(1000); + System.out.println(Arrays.toString(a1)); + + // join +// a1 = new int[] {}; +// a1 = new int[] {1}; + a1 = new int[] {1,2,3}; + String str = arrayUtil.join(a1, "-"); + System.out.println(str); + + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/test/ArrayUtilTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/test/ArrayUtilTest.java new file mode 100644 index 0000000000..58de9f3efb --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/array/test/ArrayUtilTest.java @@ -0,0 +1,156 @@ +package com.github.miniyk2012.coding2017.coderising.array.test; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import com.github.miniyk2012.coding2017.coderising.array.ArrayUtil; + +public class ArrayUtilTest +{ + private ArrayUtil myArray; + + @Before + public void setUp() throws Exception + { + myArray = new ArrayUtil(); + } + + @Test + public void testReverseArray() + { + int[] a = {1, 2, 1, 3, 5, 6}; + int[] b = {6, 5, 3, 1, 2, 1}; + + myArray.reverseArray(a); + assertArrayEquals(a, b); + + int[] c = new int[0]; + myArray.reverseArray(c); + assertArrayEquals(c, new int[0]); + + } + + @Test + public void testRemoveZero() + { + int[] oldArr= {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 1, 2, 0, 5}; + int b[] = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 1, 2, 5}; + int[] c = myArray.removeZero(oldArr); + assertArrayEquals(b, c); + + int[] d = null; + int[] e = myArray.removeZero(d); + assertNull(e); + + } + + @Test + public void testMerge() + { + int a1[] = {1, 2, 3, 4, 5}; + int b1[] = {3, 4, 5, 6, 7, 8}; + int c1[] = {1, 2, 3, 4, 5, 6, 7, 8}; + int[] newArray1 = myArray.merge(a1, b1); + assertArrayEquals(c1, newArray1); + + int a2[] = new int[0]; + int b2[] = {0, 2, 3, 6, 7, 8}; + int c2[] = {0, 2, 3, 6, 7, 8}; + int[] newArray2 = myArray.merge(a2, b2); + assertArrayEquals(c2, newArray2); + + int a3[] = {0, 2, 3, 6, 7, 8}; + int b3[] = new int[0]; + int c3[] = {0, 2, 3, 6, 7, 8}; + int[] newArray3 = myArray.merge(a3, b3); + assertArrayEquals(c3, newArray3); + + int[] a4 = null; + int[] b4 = null; + int[] newArray4 = myArray.merge(a4, b4); + assertNull(newArray4); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testGrow() throws Exception + { + int[] a = {3, 5, 7, 8, 9}; + int [] oldA = Arrays.copyOf(a, a.length); + int[] b = {3, 5, 7, 8, 9, 0, 0, 0}; + int[] newArray = myArray.grow(a, 3); + assertArrayEquals(b, newArray); + assertArrayEquals(a, oldA); + + int[] c = null; + int[] newArray1 = myArray.grow(c, 3); + assertNull(newArray1); + + // size < 0 鎶涘嚭寮傚父 + expectedEx.expect(Exception.class); + myArray.grow(a, -3); + } + + @Test + public void testFibonacci() + { + //max == 1鏃惰繑鍥炵┖鏁扮粍 + int[] array1 = myArray.fibonacci(1); + int[] b = new int[0]; + assertArrayEquals(array1, b); + + + int[] array2= myArray.fibonacci(35); + int[] c = {1, 1, 2, 3, 5, 8, 13, 21, 34 }; + assertArrayEquals(c, array2); + } + + @Test + public void testGetPrimes() + { + int[] a = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; + int[] array1 = myArray.getPrimes(35); + assertArrayEquals(a, array1); + + //max <= 2鐨勬椂鍊欐病鏈夌礌鏁帮紝鏁扮粍涓虹┖鏁扮粍 + int[] array2 = myArray.getPrimes(1); + int[] b = new int[0]; + assertArrayEquals(array2, b); + } + + @Test + public void testGetPerfectNumbers() + { + int[] array = myArray.getPerfectNumbers(10000); + int[] a = {6, 28, 496, 8128 }; + assertArrayEquals(a, array); + } + + @Test + public void testJoin() + { + int[] Array0 = {3, 5, 7, 8, 9}; + String s0 = myArray.join(Array0, "-"); + String s1 = "3-5-7-8-9"; + assertEquals(s1, s0); + + int[] Array1 = {3}; + String s2 = myArray.join(Array1, "-"); + String s3 = "3"; + assertEquals(s2, s3); + + //浼犻掔┖鏁扮粍鏃讹紝杩斿洖绌哄瓧绗︿覆 + int[] Array2 = new int[0]; + String s4 = myArray.join(Array2, "-"); + String s5 = ""; + assertEquals(s4, s5); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LoginAction.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..b89a242df6 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LoginAction.java @@ -0,0 +1,48 @@ +package com.github.miniyk2012.coding2017.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 void setMessage(String message) { + this.message = message; + } + + 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; + } + + @Override + public String toString() { + return "LoginAction [name=" + name + ", password=" + password + ", message=" + message + "]"; + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LogoutAction.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LogoutAction.java new file mode 100644 index 0000000000..f283373b48 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/LogoutAction.java @@ -0,0 +1,48 @@ +package com.github.miniyk2012.coding2017.coderising.litestruts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author liuxin + * + */ +public class LogoutAction{ + private String name ; + private String password; + private String message; + + public String getName() { + return name; + } + + public String getPassword() { + return password; + } + + public void setMessage(String message) { + this.message = message; + } + + @Override + public String toString() { + return "LogoutAction [name=" + name + ", password=" + password + ", message=" + message + "]"; + } + + public String execute(){ + if("test".equals(name) && "1234".equals(password)){ + this.message = "logout successful"; + return "success"; + } + this.message = "logout failed,please check your user/pwd"; + return "error"; + } + + 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/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/Struts.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..54e8468077 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/Struts.java @@ -0,0 +1,173 @@ +package com.github.miniyk2012.coding2017.coderising.litestruts; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.logging.Logger; +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Field; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.io.SAXReader; +import org.dom4j.Element; + + +public class Struts { + + /** dom4j object model representation of a xml document. Note: We use the interface(!) not its implementation */ + private static Document doc; + private static Element aElement; + private static Object object; + private static View view; + private static final Logger logger = Logger.getLogger(Struts.class.getName()); + + public static View runAction(String actionName, Map parameters) throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + readXml(); + String retValue = processAction(actionName, parameters); + view = generateView(retValue); + return view; + } + + private static View generateView(String retValue) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + view = new View(); + Map map = getFields(); + String jsp = getJsp(retValue); + view.setParameters(map); + view.setJsp(jsp); + return view; + } + + private static String getJsp(String retValue) { + for (Iterator i = aElement.elementIterator( "result" ); i.hasNext();) { + Element result = (Element) i.next(); + if (result.attributeValue("name").equals(retValue)) { + return result.getText(); + } + } + return ""; + } + + /** + * @return + * @throws IntrospectionException + * @throws IllegalAccessException + * @throws InvocationTargetException + */ + private static Map getFields() + throws IntrospectionException, IllegalAccessException, InvocationTargetException { + Map map = new HashMap<>(); + Class clazz = object.getClass(); + Field[] fields = object.getClass().getDeclaredFields();//鑾峰緱灞炴 + for (Field field : fields) { + PropertyDescriptor pd = new PropertyDescriptor(field.getName(), + clazz); + Method getMethod = pd.getReadMethod();//鑾峰緱get鏂规硶 + String value = (String) getMethod.invoke(object);//鎵цget鏂规硶杩斿洖涓涓狾bject + map.put(field.getName(), value); + } + return map; + } + + private static void readXml() throws DocumentException { + String fileName = Thread.currentThread().getContextClassLoader().getResource("").getPath() + + "com/github/miniyk2012/coding2017/coderising/litestruts/struts.xml"; + File aFile = new File(fileName); + SAXReader xmlReader = new SAXReader(); + doc = xmlReader.read(aFile); + } + + private static String processAction(String actionName, Map parameters) throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + generateObject(actionName); + setFields(parameters); + return doExecute(); + } + + /** + * @return + * @throws NoSuchMethodException + * @throws IllegalAccessException + * @throws InvocationTargetException + */ + private static String doExecute() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + Class c = object.getClass(); + Method method = c.getMethod("execute"); + String ret = (String) method.invoke(object); + return ret; + } + + /** + * @param parameters + * @throws NoSuchMethodException + * @throws IllegalAccessException + * @throws InvocationTargetException + */ + private static void setFields(Map parameters) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + for (Map.Entry entry: parameters.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + key = "set" + key.substring(0, 1).toUpperCase() + key.substring(1); + Class c = object.getClass(); + Method method = c.getMethod(key, String.class); + method.invoke(object, value); + } + } + + /** + * @param actionName + * @throws InstantiationException + * @throws IllegalAccessException + * @throws ClassNotFoundException + */ + private static void generateObject(String actionName) + throws InstantiationException, IllegalAccessException, ClassNotFoundException { + Element root = doc.getRootElement(); + String className = null; + for ( Iterator i = root.elementIterator(); i.hasNext(); ) { + Element actionElement = (Element) i.next(); + if (actionElement.attributeValue("name").equals(actionName)) { + aElement = actionElement; + className = actionElement.attributeValue("class"); + break; + } + } + if (className == null) throw new InstantiationException("no such className"); + object = Class.forName(className).newInstance(); + } + + + public static void main(String args[]) throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException + { + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + View view = runAction("login", params); + logger.info(view.toString()); + } + +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/StrutsTest.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..9443a32d97 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/StrutsTest.java @@ -0,0 +1,75 @@ +package com.github.miniyk2012.coding2017.coderising.litestruts; + +import java.beans.IntrospectionException; +import java.lang.reflect.InvocationTargetException; +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, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + + 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, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + 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")); + } + + @Test + public void testLogoutActionSuccess() throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + + String actionName = "logout"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/welcome.jsp", view.getJsp()); + Assert.assertEquals("logout successful", view.getParameters().get("message")); + } + + @Test + public void testLogoutActionFailed() throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IntrospectionException { + String actionName = "logout"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/error.jsp", view.getJsp()); + Assert.assertEquals("logout failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/View.java b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/View.java new file mode 100644 index 0000000000..46759e7d01 --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/View.java @@ -0,0 +1,27 @@ +package com.github.miniyk2012.coding2017.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; + } + @Override + public String toString() { + return "View [jsp=" + jsp + ", parameters=" + parameters + "]"; + } +} diff --git a/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/struts.xml b/group02/812350401/src/com/github/miniyk2012/coding2017/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..0d3fd6495f --- /dev/null +++ b/group02/812350401/src/com/github/miniyk2012/coding2017/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 diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/array/ArrayUtil.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/array/ArrayUtil.java new file mode 100644 index 0000000000..05e26ebe51 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/array/ArrayUtil.java @@ -0,0 +1,228 @@ +package com.github.eloiseSJTU.coding2017.array; + +import java.security.InvalidParameterException; + +import com.github.eloiseSJTU.coding2017.basic.ArrayList; +import com.github.eloiseSJTU.coding2017.basic.List; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 渚嬪锛 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) { + if (origin == null) { + return; + } + int left = 0; + int right = origin.length - 1; + while (left < right) { + int tmp = origin[left]; + origin[left] = origin[right]; + origin[right] = tmp; + left++; + right--; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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) { + if (oldArray == null) { + return null; + } + int count = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + count++; + } + } + int[] newArray = new int[count]; + int index = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArray[index++] = oldArray[i]; + } + } + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 渚嬪 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) { + if (array1 == null) { + return array2; + } + if (array2 == null) { + return array1; + } + int len1 = array1.length; + int len2 = array2.length; + ArrayList arrayList = new ArrayList(); + int i = 0; + int j = 0; + while (i < len1 && j < len2) { + if (array1[i] < array2[j]) { + arrayList.add(array1[i]); + i++; + } else if (array1[i] > array2[j]) { + arrayList.add(array2[j]); + j++; + } else { + arrayList.add(array1[i]); + i++; + j++; + } + } + while (i < len1) { + arrayList.add(array1[i++]); + } + while (j < len2) { + arrayList.add(array2[j++]); + } + return toArray(arrayList); + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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) { + if (oldArray == null) { + return null; + } + if (size < 0) { + throw new InvalidParameterException("size can't be negative"); + } + int[] newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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]; + } + ArrayList arrayList = new ArrayList(); + int a = 1; + arrayList.add(a); + int b = 1; + arrayList.add(b); + int c = a + b; + while (c < max) { + arrayList.add(c); + a = b; + b = c; + c = a + b; + } + + return toArray(arrayList); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + ArrayList arrayList = new ArrayList(); + for (int i = 2; i < max; i++) { + boolean pn = true; + for (int j = 0; j < arrayList.size(); j++) { + if (i % (int) arrayList.get(j) == 0) { + pn = false; + break; + } + } + if (pn) { + arrayList.add(i); + } + } + + return toArray(arrayList); + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + ArrayList arrayList = new ArrayList(); + for (int i = 2; i < max; i++) { + int sum = 0; + for (int j = 1; j < i; j++) { + if (i % j == 0) { + sum += j; + } + } + if (sum == i) { + arrayList.add(i); + } + } + return toArray(arrayList); + } + + /** + * 鐢╯eperator 鎶婃暟缁 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 ""; + } + StringBuffer stringBuffer = new StringBuffer(); + int i = 0; + for (; i < array.length - 1; i++) { + stringBuffer.append(array[i] + seperator); + } + stringBuffer.append(array[i]); + return stringBuffer.toString(); + } + + private int[] toArray(List list) { + int size = list.size(); + int[] result = new int[size]; + for (int i = 0; i < size; i++) { + result[i] = (int) list.get(i); + } + return result; + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/array/test/ArrayUtilTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/array/test/ArrayUtilTest.java new file mode 100644 index 0000000000..88a5108d07 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/array/test/ArrayUtilTest.java @@ -0,0 +1,142 @@ +package com.github.eloiseSJTU.coding2017.array.test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.github.eloiseSJTU.coding2017.array.ArrayUtil; + +public class ArrayUtilTest { + private ArrayUtil myArray; + + @Before + public void setUp() throws Exception { + myArray = new ArrayUtil(); + } + + @Test + public void testReverseArray() { + int[] a = { 1, 2, 1, 3, 5, 6 }; + int[] b = { 6, 5, 3, 1, 2, 1 }; + + myArray.reverseArray(a); + assertArrayEquals(a, b); + + int[] c = new int[0]; + myArray.reverseArray(c); + assertArrayEquals(c, new int[0]); + } + + @Test + public void testRemoveZero() { + int[] oldArr = { 1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 1, 2, 0, 5 }; + int b[] = { 1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 1, 2, 5 }; + int[] c = myArray.removeZero(oldArr); + assertArrayEquals(b, c); + + int[] d = null; + int[] e = myArray.removeZero(d); + assertNull(e); + } + + @Test + public void testMerge() { + int a1[] = { 1, 2, 3, 4, 5 }; + int b1[] = { 3, 4, 5, 6, 7, 8 }; + int c1[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + int[] newArray1 = myArray.merge(a1, b1); + assertArrayEquals(c1, newArray1); + + int a2[] = new int[0]; + int b2[] = { 0, 2, 3, 6, 7, 8 }; + int c2[] = { 0, 2, 3, 6, 7, 8 }; + int[] newArray2 = myArray.merge(a2, b2); + assertArrayEquals(c2, newArray2); + + int a3[] = { 0, 2, 3, 6, 7, 8 }; + int b3[] = new int[0]; + int c3[] = { 0, 2, 3, 6, 7, 8 }; + int[] newArray3 = myArray.merge(a3, b3); + assertArrayEquals(c3, newArray3); + + int[] a4 = null; + int[] b4 = null; + int[] newArray4 = myArray.merge(a4, b4); + assertNull(newArray4); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testGrow() { + int[] a = { 3, 5, 7, 8, 9 }; + int[] b = { 3, 5, 7, 8, 9, 0, 0, 0 }; + int[] newArray = myArray.grow(a, 3); + assertArrayEquals(b, newArray); + + int[] c = null; + int[] newArray1 = myArray.grow(c, 3); + assertNull(newArray1); + + // size < 0 鎶涘嚭寮傚父 + expectedEx.expect(Exception.class); + myArray.grow(a, -3); + } + + @Test + public void testFibonacci() { + // max == 1鏃惰繑鍥炵┖鏁扮粍 + int[] array1 = myArray.fibonacci(1); + int[] b = new int[0]; + assertArrayEquals(array1, b); + + int[] array2 = myArray.fibonacci(35); + int[] c = { 1, 1, 2, 3, 5, 8, 13, 21, 34 }; + assertArrayEquals(c, array2); + } + + @Test + public void testGetPrimes() { + int[] a = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; + int[] array1 = myArray.getPrimes(35); + assertArrayEquals(a, array1); + + // max <= 2鐨勬椂鍊欐病鏈夌礌鏁帮紝鏁扮粍涓虹┖鏁扮粍 + int[] array2 = myArray.getPrimes(1); + int[] b = new int[0]; + assertArrayEquals(array2, b); + } + + @Test + public void testGetPerfectNumbers() { + int[] array = myArray.getPerfectNumbers(10000); + int[] a = { 6, 28, 496, 8128 }; + assertArrayEquals(a, array); + } + + @Test + public void testJoin() { + int[] Array0 = { 3, 5, 7, 8, 9 }; + String s0 = myArray.join(Array0, "-"); + String s1 = "3-5-7-8-9"; + assertEquals(s1, s0); + + int[] Array1 = { 3 }; + String s2 = myArray.join(Array1, "-"); + String s3 = "3"; + assertEquals(s2, s3); + + // 浼犻掔┖鏁扮粍鏃讹紝杩斿洖绌哄瓧绗︿覆 + int[] Array2 = new int[0]; + String s4 = myArray.join(Array2, "-"); + String s5 = ""; + assertEquals(s4, s5); + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/LoginAction.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/LoginAction.java new file mode 100644 index 0000000000..f95e054d95 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/LoginAction.java @@ -0,0 +1,46 @@ +package com.github.eloiseSJTU.coding2017.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; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/Struts.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/Struts.java new file mode 100644 index 0000000000..11df02b877 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/Struts.java @@ -0,0 +1,81 @@ +package com.github.eloiseSJTU.coding2017.litestruts; + +import java.beans.PropertyDescriptor; +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + View view = new View(); + SAXReader saxReader = new SAXReader(); + try { + // 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + Document document = saxReader.read(new File(getPackagePath() + "struts.xml")); + Element struts = document.getRootElement(); + for (Iterator i = struts.elementIterator("action"); i.hasNext();) { + Element action = (Element) i.next(); + // 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class锛屼緥濡侺oginAction锛岄氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + if (actionName.equals(action.attributeValue("name"))) { + String className = action.attributeValue("class"); + Class clazz = Class.forName(className); + Object object = clazz.newInstance(); + // 鏍规嵁parameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + // ("name"="test", "password"="1234")锛岄偅灏卞簲璇ヨ皟鐢 + // setName鍜宻etPassword鏂规硶 + for (Map.Entry entry : parameters.entrySet()) { + PropertyDescriptor descriptor = new PropertyDescriptor(entry.getKey(), clazz); + Method method = descriptor.getWriteMethod(); + method.invoke(object, entry.getValue()); + } + // 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xecute鏂规硶锛屽苟鑾峰緱杩斿洖鍊 + Method excute = clazz.getMethod("execute"); + String result = (String) excute.invoke(object); + // 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛夛紝閫氳繃鍙嶅皠鏉ヨ皟鐢紝 + // 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap锛屼緥濡倇"message": "鐧诲綍鎴愬姛"}锛 + // 鏀惧埌View瀵硅薄鐨刾arameters + Map map = new HashMap<>(); + Field[] fields = clazz.getDeclaredFields(); + for (Field field : fields) { + PropertyDescriptor descriptor = new PropertyDescriptor(field.getName(), clazz); + Method method = descriptor.getReadMethod(); + Object value = method.invoke(object); + map.put(field.getName(), value); + } + view.setParameters(map); + // 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 + // 纭畾鍝竴涓猨sp鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + for (Iterator j = action.elementIterator("result"); j.hasNext();) { + Element element = (Element) j.next(); + if (result.equals(element.attributeValue("name"))) { + view.setJsp(element.getText()); + break; + } + } + break; + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + + return view; + } + + private static String getPackagePath() { + String path = Struts.class.getResource("").toString().replace("/", File.separator); + if (path.startsWith("file")) { + path = path.substring(5); + } + return path; + } + +} diff --git a/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/View.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/View.java new file mode 100644 index 0000000000..093026d7c3 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/View.java @@ -0,0 +1,26 @@ +package com.github.eloiseSJTU.coding2017.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/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/struts.xml b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/struts.xml new file mode 100644 index 0000000000..ba5aa33139 --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/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/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/test/StrutsTest.java b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/test/StrutsTest.java new file mode 100644 index 0000000000..f52b71975f --- /dev/null +++ b/group02/851113375/src/com/github/eloiseSJTU/coding2017/litestruts/test/StrutsTest.java @@ -0,0 +1,41 @@ +package com.github.eloiseSJTU.coding2017.litestruts.test; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +import com.github.eloiseSJTU.coding2017.litestruts.Struts; +import com.github.eloiseSJTU.coding2017.litestruts.View; + +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/group02/953840070/src/com/github/lqingchenl/coding2017/array/ArrayUtil.java b/group02/953840070/src/com/github/lqingchenl/coding2017/array/ArrayUtil.java new file mode 100644 index 0000000000..a221c781b5 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/array/ArrayUtil.java @@ -0,0 +1,296 @@ +package com.github.lqingchenl.coding2017.array; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 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 length = origin.length; + + int[] b = new int[length]; + for (int i = 0; i < length; i++) { + b[i] = origin[i]; + } + for (int i = 0; i < length; i++) { + origin[length - i - 1] = b[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) { + if (oldArray == null) { + return null; + } + int length = oldArray.length; + for (int i : oldArray) { + if (i == 0) { + length--; + } + } + + int[] newArray = new int[length]; + int j = 0; + for (int i : oldArray) { + if (i != 0) { + newArray[j] = i; + j++; + } + } + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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) { + if (array1 == null || array1.length == 0) { + return array2; + } + if (array2 == null || array2.length == 0) { + return array1; + } + + Map map = new HashMap<>(); + + + int sameNum = 0; + for (int i : array1) { + for (int j : array2) { + if (i == j) { + sameNum++; + map.put(i, 1); + } + } + } + + int length = array1.length + array2.length - sameNum; + int[] newArray = new int[length]; + int index = 0; + + for (int i : array1) { + newArray[index] = i; + index++; + } + for (int j : array2) { + if (map.get(j) == null) { + newArray[index] = j; + index++; + } + } + + for (int i = 0; i < newArray.length - 1; i++) { + for (int j = i + 1; j < newArray.length; j++) { + if (newArray[i] > newArray[j]) { + int temp = newArray[j]; + newArray[j] = newArray[i]; + newArray[i] = temp; + } + } + } + + return newArray; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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) throws Exception { + + if (oldArray == null) { + return null; + } + if (size < 0) { + throw new Exception("12"); + } + + int newLength = oldArray.length + size; + int[] newArray = new int[newLength]; + for (int i = 0; i < newLength; i++) { + if (i < oldArray.length) { + newArray[i] = oldArray[i]; + } else { + newArray[i] = 0; + } + + } + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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 index = 0; + for (int i = 0; i < Integer.MAX_VALUE; i++) { + if (getFibonacci(i) > max) { + index = i; + break; + } + } + int[] array = new int[index]; + for (int i = 0; i < index; i++) { + array[i] = getFibonacci(i); + } + return array; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + if (max == 1) { + return new int[0]; + } + + int length = 0; + for (int i = 1; i <= max; i++) { + if (isPrime(i)) { + length++; + } + } + int[] array = new int[length]; + int j = 0; + for (int i = 1; i <= max; i++) { + if (isPrime(i)) { + array[j] = i; + j++; + } + } + + return array; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + StringBuffer stringBuffer = new StringBuffer(); + for (int i = 1; i <= max; i++) { + int temp = 0; + for (int n = 1; n < i / 2 + 1; n++) { + if (i % n == 0) { + temp += n; + } + } + if (temp == i) { + stringBuffer.append(i).append("-"); + } + } + String[] strArray = stringBuffer.toString().split("-"); + int length = stringBuffer.toString().split("-").length; + int[] intAarray = new int[length]; + for (int i=0; i parameters) { + + try { + Document d = Jsoup.parse(new File("src/com/github/lqingchenl/coding2017/litestruts/struts.xml"), "UTF-8"); //璇诲彇閰嶇疆鏂囦欢 + String classStr = null; + + Map resultMap = new HashMap<>(); + for (Element element : d.select("action")) { + if (element.attr("name").equals(actionName)) { + classStr = element.attr("class"); + for (Element element1 : element.select("result")) { + resultMap.put(element1.attr("name"), element1.text()); + } + } + } + + if (StringUtil.isBlank(classStr)) { + return null; + } + Class c = Class.forName(classStr); + Object object = c.newInstance(); //鍒涘缓瀵硅薄 + //鍐欐暟鎹 setName鍜宻etPassword鏂规硶 + for (Map.Entry entry : parameters.entrySet()) { + PropertyDescriptor pd = new PropertyDescriptor(entry.getKey(), c);//浣跨敤java.beans.PropertyDescriptor鑾峰彇Method杩涜鏂规硶璋冪敤 + Method method = pd.getWriteMethod();//鑾峰緱鍐欐柟娉 + method.invoke(object, entry.getValue()); + } + + //閫氳繃鍙嶅皠锛屾墽琛宔xecute鏂规硶 + Method method = c.getDeclaredMethod("execute", null); + String result = (String) method.invoke(object); + +// 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛,閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , +// 鏀惧埌View瀵硅薄鐨刾arameters + Map map = new HashMap<>(); + Field[] fields = c.getDeclaredFields(); + for (Field field : fields) { + PropertyDescriptor pd = new PropertyDescriptor(field.getName(), c); // message娌℃湁set鏂规硶锛屾姤閿 + Method getMethod = pd.getReadMethod(); + String str = (String) getMethod.invoke(object); + map.put(field.getName(), str); + + } +// 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛屾斁鍒癡iew瀵硅薄鐨刯sp瀛楁涓 + View view = new View(); + view.setParameters(map); + String jsp = resultMap.get(result); + view.setJsp(jsp); + return view; + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + +} diff --git a/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/StrutsTest.java b/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/StrutsTest.java new file mode 100644 index 0000000000..e17b6154a2 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/StrutsTest.java @@ -0,0 +1,40 @@ +package com.github.lqingchenl.coding2017.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/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/View.java b/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/View.java new file mode 100644 index 0000000000..487232ce7b --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/View.java @@ -0,0 +1,26 @@ +package com.github.lqingchenl.coding2017.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/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/struts.xml b/group02/953840070/src/com/github/lqingchenl/coding2017/litestruts/struts.xml new file mode 100644 index 0000000000..73218d4675 --- /dev/null +++ b/group02/953840070/src/com/github/lqingchenl/coding2017/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/group04/1906242834/src/com/coderising/litestruts/struts.xml b/group04/1906242834/src/com/coderising/litestruts/struts.xml index ffc6373269..e64e8017a9 100644 --- a/group04/1906242834/src/com/coderising/litestruts/struts.xml +++ b/group04/1906242834/src/com/coderising/litestruts/struts.xml @@ -1,6 +1,5 @@ - @@ -11,4 +10,5 @@ /jsp/welcome.jsp /jsp/error.jsp - \ No newline at end of file + + diff --git a/group05/1026626960/.classpath b/group05/1026626960/.classpath index fb5011632c..bb62e29268 100644 --- a/group05/1026626960/.classpath +++ b/group05/1026626960/.classpath @@ -2,5 +2,8 @@ + + + diff --git a/group05/1026626960/src/cn/study2/array/ArrayUtil.java b/group05/1026626960/src/cn/study2/array/ArrayUtil.java new file mode 100644 index 0000000000..a57881918b --- /dev/null +++ b/group05/1026626960/src/cn/study2/array/ArrayUtil.java @@ -0,0 +1,213 @@ +package cn.study2.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] + */ + public void exchangeArray(int[] a){ + int index = a.length; + int a1 = 0; + for(int i = 1; i < index/2; i++){ + a[i] = a1; + a[i] = a[index - i]; + a[index - i] = 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} + */ + public int[] removeZero(){ + int oldArr[] = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int size = 0; + for(int i = 0; i < oldArr.length; i++){ + if(oldArr[i] != 0){ + size++; + } + } + int newArr[] = new int[size]; + for(int i = 0; i < oldArr.length; i++){ + int j = 0; + if(oldArr[i] != 0){ + newArr[j] = oldArr[i]; + j++; + } + } + return newArr; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + */ + public int[] reshapeArr(int a1[], int a2[]){ + int size = a1.length+a2.length; + int a3[] = new int[size]; + //未完成 + return a3; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + */ + public int[] addSize(int oldArr[],int size){ + int newSize = oldArr.length+size; + int newArr[] = new int[newSize]; + for(int i = 0; i < newArr.length; i++){ + if(i < oldArr.length){ + newArr[i] = oldArr[i]; + } + newArr[i] = 0; + } + return newArr; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + */ + public int[] method1(int max){ + int Arr[] = new int[max]; + if(max <= 1){ + return null; + }else if(max <= 2){ + int a[] = {1,1,2}; + return a; + }else{ + Arr[0] = 1; + Arr[1] = 1; + Arr[2] = 2; + int n = 3; + while(n < max){ + Arr[n] = Arr[n-2] + Arr[n-1]; + } + return Arr; + } + } + + /** + * 求给定数的斐波拉契数 + */ + //未完成 + + + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + */ + public int[] getPrimes(int max) { + int size = 0; + for (int i = 0; i < max; i++) { + if (isPrimes(i)) { + size++; + } + } + int Arr[] = new int[size]; + int j = 0; + for (int i = 0; i < max; i++) { + if (isPrimes(i)) { + Arr[j++] = i; + } + } + return Arr; + } + + /** + * 判断是否为素数 + */ + public boolean isPrimes(int num){ + if(num<=1){ + return false; + }else{ + for(int i=2;i + /index.jsp + /login.jsp + + */ +public class ActionMapping { + // 请求路径名称 + private String name; + // 处理aciton类的全名 + private String className; + // 处理方法 + private String method; + // 结果视图集合 + private Map results; + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getClassName() { + return className; + } + public void setClassName(String className) { + this.className = className; + } + public String getMethod() { + return method; + } + public void setMethod(String method) { + this.method = method; + } + public Map getResults() { + return results; + } + public void setResults(Map results) { + this.results = results; + } +} diff --git a/group05/1026626960/src/cn/study2/myStruts/Result.java b/group05/1026626960/src/cn/study2/myStruts/Result.java new file mode 100644 index 0000000000..0ed80bd80f --- /dev/null +++ b/group05/1026626960/src/cn/study2/myStruts/Result.java @@ -0,0 +1,28 @@ +package cn.study2.myStruts; + +public class Result { + // 跳转的结果标记 + private String name; + // 跳转类型,默认为转发; "redirect"为重定向 + private String type; + // 跳转的页面 + private String page; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getType() { + return type; + } + public void setType(String type) { + this.type = type; + } + public String getPage() { + return page; + } + public void setPage(String page) { + this.page = page; + } +} diff --git a/group05/1026626960/src/cn/study2/myStruts/Test.java b/group05/1026626960/src/cn/study2/myStruts/Test.java new file mode 100644 index 0000000000..3f571354f9 --- /dev/null +++ b/group05/1026626960/src/cn/study2/myStruts/Test.java @@ -0,0 +1,7 @@ +package cn.study2.myStruts; + +public class Test { + public static void main(String[] args) { + new readStrutsXml(); + } +} diff --git a/group05/1026626960/src/cn/study2/myStruts/readStrutsXml.java b/group05/1026626960/src/cn/study2/myStruts/readStrutsXml.java new file mode 100644 index 0000000000..c90fdfe5af --- /dev/null +++ b/group05/1026626960/src/cn/study2/myStruts/readStrutsXml.java @@ -0,0 +1,98 @@ +package cn.study2.myStruts; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.Test; + +/** + * 本程序模拟读取类似一下struts.xml文件 + * + + + + + /index.jsp + /login.jsp + + + + /login + + + + + + * @author zhengliang + * + */ +public class readStrutsXml { + // 保存action的集合 + private Map allActions = new HashMap(); + public readStrutsXml() { + this.init(); + } + + // 初始化allActions集合 + private void init() { + /********DOM4J读取配置文件***********/ + System.out.println("正在读取mystruts.xml文件"); + try { + // 得到解析器 + SAXReader reader = new SAXReader(); + // 得到src/mystruts.xml 文件流 + InputStream inStream = this.getClass().getResourceAsStream("/mystruts.xml"); + // 加载文件 + Document doc = reader.read(inStream); + // 获取根 + Element root = doc.getRootElement(); + // 得到package节点 + Element ele_package = root.element("package"); + // 得到package节点下, 所有的action子节点 + List listAction = ele_package.elements("action"); + // 遍历 ,封装 + for (Element ele_action : listAction) { + // 封装一个ActionMapping对象 + ActionMapping actionMapping = new ActionMapping(); + actionMapping.setName(ele_action.attributeValue("name")); + actionMapping.setClassName(ele_action.attributeValue("class")); + actionMapping.setMethod(ele_action.attributeValue("method")); + + // 封装当前aciton节点下所有的结果视图 + Map results = new HashMap(); + + // 得到当前action节点下所有的result子节点 + Iterator it = ele_action.elementIterator("result"); + while (it.hasNext()) { + // 当前迭代的每一个元素都是 + Element ele_result = it.next(); + // 封装对象 + Result res = new Result(); + res.setName(ele_result.attributeValue("name")); + res.setType(ele_result.attributeValue("type")); + res.setPage(ele_result.getTextTrim()); + // 添加到集合 + results.put(res.getName(), res); + } + // 设置到actionMapping中 + actionMapping.setResults(results); + + // 6.x actionMapping添加到map集合 + allActions.put(actionMapping.getName(), actionMapping); + } + + System.out.println("struts.xml文件读取完毕"); + + } catch (Exception e) { + throw new RuntimeException("启动时候初始化错误",e); + } + } + + +} diff --git a/group05/1026626960/src/mystruts.xml b/group05/1026626960/src/mystruts.xml new file mode 100644 index 0000000000..2a3b40e879 --- /dev/null +++ b/group05/1026626960/src/mystruts.xml @@ -0,0 +1,15 @@ + + + + + /index.jsp + /login.jsp + + + + /login + + + + + \ No newline at end of file diff --git a/group05/1094051862/test01/.classpath b/group05/1094051862/test01/.classpath index 04cc82dc42..9794cd8084 100644 --- a/group05/1094051862/test01/.classpath +++ b/group05/1094051862/test01/.classpath @@ -3,5 +3,6 @@ + diff --git a/group05/1094051862/test01/src/com/coderising/array/ArrayUtil.java b/group05/1094051862/test01/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..80c2bf7c32 --- /dev/null +++ b/group05/1094051862/test01/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,235 @@ +package com.coderising.array; + +import org.junit.experimental.max.MaxCore; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] 濡傛灉 a = + * [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public static void reverseArray(int[] origin) { + if (origin.length < 2) + return; + int temp; + for (int i = 0; i < origin.length >> 1; i++) { + temp = origin[i]; + origin[i] = origin[origin.length - 1 - i]; + origin[origin.length - 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 static int[] removeZero(int[] oldArray) { + if (oldArray == null || oldArray.length == 0) + return null; + int zeros = 0;// 鏁扮粍涓0鍏冪礌鐨勪釜鏁 + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) + zeros++; + } + int[] newArr = new int[oldArray.length - zeros]; + for (int i = 0, j = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArr[j] = oldArray[i]; + j++; + } + } + return newArr; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 渚嬪 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2) { + int[] mergedArr = new int[array1.length + array2.length]; + int temp; + int i = 0, j = 0, index = 0, size = 0; + while (i < array1.length && j < array2.length) { + //涓や釜鏁扮粍閮芥病閬嶅巻鍒版渶鍚庝竴涓厓绱 + if (i != array1.length - 1 && j != array2.length - 1) { + if (array1[i] < array2[j]) { + temp = array1[i++]; + } else if (array1[i] > array2[j]) { + temp = array2[j++]; + } else { + //閬囧埌鐩哥瓑鍏冪礌锛屽瓨鏀句换鎰忎竴涓氨瀹炵幇鍘婚噸浜 + temp = array1[i++]; + j++; + } + mergedArr[index++] = temp; + size++; + //array1閬嶅巻鍒版渶鍚庝竴涓厓绱 + } else if (i == array1.length - 1 && j != array2.length - 1) { + if (array1[i] < array2[j]) { + temp = array1[i]; + mergedArr[index++] = temp; + size++; + //灏哸rray2鐨勫墿浣欏厓绱犲鍒跺埌mergedArr涓 + System.arraycopy(array2, j, mergedArr, index, array2.length - j); + size += array2.length - j; + break; + } else if (array1[i] > array2[j]) { + temp = array2[j++]; + size++; + } else { + System.arraycopy(array2, j, mergedArr, index, array2.length - j); + size += array2.length - j; + break; + } + //array2閬嶅巻鍒版渶鍚庝竴涓厓绱 + } else if (i != array1.length - 1 && j == array2.length - 1) { + if (array1[i] > array2[j]) { + temp = array2[j]; + mergedArr[index++] = temp; + size++; + //灏哸rray1鐨勫墿浣欏厓绱犲鍒跺埌mergedArr涓 + System.arraycopy(array1, i, mergedArr, index, array1.length - i); + size += array1.length - i; + break; + } else if (array1[i] < array2[j]) { + temp = array2[i++]; + size++; + } else { + System.arraycopy(array1, i, mergedArr, index, array1.length - i); + size += array1.length - i; + break; + } + } + } + //鏋勯犳柊鏁扮粍锛屽幓闄ergedArr涓熬閮ㄨ嫢骞0鍏冪礌 + int[] result = new int[size]; + System.arraycopy(mergedArr, 0, result, 0, size); + return result; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + int[] arr = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, arr, 0, oldArray.length); + return arr; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 渚嬪锛 max = 15 , + * 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max < 2) return null; + int[] a = new int[max]; + a[0] = 1; + a[1] = 1; + int size = 2; + for (int i = 2;; i++) { + a[i] = a[i-1] + a[i-2]; + if (a[i] > max) break; + size ++; + } + int[] fibonacci = new int[size]; + System.arraycopy(a, 0, fibonacci, 0, size); + return fibonacci; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + if (max < 2) { + return null; + } + int[] a = new int[max]; + int size = 0; + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + a[size++] = i; + } + } + int[] primes = new int[size]; + System.arraycopy(a, 0, primes, 0, size); + return primes; + } + private static boolean isPrime(int i) { + for (int j = 2; j*j <= i; j++) { + if (i % j == 0) { + return false; + } + } + return true; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + int[] a = new int[max]; + int size = 0; + for (int i = 6; i < max; i++) { + if (isPerfectNumber(i)) { + a[size++] = i; + } + } + int[] perfectNumbers = new int[size]; + System.arraycopy(a, 0, perfectNumbers, 0, size); + return perfectNumbers; + } + private static boolean isPerfectNumber(int i) { + int sum = 0; + for (int j = 1; j <= i >> 1; j++) { + if (i % j == 0) { + sum += j; + } + } + if (i == sum) return true; + else return false; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 渚嬪array= [3,8,9], seperator = "-" 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator) { + StringBuilder sb = new StringBuilder(); + for (int i : array) { + sb.append(i); + sb.append(seperator); + } + return sb.substring(0, sb.lastIndexOf(seperator)); + } + +} diff --git a/group05/1094051862/test01/src/com/coderising/array/ArrayUtilTest.java b/group05/1094051862/test01/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..ae75b41379 --- /dev/null +++ b/group05/1094051862/test01/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,91 @@ +package com.coderising.array; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import sun.misc.Perf.GetPerfAction; + +public class ArrayUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReverseArray() { + int[] a = {7,9,30,3,5}; + int[] b = a.clone(); + ArrayUtil.reverseArray(a); + for (int i = 0; i < b.length; i ++) { + Assert.assertEquals(b[i], a[b.length - 1 - i]); + } + + } + + @Test + public void testRemoveZero() { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] result = {1,3,4,5,6,6,5,4,7,6,7,5}; + int[] removeZero = ArrayUtil.removeZero(oldArr); + assertArrayEquals(result, removeZero); + } + + @Test + public void testMerge() { + int[] a1 = {3,5,7,8}; + int[] a2 = {4,5,6,7}; + int[] a3 = {3,4,5,6,7,8}; + int[] merge = ArrayUtil.merge(a1, a2); + assertArrayEquals(a3, merge); + } + + @Test + public void testGrow() { + int[] oldArray = {2,3,6}; + int size = 3; + int[] growedArr = {2,3,6,0,0,0}; + assertArrayEquals(growedArr, ArrayUtil.grow(oldArray, size)); + } + + @Test + public void testFibonacci() { + int[] fibonacci = ArrayUtil.fibonacci(15); + for (int i : fibonacci) { + System.out.println(i); + } + } + + @Test + public void testGetPrimes() { + int[] primes = ArrayUtil.getPrimes(3); + for (int i : primes) { + System.out.println(i); + } + } + + @Test + public void testGetPerfectNumbers() { + int[] perfectNumbers = ArrayUtil.getPerfectNumbers(10000); + int[] expecteds = {6,28,496,8128}; + assertArrayEquals(expecteds, perfectNumbers); + for (int i : perfectNumbers) { + System.out.println(i); + } + } + + @Test + public void testJoin() { + int[] arr = {3,4,5}; + String join = ArrayUtil.join(arr, "-"); + assertEquals("3-4-5", join); + } + +} diff --git a/group05/1094051862/test01/src/com/coderising/litestruts/LoginAction.java b/group05/1094051862/test01/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group05/1094051862/test01/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; + } +} diff --git a/group05/1094051862/test01/src/com/coderising/litestruts/Struts.java b/group05/1094051862/test01/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..c9e9f50631 --- /dev/null +++ b/group05/1094051862/test01/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,181 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.xml.parsers.ParserConfigurationException; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.DocumentHelper; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.Test; +import org.omg.PortableInterceptor.ObjectIdHelper; +import org.xml.sax.SAXException; + +import com.coding.basic.ArrayList; +import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ; + +public class Struts { + @Test + public static View runAction(String actionName, + Map parameters) { + + /* + * + * 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + * + * 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + * 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 ("name"="test" , + * "password"="1234") , 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + * + * 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + * + * 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 + * {"message": "鐧诲綍鎴愬姛"} , 鏀惧埌View瀵硅薄鐨刾arameters + * + * 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + * 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + + // DOM4J鏂瑰紡瑙f瀽xml + // 璇诲彇鏂囦欢 杞崲鎴怐ocument + View view = new View(); + if (actionName == null || actionName.isEmpty()) { + return view; + } + try { + //鑾峰彇action鏍囩鍏冪礌 + Element actionEle = getActionElement(actionName); + + String qualifiedName = actionEle.attributeValue("class"); + Class clazz = Class.forName(qualifiedName); + Object instanceOfAction = getParameteredInstance(parameters, clazz); + Method executeMethod = clazz.getMethod("execute"); + + //璋冪敤execute鏂规硶锛屾敼鍙榓ction瀹炰緥锛屼竴瀹氳鍦ㄦ墽琛実etResultMap鏂规硶涔嬪墠鎵ц + String executeResult = (String) executeMethod.invoke(instanceOfAction); + Method[] methods = clazz.getMethods(); + Map resultMap = getResultMap(instanceOfAction, methods); + view.setParameters(resultMap); + + List resultEles = actionEle.elements("result"); + String jspPath = getJspPath(executeResult, resultEles); + view.setJsp(jspPath); + } catch (ClassNotFoundException e1) { + e1.printStackTrace(); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return view; + } + + private static String getJspPath(String result, List resultEles) { + for (int i = 0; i < resultEles.size(); i++) { + String text = resultEles.get(i).attributeValue("name"); + if (text.equals(result)) { + String jspString = resultEles.get(i).getText(); + return jspString; + } + } + return null; + } + + private static Map getResultMap(Object obj, Method[] methodsOfObj) + throws IllegalAccessException, InvocationTargetException { + Map resultMap = new HashMap(); + Method m; + for (int i = 0; i < methodsOfObj.length; i++) { + if (methodsOfObj[i].getName().startsWith("get")) { + m = methodsOfObj[i]; + String methodName = getMethodNameInLowerCase(m); + Object result = m.invoke(obj); + resultMap.put(methodName.substring(3), result); + } + } + return resultMap; + } + + private static String getMethodNameInLowerCase(Method m) { + String methodName = m.getName(); + StringBuilder sbr = new StringBuilder(methodName); + sbr.setCharAt(3, Character.toLowerCase(sbr.charAt(3))); + methodName = sbr.toString(); + return methodName; + } + + private static Object getParameteredInstance(Map parameters, + Class clazz) { + Object obj = null; + try { + obj = clazz.newInstance(); + Map methodsAndParams = new HashMap(); + Set keySet = parameters.keySet(); + for (String k : keySet) { + String methodName = getMethodNameByProperty(k); + methodsAndParams.put(methodName, parameters.get(k)); + Method setter = clazz.getMethod(methodName, parameters.get(k) + .getClass()); + setter.invoke(obj, parameters.get(k)); + } + } catch (InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return obj; + } + + private static String getMethodNameByProperty(String k) { + StringBuilder sb = new StringBuilder(k); + sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); + sb = sb.insert(0, "set"); + String method = sb.toString();// 鑾峰緱鏂规硶鍚 + return method; + } + + @SuppressWarnings("unchecked") + private static Element getActionElement(String actionName) throws DocumentException { + SAXReader reader = new SAXReader(); + Document document = reader.read(new File( + "src/com/coderising/litestruts/struts.xml")); + Element root = document.getRootElement(); + List actNodes = root.elements("action"); + Element e = null; + String className; + for (int i = 0; i < actNodes.size(); i++) { + className = actNodes.get(i).attributeValue("name"); + if (className != null && className.equals(actionName)) { + e = actNodes.get(i); + break; + } + } + return e; + } + +} diff --git a/group05/1094051862/test01/src/com/coderising/litestruts/StrutsTest.java b/group05/1094051862/test01/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group05/1094051862/test01/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group05/1094051862/test01/src/com/coderising/litestruts/View.java b/group05/1094051862/test01/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group05/1094051862/test01/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/group05/1094051862/test01/src/com/coderising/litestruts/struts.xml b/group05/1094051862/test01/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..c017732676 --- /dev/null +++ b/group05/1094051862/test01/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 diff --git a/group05/1094051862/test01/src/com/coding/basic/ArrayList.java b/group05/1094051862/test01/src/com/coding/basic/ArrayList.java index af757a217e..60fe8b9150 100644 --- a/group05/1094051862/test01/src/com/coding/basic/ArrayList.java +++ b/group05/1094051862/test01/src/com/coding/basic/ArrayList.java @@ -8,7 +8,7 @@ public class ArrayList implements List { private Object[] elementData = new Object[10]; - private int increaseSize = 3; + private int increaseSize = 10; private void increaseArray() { Object[] newData = Arrays.copyOf(elementData, elementData.length + increaseSize); elementData = newData; diff --git "a/group05/1377699408/\344\275\234\344\270\232/20170226/src/list/ArrayList.java" "b/group05/1377699408/\344\275\234\344\270\232/20170226/src/list/ArrayList.java" deleted file mode 100644 index f015ad8623..0000000000 --- "a/group05/1377699408/\344\275\234\344\270\232/20170226/src/list/ArrayList.java" +++ /dev/null @@ -1,54 +0,0 @@ -package list; - -import java.util.Arrays; - -public class ArrayList { - private transient static int INITIAL_SIZE = 10; - private transient int arrayLength; - private transient int size; - private transient E[] array; - public ArrayList(){ - array = (E[]) new Object[INITIAL_SIZE]; - arrayLength = INITIAL_SIZE; - } - public ArrayList(int size){ - if(size<=0){ - throw new IllegalArgumentException("鍙傛暟涓嶅彲浠ュ皬浜0"); - } - array = (E[])new Object[size]; - arrayLength = array.length; - ensureCapacity(size); - this.size = size; - } - public int size(){ - return size; - } - public void add(E e){ - ensureCapacity(size+1); - array[size] = e; - size++; - } - public E get(int index){ - if(index<0 || index > size){ - throw new IllegalArgumentException("绱㈠紩瓒婄晫"); - } - return array[index]; - - } - public E set(int index, E e){ - if(index<0 || index>size){ - throw new IllegalArgumentException("绱㈠紩瓒婄晫"); - } - E result = array[index]; - array[index] = e; - return result; - } - private void ensureCapacity(int size){ - E[] oldArray = array; - int oldSize = arrayLength; - while(size>arrayLength){ - arrayLength = arrayLength + (arrayLength >> 1); - array = Arrays.copyOf(oldArray, arrayLength); - } - } -} diff --git "a/group05/1377699408/\344\275\234\344\270\232/20170226/src/test/ArrayListTest.java" "b/group05/1377699408/\344\275\234\344\270\232/20170226/src/test/ArrayListTest.java" deleted file mode 100644 index e0d277eb10..0000000000 --- "a/group05/1377699408/\344\275\234\344\270\232/20170226/src/test/ArrayListTest.java" +++ /dev/null @@ -1,59 +0,0 @@ -package test; - -import junit.framework.TestCase; -import list.ArrayList; - -public class ArrayListTest extends TestCase { - - protected void setUp() throws Exception { - super.setUp(); - } - - protected void tearDown() throws Exception { - super.tearDown(); - } - - public void testArrayList() { - ArrayList a = new ArrayList(); - assertEquals(a.size(), 0); - } - - public void testArrayListInt() { - ArrayList a = new ArrayList(9); - assertEquals(a.size(), 9); - } - - public void testSize() { - ArrayList a = new ArrayList(9); - assertEquals(a.size(), 9); - ArrayList a2 = new ArrayList(100); - assertEquals(a2.size(), 100); - } - - public void testAdd() { - ArrayList a = new ArrayList(); - for (int i = 0; i < 1000; i++) { - a.add(5); - assertEquals(a.size(), i+1); - assertEquals(a.get(i), new Integer(5)); - } - } - - public void testGet() { - ArrayList a = new ArrayList(); - a.add(6); - assertEquals(a.get(0), new Integer(6)); - - } - - public void testSet() { - ArrayList a = new ArrayList(); - for (int i = 0; i < 100; i++) { - a.add(56); - } - a.set(5, 66); - assertEquals(a.get(5), new Integer(66)); - assertEquals(a.get(7), new Integer(56)); - } - -} diff --git a/group05/183127807/HomeWork0226/.idea/workspace.xml b/group05/183127807/HomeWork0226/.idea/workspace.xml index bea69f9904..927ae842b9 100644 --- a/group05/183127807/HomeWork0226/.idea/workspace.xml +++ b/group05/183127807/HomeWork0226/.idea/workspace.xml @@ -1,7 +1,9 @@ - + + + @@ -465,6 +467,7 @@ + @@ -586,6 +589,12 @@ + project + + + + + @@ -623,15 +632,16 @@ - + + - - + @@ -648,7 +658,7 @@ - + @@ -677,6 +687,95 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -974,7 +1073,7 @@ - + @@ -984,7 +1083,7 @@ - + @@ -1001,7 +1100,7 @@ - + @@ -1023,7 +1122,7 @@ - + @@ -1046,8 +1145,8 @@ - - + + diff --git a/group05/183127807/HomeWork0305/out/production/HomeWork0305/com/coderising/litestruts/struts.xml b/group05/183127807/HomeWork0305/out/production/HomeWork0305/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..0325783136 --- /dev/null +++ b/group05/183127807/HomeWork0305/out/production/HomeWork0305/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 diff --git a/group05/183127807/HomeWork0305/src/com/coderising/array/ArrayUtil.java b/group05/183127807/HomeWork0305/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..510efb2565 --- /dev/null +++ b/group05/183127807/HomeWork0305/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,201 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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 length = origin.length; + for (int i = 0, j = length / 2; i < j; i++) { + int temp = origin[i]; + origin[i] = origin[length - i - 1]; + 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 newLength = 0; + for (int t : oldArray){ + newLength += t != 0 ? 1 : 0; + } + int[] newArr = new int[newLength]; + int i = 0; + for (int t : oldArray) { + if (t != 0) { + newArr[i] = t; + i++; + } + } + return newArr; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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 array1Length = array1.length; + int array2Length = array2.length; + + int[] newArray=new int[array1Length+array2Length]; + + int i = 0, j = 0, index = 0; + + while (i < array1.length && j < array2.length) { + if (array1[i] <= array2[j]) { + if (array1[i] == array2[j]) { + j++; + } else { + newArray[index] = array1[i]; + i++; + index++; + } + } else { + newArray[index] = array2[j]; + j++; + index++; + } + } + //褰揳rray2涓簄ull + while (i < array1.length) { + newArray[index] = array1[i]; + index++; + i++; + } + //褰揳rray1涓簄ull + while (j < array2.length) { + newArray[index] = array2[j]; + j++; + index++; + } + return newArray; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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 oldArrLength = oldArray.length; + int newArrLength = oldArrLength + size; + int[] newArray = new int[newArrLength]; + System.arraycopy(oldArray,0,newArray,0,newArrLength); + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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[] fibonacci = new int[max]; + if (max <=1) { + return null; + } else if (max == 2) { + fibonacci[0] = 1; + fibonacci[1] = 1; + } else { + for (int i =2;i parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + View view = new View(); + SAXReader reader = new SAXReader(); + String classValue = ""; + + + try { + Document document= reader.read(new File("src/com/coderising/litestruts/struts.xml")); + //鑾峰彇鏍硅妭鐐 + Element rootElement = document.getRootElement(); + //鏍硅妭鐐瑰睘鎬 + List elements = rootElement.elements(); + Element actionElement = null; + for (Element element : elements) { + Attribute actionNameAttr = element.attribute("name"); + if (actionNameAttr.getValue().equals(actionName)) { + Attribute classAttr = element.attribute("class"); + classValue = classAttr.getValue(); + actionElement = element; + } + } + + + + Class newClass = Class.forName(classValue); + Object function = newClass.newInstance(); + Field[] fields = newClass.getDeclaredFields(); + String jsp = ""; + + Set> entrySet = parameters.entrySet(); + for (Map.Entry entry:entrySet) { + String name = (String) entry.getKey(); + String password = (String) entry.getValue(); + + Method setName = newClass.getDeclaredMethod("setName", String.class); + setName.invoke(function, name); + + Method setPassword = newClass.getDeclaredMethod("setPassword", String.class); + setPassword.invoke(function, password); + + Method executeMethod = newClass.getDeclaredMethod("execute"); + executeMethod.invoke(function); + + List resultElements = actionElement.elements(); + for(Element e:resultElements) { + Attribute resultNameAttr = e.attribute("name"); + if ( resultNameAttr.getValue().equals(executeMethod.invoke(function))){ + jsp = e.getText(); + break; + } + } + } + + HashMap hashMap = new HashMap<>(); + for (Field field : fields) { + Method getMessage = newClass.getDeclaredMethod("getMessage"); + Object message = getMessage.invoke(function); + hashMap.put(field.getName(), message); + } + + view.setParameters(hashMap); + view.setJsp(jsp); + + + } 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 view; + } + +} diff --git a/group05/183127807/HomeWork0305/src/com/coderising/litestruts/StrutsTest.java b/group05/183127807/HomeWork0305/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group05/183127807/HomeWork0305/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group05/183127807/HomeWork0305/src/com/coderising/litestruts/View.java b/group05/183127807/HomeWork0305/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group05/183127807/HomeWork0305/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/group05/183127807/HomeWork0305/src/com/coderising/litestruts/struts.xml b/group05/183127807/HomeWork0305/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..0325783136 --- /dev/null +++ b/group05/183127807/HomeWork0305/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 diff --git a/group05/284422826/.gitignore b/group05/284422826/.gitignore index 0aca6d5fe8..a906dbdd56 100644 --- a/group05/284422826/.gitignore +++ b/group05/284422826/.gitignore @@ -1,4 +1,6 @@ /bin/ +/lib/ +/out/ *.class *.settings *.project @@ -6,4 +8,4 @@ */.settings *.iml /.idea -/**/target/**/* \ No newline at end of file +/**/target/**/* diff --git a/group05/284422826/src/com/coderising/array/ArrayUtil.java b/group05/284422826/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..440a930be0 --- /dev/null +++ b/group05/284422826/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,268 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayUtil { + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + * 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public int[] reverseArray(int[] origin) { + if (origin == null || 0 == origin.length) { + throw new NullPointerException(); + } + + int N = origin.length; + for (int i = 0; i < N / 2; i++) { + int temp = origin[i]; + origin[i] = origin[N - 1 - i]; + origin[N - 1 - i] = temp; + } + + return origin; + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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) { + if (oldArray == null || 0 == oldArray.length) { + throw new NullPointerException(); + } + + int count = 0; + for (int i = 0; i < oldArray.length; i++) { + if (0 != oldArray[i]) { + count++; + } + } + + int[] newArray = new int[count]; + int j = 0; + for (int i = 0; i < oldArray.length; i++) { + if (0 != oldArray[i]) { + newArray[j] = oldArray[i]; + j++; + } + } + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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) { + if (array1.length == 0 && array2.length == 0) { + throw new NullPointerException(); + } else if (array1.length == 0) { + return array2; + } else if (array2.length == 0) { + return array1; + } + int count = array1.length + array2.length; + for (int i = 0; i < array1.length; i++) { + for (int j = 0; j < array2.length; j++) { + if (array1[i] == array2[j]) { + count--; + } + } + } + int[] newArray = new int[count]; + int m = 0, n = 0; + for (int i = 0; i < count; i++) { + if (m == array1.length) { + System.arraycopy(array2, n, newArray, i, array2.length - n); + break; + } + + if (n == array2.length) { + System.arraycopy(array1, m, newArray, i, array1.length - m); + break; + } + + if (array1[m] < array2[n]) { + newArray[i] = array1[m]; + m++; + } else if (array1[m] == array2[n]) { + newArray[i] = array1[m]; + m++; + n++; + } else { + newArray[i] = array2[n]; + n++; + } + } + return newArray; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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) { + if (oldArray == null || 0 == oldArray.length) { + int[] array = new int[size]; + return array; + } + int[] target = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, target, 0, oldArray.length); + return target; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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 (0 == max) { + throw new IllegalArgumentException(); + } else if (1 == max) { + int[] array = {}; + return array; + } + + int length = 0; + List list = new ArrayList<>(); + for (int i = 1; i < max; i++) { + if (fibo(i) < max) { + length++; + list.add(fibo(i)); + } else { + break; + } + } + + int[] array = new int[length]; + for (int i = 0; i < length; i++) { + array[i] = list.get(i); + } + return array; + } + + + public int fibo(int N) { + if (N <= 2) { + return 1; + } else { + return fibo(N - 1) + fibo(N - 2); + } + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + List list = new ArrayList<>(); + + int count = 0; + for (int i = 0; i < max; i++) { + if (isPrime(i)) { + count++; + list.add(i); + } + } + + int[] array = new int[count]; + for (int i = 0; i < count; i++) { + array[i] = list.get(i); + } + + return array; + } + + public boolean isPrime(int N) { + if (N < 2) { + return false; + } + + for (int i = 2; i * i <= N; i++) { + if (N % i == 0) { + return false; + } + } + return true; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int count = 0; + List list = new ArrayList<>(); + for (int i = 1; i < max; i++) { + if (isPerfectNumber(i)) { + count++; + list.add(i); + } + } + + int[] array = new int[count]; + for (int i = 0; i < count; i++) { + array[i] = list.get(i); + } + return array; + } + + public boolean isPerfectNumber(int N) { + int sum = 0; + for (int i = 1; i < N; i++) { + if (N % i == 0) { + sum += i; + } + } + return sum == N; + } + + /** + * 鐢╯eperator 鎶婃暟缁 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]).append(seperator); + } + sb.append(array[array.length - 1]); + return sb.toString(); + } +} diff --git a/group05/284422826/src/com/coderising/array/ArrayUtilTest.java b/group05/284422826/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..0b37ac11fb --- /dev/null +++ b/group05/284422826/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,82 @@ +package com.coderising.array; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ArrayUtilTest { + private ArrayUtil arrayUtil = null; + + @Before + public void setUp() throws Exception { + arrayUtil = new ArrayUtil(); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void reverseArray() throws Exception { + int[] a = {7, 9, 30, 3}; + int[] b = {3, 30, 9, 7}; + assertArrayEquals(arrayUtil.reverseArray(a), b); + } + + @Test + public void removeZero() throws Exception { + int[] oldArr = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + int[] newArray = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}; + assertArrayEquals(arrayUtil.removeZero(oldArr), newArray); + } + + @Test + public void merge() throws Exception { + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + int[] newArray = {3, 4, 5, 6, 7, 8}; + assertArrayEquals(arrayUtil.merge(a1, a2), newArray); + } + + @Test + public void grow() throws Exception { + int[] oldArray = {2, 3, 6}; + int size = 3; + int[] newArray = {2, 3, 6, 0, 0, 0}; + assertArrayEquals(arrayUtil.grow(oldArray, size), newArray); + } + + @Test + public void fibonacci() throws Exception { + int max = 15; + int[] array = {1, 1, 2, 3, 5, 8, 13}; + assertArrayEquals(arrayUtil.fibonacci(max), array); + } + + @Test + public void getPrimes() throws Exception { + int[] array = {2, 3, 5, 7, 11, 13, 17, 19}; + int max = 23; + assertArrayEquals(arrayUtil.getPrimes(max), array); + } + + @Test + public void getPerfectNumbers() throws Exception { + int max = 100; + int[] array = {6, 28}; + assertArrayEquals(arrayUtil.getPerfectNumbers(max), array); + } + + @Test + public void join() throws Exception { + int[] array = {3, 8, 9}; + String seperator = "-"; + String str = "3-8-9"; + Assert.assertEquals(arrayUtil.join(array, seperator), str); + } + +} \ No newline at end of file diff --git a/group05/284422826/src/com/coderising/litestruts/LoginAction.java b/group05/284422826/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group05/284422826/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; + } +} diff --git a/group05/284422826/src/com/coderising/litestruts/Struts.java b/group05/284422826/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..4d9004db93 --- /dev/null +++ b/group05/284422826/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,143 @@ +package com.coderising.litestruts; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + + +public class Struts { + private static String className = null; + + /* 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + public static View runAction(String actionName, Map parameters) { + View view = new View(); + Element element = null; + + element = parseXml(actionName); + Class clazz = null; + Object obj = null; + try { + clazz = Class.forName(className); + obj = clazz.newInstance(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } + + if (setValue(parameters, clazz, obj)) return null; + + String result = null; + + try { + Method method = clazz.getDeclaredMethod("execute"); + result = (String) method.invoke(obj); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + Map map = new HashMap(); + Field[] field = clazz.getDeclaredFields(); + try { + for (Field f : field) { + f.setAccessible(true); + String fieldName = f.toString().substring(f.toString().lastIndexOf(".")+1); + map.put(fieldName, f.get(obj)); + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + + view.setParameters(map); + view.setJsp(getResultString(result, element)); + + return view; + } + + private static boolean setValue(Map parameters, Class clazz, Object obj) { + for (Map.Entry entry : parameters.entrySet()) { + String name = entry.getKey(); + String value = entry.getValue(); + + if (name == null || "".equals(name)) { + System.out.println("灞炴у悕绉颁笉鑳戒负绌猴紒"); + return true; + } + System.out.println("Key = " + name + ", Value = " + value); + try { + Field field = clazz.getDeclaredField(name); + field.setAccessible(true); + field.set(obj, value); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + return false; + } + + private static Element parseXml(String name) { + Element el = null; + SAXReader reader = new SAXReader(); + try { + Document document = reader.read(new File("src/com/coderising/litestruts/struts.xml")); + Element root = document.getRootElement(); + Iterator it = root.elementIterator(); + while (it.hasNext()) { + Element element = ((Element) it.next()); + if (name.equals(element.attributeValue("name"))) { + className = element.attributeValue("class"); + el = element; + } + } + } catch (DocumentException e) { + e.printStackTrace(); + } + + return el; + } + + private static String getResultString(String name, Element element) { + String result = null; + Iterator iterator = element.elementIterator(); + while (iterator.hasNext()) { + Element e = ((Element) iterator.next()); + if (name.equals(e.attributeValue("name"))) { + result = e.getText(); + } + } + return result; + } + +} diff --git a/group05/284422826/src/com/coderising/litestruts/StrutsTest.java b/group05/284422826/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group05/284422826/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group05/284422826/src/com/coderising/litestruts/View.java b/group05/284422826/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group05/284422826/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/group05/284422826/src/com/coderising/litestruts/struts.xml b/group05/284422826/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..a7cb57e188 --- /dev/null +++ b/group05/284422826/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 diff --git a/group05/284422826/src/com/coding2017/basic/ArrayList.java b/group05/284422826/src/com/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..3d2182ae5a --- /dev/null +++ b/group05/284422826/src/com/coding2017/basic/ArrayList.java @@ -0,0 +1,130 @@ +package com.coding2017.basic; + +/** + * 鍔熻兘锛氬疄鐜癆rrayList. + * @author zhanglifeng. + */ +public class ArrayList implements List { + private int size = 0; //褰撳墠鏁扮粍澶у皬 + + private Object[] elementData = new Object[5]; //鍒濆鏁扮粍 + + /** + * 灏嗗璞娣诲姞鍒癆rrayList涓. + * @param o:闇瑕佹坊鍔犵殑瀵硅薄. + */ + public void add(Object o) { + ensureCapacity(size + 1); //纭繚鏁扮粍鐨勫閲忓彲浠ヨ鐨勪笅size + 1涓厓绱狅紝濡傛灉涓嶅鍒欐墿瀹 + + elementData[size] = o; //灏唎娣诲姞鍒版暟缁勪腑 + size++; //鏁扮粍澶у皬澧炲姞1 + } + + /** + * 灏嗗璞娣诲姞鍒癆rrayList鐨勬寚瀹氫綅缃. + * @param index: 鎸囧畾浣嶇疆. + * @param o: 闇瑕佹坊鍔犵殑瀵硅薄. + */ + public void add(int index, Object o) { + rangeCheck(index); //鍒ゆ柇鎸囧畾鐨勪綅缃甶ndex鏄惁鍚堟硶 + + ensureCapacity(size + 1); //纭繚鏁扮粍鐨勫閲忓彲浠ヨ鐨勪笅size + 1涓厓绱狅紝濡傛灉涓嶅鍒欐墿瀹 + + System.arraycopy(elementData, index, elementData, index + 1, size - index); //灏唅ndex浣嶇疆鍒扮粨鏉熶綅缃墍鏈夌殑鏁扮粍寰鍚庣Щ鍔ㄤ竴涓綅缃 + elementData[index] = o; //灏嗗璞娣诲姞鍒癷ndex浣嶇疆 + size++;//鏁扮粍澶у皬澧炲姞1 + } + + public Object get(int index) { + rangeCheck(index); + return elementData[index]; + } + + public Object remove(int index) { + rangeCheck(index); + + if (index != elementData.length - 1) { + System.arraycopy(elementData, index + 1, elementData, index, size - 1 - index); + } + + size--; + return elementData; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new ArrayListIterator(this); + } + + private void ensureCapacity(int number) { + if (number > elementData.length) { + elementData = grow(elementData, 1); + } + } + + public Object[] grow(Object[] src, int step) { + Object[] target = new Object[src.length + step]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + + public void rangeCheck(int index){ + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + } + + private class ArrayListIterator implements Iterator { + ArrayList arrayList = null; + int current = 0; + + private ArrayListIterator(ArrayList arrayList) { + this.arrayList = arrayList; + } + + @Override + public boolean hasNext() { + current++; + return current > arrayList.size() ? false : true; + } + + @Override + public Object next() { + return elementData[current]; + } + + } + + public static void main(String[] args) { + ArrayList arrayList = new ArrayList(); + arrayList.add("s1"); + arrayList.add("s2"); + arrayList.add("s3"); + arrayList.add("s4"); + arrayList.add(3, "s33"); + arrayList.add("s5"); + + System.out.println(arrayList.size()); + + System.out.println(arrayList.get(2)); + + arrayList.remove(3); + + System.out.println(arrayList.size()); + + arrayList.add("s1"); + System.out.println(arrayList.size()); + arrayList.remove(5); + System.out.println(arrayList.size()); + + Iterator it = arrayList.iterator(); + while(it.hasNext()){ + System.out.print(it.next() + " "); + } + System.out.println(); + } + +} diff --git a/group05/284422826/src/com/coding2017/basic/BinaryTreeNode.java b/group05/284422826/src/com/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..f4ce9f561c --- /dev/null +++ b/group05/284422826/src/com/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,37 @@ +package com.coding2017.basic; + +public class BinaryTreeNode { + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(Object data) { + this.data = data; + left = null; + right = null; + } + + public Object getData() { + return this.data; + } + public void setData(Object data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return this.left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return this.right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + +} diff --git a/group05/284422826/src/com/coding2017/basic/Iterator.java b/group05/284422826/src/com/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..19e214cfbb --- /dev/null +++ b/group05/284422826/src/com/coding2017/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group05/284422826/src/com/coding2017/basic/LinkedList.java b/group05/284422826/src/com/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..f0ab5d7969 --- /dev/null +++ b/group05/284422826/src/com/coding2017/basic/LinkedList.java @@ -0,0 +1,168 @@ +package com.coding2017.basic; + +import java.util.NoSuchElementException; + +/** + * 鍔熻兘锛氬疄鐜癓inkedList. + * @author zhanglifeng. + */ +public class LinkedList implements List { + private Node head, tail; + private int size; + + private Node getNodeByIndex(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("绾挎ц〃绱㈠紩瓒婄晫"); + } + Node current = head; + for (int i = 0; i < size && current != null; i++, current = current.next) { + if (i == index) { + return current; + } + } + return null; + } + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("绾挎ц〃绱㈠紩瓒婄晫"); + } + + if (0 == index) { + addFirst(o); + }else{ + Node node = getNodeByIndex(index - 1); + node.next = new Node(o, node.next); + size ++; + } + } + + public Object get(int index) { + return getNodeByIndex(index).data; + } + + public Object remove(int index) { + if (index < 0 || index > size - 1) { + throw new IndexOutOfBoundsException("绾挎ц〃绱㈠紩瓒婄晫"); + } + + if(0 == index){ + return removeFirst(); + }else if(size - 1 == index){ + return removeLast(); + }else{ + Node node = getNodeByIndex(index); + Node preNode = getNodeByIndex(index - 1); + preNode.next = node.next; + size --; + return node.data; + } + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node currentHead = head; + Node newNode = new Node(o, currentHead); + head = newNode; + if(currentHead == null){ + tail = newNode; + } + + size++; + } + + public void addLast(Object o) { + Node currentTail = tail; + Node newNode = new Node(o, null); + tail = newNode; + if(currentTail == null){ + head = newNode; + }else { + currentTail.next = newNode; + } + size++; + } + + public Object removeFirst() { + if(head == null){ + throw new NoSuchElementException(); + } + Node node = new Node(head.data, null); + head = head.next; + size --; + return node.data; + } + + public Object removeLast() { + if(tail == null){ + throw new NoSuchElementException(); + } + Node node = getNodeByIndex(size - 1); + node.next = null; + size --; + return node.data; + } + + public Iterator iterator() { + return new LinkedListIterator(this); + } + + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + private class LinkedListIterator implements Iterator { + LinkedList linkedList = null; + private int current = 0; + + public LinkedListIterator(LinkedList linkedList) { + this.linkedList = linkedList; + } + + @Override + public boolean hasNext() { + return current < size; + } + + @Override + public Object next() { + return linkedList.get(current ++); + } + } + + public static void main(String[] args) { + LinkedList linkedList = new LinkedList(); + linkedList.add("s1"); + linkedList.add("s2"); + linkedList.add("s3"); + linkedList.addFirst("s0"); + linkedList.addLast("s4"); + + Iterator it = linkedList.iterator(); + while(it.hasNext()){ + System.out.print(it.next() + " "); + } + System.out.println(); + System.out.println("绗3涓厓绱狅細" + linkedList.get(3)); + + System.out.println(linkedList.removeFirst()); + System.out.println(linkedList.size()); + System.out.println("Last element:" + linkedList.removeLast()); + System.out.println(linkedList.size()); + System.out.println("绗2涓厓绱狅細" + linkedList.remove(2)); + System.out.println(linkedList.size()); + } +} diff --git a/group05/284422826/src/com/coding2017/basic/List.java b/group05/284422826/src/com/coding2017/basic/List.java new file mode 100644 index 0000000000..25c197cc18 --- /dev/null +++ b/group05/284422826/src/com/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.coding2017.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/group05/284422826/src/com/coding2017/basic/Queue.java b/group05/284422826/src/com/coding2017/basic/Queue.java new file mode 100644 index 0000000000..57d63f43bf --- /dev/null +++ b/group05/284422826/src/com/coding2017/basic/Queue.java @@ -0,0 +1,25 @@ +package com.coding2017.basic; + +import java.util.EmptyStackException; + +public class Queue { + private LinkedList elementData = new LinkedList(); + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + if (elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.removeFirst(); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group05/284422826/src/com/coding2017/basic/Stack.java b/group05/284422826/src/com/coding2017/basic/Stack.java new file mode 100644 index 0000000000..16e72a3942 --- /dev/null +++ b/group05/284422826/src/com/coding2017/basic/Stack.java @@ -0,0 +1,31 @@ +package com.coding2017.basic; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(0); + } + + public Object pop(){ + if (elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.remove(elementData.size() - 1); + } + + public Object peek(){ + if (elementData.size() == 0) { + throw new EmptyStackException(); + } + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group05/289326186/.classpath b/group05/289326186/.classpath new file mode 100644 index 0000000000..fe22904a9e --- /dev/null +++ b/group05/289326186/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group05/289326186/.gitignore b/group05/289326186/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/289326186/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/289326186/.project b/group05/289326186/.project new file mode 100644 index 0000000000..5364d34f0e --- /dev/null +++ b/group05/289326186/.project @@ -0,0 +1,23 @@ + + + work + + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/group05/289326186/.settings/org.eclipse.jdt.core.prefs b/group05/289326186/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..0c68a61dca --- /dev/null +++ b/group05/289326186/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +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.compliance=1.8 +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/group05/289326186/.settings/org.eclipse.wst.common.project.facet.core.xml b/group05/289326186/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000000..f4ef8aa0a5 --- /dev/null +++ b/group05/289326186/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,4 @@ + + + + diff --git a/group05/289326186/src/com/coderising/array/ArrayUtil.java b/group05/289326186/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..628bb4564c --- /dev/null +++ b/group05/289326186/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,270 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] 濡傛灉 a = + * [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public int[] reverseArray(int[] origin) { + int[] array = new int[origin.length]; + for (int i = 0; i < origin.length; i++) { + array[i] = origin[origin.length - 1 - i]; + } + return array; + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 length = 0; + for (int arr : oldArray) { + if (arr != 0) { + length++; + } + } + int[] newArray = new int[length]; + int j = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArray[j] = oldArray[i]; + j++; + } + } + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 渚嬪 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) { + Set set = new TreeSet(); + + List list1 = new ArrayList(); + List list2 = new ArrayList(); + for (int i = 0; i < array1.length; i++) { + list1.add(array1[i]); + } + for (int i = 0; i < array2.length; i++) { + list1.add(array2[i]); + } + set.addAll(list1); + set.addAll(list2); + int length = set.size(); + int[] array = new int[length]; + int i = 0; + for (int a : set) { + array[i] = a; + i++; + } + return array; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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 length = oldArray.length + size; + int[] newArray = new int[length]; + for (int i = 0; i < newArray.length; i++) { + if (i > oldArray.length - 1) { + newArray[i] = 0; + } else { + newArray[i] = oldArray[i]; + } + } + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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 < 2) { + int[] array = {}; + return array; + } + List list = new ArrayList(); + for (int i = 1; i < max; i++) { + System.out.println("iiiii:" + getFibonacci(i)); + if (max < getFibonacci(i)) { + break; + } else { + list.add(getFibonacci(i)); + } + } + int[] array = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + array[i] = list.get(i); + } + return array; + } + + /** + * 鑾峰彇鏂愭尝閭e鏁板垪 + * + * @param n + * @return + */ + private int getFibonacci(int n) { + if (n <= 2) { + return 1; + } else { + return getFibonacci(n - 1) + getFibonacci(n - 2); + } + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + List list = new ArrayList(); + for (int i = 1; i < max; i++) { + if (isPrime(i)) { + list.add(i); + } + } + int[] array = new int[list.size()]; + for (int i = 0; i < array.length; i++) { + array[i] = list.get(i); + } + return array; + } + + /** + * 鍒ゆ柇涓涓暟鏄惁涓虹礌鏁 + * + * @param a + * @return + */ + private boolean isPrime(int a) { + if (a < 2) + return false; + for (int i = 2; i < a; i++) { + if (a % i == 0) { + return false; + } + } + return true; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + List list = new ArrayList(); + int sum = 0; + for(int i=1; i parameters) throws Exception { + // 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + // 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + // 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + // ("name"="test" , "password"="1234") , + // 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + // + // 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + // + // 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + // 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + // 鏀惧埌View瀵硅薄鐨刾arameters + // + // 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + // 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + View view = new View(); + // 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + SAXReader reader = new SAXReader(); + // 璇诲彇鏂囦欢 杞崲鎴怐ocument + Document document = reader.read(new File("src/com/coderising/litestruts/struts.xml")); + // 鑾峰彇鏍硅妭鐐瑰厓绱犲璞 + Element root = document.getRootElement(); + //鏍硅妭鐐圭殑鍏冪礌 + Iterator it = root.elementIterator(); + while(it.hasNext()){ + Element e = (Element) it.next(); + if(actionName.equals(e.attributeValue("name"))){ + // 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + String className = e.attributeValue("class"); + Class clazz = Class.forName(className); + Object obj = clazz.newInstance(); + //鑾峰彇绫荤殑鎵鏈夊睘鎬 + BeanInfo beanInfo = Introspector.getBeanInfo(clazz); + PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); + for(PropertyDescriptor pd : pds){ + for (Entry entry : parameters.entrySet()) { + String key = entry.getKey(); + if(pd.getName().equals(key)){ + Method setMethod = pd.getWriteMethod();//鑾峰緱set鏂规硶 + setMethod .invoke(obj, entry.getValue());//璋冪敤 + break; + } + } + } + //2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + Method method = clazz.getMethod("execute", null); + Object result = method.invoke(obj); + Map map = new HashMap(); + if("success".equals(result)){ + map.put("message", "login successful"); + }else{ + map.put("message", "login failed,please check your user/pwd"); + } + // 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + // 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + // 鏀惧埌View瀵硅薄鐨刾arameters + for(PropertyDescriptor pd : pds){ + for (Entry entry : parameters.entrySet()) { + String key = entry.getKey(); + if(pd.getName().equals(key)){ + Method getMethod = pd.getReadMethod();//鑾峰緱get鏂规硶 + Object getresult = getMethod .invoke(obj);//璋冪敤 + map.put(pd.getName(), getresult.toString()); + break; + } + } + } + view.setParameters(map); + //閬嶅巻action鐨勫瓙鍏冪礌 + Iterator it2 = e.elementIterator(); + while(it2.hasNext()){ + Element resultEle = (Element) it2.next(); + if(resultEle.attributeValue("name").equals(result)){ + view.setJsp(resultEle.getText()); + break; + } + } + } + } + return view; + } + + public static void main(String[] args) throws Exception { + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + View view = runAction(actionName, params); + System.out.println("view jsp:"+view.getJsp()); + System.out.println("view param:"+view.getParameters()); + } + +} diff --git a/group05/289326186/src/com/coderising/litestruts/StrutsTest.java b/group05/289326186/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..c4e1f588d0 --- /dev/null +++ b/group05/289326186/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +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 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/group05/289326186/src/com/coderising/litestruts/View.java b/group05/289326186/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group05/289326186/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/group05/289326186/src/com/coderising/litestruts/struts.xml b/group05/289326186/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group05/289326186/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 diff --git a/group05/351592484/.gitignore b/group05/351592484/.gitignore new file mode 100644 index 0000000000..0aca6d5fe8 --- /dev/null +++ b/group05/351592484/.gitignore @@ -0,0 +1,9 @@ +/bin/ +*.class +*.settings +*.project +*.classpath +*/.settings +*.iml +/.idea +/**/target/**/* \ No newline at end of file diff --git a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/ArrayList.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/ArrayList.java similarity index 100% rename from group05/284422826/src/com/github/zhanglifeng/coding2017/basic/ArrayList.java rename to group05/351592484/src/com/github/zhanglifeng/coding2017/basic/ArrayList.java diff --git a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/BinaryTreeNode.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/BinaryTreeNode.java similarity index 100% rename from group05/284422826/src/com/github/zhanglifeng/coding2017/basic/BinaryTreeNode.java rename to group05/351592484/src/com/github/zhanglifeng/coding2017/basic/BinaryTreeNode.java diff --git a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Iterator.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Iterator.java similarity index 100% rename from group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Iterator.java rename to group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Iterator.java diff --git a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/LinkedList.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/LinkedList.java similarity index 100% rename from group05/284422826/src/com/github/zhanglifeng/coding2017/basic/LinkedList.java rename to group05/351592484/src/com/github/zhanglifeng/coding2017/basic/LinkedList.java diff --git a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/List.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/List.java similarity index 100% rename from group05/284422826/src/com/github/zhanglifeng/coding2017/basic/List.java rename to group05/351592484/src/com/github/zhanglifeng/coding2017/basic/List.java diff --git a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Queue.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Queue.java similarity index 100% rename from group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Queue.java rename to group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Queue.java diff --git a/group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Stack.java b/group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Stack.java similarity index 100% rename from group05/284422826/src/com/github/zhanglifeng/coding2017/basic/Stack.java rename to group05/351592484/src/com/github/zhanglifeng/coding2017/basic/Stack.java diff --git a/group05/371492887/task_02/.classpath b/group05/371492887/task_02/.classpath new file mode 100644 index 0000000000..b699c050f9 --- /dev/null +++ b/group05/371492887/task_02/.classpath @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/group05/371492887/task_02/.gitignore b/group05/371492887/task_02/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/371492887/task_02/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group05/371492887/task_02/.project b/group05/371492887/task_02/.project new file mode 100644 index 0000000000..1cadc86932 --- /dev/null +++ b/group05/371492887/task_02/.project @@ -0,0 +1,23 @@ + + + task_02 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.jdt.core.javanature + + diff --git a/group05/371492887/task_02/pom.xml b/group05/371492887/task_02/pom.xml new file mode 100644 index 0000000000..319e86e55c --- /dev/null +++ b/group05/371492887/task_02/pom.xml @@ -0,0 +1,37 @@ + + 4.0.0 + java_207 + task_02 + 0.0.1-SNAPSHOT + + + + dom4j + dom4j + 1.6.1 + + + + + src + + + src + + **/*.java + + + + + + maven-compiler-plugin + 3.1 + + + + + + + + \ No newline at end of file diff --git a/group05/371492887/task_02/src/com/coderising/action/LoginAction.java b/group05/371492887/task_02/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..72e00894bb --- /dev/null +++ b/group05/371492887/task_02/src/com/coderising/action/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.action; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @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/group05/371492887/task_02/src/com/nitasty/array/ArrayUtil.java b/group05/371492887/task_02/src/com/nitasty/array/ArrayUtil.java new file mode 100644 index 0000000000..1138368352 --- /dev/null +++ b/group05/371492887/task_02/src/com/nitasty/array/ArrayUtil.java @@ -0,0 +1,269 @@ +package com.nitasty.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class ArrayUtil { + + + public static void main(String[] args) { + ArrayUtil util=new ArrayUtil(); + + int[] origin={7, 9 ,10, 30, 3}; + util.reverseArray(origin); + int[] oldArray={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] a1={3, 5,7,9,10}; + int[] a2={4,6,100,111,132}; + + System.out.println(Arrays.toString(origin)); + System.out.println(Arrays.toString(util.removeZero(oldArray))); + System.out.println(Arrays.toString(util.merge(a1,a2))); + System.out.println(Arrays.toString(util.grow(a1,3))); + System.out.println(Arrays.toString(util.fibonacci(100))); + System.out.println(Arrays.toString(util.getPrimes(100))); + System.out.println(Arrays.toString(util.getPerfectNumbers(1000))); + System.out.println(util.join(oldArray,"--")); + } + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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 dataBus; + int len=origin.length; + for(int i=0;i>1;i++){ + dataBus=origin[i]; + origin[i]=origin[len-i-1]; + origin[len-i-1]=dataBus; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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[] arrayBus=new int[oldArray.length]; + int count = 0; + //鍒╃敤涓棿鏁板兼潵鍓旈櫎0 + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i]!=0) + arrayBus[count++]=oldArray[i]; + } + //杩斿洖鏂扮殑鏁板 + int[] newArray=new int[count]; + System.arraycopy(arrayBus, 0, newArray, 0, count); + + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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 len1=array1.length; + int len2=array2.length; + List longList=new ArrayList(); + List shortList=new ArrayList(); + //鏁扮粍杞琹ist锛屾湁娌℃湁绠鍗曠殑鏂规硶鍟婃垜鎿 + if(len1>len2){ + for (int i = 0; i < len1; i++) { + longList.add(array1[i]); + } + for (int i = 0; i < len2; i++) { + shortList.add(array2[i]); + } + }else{ + for (int i = 0; i < len1; i++) { + shortList.add(array1[i]); + } + for (int i = 0; i < len2; i++) { + longList.add(array2[i]); + } + } + + //灏嗙煭list涓殑鍊兼彃鍏ラ暱list涓 + int j=0; + for (int i = 0; i < longList.size(); i++) { + if(j==shortList.size()) + continue; + if((Integer)shortList.get(j)<(Integer)longList.get(i)){ + longList.add(i, shortList.get(j)); + j++; + }else if(((Integer)shortList.get(j)).equals((Integer)longList.get(i))){ + continue; + }else{ + if(i==(longList.size()-1)){ + longList.add(shortList.get(j)); + j++; + } + } + } + + //list鍐嶈浆鏁扮粍路路路闃胯タ鍚 + int[] intArray=new int[longList.size()]; + + for (int i = 0; i < longList.size(); i++) { + intArray[i]=longList.get(i); + } + + return intArray; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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 len=oldArray.length; + int[] newArray=new int[len+size]; + + System.arraycopy(oldArray, 0, newArray, 0, len); + + for(int i=len;i list=new ArrayList(); + for (int i = 1; i < max; i++) { + if(isPrimeNumber(i)) + list.add(i); + } + + int[] intArr=new int[list.size()]; + for (int i = 0; i < intArr.length; i++) { + intArr[i]=list.get(i); + } + return intArr; + } + + private boolean isPrimeNumber(int n) + { + if (n==2) + { + return true; + } + + if (n%2==0) + { + return false; + } + + int sqrtn=(int)Math.sqrt((double)n); + boolean flag=true; + + for (int i=3;i<=sqrtn;i+=2) + { + if (n%i==0) + { + flag=false; + } + } + return flag; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + + List list=new ArrayList(); + + for (int i = 1; i < max; i++) { + int sum=0; + for (int j = 1; j < i; j++) { + if(i%j==0) + sum+=j; + } + if(sum==i) + list.add(i); + } + + int[] intArr=new int[list.size()]; + for (int i = 0; i < intArr.length; i++) { + intArr[i]=list.get(i); + } + return intArr; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuffer buff=new StringBuffer(); + for (int i = 0; i < array.length; i++) { + buff.append(array[i]); + if(i parameters) { + + View view=new View(); + + /* + * + * 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + * + * + * 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + * 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 ("name"="test" , + * "password"="1234") , 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + * + * 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + * + * 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 + * {"message": "鐧诲綍鎴愬姛"} , 鏀惧埌View瀵硅薄鐨刾arameters + * + * 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + * 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + // 0.璇诲彇閰嶇疆鏂囦欢struts.xml + try { + // 灏唖truts.xml杞崲鎴愯緭鍏ユ祦 + InputStream in = new FileInputStream(new File( + "src/com/nitasty/litestruts/struts.xml")); + // 绌夸欢SAXReader璇诲彇鍣紝鐢ㄤ簬璇诲彇xml + SAXReader saxReader = new SAXReader(); + // + Document document = saxReader.read(in); + // 鑾峰彇鏍硅妭鐐瑰璞 + Element rootElement = document.getRootElement(); + //鑾峰彇action鑺傜偣鍒楄〃 + List elementList=rootElement.elements(); + //鍔犺浇绗竴涓猘ction鐨刢lass绫 + Element login=elementList.get(0); + Class clazz=Class.forName(login.attribute("class").getStringValue()); + //new涓涓class瀹炰緥 + Object obj=clazz.newInstance(); + //鑾峰彇name鍜宲assword + String name=parameters.get("name"); + String password=parameters.get("password"); + //鑾峰彇setName鏂规硶 + Method setName=clazz.getMethod("setName",String.class); + //鑾峰彇setPassword鏂规硶 + Method setPassword=clazz.getMethod("setPassword",String.class); + //鑾峰彇execute鏂规硶 + Method execute=clazz.getMethod("execute"); + + //鎵ц鑾峰彇鐨勬柟娉 + setName.invoke(obj,name); + setPassword.invoke(obj,password); + String result=(String) execute.invoke(obj); + List results=login.elements(); + for (int i = 0; i < results.size(); i++) { + if(result.equalsIgnoreCase(results.get(i).attribute(0).getStringValue())){ + view.setJsp(results.get(i).getTextTrim()); + } + } + + //鑾峰彇message灞炴 + Field fld=clazz.getDeclaredField("message"); + //鍏佽璁块棶绉佹湁灞炴 + fld.setAccessible(true); + //鑾峰彇璇ュ睘鎬у + String message=(String) fld.get(obj); + + //灏嗙粨鏋滆繑鍥 + Map map=new HashMap(); + map.put("message", message); + view.setParameters(map); + + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + return view; + } + + + /** + * 娴嬭瘯鐢 + * @param args + */ + public static void main(String[] args) { + // 0.璇诲彇閰嶇疆鏂囦欢struts.xml + try { + // 灏唖truts.xml杞崲鎴愯緭鍏ユ祦 + InputStream in = new FileInputStream(new File( + "src/com/nitasty/litestruts/struts.xml")); + // 鍒涘缓SAXReader璇诲彇鍣紝鐢ㄤ簬璇诲彇xml + SAXReader saxReader = new SAXReader(); + // + Document document = saxReader.read(in); + // 鑾峰彇鏍硅妭鐐瑰璞 + Element rootElement = document.getRootElement(); + List elementList=rootElement.elements(); + + System.out.println(elementList.get(0).attribute("name").getStringValue()); + System.out.println(elementList.get(0).attribute("class").getStringValue()); + + + Class clazz=Class.forName(elementList.get(0).attribute("class").getStringValue()); + + Object obj=clazz.newInstance(); + + Method setName=clazz.getMethod("setName",String.class); + Method setPassword=clazz.getMethod("setPassword",String.class); + setName.invoke(obj,"test"); + setPassword.invoke(obj,"1234"); + Method execute=clazz.getMethod("execute"); + String str=(String) execute.invoke(obj); + Field fld=clazz.getDeclaredField("message"); + fld.setAccessible(true); + String message=(String) fld.get(obj); + + System.out.println(str); + System.out.println(message); + elementList.get(0).attribute("name").getStringValue(); + elementList.get(0).attribute("class").getStringValue(); + + Map map = new HashMap(); +// map = getAttributes(rootElement, map); + + // Element login=element.element("result"); + // System.out.println(login.getText()); + + // for (Iterator it=rootElement.elementIterator(); it.hasNext();) { + // System.out.println(it.next()); + // } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchFieldException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} diff --git a/group05/371492887/task_02/src/com/nitasty/litestruts/StrutsTest.java b/group05/371492887/task_02/src/com/nitasty/litestruts/StrutsTest.java new file mode 100644 index 0000000000..f9dc6be21f --- /dev/null +++ b/group05/371492887/task_02/src/com/nitasty/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.nitasty.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/group05/371492887/task_02/src/com/nitasty/litestruts/View.java b/group05/371492887/task_02/src/com/nitasty/litestruts/View.java new file mode 100644 index 0000000000..28f3d586f2 --- /dev/null +++ b/group05/371492887/task_02/src/com/nitasty/litestruts/View.java @@ -0,0 +1,23 @@ +package com.nitasty.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/group05/371492887/task_02/src/com/nitasty/litestruts/struts.xml b/group05/371492887/task_02/src/com/nitasty/litestruts/struts.xml new file mode 100644 index 0000000000..cafe98fdea --- /dev/null +++ b/group05/371492887/task_02/src/com/nitasty/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/group05/399258474/.classpath b/group05/399258474/.classpath index b387714202..5dc752a7bd 100644 --- a/group05/399258474/.classpath +++ b/group05/399258474/.classpath @@ -3,5 +3,6 @@ + diff --git a/group05/399258474/src/com/coderising/array/ArrayUtil.java b/group05/399258474/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..57efb6de72 --- /dev/null +++ b/group05/399258474/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,247 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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[] arr = new int[origin.length]; + for (int i = 0; i < origin.length; i++) { + arr[i] = origin[origin.length-i-1]; + } + for (int i = 0; i < arr.length; i++) { + origin[i] = arr[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 size = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i] != 0){ + size ++; + } + } + int[] newArray = new int[size]; + for (int i = 0,j = 0; i < oldArray.length; i++) { + if(oldArray[i] != 0){ + newArray[j] = oldArray[i]; + j++; + } + } + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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[] totalArr = new int[array1.length + array2.length]; + for (int i = 0; i < array1.length; i++) { + totalArr[i] = array1[i]; + } + for (int j = array1.length,i = 0; i < array2.length; i++) { + totalArr[j] = array2[i]; + j++; + } + //鎺掑簭 + for (int i = 0; i < totalArr.length; i++) { + for (int j = 0; j < totalArr.length-i-1; j++) { + if(totalArr[j] > totalArr[j+1]){ + int temp = totalArr[j+1]; + + totalArr[j+1] = totalArr[j]; + totalArr[j] = temp; + } + } + } + //鍘婚噸 + if(totalArr.length < 2){ + return totalArr; + } + int size = 1; + for (int i = 0; i < totalArr.length-1; i++) { + if(totalArr[i] != totalArr[i+1]){ + size ++ ; + } + } + int[] newArr = new int[size]; + for (int i = 0,j = 0; i < totalArr.length-1; i++) { + if(totalArr[i] != totalArr[i+1]){ + newArr[j] = totalArr[i]; + j++; + } + } + newArr[size-1] = totalArr[totalArr.length-1]; + + return newArr; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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 size = 0; + while(f(size) < max){ + size++; + } + int[] arr = new int[size]; + for (int i = 0; i < arr.length; i++) { + arr[i] = f(i); + } + return arr; + } + + public int f(int n){ + if(n == 0 || n ==1){ + return 1; + }else{ + return f(n-1)+f(n-2); + } + + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max <= 2){ + return null; + } + int size = 1; + for (int i = 3; i < max; i++) { + int n = 2; + while(n < i){ + if(i%n == 0){ + break; + } + n++; + } + if(n == i){ + size ++; + } + } + int[] arr = new int[size]; + arr[0] = 2; + for (int i = 3,j = 1; i < max; i++) { + int n = 2; + while(n < i){ + if(i%n == 0){ + break; + } + n++; + } + if(n == i){ + arr[j] = i; + j ++; + } + } + return arr; + } + + + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if(max <= 5){ + return null; + } + int size = 0; + for (int i = 6; i < max; i++) { + int sum = 0; + int n = 1; + while(n < i){ + if(i % n == 0){ + sum += n; + } + n++; + } + if(sum == i){ + size ++; + } + } + int[] arr = new int[size]; + for (int i = 6,j = 0; i < max; i++) { + int sum = 0; + int n = 1; + while(n < i){ + if(i % n == 0){ + sum += n; + } + n++; + } + if(sum == i){ + arr[j] = i; + j ++; + } + } + return arr; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + String str = ""; + for (int i = 0; i < array.length; i++) { + if(i != array.length-1){ + str += (array[i] + seperator); + }else{ + str += array[i]; + } + } + return str; + } + + +} diff --git a/group05/399258474/src/com/coderising/array/ArrayUtilTest.java b/group05/399258474/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..952b6cd26d --- /dev/null +++ b/group05/399258474/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,92 @@ +package com.coderising.array; + +import java.util.Arrays; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coding.basic.ArrayList; + +public class ArrayUtilTest { + ArrayUtil util = new ArrayUtil(); + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + System.out.println("-----------------------------"); + } + + @Test + public void reverseArraytest() { + int[] origin = new int[]{7,9,30,3}; + System.out.println(Arrays.toString(origin)); + util.reverseArray(origin); + System.out.println("鏁扮粍缃崲锛"+Arrays.toString(origin)); + } + + @Test + public void removeZeroTest(){ + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] newArr = util.removeZero(oldArr); + System.out.println(Arrays.toString(oldArr) +"鏁扮粍鍘0鍚庝负锛"+ Arrays.toString(newArr)); + } + + @Test + public void mergeTest(){ + int[] a1 = {3,5,7,8}; + int[] a2 = {4,5,6,7}; + int[] a3 = util.merge(a1, a2); + System.out.println(Arrays.toString(a1)+Arrays.toString(a2)+"鍚堝苟銆佹帓搴忋佸幓閲嶅悗涓猴細"+Arrays.toString(a3)); + } + + @Test + public void growTest(){ + int[] oldArray = {2,3,6}; + int i = 3; + int[] newArray = util.grow(oldArray, i); + System.out.println(Arrays.toString(oldArray) +" 鎵╁ "+ i +"鍚庝负锛"+Arrays.toString(newArray)); + } + + @Test + public void fibonacciTest(){ + int i = 15; + int[] arr = util.fibonacci(i); + System.out.println("鏂愭尝閭e鏁板垪鏈澶ф暟灏忎簬"+ i +"鐨勬暟缁勬槸锛" + Arrays.toString(arr)); + } + + @Test + public void getPrimesTest(){ + int max = 23; + int[] arr = util.getPrimes(max); + System.out.println("鏈澶ф暟灏忎簬"+ max +"鐨勭礌鏁版暟缁勬槸锛" + Arrays.toString(arr)); + } + + @Test + public void getPerfectNumbersTest(){ + int max = 1000; + int[] arr = util.getPerfectNumbers(max); + System.out.println("鏈澶ф暟灏忎簬"+ max +"鐨勫畬鏁版暟缁勬槸锛" + Arrays.toString(arr)); + } + + @Test + public void joinTest(){ + int[] arr = {3,8,9}; + String seperator = "-"; + String str = util.join(arr, seperator); + System.out.println(Arrays.toString(arr)+"鏁扮粍鐢╘""+seperator+"\"杩炴帴鍚庝负锛"+str); + } + +} + + + + + + + + + diff --git a/group05/399258474/src/com/coderising/litestruts/LoginAction.java b/group05/399258474/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group05/399258474/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; + } +} diff --git a/group05/399258474/src/com/coderising/litestruts/Struts.java b/group05/399258474/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..79c82a78f4 --- /dev/null +++ b/group05/399258474/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,162 @@ +package com.coderising.litestruts; + +import java.beans.PropertyDescriptor; +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + SAXReader saxReader = new SAXReader(); + View view = new View(); + try { + Document document = saxReader.read(new File("src/com/coderising/litestruts/struts.xml")); + //鑾峰彇鏍瑰厓绱 + Element root = document.getRootElement(); + //鏍规嵁鍙傛暟鑾峰彇瀛愬厓绱 + List listElement=root.elements();//鎵鏈変竴绾у瓙鑺傜偣鐨刲ist + Element actionElement = null; + String classStr = ""; + for(Element e:listElement){ + Attribute actionNameAttr = e.attribute("name"); + if(actionNameAttr.getValue().equals(actionName)){ + //鑾峰彇灞炴у + Attribute classAttr = e.attribute("class"); + classStr = classAttr.getValue(); + actionElement = e; + break; + } + } + + //閫氳繃鍙嶅皠鍒涘缓瀵硅薄 + Class clazz = Class.forName(classStr); + Object c = clazz.newInstance(); + Field[] fields = clazz.getDeclaredFields(); + //閫氳繃set鏂规硶璧嬪 + Set> set = parameters.entrySet(); + for(Entry entry : set){ + String key = entry.getKey(); + String val = entry.getValue(); + for(Field f :fields){ + PropertyDescriptor pd = getPropertyDescriptor(clazz,key,true); + Method setMethod = pd.getWriteMethod(); + setMethod.invoke(c, new Object[]{val}); + } + } + + //閫氳繃杩斿洖鍊肩‘瀹歫sp + String jspStr = ""; + Method executeMethod = clazz.getDeclaredMethod("execute"); + executeMethod.setAccessible(true); + String str = (String) executeMethod.invoke(c); + List resultElements = actionElement.elements(); + for(Element e:resultElements){ + Attribute resultNameAttr = e.attribute("name"); + if(resultNameAttr.getValue().equals(str)){ + jspStr = e.getText(); + break; + } + + } + + //鑾峰彇鎵鏈夌殑get鏂规硶,瀛樺叆map + HashMap map = new HashMap(); + for(Field f :fields){ + PropertyDescriptor pd = getPropertyDescriptor(clazz,f.getName(),false); + Method getMethod = pd.getReadMethod(); + Object value = getMethod.invoke(c, new Object[]{}); + map.put(f.getName(), value); + } + + //纭畾杩斿洖鐨刅iew + + view.setJsp(jspStr); + view.setParameters(map); + + + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + return view; + } + + public static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName,boolean hasSetMethod) { + StringBuffer sb = new StringBuffer(); + Method setMethod = null; + Method getMethod = null; + PropertyDescriptor pd = null; + try { + Field f = clazz.getDeclaredField(propertyName);//鏍规嵁瀛楁鍚嶆潵鑾峰彇瀛楁 + if (f!= null) { + //鏋勫缓鏂规硶鐨勫悗缂 + String methodEnd = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); + if(hasSetMethod){ + sb.append("set" + methodEnd); + setMethod = clazz.getDeclaredMethod(sb.toString(), new Class[]{ f.getType() }); + } + sb.delete(0, sb.length());//娓呯┖ + sb.append("get" + methodEnd);//鏋勫缓get鏂规硶 + //鏋勫缓get 鏂规硶 + getMethod = clazz.getDeclaredMethod(sb.toString(), new Class[]{ }); + //鏋勫缓涓涓睘鎬ф弿杩板櫒 鎶婂搴斿睘鎬 propertyName 鐨 get 鍜 set 鏂规硶淇濆瓨鍒板睘鎬ф弿杩板櫒涓 + pd = new PropertyDescriptor(propertyName, getMethod, setMethod); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + + return pd; + } + +} diff --git a/group05/399258474/src/com/coderising/litestruts/StrutsTest.java b/group05/399258474/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..56bb5b23d0 --- /dev/null +++ b/group05/399258474/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,48 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +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() { + 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/group05/399258474/src/com/coderising/litestruts/View.java b/group05/399258474/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group05/399258474/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/group05/399258474/src/com/coderising/litestruts/struts.xml b/group05/399258474/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..246b3595ad --- /dev/null +++ b/group05/399258474/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 diff --git a/group05/399258474/src/com/coding/basic/ArrayList.java b/group05/399258474/src/com/coding/basic/ArrayList.java index 5030b8d001..78706c993f 100644 --- a/group05/399258474/src/com/coding/basic/ArrayList.java +++ b/group05/399258474/src/com/coding/basic/ArrayList.java @@ -4,7 +4,7 @@ public class ArrayList implements List { private int size = 0; - private Object[] elementData = new Object[2]; + private Object[] elementData = new Object[10]; public void add(Object o){ int len = elementData.length; diff --git a/group05/399258474/src/test/BasicTest.java b/group05/399258474/src/com/coding/basic/test/BasicTest.java similarity index 92% rename from group05/399258474/src/test/BasicTest.java rename to group05/399258474/src/com/coding/basic/test/BasicTest.java index 7339881525..8febc26102 100644 --- a/group05/399258474/src/test/BasicTest.java +++ b/group05/399258474/src/com/coding/basic/test/BasicTest.java @@ -1,4 +1,4 @@ -package test; +package com.coding.basic.test; import org.junit.Test; diff --git a/group05/515505513/Task02/.classpath b/group05/515505513/Task02/.classpath new file mode 100644 index 0000000000..916d837ac0 --- /dev/null +++ b/group05/515505513/Task02/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group05/515505513/Task02/.gitignore b/group05/515505513/Task02/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group05/515505513/Task02/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group15/1500_369516660/.project b/group05/515505513/Task02/.project similarity index 92% rename from group15/1500_369516660/.project rename to group05/515505513/Task02/.project index c2f1bb9994..e4d9a52aa9 100644 --- a/group15/1500_369516660/.project +++ b/group05/515505513/Task02/.project @@ -1,6 +1,6 @@ - codingLeaning + Task02 diff --git a/group05/515505513/Task02/.settings/org.eclipse.jdt.core.prefs b/group05/515505513/Task02/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group05/515505513/Task02/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +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/group05/515505513/Task02/lib/dom4j-2.0.0.jar b/group05/515505513/Task02/lib/dom4j-2.0.0.jar new file mode 100644 index 0000000000..fd9e2ccd44 Binary files /dev/null and b/group05/515505513/Task02/lib/dom4j-2.0.0.jar differ diff --git a/group05/515505513/Task02/src/com/coderising/action/LoginAction.java b/group05/515505513/Task02/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..5cdf1b78e4 --- /dev/null +++ b/group05/515505513/Task02/src/com/coderising/action/LoginAction.java @@ -0,0 +1,45 @@ +package com.coderising.action; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = 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; + } + +} diff --git a/group05/515505513/Task02/src/com/coderising/action/Struts.java b/group05/515505513/Task02/src/com/coderising/action/Struts.java new file mode 100644 index 0000000000..7a342e28bb --- /dev/null +++ b/group05/515505513/Task02/src/com/coderising/action/Struts.java @@ -0,0 +1,165 @@ +package com.coderising.action; +import java.beans.PropertyDescriptor; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + //0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + SAXReader saxReader = new SAXReader(); + View view = new View(); + Element actionElement = null; + String classVal=""; + try { + Document read = saxReader.read(new File("src/com/coderising/action/struts.xml")); + //鑾峰緱鏍硅妭鐐 + Element rootElement = read.getRootElement(); + //鑾峰緱鏍硅妭鐐逛笅鐨勪竴绾ц妭鐐 + List elements = rootElement.elements(); + for (Element element : elements) { + Attribute attribute = element.attribute("name"); + //濡傛灉绛変簬浼犻掕繘鏉ョ殑actionName + if(attribute.getValue().equals(actionName)){ + actionElement = element; + //鑾峰緱灞炴у + Attribute classAttr = element.attribute("class"); + classVal = classAttr.getValue(); + } + } + +// 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 +// 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 +// ("name"="test" , "password"="1234") , +// 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + //com.coderising.action.LoginAction + + Class clazz = Class.forName(classVal); + Object c = clazz.newInstance(); + Field[] fields = clazz.getDeclaredFields(); + Set> set = parameters.entrySet(); + for (Entry entry : set) { + String key = entry.getKey(); + String value = entry.getValue(); + for (Field f : fields) { + setProperty(c, key, value); + } + } + + //2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + Method executeMethod = clazz.getDeclaredMethod("execute"); + executeMethod.setAccessible(true); + String jspStr = ""; + String str = (String) executeMethod.invoke(c); + List resultNameJSp = actionElement.elements(); + for (Element element : resultNameJSp) { + Attribute attribute = element.attribute("name"); + if(attribute.getValue().equals(str)){ + jspStr = element.getText(); + break; + } + } + +// 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, +// 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , +// 鏀惧埌View瀵硅薄鐨刾arameters + HashMap hashMap = new HashMap<>(); + for (Field f : fields) { + Object value = getProperty(c, f.getName()); + hashMap.put(f.getName(),value); + } + +// 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 +// 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + view.setJsp(jspStr); + //System.out.println("====="+view.getJsp()); + view.setParameters(hashMap); + //System.out.println(view.getParameters().get("message")); + + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return view; + } + + public static PropertyDescriptor getPropertyDescriper(Class clazz,String propertyName){ + //鏋勫缓涓涓彲鍙樺瓧绗︿覆鐢ㄦ潵鏋勫缓鏂规硶鍚嶇О + StringBuffer sBuffer = new StringBuffer(); + Method setMethod = null; + Method getMethod = null; + PropertyDescriptor pd = null; + try { + //鏍规嵁瀛楁鍚嶆潵鑾峰彇瀛楁 + Field f = clazz.getDeclaredField(propertyName); + if(f!=null){ + //鏋勫缓鏂规硶鐨勫悗缂 + String methodEnd = propertyName.substring(0,1).toUpperCase()+propertyName.substring(1); + sBuffer.append("set"+methodEnd);//鏋勫缓set鏂规硶 + setMethod = clazz.getDeclaredMethod(sBuffer.toString(),new Class[]{f.getType()}); + sBuffer.delete(0, sBuffer.length());//娓呯┖鏁翠釜鍙彉瀛楃涓 + sBuffer.append("get"+methodEnd);//鏋勫缓get鏂规硶 + getMethod = clazz.getDeclaredMethod(sBuffer.toString(),new Class[]{}); + //鏋勫缓涓涓睘鎬ф弿杩板櫒 鎶婂搴斿睘鎬 propertyName 鐨 get 鍜 set 鏂规硶淇濆瓨鍒板睘鎬ф弿杩板櫒涓 + pd = new PropertyDescriptor(propertyName, getMethod, setMethod); + } + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return pd; + } + + /** + * @param obj + * @param propertyName + * @param value + */ + public static void setProperty(Object obj,String propertyName,Object value){ + //鑾峰彇瀵硅薄鐨勭被鍨 + Class clazz = obj.getClass(); + PropertyDescriptor pd = getPropertyDescriper(clazz,propertyName); + //浠庡睘鎬ф弿杩板櫒涓幏鍙杝et鏂规硶 + Method setMethod = pd.getWriteMethod(); + try { + setMethod.invoke(obj, new Object[]{value}); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + public static Object getProperty(Object obj,String propertyName){ + Class clazz = obj.getClass(); + PropertyDescriptor pd = getPropertyDescriper(clazz,propertyName); + Method getMethod = pd.getReadMethod(); + Object value= null; + try { + value = getMethod.invoke(obj,new Object[]{}); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return value; + } + + +} diff --git a/group05/515505513/Task02/src/com/coderising/action/StrutsTest.java b/group05/515505513/Task02/src/com/coderising/action/StrutsTest.java new file mode 100644 index 0000000000..f162786054 --- /dev/null +++ b/group05/515505513/Task02/src/com/coderising/action/StrutsTest.java @@ -0,0 +1,42 @@ +package com.coderising.action; + +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/group05/515505513/Task02/src/com/coderising/action/View.java b/group05/515505513/Task02/src/com/coderising/action/View.java new file mode 100644 index 0000000000..11cb1872e5 --- /dev/null +++ b/group05/515505513/Task02/src/com/coderising/action/View.java @@ -0,0 +1,23 @@ +package com.coderising.action; + +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/group05/515505513/Task02/src/com/coderising/action/struts.xml b/group05/515505513/Task02/src/com/coderising/action/struts.xml new file mode 100644 index 0000000000..dd598a3664 --- /dev/null +++ b/group05/515505513/Task02/src/com/coderising/action/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/group05/515505513/Task02/src/com/coderising/array/ArrayUtil.java b/group05/515505513/Task02/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..eca61be119 --- /dev/null +++ b/group05/515505513/Task02/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,221 @@ +package com.coderising.array; + +import java.util.Iterator; +import java.util.TreeSet; + +import org.junit.Test; + +public class ArrayUtil { + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin + * @return leon1900 + */ + public void reverseArray(int[] origin){ + for (int i = 0; i < origin.length/2; i++) { + int temp = origin[origin.length-1-i]; + origin[origin.length-1-i] = origin[i]; + origin[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 leon1900 + */ + public int[] removeZero(int[] oldArray){ + int size = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i] !=0 ){ + size++; + } + } + int [] newArray = new int [size]; + for (int i = 0,j = 0; i < oldArray.length; i++) { + if(oldArray[i]!=0){ + newArray[j++] = oldArray[i]; + } + } + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * @param array1 + * @param array2 + * @return leon1900 + */ + + public int[] merge(int[] array1, int[] array2){ + TreeSet ts = new TreeSet<>(); + for (int i = 0; i < array1.length; i++) { + ts.add(array1[i]); + } + for (int i = 0; i < array2.length; i++) { + ts.add(array2[i]); + } + int size = ts.size(); + int[] newArr = new int[size]; + int i = 0; + for (Integer integer : ts) { + newArr[i++] = integer; + } + return newArr; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return leon1900 + */ + public int[] grow(int [] oldArray, int size){ + int newsize = oldArray.length+size; + int [] newArr = new int[newsize]; + for (int i = 0; i < oldArray.length; i++) { + newArr[i] = oldArray[i]; + } + return newArr; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * @param max + * @return leon1900 + */ + public int[] fibonacci(int max){ + int size = 1; + while(fun(size) + + dom4j + dom4j + 1.6.1 + junit junit diff --git a/group05/578505552/src/main/java/com/coderising/Q&A.md b/group05/578505552/src/main/java/com/coderising/Q&A.md new file mode 100644 index 0000000000..29fcbb1560 --- /dev/null +++ b/group05/578505552/src/main/java/com/coderising/Q&A.md @@ -0,0 +1,5 @@ +# Struts涓幓鑾峰緱getter鎴栬卻etter鏂规硶鐨勬椂鍊欐庢牱姣旇緝闈犺氨锛熺洰鍓嶆湁涓ょ鎬濊矾 +## 鑾峰彇Struts涓墍鏈変互鈥済et鈥濇垨鈥渟et鈥濆紑澶寸殑鏂规硶 +### 闂1锛氬彲鑳芥湁涓嶆槸getter鎴栬卻etter鐨勬柟娉曚篃鏄互鈥済et鈥濇垨鑰呪渟et鈥濆紑澶 +## 鍏堣幏鍙朣truts涓墍鏈夌殑鎴愬憳鍙橀噺鐨勫悕瀛楋紝鍐嶄笌瀛楃涓测済et鈥濇垨鈥渟et鈥濇嫾鎺ュ嚭getter鎴栬卻etter鏂规硶鍚嶏紝鍐嶉氳繃鏂规硶鍚嶇敤鍙嶅皠鑾峰彇鏂规硶 +### 闂1锛氬洜涓烘嫾鎺ユ柟娉曞悕鑲畾浠ラ┘宄板懡鍚嶆硶鏉ユ嫾鎺ワ紝杩欏氨瑕佹眰getter鍜宻etter鏂规硶涔熸槸浠ヨ繖涓鍒欏懡鍚嶏紝铏界劧ide鑷姩鐢熸垚鐨勬柟娉曟槸濡傛锛屼絾涓嶈兘淇濆瓨鎬绘槸濡傛銆 \ No newline at end of file diff --git a/group05/578505552/src/main/java/com/coderising/array/ArrayUtil.java b/group05/578505552/src/main/java/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..2c248db331 --- /dev/null +++ b/group05/578505552/src/main/java/com/coderising/array/ArrayUtil.java @@ -0,0 +1,242 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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 length = origin.length; + int i = 0; + int j = length - 1; + while (i < j){ + int tmp = origin[i]; + origin[i] = origin[j]; + origin[j] = tmp; + i++; + j--; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 length = oldArray.length; + int[] newArray = new int[length]; + int j = 0; + for (int i = 0; i < length; i++) { + if (oldArray[i] != 0){ + newArray[j++] = oldArray[i]; + } + } + int[] res = new int[j]; + System.arraycopy(newArray, 0, res, 0, j); + return res; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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 length1 = array1.length; + int length2 = array2.length; + int i = 0; + int j = 0; + + int[] res = new int[length1 + length2]; + int k = 0; + while (i < length1 || j < length2){ + int next = Integer.MIN_VALUE; + if (i < length1 && j < length2){ + if (array1[i] == array2[j]){ + next = array1[i]; + i++; + j++; + } else if (array1[i] < array2[j]){ + next = array1[i++]; + } else { + next = array2[j++]; + } + } else if (i < length1){ + next = array1[i++]; + } else { + next = array2[j++]; + } + + if (k == 0){ + res[k++] = next; + } else if (next > res[k-1]){ + res[k++] = next; + } + } + + int[] merged = new int[k]; + System.arraycopy(res, 0, merged, 0, k); + return merged; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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){ + if (size < 0){ + throw new IllegalArgumentException("illegal size"); + } + int newLength = oldArray.length + size; + int[] newArray = new int[newLength]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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[] res = new int[max]; + int i = 1; + int j = 1; + int k = 0; + res[k++] = 1; + res[k++] = 1; + + int tmp = i + j; + while (tmp < max){ + res[k++] = tmp; + i = j; + j = tmp; + tmp = i + j; + } + + int[] result = new int[k]; + System.arraycopy(res, 0, result, 0, k); + return result; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if (max < 0){ + return new int[0]; + } + int[] res = new int[max]; + int k = 0; + for (int i = 2; i < max; i++) { + if (isPrime(i)){ + res[k++] = i; + } + } + int[] result = new int[k]; + System.arraycopy(res, 0, result, 0, k); + return result; + } + + private boolean isPrime(int num){ + + if (num < 1){ + return false; + } + for (int i = 2; i <= num / 2; i++) { + if (num % i == 0){ + return false; + } + + } + return true; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + + if (max < 0){ + return new int[0]; + } + int[] res = new int[max]; + int k = 0; + for (int i = 0; i < max; i++) { + if (isPerfectNumbers(i)){ + res[k++] = i; + } + } + int[] result = new int[k]; + System.arraycopy(res, 0, result, 0, k); + return result; + } + + private boolean isPerfectNumbers(int num){ + + return num == getFactorSum(num); + } + + private int getFactorSum(int num){ + if (num == 0 || num == 1){ + return -1; + } + int sum = 0; + for (int i = 1; i <= num / 2; i++) { + if (num % i == 0){ + sum += i; + } + } + return sum; + } + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param separator + * @return + */ + public String join(int[] array, String separator){ + + if (array.length <= 0){ + return ""; + } + + StringBuffer stringBuffer = new StringBuffer(); + for (int i = 0; i < array.length - 1; i++) { + stringBuffer.append(String.valueOf(array[i])).append(separator); + } + stringBuffer.append(String.valueOf(array[array.length-1])); + return stringBuffer.toString(); + } + +} diff --git a/group05/578505552/src/main/java/com/coderising/litestruts/Action.java b/group05/578505552/src/main/java/com/coderising/litestruts/Action.java new file mode 100644 index 0000000000..743f452030 --- /dev/null +++ b/group05/578505552/src/main/java/com/coderising/litestruts/Action.java @@ -0,0 +1,38 @@ +package com.coderising.litestruts; + + +import java.util.Map; + +/** + * Created by songbao.yang on 2017/3/1. + * + */ +public class Action { + private String name; + private String className; + private Map results; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public Map getResults() { + return results; + } + + public void setResults(Map results) { + this.results = results; + } +} diff --git a/group05/578505552/src/main/java/com/coderising/litestruts/LoginAction.java b/group05/578505552/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..b74dfa425d --- /dev/null +++ b/group05/578505552/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,38 @@ +package com.coderising.litestruts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * + */ +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/group05/578505552/src/main/java/com/coderising/litestruts/Struts.java b/group05/578505552/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..95b4d37a52 --- /dev/null +++ b/group05/578505552/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,131 @@ +package com.coderising.litestruts; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; +import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + +// 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + Action action = matchAction(parseXml("/struts.xml"), actionName); + try { + +// 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛, +// 鏍规嵁parameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶 + Class clazz = Class.forName(action.getClassName()); + Object instance = clazz.newInstance(); + for (String key : parameters.keySet()){ + try { + PropertyDescriptor propertyDescriptor = new PropertyDescriptor(key, clazz); + Method setMethod = propertyDescriptor.getWriteMethod(); + setMethod.invoke(instance, parameters.get(key)); + } catch (IntrospectionException e) { + e.printStackTrace(); + } + } + +// 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + Method exectueMethod = null; + String result = null; + try { + exectueMethod = clazz.getMethod("execute"); + result = (String)exectueMethod.invoke(instance); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + +// 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, +// 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , +// 鏀惧埌View瀵硅薄鐨刾arameters + Map hashMap = new HashMap(); + Field[] declaredFields = clazz.getDeclaredFields(); + for (Field field : declaredFields){ + String name = field.getName(); + try { + PropertyDescriptor propertyDescriptor = new PropertyDescriptor(name, clazz); + Method getMethod = propertyDescriptor.getReadMethod(); + Object res = getMethod.invoke(instance); + hashMap.put(name, res); + } catch (IntrospectionException e) { + e.printStackTrace(); + } + } + +// 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 +// 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + View view = new View(); + view.setJsp((String)action.getResults().get(result)); + view.setParameters(hashMap); + return view; + + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + return null; + } + + private static Element parseXml(String resourcePath){ + + InputStream resourceAsStream = Struts.class.getResourceAsStream(resourcePath); + SAXReader saxReader = new SAXReader(); + try { + Document document = saxReader.read(resourceAsStream); + Element rootElement = document.getRootElement(); + return rootElement; + } catch (DocumentException e) { + e.printStackTrace(); + } + + throw new RuntimeException("fail to parse xml"); + } + + private static Action matchAction(Element rootElement, String actionName){ + + List actions = rootElement.elements("action"); + Iterator iterator = actions.iterator(); + Action action = new Action(); + while (iterator.hasNext()){ + Element actionElement = (Element) iterator.next(); + String nameAttributeValue = actionElement.attributeValue("name"); + if (actionName.equals(nameAttributeValue)){ + action.setName(nameAttributeValue); + action.setClassName(actionElement.attributeValue("class")); + List results = actionElement.elements("result"); + Map resultMap = new HashMap(); + Iterator it = results.iterator(); + while (it.hasNext()){ + Element resultElement = (Element)it.next(); + resultMap.put(resultElement.attributeValue("name"), (String)resultElement.getData()); + } + action.setResults(resultMap); + } + } + + return action; + } + + + + +} diff --git a/group05/578505552/src/main/java/com/coderising/litestruts/View.java b/group05/578505552/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group05/578505552/src/main/java/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/group05/578505552/src/main/java/com/coderising/litestruts/XmlUtil.java b/group05/578505552/src/main/java/com/coderising/litestruts/XmlUtil.java new file mode 100644 index 0000000000..6e3c261882 --- /dev/null +++ b/group05/578505552/src/main/java/com/coderising/litestruts/XmlUtil.java @@ -0,0 +1,8 @@ +package com.coderising.litestruts; + +/** + * + * Created by songbao.yang on 2017/2/28. + */ +public class XmlUtil { +} diff --git a/group05/578505552/src/main/java/com/coding/basic/BinaryTreeNode.java b/group05/578505552/src/main/java/com/coding/basic/BinaryTreeNode.java index 0610a542b5..3a36db4031 100644 --- a/group05/578505552/src/main/java/com/coding/basic/BinaryTreeNode.java +++ b/group05/578505552/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -30,6 +30,9 @@ public void setRight(BinaryTreeNode right) { } public BinaryTreeNode insert(Integer o){ + if (o == null){ + throw new IllegalArgumentException("can not insert null"); + } BinaryTreeNode newNode = new BinaryTreeNode(); newNode.data = o; diff --git a/group05/578505552/src/main/java/com/coding/basic/LinkedList.java b/group05/578505552/src/main/java/com/coding/basic/LinkedList.java index 58accde4d8..d1fa42bf4c 100644 --- a/group05/578505552/src/main/java/com/coding/basic/LinkedList.java +++ b/group05/578505552/src/main/java/com/coding/basic/LinkedList.java @@ -13,7 +13,7 @@ public class LinkedList implements List { //head浣滀负涓涓妭鐐癸紝鍏秐ext鐨勫兼寚鍚慙ist涓湡姝g殑绗竴涓妭鐐 public LinkedList() { - Node head = new Node(); + head = new Node(); head.next = null; head.data = null; elementCount = 0; @@ -60,7 +60,7 @@ public Object get(int index){ for (int i = 0; i < index; i++) { cursor = cursor.next; } - return cursor.next; + return cursor.next.data; } public Object remove(int index){ diff --git a/group05/578505552/src/main/java/com/coding/basic/List.java b/group05/578505552/src/main/java/com/coding/basic/List.java index a979b2fdb9..e26fbe7c98 100644 --- a/group05/578505552/src/main/java/com/coding/basic/List.java +++ b/group05/578505552/src/main/java/com/coding/basic/List.java @@ -10,4 +10,5 @@ public interface List { public Object get(int index); public Object remove(int index); public int size(); + public Iterator iterator(); } diff --git a/group05/578505552/src/main/resources/struts.xml b/group05/578505552/src/main/resources/struts.xml new file mode 100644 index 0000000000..0a44e0927e --- /dev/null +++ b/group05/578505552/src/main/resources/struts.xml @@ -0,0 +1,12 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group05/578505552/src/test/java/com/coderising/array/ArrayUtilTest.java b/group05/578505552/src/test/java/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..607f4fed5b --- /dev/null +++ b/group05/578505552/src/test/java/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,206 @@ +package com.coderising.array; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; + +/** + * Created by songbao.yang on 2017/3/2. + * + */ +public class ArrayUtilTest { + + private ArrayUtil arrayUtil; + + @Before + public void setUp() throws Exception { + arrayUtil = new ArrayUtil(); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void reverseArray() throws Exception { + + int[][] actualAndExpected = { + {}, {}, {0}, {0}, + {1,2,3,4,5,6}, {6,5,4,3,2,1}, + {7,9,30,3,4}, {4,3,30,9,7} + }; + + for (int i = 0; i < actualAndExpected.length; i += 2) { + int[] acutal = actualAndExpected[i]; + int[] expected = actualAndExpected[i+1]; + arrayUtil.reverseArray(acutal); + Assert.assertTrue("wrong index: " + String.valueOf(i), isArrayEqual(expected, acutal)); + } + } + + @Test + public void removeZero() throws Exception { + int[][] actualAndExpected = { + {}, {}, {0}, {}, + {1,0,3,0,5,0}, {1,3,5}, + {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}, {1,3,4,5,6,6,5,4,7,6,7,5} + }; + + for (int i = 0; i < actualAndExpected.length; i += 2) { + int[] acutal = actualAndExpected[i]; + int[] expected = actualAndExpected[i+1]; + int[] ints = arrayUtil.removeZero(acutal); + Assert.assertTrue("wrong index: " + String.valueOf(i), isArrayEqual(expected, ints)); + } + } + + @Test + public void merge() throws Exception { + int[][] actualAndExpected = { + {}, {}, {}, + {}, {0}, {0}, + {3,5,7,8}, {4,5,6,7},{3,4,5,6,7,8}, + {1,2,3,4,5,}, {6,7,8,9,10}, {1,2,3,4,5,6,7,8,9,10} + }; + + for (int i = 0; i < actualAndExpected.length; i += 3) { + int[] array1 = actualAndExpected[i]; + int[] array2 = actualAndExpected[i+1]; + int[] expected = actualAndExpected[i+2]; + int[] result = arrayUtil.merge(array1, array2); + Assert.assertTrue("wrong index: " + String.valueOf(i), isArrayEqual(expected, result)); + } + + } + + @Test + public void grow() throws Exception { + int[][] actualAndExpected = { + {}, {}, + {1}, {}, + {5}, {0,0,0,0,0}, + {0},{2,3,6}, + {3}, {2,3,6,0,0,0} + }; + + for (int i = 0; i < actualAndExpected.length; i += 3) { + int[] oldArray = actualAndExpected[i]; + int size = actualAndExpected[i+1][0]; + int[] expected = actualAndExpected[i+2]; + int[] newArray = arrayUtil.grow(oldArray, size); + Assert.assertTrue("wrong index: " + String.valueOf(i), isArrayEqual(expected, newArray)); + } + } + + @Test + public void fibonacci() throws Exception { + int[][] actualAndExpected = { + {0}, {}, + {1}, {}, + {2}, {1,1}, + {3}, {1,1,2}, + {4}, {1,1,2,3}, + {15}, {1,1,2,3,5,8,13}, + }; + + for (int i = 0; i < actualAndExpected.length; i += 2) { + int max = actualAndExpected[i][0]; + int[] expected = actualAndExpected[i+1]; + int[] actual = arrayUtil.fibonacci(max); + Assert.assertTrue("wrong index: " + String.valueOf(i), isArrayEqual(expected, actual)); + } + } + + @Test + public void getPrimes() throws Exception { + int[][] actualAndExpected = { + {-1}, {}, + {0}, {}, + {1}, {}, + {2}, {}, + {3}, {2}, + {4}, {2,3}, + {23}, {2,3,5,7,11,13,17,19}, + }; + + for (int i = 0; i < actualAndExpected.length; i += 2) { + int max = actualAndExpected[i][0]; + int[] expected = actualAndExpected[i+1]; + int[] actual = arrayUtil.getPrimes(max); + Assert.assertTrue("wrong index: " + String.valueOf(i), isArrayEqual(expected, actual)); + } + } + + @Test + public void getPerfectNumbers() throws Exception { + int[][] actualAndExpected = { + {-1}, {}, + {0}, {}, + {1}, {}, + {2}, {}, + {7}, {6}, + {30}, {6,28}, + {500}, {6,28,496}, + {10000}, {6,28,496,8128} + }; + + for (int i = 0; i < actualAndExpected.length; i += 2) { + int max = actualAndExpected[i][0]; + int[] expected = actualAndExpected[i+1]; + int[] actual = arrayUtil.getPerfectNumbers(max); + Assert.assertTrue("wrong index: " + String.valueOf(i), isArrayEqual(expected, actual)); + } + } + + @Test + public void join() throws Exception { + int[][] arrays = { + {}, + {3,8,9}, + {1}, + {0,0,0,0,0}, + {1,2,3,4,5} + }; + String[] separators = {"", "-", "+", "*", "00"}; + String[] expecteds = { + "", + "3-8-9", + "1", + "0*0*0*0*0", + "1002003004005" + }; + for (int i = 0; i < arrays.length; i++) { + int[] array = arrays[i]; + String separator = separators[i]; + String expected = expecteds[i]; + String actual = arrayUtil.join(array, separator); + Assert.assertTrue("wrong index: " + String.valueOf(i), expected.equals(actual)); + } + } + + private boolean isArrayEqual(int[] expected, int[] actual){ + if (expected.length != actual.length){ + System.out.println("expected.length != actual.length"); + System.out.println("expected: " + Arrays.toString(expected)); + System.out.println("actual: " + Arrays.toString(actual)); + return false; + } + + for (int i = 0; i < expected.length; i++) { + if (expected[i] != actual[i]){ + System.out.println("expected[i] != actual[i]"); + System.out.println("expected: " + Arrays.toString(expected)); + System.out.println("actual: " + Arrays.toString(actual)); + return false; + } + } + + return true; + } + +} \ No newline at end of file diff --git a/group05/578505552/src/test/java/com/coderising/litestruts/StrutsTest.java b/group05/578505552/src/test/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..6df05450b6 --- /dev/null +++ b/group05/578505552/src/test/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,40 @@ +package com.coderising.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + +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/group05/578505552/src/test/java/com/coding/basic/BinaryTreeNodeTest.java b/group05/578505552/src/test/java/com/coding/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..d1e408554e --- /dev/null +++ b/group05/578505552/src/test/java/com/coding/basic/BinaryTreeNodeTest.java @@ -0,0 +1,38 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Created by songbao.yang on 2017/2/28. + * + */ +public class BinaryTreeNodeTest { + + private BinaryTreeNode node; + + @Before + public void setUp() throws Exception { + node = new BinaryTreeNode(); + node.setData(100); + node.setLeft(null); + node.setRight(null); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void insert() throws Exception { + + for (int i = 0; i < 100; i++) { + int ele = (int)Math.floor(Math.random() * 200); + node.insert(ele); + } + } +} \ No newline at end of file diff --git a/group05/578505552/src/test/java/com/coding/basic/ArrayListTest.java b/group05/578505552/src/test/java/com/coding/basic/ListTest.java similarity index 81% rename from group05/578505552/src/test/java/com/coding/basic/ArrayListTest.java rename to group05/578505552/src/test/java/com/coding/basic/ListTest.java index 87b9906c29..0c786de96c 100644 --- a/group05/578505552/src/test/java/com/coding/basic/ArrayListTest.java +++ b/group05/578505552/src/test/java/com/coding/basic/ListTest.java @@ -9,16 +9,17 @@ * Created by songbao.yang on 2017/2/21. * */ -public class ArrayListTest { +public class ListTest { - private ArrayList list; + private List list; public static final int SIZE = 10000; @Before public void setUp() throws Exception { - list = new ArrayList(); +// list = new ArrayList(); + list = new LinkedList(); } @After @@ -71,10 +72,12 @@ public void remove() throws Exception { add(); for (int i = 0; i < SIZE; i++) { - System.out.println("remove: " + i); - list.remove(0); + int oldSize = list.size(); + int randomIndex = (int)Math.floor(Math.random() * oldSize); + list.remove(randomIndex); + int newSize = list.size(); + Assert.assertEquals(newSize, oldSize-1); } - } @Test diff --git a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.gitignore b/group05/591010847/.gitignore similarity index 75% rename from group05/591010847/job_2017_02/job_2017_02_19_data_structure/.gitignore rename to group05/591010847/.gitignore index dd9d181eaa..7a02c10a7e 100644 --- a/group05/591010847/job_2017_02/job_2017_02_19_data_structure/.gitignore +++ b/group05/591010847/.gitignore @@ -5,3 +5,6 @@ Homestead.json .env .idea .xml +/out +.class +.DS_Store diff --git a/group05/591010847/job_2017_02/.gitignore b/group05/591010847/job_2017_02/.gitignore new file mode 100644 index 0000000000..a93e887bab --- /dev/null +++ b/group05/591010847/job_2017_02/.gitignore @@ -0,0 +1 @@ +libs diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/job_2017_02_26_Struts.iml b/group05/591010847/job_2017_02/job_2017_02_26_Struts/job_2017_02_26_Struts.iml new file mode 100644 index 0000000000..a141f7b1fe --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/job_2017_02_26_Struts.iml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/res/struts.xml b/group05/591010847/job_2017_02/job_2017_02_26_Struts/res/struts.xml new file mode 100644 index 0000000000..92c2e5800b --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/res/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/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/array/ArrayUtil.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/array/ArrayUtil.java new file mode 100644 index 0000000000..75ae466816 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/array/ArrayUtil.java @@ -0,0 +1,229 @@ +package com.java.xiaoqin.array; + +import java.util.Objects; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 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) { + if (null != origin) { + int[] temp = new int[origin.length]; + System.arraycopy(origin, 0, temp, 0, origin.length); + for (int index = origin.length; index > 0; index--) { + origin[origin.length - index] = temp[index - 1]; + } + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 size = 0; + int[] newArray = new int[oldArray.length]; + for (int indexO = 0; indexO < oldArray.length; indexO++) { + if (0 == oldArray[indexO]) { + continue; + } + newArray[size++] = oldArray[indexO]; + } + int[] returnArray = new int[size]; + System.arraycopy(newArray, 0, returnArray, 0, returnArray.length); + return returnArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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[] mergeArray = new int[array1.length + array2.length]; + int size = 0; + for (int index = 0, max = Math.max(array1.length, array2.length); index < max; index++) { + Object arrObj1 = null; + Object arrObj2 = null; + if (index < array1.length) { + arrObj1 = array1[index]; + } + if (index < array2.length) { + arrObj2 = array2[index]; + } + if (null != arrObj1 && Objects.equals(arrObj1, arrObj2)) { + arrObj2 = null; + } + for (int indexMerge = 0; indexMerge < size; indexMerge++) { + if (Objects.equals(arrObj1, mergeArray[indexMerge])) { + arrObj1 = null; + } + if (Objects.equals(arrObj2, mergeArray[indexMerge])) { + arrObj2 = null; + break; + } + } + if (null != arrObj1 && null != arrObj2 && (int) arrObj1 > (int) arrObj2) { + arrObj1 = (int) arrObj1 + (int) arrObj2; + arrObj2 = (int) arrObj1 - (int) arrObj2; + arrObj1 = (int) arrObj1 - (int) arrObj2; + } + if (null != arrObj1) { + mergeArray[size++] = (int) arrObj1; + } + if (null != arrObj2) { + mergeArray[size++] = (int) arrObj2; + } + } + int[] resultArr = new int[size]; + System.arraycopy(mergeArray, 0, resultArr, 0, size); + return resultArr; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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 indexOld = 0; indexOld < oldArray.length; indexOld++) { + newArray[indexOld] = oldArray[indexOld]; + } + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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[] newArray = new int[0]; + if (max > 1) { + int size = 0; + newArray = new int[max]; + if (max >= 1) { + for (int n = 1; n < Integer.MAX_VALUE; n++) { + int fibonacci = mFibonacci(n); + if (fibonacci < max) { + newArray[size++] = fibonacci; + } else { + break; + } + } + } + int[] resultArr = new int[size]; + System.arraycopy(newArray, 0, resultArr, 0, size); + newArray = resultArr; + } + return newArray; + } + + private int mFibonacci(int n) { + if (n == 0) return 0; + else if (n == 1) return 1; + else return mFibonacci(n - 1) + mFibonacci(n - 2); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int[] newArray = new int[max]; + int size = 0; + for (int index = 2; index < max; index++) { + if (isPrimes(index)) { + newArray[size++] = index; + } + } + int[] resultArr = new int[size]; + System.arraycopy(newArray, 0, resultArr, 0, size); + return resultArr; + } + + private boolean isPrimes(int number) { + int i = 1; + while (++i < number) { + if (number % i == 0) { + return false; + } + } + return true; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int[] newArray = new int[max]; + int size = 0; + for (int i = 6; i < max; i++) { + int total = 0; + for (int index = 1, length = i / 2; index <= length; index++) { + if (i % index == 0) { + total += index; + } + } + if (i == total) { + newArray[size++] = i; + } + } + int[] resultArr = new int[size]; + System.arraycopy(newArray, 0, resultArr, 0, size); + return resultArr; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + StringBuilder arrBuilder = new StringBuilder(); + for (int arr : array) { + if (arrBuilder.length() > 0) { + arrBuilder.append(seperator); + } + arrBuilder.append(arr); + } + return arrBuilder.toString(); + } + + +} diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/Struts.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/Struts.java new file mode 100644 index 0000000000..0726c17779 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/Struts.java @@ -0,0 +1,64 @@ +package com.java.xiaoqin.litestruts; + +import com.java.xiaoqin.litestruts.bean.View; +import com.java.xiaoqin.litestruts.manager.StrutsParseManager; +import com.java.xiaoqin.litestruts.util.ReflectUtils; + +import java.net.URL; +import java.util.Map; + + +public class Struts { + + private static final String METHOD_EXECUTE = "execute"; + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + URL resourceURL = Struts.class.getResource("/struts.xml"); + StrutsParseManager.getInstance().init(resourceURL.getPath()); + + String className = StrutsParseManager.getInstance().findClassNameByActionName(actionName); + + Object instance = ReflectUtils.newInstance(className); + if (null != parameters) { + for (Map.Entry paramSet : parameters.entrySet()) { + ReflectUtils.setMethod(instance, paramSet.getKey(), paramSet.getValue()); + } + } + + Object resultExecute = ReflectUtils.executeMethod(instance, METHOD_EXECUTE); + + Map getMap = ReflectUtils.executeGets(instance); + + View view = new View(); + view.setParameters(getMap); + + if (resultExecute instanceof String) { + String result = StrutsParseManager.getInstance().getResult(actionName, (String) resultExecute); + view.setJsp(result); + } + + return view; + } + +} diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/action/LoginAction.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/action/LoginAction.java new file mode 100644 index 0000000000..de6a7aefca --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/action/LoginAction.java @@ -0,0 +1,39 @@ +package com.java.xiaoqin.litestruts.action; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @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/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/action/LogoutAction.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/action/LogoutAction.java new file mode 100644 index 0000000000..b26cce5630 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/action/LogoutAction.java @@ -0,0 +1,7 @@ +package com.java.xiaoqin.litestruts.action; + +/** + * Created by xiaoqin on 17-3-5. + */ +public class LogoutAction { +} diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/bean/ActionBean.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/bean/ActionBean.java new file mode 100644 index 0000000000..91f9f8f213 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/bean/ActionBean.java @@ -0,0 +1,41 @@ +package com.java.xiaoqin.litestruts.bean; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by xiaoqin on 17-3-5. + */ +public class ActionBean { + private String className; + private Map result; + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public Map getResult() { + return result; + } + + public void setResult(Map result) { + this.result = result; + } + + public void addResult(String name, String text) { + if (null == result) result = new HashMap<>(); + result.put(name, text); + } + + @Override + public String toString() { + return "ActionBean{" + + "className='" + className + '\'' + + ", result=" + result + + '}'; + } +} diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/bean/View.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/bean/View.java new file mode 100644 index 0000000000..d3cb44085c --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/bean/View.java @@ -0,0 +1,23 @@ +package com.java.xiaoqin.litestruts.bean; + +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/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/manager/StrutsParseManager.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/manager/StrutsParseManager.java new file mode 100644 index 0000000000..fc7ac10f89 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/manager/StrutsParseManager.java @@ -0,0 +1,95 @@ +package com.java.xiaoqin.litestruts.manager; + +import com.java.xiaoqin.litestruts.bean.ActionBean; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Objects; + +/** + * Created by xiaoqin on 17-3-5. + */ +public class StrutsParseManager { + + private final static String ACTION = "action"; + private final static String ACTION_NAME = "name"; + private final static String ACTION_CLASS = "class"; + private static final String RESULT_NAME = "name"; + + private final static StrutsParseManager INSTANCE = new StrutsParseManager(); + + + private Map mActionBeanMap = new HashMap<>(); + + private StrutsParseManager() { + } + + public static StrutsParseManager getInstance() { + return INSTANCE; + } + + public void init(String path) { + if (!Objects.isNull(path) && !"".equals(path)) { + parseXML(path); + } else { + throw new NullPointerException("path is null"); + } + } + + private void parseXML(String path) { + try { + SAXReader reader = new SAXReader(); + Document document = reader.read(new File(path)); + Element rootElement = document.getRootElement(); + Iterator actionIterator = rootElement.elementIterator(ACTION); + while (actionIterator.hasNext()) { + Object actionObj = actionIterator.next(); + if (actionObj instanceof Element) { + String name = ((Element) actionObj).attributeValue(ACTION_NAME); + String className = ((Element) actionObj).attributeValue(ACTION_CLASS); + ActionBean actionBean = new ActionBean(); + actionBean.setClassName(className); + mActionBeanMap.put(name, actionBean); + Iterator iteratorResult = ((Element) actionObj).elementIterator(); + while (iteratorResult.hasNext()) { + Object resultObj = iteratorResult.next(); + if (resultObj instanceof Element) { + String resultName = ((Element) resultObj).attributeValue(RESULT_NAME); + String resultText = ((Element) resultObj).getText(); + actionBean.addResult(resultName, resultText); + } + } + } + } + } catch (DocumentException e) { + e.printStackTrace(); + } + } + + + public String findClassNameByActionName(String actionName) { + String className = ""; + if (mActionBeanMap.containsKey(actionName)) { + className = mActionBeanMap.get(actionName).getClassName(); + } + return className; + } + + public String getResult(String actionName, String result) { + String resultResult = ""; + if (mActionBeanMap.containsKey(actionName)) { + Map actionResult = mActionBeanMap.get(actionName).getResult(); + if (null != actionResult && actionResult.containsKey(result)) { + resultResult = actionResult.get(result); + } + + } + return resultResult; + } +} diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/util/ReflectUtils.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/util/ReflectUtils.java new file mode 100644 index 0000000000..1fc3ff1e1c --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/com/java/xiaoqin/litestruts/util/ReflectUtils.java @@ -0,0 +1,75 @@ +package com.java.xiaoqin.litestruts.util; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by xiaoqin on 17-3-5. + */ +public class ReflectUtils { + + public static Object newInstance(String className) { + Object obj = null; + try { + Class aClass = Class.forName(className); + obj = aClass.newInstance(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } + return obj; + } + + public static void setMethod(Object obj, String methodName, String paramter) { + try { + StringBuilder methodNameBuilder = new StringBuilder(); + methodNameBuilder.append("set").append(methodName.substring(0, 1).toUpperCase()).append(methodName.substring(1)); + Method method = obj.getClass().getMethod(methodNameBuilder.toString(), String.class); + method.invoke(obj, paramter); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + + public static Object executeMethod(Object obj, String methodName) { + Object resultObj = null; + try { + Method method = obj.getClass().getMethod(methodName); + resultObj = method.invoke(obj); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return resultObj; + } + + public static Map executeGets(Object obj) { + Map resultMap = new HashMap<>(); + try { + Method[] methods = obj.getClass().getMethods(); + for (Method method : methods) { + String methodName = method.getName(); + if (methodName.startsWith("get")) { + resultMap.put(methodName.substring(3, 4).toLowerCase() + methodName.substring(4), method.invoke(obj)); + } + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return resultMap; + } +} diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/test/com/java/xiaoqin/array/ArrayUtilTest.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/test/com/java/xiaoqin/array/ArrayUtilTest.java new file mode 100644 index 0000000000..ea196a50ca --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/test/com/java/xiaoqin/array/ArrayUtilTest.java @@ -0,0 +1,110 @@ +package test.com.java.xiaoqin.array; + +import com.java.xiaoqin.array.ArrayUtil; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +/** + * ArrayUtil Tester. + * + * @author + * @version 1.0 + * @since
涓夋湀 4, 2017
+ */ +public class ArrayUtilTest { + + private ArrayUtil mArrayUtil; + + @Before + public void before() throws Exception { + mArrayUtil = new ArrayUtil(); + } + + @After + public void after() throws Exception { + mArrayUtil = null; + } + + /** + * Method: reverseArray(int[] origin) + */ + @Test + public void testReverseArray() throws Exception { + int[] origin = {7, 9, 30, 3}; + mArrayUtil.reverseArray(origin); + System.out.println(Arrays.toString(origin)); + } + + /** + * Method: removeZero(int[] oldArray) + */ + @Test + public void testRemoveZero() throws Exception { + int oldArr[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + System.out.println(Arrays.toString(mArrayUtil.removeZero(oldArr))); + } + + /** + * Method: merge(int[] array1, int[] array2) + */ + @Test + public void testMerge() throws Exception { + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + int[] mergeArr = mArrayUtil.merge(a1, a2); + System.out.println(Arrays.toString(mergeArr)); + } + + /** + * Method: grow(int[] oldArray, int size) + */ + @Test + public void testGrow() throws Exception { + int[] oldArray = {2, 3, 6}; + int[] growArray = mArrayUtil.grow(oldArray, 3); + System.out.println(Arrays.toString(growArray)); + } + + /** + * Method: fibonacci(int max) + */ + @Test + public void testFibonacci() throws Exception { + int[] fibonacciArray = mArrayUtil.fibonacci(1); + System.out.println(Arrays.toString(fibonacciArray)); + fibonacciArray = mArrayUtil.fibonacci(15); + System.out.println(Arrays.toString(fibonacciArray)); + } + + /** + * Method: getPrimes(int max) + */ + @Test + public void testGetPrimes() throws Exception { + int[] primes = mArrayUtil.getPrimes(23); + System.out.println(Arrays.toString(primes)); + } + + /** + * Method: getPerfectNumbers(int max) + */ + @Test + public void testGetPerfectNumbers() throws Exception { + int[] perfectNumbers = mArrayUtil.getPerfectNumbers(10000); + System.out.println(Arrays.toString(perfectNumbers)); + } + + /** + * Method: join(int[] array, String seperator) + */ + @Test + public void testJoin() throws Exception { + String join = mArrayUtil.join(new int[]{3, 8, 9}, "-"); + System.out.println(join); + } + + +} diff --git a/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/test/com/java/xiaoqin/litestruts/StrutsTest.java b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/test/com/java/xiaoqin/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a6b8645648 --- /dev/null +++ b/group05/591010847/job_2017_02/job_2017_02_26_Struts/src/test/com/java/xiaoqin/litestruts/StrutsTest.java @@ -0,0 +1,41 @@ +package test.com.java.xiaoqin.litestruts; + +import com.java.xiaoqin.litestruts.Struts; +import com.java.xiaoqin.litestruts.bean.View; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +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/group05/810574652/src/com/github/PingPi357/coding2017/basic/ArrayList.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/ArrayList.java similarity index 100% rename from group05/810574652/src/com/github/PingPi357/coding2017/basic/ArrayList.java rename to group05/810574652/src/com/github/PingPi357/coding2017/homework1/ArrayList.java diff --git a/group05/810574652/src/com/github/PingPi357/coding2017/basic/LinkedList.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/LinkedList.java similarity index 100% rename from group05/810574652/src/com/github/PingPi357/coding2017/basic/LinkedList.java rename to group05/810574652/src/com/github/PingPi357/coding2017/homework1/LinkedList.java diff --git a/group05/810574652/src/com/github/PingPi357/coding2017/basic/List.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/List.java similarity index 100% rename from group05/810574652/src/com/github/PingPi357/coding2017/basic/List.java rename to group05/810574652/src/com/github/PingPi357/coding2017/homework1/List.java diff --git a/group05/810574652/src/com/github/PingPi357/coding2017/basic/Queue.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/Queue.java similarity index 100% rename from group05/810574652/src/com/github/PingPi357/coding2017/basic/Queue.java rename to group05/810574652/src/com/github/PingPi357/coding2017/homework1/Queue.java diff --git a/group05/810574652/src/com/github/PingPi357/coding2017/basic/Stack.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/Stack.java similarity index 100% rename from group05/810574652/src/com/github/PingPi357/coding2017/basic/Stack.java rename to group05/810574652/src/com/github/PingPi357/coding2017/homework1/Stack.java diff --git "a/group05/810574652/src/com/github/PingPi357/coding2017/basic/\350\257\264\346\230\216" "b/group05/810574652/src/com/github/PingPi357/coding2017/homework1/\350\257\264\346\230\216" similarity index 100% rename from "group05/810574652/src/com/github/PingPi357/coding2017/basic/\350\257\264\346\230\216" rename to "group05/810574652/src/com/github/PingPi357/coding2017/homework1/\350\257\264\346\230\216" diff --git a/group05/810574652/src/com/github/PingPi357/coding2017/homework2/ArrayUtil.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/ArrayUtil.java new file mode 100644 index 0000000000..b12c28c589 --- /dev/null +++ b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/ArrayUtil.java @@ -0,0 +1,203 @@ +package com.github.PingPi357.coding2017.homework2; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Arrays; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] 濡傛灉 a = + * [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public int[] reverseArray(int[] origin) { + int len = origin.length; + int[] temp = new int[len]; + for (int i = 0; i < len; i++) { + temp[i] = origin[--len]; + } + return 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[] newArray = new int[oldArray.length]; + int i = 0; + for (int element : oldArray) { + if (element != 0) { + newArray[i++] = element; + } + } + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 渚嬪 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) { + ArrayList list = new ArrayList(); + int k1=0; + int k2 = 0; + for (int i = 0; i < array1.length; i++) { + for (int j = k2; j < array2.length; j++) { + if (array1[i] < array2[j]) { + list.add(array1[i]); + k1++; + break; + } else if(array1[i] > array2[j]) { + list.add(array2[j]); + k2++; + }else{ + list.add(array1[i]); + k1++; + k2++; + break; + } + } + if(k2==array2.length-1){ + for (; k1 <= array1.length-1 ; k1++) { + list.add(array1[k1]); + } + break; + } + if(k1== array1.length-1){ + for (; k2 <= array2.length-1 ; k2++) { + list.add(array2[k2]); + } + break; + } + } + + + int[] mergeArray = arrayListToArray(list); + return mergeArray; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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[] growArray = Arrays.copyOf(oldArray, oldArray.length + size); + return growArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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 a = 1; + int b = 1; + int fibMaxNum = 1; + ArrayList list=new ArrayList(); + list.add(1); + if (max <= 1) { + return new int[0]; + } + while (max > fibMaxNum) { + list.add(fibMaxNum); + fibMaxNum = a + b; + b = fibMaxNum; + a = b; + } + return arrayListToArray(list); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + ArrayList list =new ArrayList(); + for(int i=2; i <=max-1;i++){ + boolean flag=true; + for(int j=2; j < Math.floor(i/2); j++){ + if(i%j==0){ + flag=false; + break; + } + } + if(!flag){ + list.add(i); + } + } + return arrayListToArray(list); + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + ArrayList list =new ArrayList(); + int sum=0; + if(max<=1){ + return null; + } + for(int i=1;i <=(max-1);i++){ + for(int j=1; j<=i/2;j++){ + if(i%j==0){ + sum+=j; + } + } + if(sum==i){ + list.add(i); + } + } + return arrayListToArray(list); + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 渚嬪array= [3,8,9], seperator = "-" 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + StringBuilder sb=new StringBuilder(); + sb.append(array[0]); + for(int i=1;i list) { + int[] Array = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + Array[i] = (int) list.get(i); + } + return Array; + } +} diff --git a/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/LoginAction.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/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; + } +} diff --git a/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/Struts.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/Struts.java new file mode 100644 index 0000000000..85e2e22de3 --- /dev/null +++ b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/Struts.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + return null; + } + +} diff --git a/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/StrutsTest.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/View.java b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/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/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/struts.xml b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/litestruts/struts.xml new file mode 100644 index 0000000000..a6cfe43e6c --- /dev/null +++ b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/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/group05/810574652/src/com/github/PingPi357/coding2017/homework2/\350\257\264\346\230\216" "b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/\350\257\264\346\230\216" new file mode 100644 index 0000000000..0ef3e2e2f8 --- /dev/null +++ "b/group05/810574652/src/com/github/PingPi357/coding2017/homework2/\350\257\264\346\230\216" @@ -0,0 +1 @@ +"litestruts"浣滀笟鏆傛椂瓒呭嚭鑳藉姏鑼冨洿鍐咃紝浠嶅湪鏌ユ壘璧勬枡瑙e喅涓...... \ No newline at end of file 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 = [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鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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; + } + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪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++){//鍥犲瓙;鑻鏄悎鏁帮紝鍒欏叾鎵鏈夊洜瀛愰兘涓嶈秴杩噑qrt(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 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬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; + } + + /** + * 鐢╯eperator 鎶婃暟缁 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,閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + 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 diff --git a/group06/1454385822/.classpath b/group06/1454385822/.classpath index fb5011632c..f68e6279c1 100644 --- a/group06/1454385822/.classpath +++ b/group06/1454385822/.classpath @@ -2,5 +2,7 @@ + + diff --git "a/group06/1454385822/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" "b/group06/1454385822/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" deleted file mode 100644 index 4399ea1b1d..0000000000 Binary files "a/group06/1454385822/CPU\357\274\214\345\206\205\345\255\230\357\274\214 \347\241\254\347\233\230\357\274\214\346\214\207\344\273\244\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273.docx" and /dev/null differ diff --git a/group06/1454385822/src/com/coding/basic/ArrayList.java b/group06/1454385822/src/com/coding/basic/homework_01/ArrayList.java similarity index 98% rename from group06/1454385822/src/com/coding/basic/ArrayList.java rename to group06/1454385822/src/com/coding/basic/homework_01/ArrayList.java index 571ca71f21..8524b89318 100644 --- a/group06/1454385822/src/com/coding/basic/ArrayList.java +++ b/group06/1454385822/src/com/coding/basic/homework_01/ArrayList.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package com.coding.basic.homework_01; import java.util.Arrays; diff --git a/group06/1454385822/src/com/coding/basic/BinaryTreeNode.java b/group06/1454385822/src/com/coding/basic/homework_01/BinaryTreeNode.java similarity index 97% rename from group06/1454385822/src/com/coding/basic/BinaryTreeNode.java rename to group06/1454385822/src/com/coding/basic/homework_01/BinaryTreeNode.java index 23cc02e579..b28b33b3d1 100644 --- a/group06/1454385822/src/com/coding/basic/BinaryTreeNode.java +++ b/group06/1454385822/src/com/coding/basic/homework_01/BinaryTreeNode.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package com.coding.basic.homework_01; public class BinaryTreeNode { diff --git a/group06/1454385822/src/com/coding/basic/homework_01/Iterator.java b/group06/1454385822/src/com/coding/basic/homework_01/Iterator.java new file mode 100644 index 0000000000..b683268cd9 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_01/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic.homework_01; + +public interface Iterator { + + public boolean hasNext(); + public Object next(); + +} diff --git a/group06/1454385822/src/com/coding/basic/LinkedList.java b/group06/1454385822/src/com/coding/basic/homework_01/LinkedList.java similarity index 99% rename from group06/1454385822/src/com/coding/basic/LinkedList.java rename to group06/1454385822/src/com/coding/basic/homework_01/LinkedList.java index 6197b9fb35..c05bca48ec 100644 --- a/group06/1454385822/src/com/coding/basic/LinkedList.java +++ b/group06/1454385822/src/com/coding/basic/homework_01/LinkedList.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package com.coding.basic.homework_01; public class LinkedList implements List{ diff --git a/group15/1500_369516660/src/com/arrayList/basic/List.java b/group06/1454385822/src/com/coding/basic/homework_01/List.java similarity index 62% rename from group15/1500_369516660/src/com/arrayList/basic/List.java rename to group06/1454385822/src/com/coding/basic/homework_01/List.java index 698e06eb42..df248690b7 100644 --- a/group15/1500_369516660/src/com/arrayList/basic/List.java +++ b/group06/1454385822/src/com/coding/basic/homework_01/List.java @@ -1,16 +1,11 @@ -package com.arrayList.basic; -/** - * 定义了增删查方法 - * @author Jodie - * - */ +package com.coding.basic.homework_01; + 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 String remove(Object o); public int size(); } diff --git a/group06/1454385822/src/com/coding/basic/Queue.java b/group06/1454385822/src/com/coding/basic/homework_01/Queue.java similarity index 94% rename from group06/1454385822/src/com/coding/basic/Queue.java rename to group06/1454385822/src/com/coding/basic/homework_01/Queue.java index c77317ef21..3909ea420a 100644 --- a/group06/1454385822/src/com/coding/basic/Queue.java +++ b/group06/1454385822/src/com/coding/basic/homework_01/Queue.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package com.coding.basic.homework_01; public class Queue { diff --git a/group06/1454385822/src/com/coding/basic/Stack.java b/group06/1454385822/src/com/coding/basic/homework_01/Stack.java similarity index 94% rename from group06/1454385822/src/com/coding/basic/Stack.java rename to group06/1454385822/src/com/coding/basic/homework_01/Stack.java index 2900430728..9b1d2c1439 100644 --- a/group06/1454385822/src/com/coding/basic/Stack.java +++ b/group06/1454385822/src/com/coding/basic/homework_01/Stack.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package com.coding.basic.homework_01; public class Stack { private ArrayList elementData = new ArrayList(); diff --git a/group06/1454385822/src/com/coding/basic/homework_02/array/ArrayUtil.java b/group06/1454385822/src/com/coding/basic/homework_02/array/ArrayUtil.java new file mode 100644 index 0000000000..36c733e7b9 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_02/array/ArrayUtil.java @@ -0,0 +1,218 @@ +package com.coding.basic.homework_02.array; + +import java.util.Arrays; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟锛屽璇ユ暟缁勭殑鍊艰繘琛岀疆鎹 + * 渚嬪锛歛 = [7, 9, 30, 3] , 缃崲鍚庝负[3, 30, 9, 7] + * 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负[4, 3, 30, 9, 7] + * @param origin + */ + public static void reverseArray(int [] origin){ + if(origin.length > 1){ // 鏁扮粍鐨勯暱搴﹀ぇ浜1缃崲鎵嶆湁鎰忎箟 + int tem; + for(int i = 0; i < origin.length / 2; i++){ + tem = origin[i]; + origin[i] = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = tem; + } + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細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 static int[] removeZero(int[] oldArray){ + int len = 0; + for(int i : oldArray){ + if(i == 0) + continue; + len++; + } + int[] result = new int[len]; + len = 0; + for(int i = 0; i < oldArray.length; i++){ + if(oldArray[i] == 0) + continue; + result[len++] = oldArray[i]; + } + return result; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍 a1,a2,鍒涘缓涓涓柊鐨勬暟缁刟3,浣垮緱a3鍖呭惈a1鍜宎2鐨勬墍鏈夊厓绱犲苟闆 + * 渚嬪a1 = [3,5,7,8] , a2 = [4,5,6,7] 鍒檃3涓篬3,4,5,6,7,8] + * @param array1 + * @param array2 + * @return + */ + public static int [] merge(int[] array1, int [] array2){ + if(array1.length == 0) + return array2; + if(array2.length == 0) + return array1; + if(array1.length == 0 && array2.length == 0) + return null; + int arrLen1 = 0; + int arrLen2 = 0; + int arrLen3 = 0; + int []result = new int[array1.length + array2.length]; + while(arrLen1 < array1.length || arrLen2 < array2.length){ + if(arrLen1 >= array1.length){ //鏁扮粍1宸茬粡娌′簡 + result[arrLen3++] = array2[arrLen2++]; + continue; + } + if(arrLen2 >= array2.length){ //鏁扮粍2宸茬粡娌′簡 + result[arrLen3++] = array1[arrLen1++]; + continue; + } + if(array1[arrLen1] > array2[arrLen2]){ + result[arrLen3++] = array2[arrLen2++]; + }else if(array1[arrLen1] < array2[arrLen2]){ + result[arrLen3++] = array1[arrLen1++]; + }else{ + result[arrLen3++] = array1[arrLen1++]; + arrLen2++; + } + } + result = Arrays.copyOf(result, arrLen3); + return result; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍oldArray鐨勫閲忚繘琛屾墿瀹癸紝鎵╁鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length * 2 + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪oldArray = {2,3,6}, size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负{2,3,6,0,0,0} + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size){ + int[] result = new int [size * 2]; + for(int i = 0; i < oldArray.length; i++){ + result[i] = oldArray[i]; + } + return result; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛 1锛 2锛 3锛 5锛 8锛 13锛 21...... ,缁欏畾涓涓渶澶у硷紝杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛歮ax = 15, 鍒欒繑鍥炵殑鏁扮粍搴旇涓簕1锛 1锛 2锛 3锛 5锛 8锛 13} + * max = 1, 鍒欒繑鍥炵┖鏁扮粍{} + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if(max == 1) + return new int[]{}; + int[] result = new int[10]; + int reLen = 2; + result[0] = result[1] = 1; + while(result[reLen - 1] + result[reLen -2] < max){ + if(reLen >= result.length) + result = grow(result, result.length); + result[reLen] = result[reLen - 1] + result[reLen -2]; + reLen++; + } + result = Arrays.copyOf(result, reLen); + + return result; + } + + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23锛岃繑鍥炵殑鏁扮粍涓簕2,3,5,7,11,13,17,19} + * @param max + * @return + */ + public static int[] getPrimes(int max){ + if(max <= 2){ + return null; + } + int[] result = new int[10]; + int len = 0; + boolean flag = true; + result[0] = 2; + if(max == 3) + return result; + len++; + for(int i = 3; i < max; i++){ + flag = true; + if(len >= result.length) + result = grow(result, result.length); + for(int j = 2; j < i; j++){ + if(i % j == 0){ + flag = false; + } + } + if(flag == true){ + result[len++] = i; + } + + } + result = Arrays.copyOf(result, len); + return result; + } + + /** + * 鎵璋"瀹屾暟"锛屾槸鎸囪繖涓暟鎭板ソ绛変簬浠栫殑鍥犲瓙涔嬪拰锛屼緥濡6 = 1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛岃繑鍥炰竴涓暟缁勶紝鏁扮粍涓槸灏忎簬max鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + int sum = 1; + int [] result = new int[10]; + int len = 0; + for(int i = 2;i < max; i++){ + sum = 1; + if(len >= result.length) + result = grow(result, result.length); + for(int j = 2; j < i; j++){ + if(i % j == 0) + sum += j; + } + if(sum == i) + result[len++] = sum; + } + result = Arrays.copyOf(result, len); + return result; + } + + + /** + * 鐢╯eperator 鎶婃暟缁刟rray缁欒繛鎺ヨ捣鏉 + * 渚嬪array = [3,8,9] ,seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param seperator + * @return + */ + public static String join(int[]array, String seperator){ + String result = new String(); + int len = 0; + result = new Integer(array[0]).toString(); + for(int i = 1; i < array.length; i++){ + result += seperator; + result += array[i]; + } + return result; + } + +} + + + + + + + + + diff --git a/group06/1454385822/src/com/coding/basic/homework_02/array/ArrayUtilTest.java b/group06/1454385822/src/com/coding/basic/homework_02/array/ArrayUtilTest.java new file mode 100644 index 0000000000..5c2c13083f --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_02/array/ArrayUtilTest.java @@ -0,0 +1,93 @@ +package com.coding.basic.homework_02.array; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + +public class ArrayUtilTest { + int[] array; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReverseArray() { + array = new int[]{7, 9, 30, 3}; +// array = new int[]{7, 9, 30, 3, 4}; + ArrayUtil.reverseArray(array); + Assert.assertArrayEquals(new int[]{3, 30, 9, 7}, array); +// Assert.assertArrayEquals(new int[]{4, 3, 30, 9, 7}, array); + } + + @Test + public void testRemoveZero(){ + array = new int[]{1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + Assert.assertArrayEquals(new int[]{1,3,4,5,6,6,5,4,7,6,7,5}, ArrayUtil.removeZero(array)); + } + + @Test + public void testMerge(){ + array = new int[]{3,5,7,8}; + int [] array2 = new int[]{4,5,6,7}; + Assert.assertArrayEquals(new int[]{3,4,5,6,7,8}, ArrayUtil.merge(array, array2)); + } + + @Test + public void testGrow(){ + array = new int[]{2, 3, 6}; + Assert.assertArrayEquals(new int[]{2, 3, 6, 0, 0, 0} ,ArrayUtil.grow(array, 3)); + } + + @Test + public void testFibonacci(){ + Assert.assertArrayEquals(new int[]{1, 1, 2, 3, 5, 8, 13}, ArrayUtil.fibonacci(15)); +// array = ArrayUtil.fibonacci(15); +// for(int i: array) +// System.out.println(i); + } + + + @Test + public void testPrimes(){ + Assert.assertArrayEquals(new int[]{2,3,5,7,11,13,17,19}, ArrayUtil.getPrimes(23)); +// array = ArrayUtil.getPrimes(23); +// for(int i : array){ +// System.out.print(i + " "); +// } + + } + + @Test + public void testPerfectNumbers(){ +// Assert.assertArrayEquals(new int[]{6}, ArrayUtil.getPerfectNumbers(7)); + array = ArrayUtil.getPerfectNumbers(100); + for(int i : array){ + System.out.print(i + " "); + } + } + + @Test + public void testJoin(){ + Assert.assertEquals("3-8-9", ArrayUtil.join(new int[]{3,8,9}, "-")); + } + +} + + + + + + + + + + + + + diff --git a/group06/1454385822/src/com/coding/basic/homework_02/litestruts/LoginAction.java b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/LoginAction.java new file mode 100644 index 0000000000..8156c45e43 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coding.basic.homework_02.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/group06/1454385822/src/com/coding/basic/homework_02/litestruts/LogoutAction.java b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/LogoutAction.java new file mode 100644 index 0000000000..e563673a89 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/LogoutAction.java @@ -0,0 +1,5 @@ +package com.coding.basic.homework_02.litestruts; + +public class LogoutAction { + +} diff --git a/group06/1454385822/src/com/coding/basic/homework_02/litestruts/Struts.java b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/Struts.java new file mode 100644 index 0000000000..5c6c57cf73 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/Struts.java @@ -0,0 +1,286 @@ +package com.coding.basic.homework_02.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + + private static String jspUrl = null; + private static String resultStatic = null; + + public static View runAction(String actionName, Map params) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, DocumentException{ + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鏍规嵁parameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + View view = new View(); + Element root = getRoot(); + Class clazz = getClazz(actionName, root); + Object obj = clazz.newInstance(); + + methodInvoke(clazz, obj, params); + String result = getExecuteInfo(clazz, obj); + setParams(clazz, obj, view); + getJsp(result, root, actionName); + view.setJsp(jspUrl); + + return view; + } + + /** + * 璇诲彇xml鏂囦欢鑾峰緱鏍硅妭鐐 + * @return + * @throws DocumentException + */ + private static Element getRoot() throws DocumentException{ + //step1:鍒涘缓SAXReader瀵硅薄 + SAXReader reader = new SAXReader(); + //step2:璇诲彇鏂囦欢 杞崲鎴怐ocument + Document document = reader.read("src/com/coding/basic/homework_02/litestruts/struts.xml"); + return document.getRootElement(); + } + + + /** + * 鏍规嵁缁欏畾鐨刟ctionName鎵惧埌瀵瑰簲鐨凜lass + * @return + * @throws ClassNotFoundException + */ + @SuppressWarnings("rawtypes") + private static Class getClazz(String actionName, Element node) throws ClassNotFoundException{ + + findClassNameByAttr(node, actionName); + if(resultStatic != null) + return Class.forName(resultStatic); + + throw new ClassNotFoundException(); + } + + /** + * 鏍规嵁resultName鎵惧埌瀵瑰簲鐨刯sp璺緞 + * @param resultName + * @param node + * @return + */ + @SuppressWarnings("unchecked") + private static void getJsp(String resultName, Element node, String actionName){ + + if(node.attributes() != null) + forEachAttr(node.attributes(), actionName, node, resultName); + + List listElement = node.elements(); + for(Element e : listElement) + getJsp(resultName, e, actionName); + + if(jspUrl != null) + return; + } + + /** + * 閬嶅巻褰撳墠缁撶偣鐨勫睘鎬 + * @param list + * @param actionName + * @param node + * @param resultName + */ + private static void forEachAttr(List list, String actionName, Element node, String resultName){ + List attrs = node.attributes(); + for(Attribute attr : attrs){ + if(resultName.equals(attr.getValue()) && !"".equals(node.getTextTrim())) + findJspByParentNode(actionName, node); + } + } + + /** + * 鏍规嵁璺熷畾鐨刟ction鎵惧埌瀵瑰簲鐨刯spUrl + * @param actionName + * @param node + */ + private static void findJspByParentNode(String actionName, Element node){ + Element parent = node.getParent(); + if(parent.attributes() != null){ + for(Attribute pattr : (List)parent.attributes()){ + if(actionName.equals(pattr.getValue())) + jspUrl = node.getTextTrim(); + } + } + } + + /** + * 鑾峰彇execute()鏂规硶杩愯鍚庣殑淇℃伅 + * @param clazz + * @param obj + * @return + * @throws NoSuchMethodException + * @throws SecurityException + * @throws IllegalAccessException + * @throws IllegalArgumentException + * @throws InvocationTargetException + */ + private static String getExecuteInfo(Class clazz, Object obj) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ + Method executeMethod = clazz.getMethod("execute", null); + + return (String)executeMethod.invoke(obj, null); + + } + + /** + * 灏嗙被涓殑getter淇℃伅鏀惧叆View + * @return + * @throws InvocationTargetException + * @throws IllegalArgumentException + * @throws IllegalAccessException + */ + public static View setParams(Class clazz, Object obj, View view) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{ + Map viewMap = getterAttr(clazz, obj); + view.setParameters(viewMap); + return view; + } + + /** + * 鎵惧嚭褰撳墠瀵硅薄鐨勬墍鏈塯etter鏂规硶锛屽皢淇℃伅鏀惧叆map涓 + * @param clazz + * @return + * @throws InvocationTargetException + * @throws IllegalArgumentException + * @throws IllegalAccessException + */ + private static Map getterAttr(Class clazz, Object obj) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{ + Map viewMap = new HashMap(); + //鑾峰彇鎵鏈夌殑getter鏂规硶 + Method[] methods = clazz.getDeclaredMethods(); + for(Method me : methods){ + if("get".equals(me.getName().substring(0, 3))) + viewMap.put(method2Attr(me), (String) me.invoke(obj, null)); + } + return viewMap; + } + + /** + * 灏嗘柟娉曞悕杞崲涓哄睘鎬у悕 + * @param method + * @return + */ + private static String method2Attr(Method method){ + StringBuilder builder = new StringBuilder(); + return builder.append(new Character(method.getName().charAt(3)).toString().toLowerCase()) + .append(method.getName().substring(4)).toString(); + } + + /** + * 璋冪敤setXXX缁欏睘鎬ц缃 + * @param params + * @throws SecurityException + * @throws NoSuchMethodException + * @throws InvocationTargetException + * @throws IllegalArgumentException + * @throws IllegalAccessException + */ + private static void methodInvoke(Class clazz, Object obj,Map params) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ + //灏嗗弬鏁板悕杞崲涓烘柟娉曞悕 + Map methodMap = methodMap(params); + + //灏嗘暟鎹畇et鍒板睘鎬т腑 + Iterator> it = methodMap.entrySet().iterator(); + while(it.hasNext()){ + Map.Entry entry = it.next(); + Method method = clazz.getMethod(entry.getKey(), String.class); + method.invoke(obj, entry.getValue()); + } + } + + /** + * 灏嗗睘鎬у悕杞崲涓烘柟娉曞悕涔嬪悗鏀惧叆鍒癿ap涓 + * @param params + * @return + */ + @SuppressWarnings("rawtypes") + private static Map methodMap(Map params){ + Map methodMap = new HashMap(); + Iterator> it = params.entrySet().iterator(); + while(it.hasNext()){ + Map.Entry entry = it.next(); + String attrName = entry.getKey(); + StringBuilder builder = new StringBuilder(); + builder.append("set").append(new Character(attrName.charAt(0)).toString().toUpperCase()).append(attrName.substring(1)); + methodMap.put(builder.toString(), entry.getValue()); + } + return methodMap; + } + + + /** + * 閬嶅巻缁撶偣鐨勫睘鎬ф壘鍒扮被鍚 + * @param attrList + * @return List attrList + * @throws ClassNotFoundException + */ + @SuppressWarnings("unchecked") + private static void findClassNameByAttr(Element node,String actionName) throws ClassNotFoundException{ + + if(!node.attributes().isEmpty()) + For2attr(actionName, node); + + List listElement = node.elements(); + for(Element e : listElement) + findClassNameByAttr(e, actionName); + + if(resultStatic != null) + return; + } + + /** + * 閬嶅巻灞炴ф壘鍒扮被鍚 + * @param actionName + * @param findAction + * @param node + */ + private static void For2attr(String actionName, Element node){ + boolean findAction = false; + for(Attribute attribute : (List)node.attributes()){ + + if(actionName.equals(attribute.getValue())){ + findAction = true; + break; + } + } + for(Attribute attribute : (List)node.attributes()){ + if(findAction == true && "class".equals(attribute.getName())){ + resultStatic = attribute.getValue(); + return; + } + } + } + + +} diff --git a/group06/1454385822/src/com/coding/basic/homework_02/litestruts/StrutsTest.java b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/StrutsTest.java new file mode 100644 index 0000000000..fc5f202c3b --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/StrutsTest.java @@ -0,0 +1,46 @@ +package com.coding.basic.homework_02.litestruts; + +import java.lang.reflect.InvocationTargetException; +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 ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, 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 ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, 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/group06/1454385822/src/com/coding/basic/homework_02/litestruts/View.java b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/View.java new file mode 100644 index 0000000000..8dfd610cb1 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coding.basic.homework_02.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/group06/1454385822/src/com/coding/basic/homework_02/litestruts/struts.xml b/group06/1454385822/src/com/coding/basic/homework_02/litestruts/struts.xml new file mode 100644 index 0000000000..53f1245121 --- /dev/null +++ b/group06/1454385822/src/com/coding/basic/homework_02/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/group06/2415980327/CodeSE01/src/com/pxshuo/basic/Iterator.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/Iterator.java similarity index 90% rename from group06/2415980327/CodeSE01/src/com/pxshuo/basic/Iterator.java rename to group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/Iterator.java index c41df9c249..2bb103d7d5 100644 --- a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/Iterator.java +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/Iterator.java @@ -1,4 +1,4 @@ -package com.pxshuo.basic; +package com.pxshuo.se01.basic; public interface Iterator { /** diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/List.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/List.java similarity index 96% rename from group06/2415980327/CodeSE01/src/com/pxshuo/basic/List.java rename to group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/List.java index 615ee9419b..e20daa44cd 100644 --- a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/List.java +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/List.java @@ -1,4 +1,4 @@ -package com.pxshuo.basic; +package com.pxshuo.se01.basic; /** * 鏁扮粍鐨勬帴鍙 * @author Pxshuo diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/TreeData.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/TreeData.java similarity index 91% rename from group06/2415980327/CodeSE01/src/com/pxshuo/basic/TreeData.java rename to group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/TreeData.java index f20a3765f1..25b8b1d733 100644 --- a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/TreeData.java +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/TreeData.java @@ -1,4 +1,4 @@ -package com.pxshuo.basic; +package com.pxshuo.se01.basic; public class TreeData implements Comparable { private int data; diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/ArrayList.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/ArrayList.java similarity index 96% rename from group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/ArrayList.java rename to group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/ArrayList.java index 68108e41a2..ae64aca982 100644 --- a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/ArrayList.java +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/ArrayList.java @@ -1,7 +1,7 @@ -package com.pxshuo.basic.impl; +package com.pxshuo.se01.basic.impl; -import com.pxshuo.basic.Iterator; -import com.pxshuo.basic.List; +import com.pxshuo.se01.basic.Iterator; +import com.pxshuo.se01.basic.List; /** * 瀹炵幇涓涓狝rrayList diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTree.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/BinaryTree.java similarity index 93% rename from group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTree.java rename to group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/BinaryTree.java index 7ea54eae78..69bee72af0 100644 --- a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTree.java +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/BinaryTree.java @@ -1,6 +1,6 @@ -package com.pxshuo.basic.impl; +package com.pxshuo.se01.basic.impl; -import com.pxshuo.basic.Iterator; +import com.pxshuo.se01.basic.Iterator; /** * 鎺掑簭浜屽弶鏍 diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTreeNode.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/BinaryTreeNode.java similarity index 98% rename from group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTreeNode.java rename to group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/BinaryTreeNode.java index d028dc5f48..57cf2da9e1 100644 --- a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/BinaryTreeNode.java +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/BinaryTreeNode.java @@ -1,4 +1,4 @@ -package com.pxshuo.basic.impl; +package com.pxshuo.se01.basic.impl; public class BinaryTreeNode { diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/LinkedList.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/LinkedList.java similarity index 96% rename from group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/LinkedList.java rename to group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/LinkedList.java index 7711e72a86..eeded51514 100644 --- a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/LinkedList.java +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/LinkedList.java @@ -1,9 +1,9 @@ -package com.pxshuo.basic.impl; +package com.pxshuo.se01.basic.impl; import java.time.Period; -import com.pxshuo.basic.Iterator; -import com.pxshuo.basic.List; +import com.pxshuo.se01.basic.Iterator; +import com.pxshuo.se01.basic.List; public class LinkedList implements List { diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Queue.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/Queue.java similarity index 94% rename from group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Queue.java rename to group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/Queue.java index b042f1d18c..5d9f4eee31 100644 --- a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Queue.java +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/Queue.java @@ -1,4 +1,4 @@ -package com.pxshuo.basic.impl; +package com.pxshuo.se01.basic.impl; /** * 鍩烘湰鏁版嵁缁撴瀯-闃熷垪 diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Stack.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/Stack.java similarity index 96% rename from group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Stack.java rename to group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/Stack.java index f3f837e117..e6237fa7e8 100644 --- a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/Stack.java +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/Stack.java @@ -1,4 +1,4 @@ -package com.pxshuo.basic.impl; +package com.pxshuo.se01.basic.impl; /** * 鍩烘湰鏁版嵁缁撴瀯-鏍 diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/package-info.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/package-info.java similarity index 82% rename from group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/package-info.java rename to group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/package-info.java index e87dca3a61..5d2c270af8 100644 --- a/group06/2415980327/CodeSE01/src/com/pxshuo/basic/impl/package-info.java +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se01/basic/impl/package-info.java @@ -11,4 +11,4 @@ * @author Pxshuo * */ -package com.pxshuo.basic.impl; \ No newline at end of file +package com.pxshuo.se01.basic.impl; \ No newline at end of file diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se02/array/ArrayUtil.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/array/ArrayUtil.java new file mode 100644 index 0000000000..e2d5a07eed --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/array/ArrayUtil.java @@ -0,0 +1,226 @@ +package com.pxshuo.se02.array; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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[] result = new int[origin.length]; + for(int i = 0;i resultList = new ArrayList<>(); + for(int i = 0,j = 0;i array2[j]){ + resultList.add(array2[j++]); + } + else if (array1[i] < array2[j]) { + resultList.add(array1[i++]); + } else { + j++; + } + } + + int[] result = new int[resultList.size()]; + for(int i = 0;i < resultList.size();i++){ + result[i] = resultList.get(i); + } + 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[] result = new int[oldArray.length+size]; + System.arraycopy(oldArray, 0, result, 0, oldArray.length); + return result; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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 resultList = new ArrayList<>(); + resultList.add(1); + resultList.add(1); + int fibon = 1,fibonPrev1 = 1,fibonPrev2 = 1; + while(fibon < max){ + fibonPrev2 = fibon; + fibon = fibonPrev1 + fibonPrev2; + fibonPrev1 = fibonPrev2; + resultList.add(fibon); + } + resultList.remove(resultList.size()-1); + + int[] result = new int[resultList.size()]; + for(int i = 0;i < resultList.size();i++){ + result[i] = resultList.get(i); + } + return result; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if (max < 2) { + return null; + }else if (max < 4) { + return new int[]{2}; + } + if (max < 6) { + return new int[]{2,3}; + } + List primeList = new ArrayList<>(); + primeList.add(2); + primeList.add(3); + boolean isPrime = true; + /** + * 闈2鍋舵暟鑲畾涓嶆槸绱犳暟 + */ + for(int i = 5;i < max; i = i + 2){ + isPrime =true; + for(int j = 1;j= max) { + primeList.remove(primeList.size() - 1); + } + + int[] result = new int[primeList.size()]; + for(int i = 0;i < primeList.size();i++){ + result[i] = primeList.get(i); + } + return result; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if (max < 7) { + return null; + } + List perfectList = new ArrayList<>(); + int sum = 1; + + for (int i = 2; i < max; i++) { + sum = 1; + for(int j = 2;j < i; j++){ + if (i%j == 0) { + sum += j; + } + } + if (sum == i) { + perfectList.add(sum); + } + } + + if (perfectList.get(perfectList.size() - 1) >= max) { + perfectList.remove(perfectList.size() - 1); + } + + int[] result = new int[perfectList.size()]; + for(int i = 0;i < perfectList.size();i++){ + result[i] = perfectList.get(i); + } + return result; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + String result = ""; + for (int i = 0; i < array.length; i++) { + result = result + array[i] + seperator; + } + if (result.endsWith(seperator)) { + result = result.substring(0, result.length()-1); + } + return result; + } + + +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/LoginAction.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/LoginAction.java new file mode 100644 index 0000000000..067a2875b1 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.pxshuo.se02.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/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/Struts.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/Struts.java new file mode 100644 index 0000000000..6801a838d7 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/Struts.java @@ -0,0 +1,151 @@ +package com.pxshuo.se02.litestruts; + +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + Element struts = strutsFactory("struts.xml"); + List actions = struts.elements(); + List resultRefs = new ArrayList<>();//action涓嬮潰鐨勭粨鏋滆烦杞 + String actionClass = "";//鑾峰彇action鐨勭被鍨 + for (Element element : actions) { + if (actionName.equals(element.attributeValue("name"))) { + actionClass = element.attributeValue("class"); + resultRefs = element.elements();//鑾峰彇缁撴灉璺宠浆 + } + } + + Iterator> iterator = parameters.entrySet().iterator();//瑙勮寖鍖栦紶閫掕繘鏉ョ殑鏁版嵁 + + try { + //鍙嶅皠瀹炰緥鍖栦竴涓璞 + Object action = Class.forName(actionClass).newInstance(); + while (iterator.hasNext()) {//鎵цset鏂规硶 + Map.Entry entry = (Map.Entry) iterator.next(); + Method setMethod = action.getClass().getDeclaredMethod("set" + Tools.upperFirst(entry.getKey()) + ,String.class);//杩欓噷鐨勭被鍨嬪间笉鐭ラ亾鎬庝箞鍙 + setMethod.invoke(action, entry.getValue()); + } + + //璋冪敤exectue + Method exectue = action.getClass().getDeclaredMethod("execute"); + String result = (String)exectue.invoke(action); + + //鐢熸垚杩斿洖鍒楄〃 + Map actionParam = new HashMap<>(); + Method[] methods = action.getClass().getDeclaredMethods(); + for (Method method : methods) { + if (method.getName().startsWith("get")) { + String methodName = Tools.lowerFirst(method.getName().substring(3)); + String methodValue = (String)method.invoke(action); + actionParam.put(methodName, methodValue); + } + } + + //杩斿洖鐨勮鍥 + View view = new View(); + view.setParameters(actionParam); + for(Element element:resultRefs){ + if (result.equals(element.attributeValue("name"))) { + view.setJsp((String)element.getData()); + } + } + + return view; + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + return null; + } + + public static Element strutsFactory(String filename) { + //璇婚厤缃枃浠 + InputStream inputStream = Struts.class.getResourceAsStream(filename); + SAXReader reader = new SAXReader(); + Document document = null; + + try { + document = reader.read(inputStream); + + Element struts = document.getRootElement(); + return struts; + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + + public static void main(String[] args) { + //strutsFactory("struts.xml"); + //runAction(null, null); + String actionName = "login"; + Element struts = strutsFactory("struts.xml"); + List actions = struts.elements(); + for (Element element : actions) { + if (actionName.equals(element.attributeValue("name"))) { + System.out.println(element.attributeValue("class")); + + for(Element element1:(List)element.elements()){ + System.out.println(element1.getData()); + } + } + } + } +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/StrutsTest.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/StrutsTest.java new file mode 100644 index 0000000000..7ed32c89f6 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.pxshuo.se02.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/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/Tools.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/Tools.java new file mode 100644 index 0000000000..66acb26157 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/Tools.java @@ -0,0 +1,33 @@ +package com.pxshuo.se02.litestruts; + +public class Tools { + /** + * 灏嗙涓涓瓧姣嶈浆涓哄ぇ鍐 + * @param string + * @return + */ + public static String upperFirst(String string) { + if (string == null || ("").equals(string)) { + return ""; + } + string = string.substring(0,1).toUpperCase() + string.substring(1); + return string; + } + + /** + * 灏嗙涓涓瓧姣嶈浆涓哄ぇ鍐 + * @param string + * @return + */ + public static String lowerFirst(String string) { + if (string == null || ("").equals(string)) { + return ""; + } + string = string.substring(0,1).toLowerCase() + string.substring(1); + return string; + } + + public static void main(String[] args) { + upperFirst("1ame"); + } +} diff --git a/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/View.java b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/View.java new file mode 100644 index 0000000000..5f017e9a8a --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/View.java @@ -0,0 +1,23 @@ +package com.pxshuo.se02.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/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/struts.xml b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/litestruts/struts.xml new file mode 100644 index 0000000000..227b855f45 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/se02/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/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java b/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java index c40ddac87d..9737d269e8 100644 --- a/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java +++ b/group06/2415980327/CodeSE01/src/com/pxshuo/test/Test.java @@ -1,13 +1,13 @@ package com.pxshuo.test; -import com.pxshuo.basic.Iterator; -import com.pxshuo.basic.TreeData; -import com.pxshuo.basic.impl.ArrayList; -import com.pxshuo.basic.impl.BinaryTree; -import com.pxshuo.basic.impl.LinkedList; -import com.pxshuo.basic.impl.Queue; -import com.pxshuo.basic.impl.Stack; +import com.pxshuo.se01.basic.Iterator; +import com.pxshuo.se01.basic.TreeData; +import com.pxshuo.se01.basic.impl.ArrayList; +import com.pxshuo.se01.basic.impl.BinaryTree; +import com.pxshuo.se01.basic.impl.LinkedList; +import com.pxshuo.se01.basic.impl.Queue; +import com.pxshuo.se01.basic.impl.Stack; public class Test { public static void main(String[] args) { diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/ArrayListTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/ArrayListTest.java similarity index 89% rename from group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/ArrayListTest.java rename to group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/ArrayListTest.java index 4249574817..26b7b977e6 100644 --- a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/ArrayListTest.java +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/ArrayListTest.java @@ -1,9 +1,9 @@ -package test.com.pxshuo.basic.impl; +package test.com.pxshuo.se01.basic.impl; import org.junit.Assert; import org.junit.Test; -import com.pxshuo.basic.impl.ArrayList; +import com.pxshuo.se01.basic.impl.ArrayList; public class ArrayListTest { ArrayList object = new ArrayList(); diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/BinaryTreeTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/BinaryTreeTest.java similarity index 81% rename from group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/BinaryTreeTest.java rename to group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/BinaryTreeTest.java index a9d67f7ba1..dc53c5988d 100644 --- a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/BinaryTreeTest.java +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/BinaryTreeTest.java @@ -1,10 +1,10 @@ -package test.com.pxshuo.basic.impl; +package test.com.pxshuo.se01.basic.impl; import org.junit.Assert; import org.junit.Test; -import com.pxshuo.basic.TreeData; -import com.pxshuo.basic.impl.BinaryTree; +import com.pxshuo.se01.basic.TreeData; +import com.pxshuo.se01.basic.impl.BinaryTree; public class BinaryTreeTest { BinaryTree object = new BinaryTree(); diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/LinkedListTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/LinkedListTest.java similarity index 84% rename from group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/LinkedListTest.java rename to group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/LinkedListTest.java index 72fd2c49f1..21fea9680f 100644 --- a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/LinkedListTest.java +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/LinkedListTest.java @@ -1,10 +1,10 @@ -package test.com.pxshuo.basic.impl; +package test.com.pxshuo.se01.basic.impl; import org.junit.Assert; import org.junit.Test; -import com.pxshuo.basic.impl.ArrayList; -import com.pxshuo.basic.impl.LinkedList; +import com.pxshuo.se01.basic.impl.ArrayList; +import com.pxshuo.se01.basic.impl.LinkedList; public class LinkedListTest { LinkedList object = new LinkedList(); diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/QueueTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/QueueTest.java similarity index 85% rename from group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/QueueTest.java rename to group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/QueueTest.java index 4f2b5735e4..edd3bfd73a 100644 --- a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/QueueTest.java +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/QueueTest.java @@ -1,9 +1,9 @@ -package test.com.pxshuo.basic.impl; +package test.com.pxshuo.se01.basic.impl; import org.junit.Assert; import org.junit.Test; -import com.pxshuo.basic.impl.Queue; +import com.pxshuo.se01.basic.impl.Queue; public class QueueTest { public Queue object = new Queue(); diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/StackTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/StackTest.java similarity index 80% rename from group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/StackTest.java rename to group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/StackTest.java index df1e254595..fc3f450e01 100644 --- a/group06/2415980327/CodeSE01/src/test/com/pxshuo/basic/impl/StackTest.java +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se01/basic/impl/StackTest.java @@ -1,10 +1,10 @@ -package test.com.pxshuo.basic.impl; +package test.com.pxshuo.se01.basic.impl; import org.junit.Assert; import org.junit.Test; -import com.pxshuo.basic.impl.Queue; -import com.pxshuo.basic.impl.Stack; +import com.pxshuo.se01.basic.impl.Queue; +import com.pxshuo.se01.basic.impl.Stack; public class StackTest { public Stack object = new Stack(); diff --git a/group06/2415980327/CodeSE01/src/test/com/pxshuo/se02/array/ArrayUntilTest.java b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se02/array/ArrayUntilTest.java new file mode 100644 index 0000000000..6a6e3d4229 --- /dev/null +++ b/group06/2415980327/CodeSE01/src/test/com/pxshuo/se02/array/ArrayUntilTest.java @@ -0,0 +1,72 @@ +package test.com.pxshuo.se02.array; + +import org.junit.Assert; +import org.junit.Test; + +import com.pxshuo.se02.array.*; + +public class ArrayUntilTest { + private ArrayUtil obj = new ArrayUtil(); + + @Test + public void reverseArrayTest() { + int[] origin = {3, 30, 9,7}; + obj.reverseArray(origin); + int[] expect = {7,9,30,3}; + Assert.assertArrayEquals(expect, origin); + } + + @Test + public void removeZeroTest() { + int[] origin = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] actual = obj.removeZero(origin); + int[] expect = {1,3,4,5,6,6,5,4,7,6,7,5}; + Assert.assertArrayEquals(expect, actual); + } + + @Test + public void mergeTest() { + int[] array1 = {3, 5, 7,8}; + int[] array2 = {4, 5, 6,7}; + int[] actual = obj.merge(array2,array1); + int[] expect = {3,4,5,6,7,8}; + Assert.assertArrayEquals(expect, actual); + } + + @Test + public void growTest() { + int[] origin = {2,3,6}; + int[] actual = obj.grow(origin,3); + int[] expect = {2,3,6,0,0,0}; + Assert.assertArrayEquals(expect, actual); + } + + @Test + public void fibonacciTest() { + int[] actual = obj.fibonacci(15); + int[] expect = {1,1,2,3,5,8,13}; + Assert.assertArrayEquals(expect, actual); + } + + @Test + public void getPrimesTest() { + int[] actual = obj.getPrimes(23); + int[] expect = {2,3,5,7,11,13,17,19}; + Assert.assertArrayEquals(expect, actual); + } + + @Test + public void getPerfectNumbersTest() { + int[] actual = obj.getPerfectNumbers(1000); + int[] expect = {6,28,496}; + Assert.assertArrayEquals(expect, actual); + } + + @Test + public void joinTest() { + int[] origin = {3,8,9}; + String actual = obj.join(origin, "-"); + String expect = "3-8-9"; + Assert.assertEquals(expect, actual); + } +} diff --git a/group06/263050006/article.txt b/group06/263050006/article.txt index 1bec070839..2eaa5b792f 100644 --- a/group06/263050006/article.txt +++ b/group06/263050006/article.txt @@ -1,2 +1,2 @@ -CPU、内存、硬盘、指令以及他们之间的关系 +锘緾PU銆佸唴瀛樸佺‖鐩樸佹寚浠や互鍙婁粬浠箣闂寸殑鍏崇郴 https://zhuanlan.zhihu.com/p/25442061 \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/array/ArrayUtil.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/array/ArrayUtil.java new file mode 100644 index 0000000000..b807a9b321 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/array/ArrayUtil.java @@ -0,0 +1,197 @@ +package com.github.chaoswang.learning.java.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.TreeSet; + + +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 static int[] reverseArray(int[] origin){ + int length = origin.length; + for(int i=0;i list = new ArrayList(); + for(int value : oldArray){ + if(value != 0){ + list.add(value); + } + } + return returnByIntArray(list); + } + + /** + * 给定两个已经排序好的整形数组, 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 static int[] merge(int[] array1, int[] array2){ + TreeSet ts1 = new TreeSet(Arrays.asList(convertToIntegerArray(array1))); + TreeSet ts2 = new TreeSet(Arrays.asList(convertToIntegerArray(array2))); + ts2.addAll(ts1); + return returnByIntArray(ts2); + } + + private static Integer[] convertToIntegerArray(int[] array){ + Integer[] returnArray = new Integer[array.length]; + for(int i=0;i collection){ + int[] returnArray = new int[collection.size()]; + int i = 0; + for(Iterator it = collection.iterator(); it.hasNext();){ + returnArray[i] = it.next(); + i++; + } + return returnArray; + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + int[] returnArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, returnArray, 0, oldArray.length); + return returnArray; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if(max <= 1){ + return new int[0]; + } + Integer[] init = {1,1}; + LinkedList result = new LinkedList(Arrays.asList(init)); + for(int tmp = -1; tmp <= max;){ + tmp = generateFibonacci(result); + } + result.removeLast(); + return returnByIntArray(result); + } + + private static int generateFibonacci(LinkedList result){ + int a = result.getLast(); + int b = result.get(result.size()-2); + result.add(a + b); + return result.getLast(); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + List list = new LinkedList(); + for(int i=2;i list = new LinkedList(); + for(int i=2;i<=max;i++){ + if(isPerfectNumber(i)){ + list.add(i); + } + } + return returnByIntArray(list); + } + + private static boolean isPerfectNumber(int number) { + int sum = 0; + for(int i=1;i action_class = new HashMap(); + private static Map> action_result = new HashMap>(); + + + public static View runAction(String actionName, + Map parameters) { + + /* + * 0. 读取配置文件struts.xml + */ + parseConfig(); + + /* + * + * 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + * 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 ("name"="test" , + * "password"="1234") , 那就应该调用 setName和setPassword方法 + */ + Class actionClass = null; + Object actionInstance = null; + try { + actionClass = Class.forName(action_class.get(actionName)); + actionInstance = actionClass.newInstance(); + for (Entry entry : parameters.entrySet()) { + String key = (String) entry.getKey(); + String value = (String) entry.getValue(); + actionClass.getMethod( + "set" + key.substring(0, 1).toUpperCase() + + key.substring(1), String.class).invoke( + actionInstance, value); + } + } catch (Exception e) { + e.printStackTrace(); + } + /* + * 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + */ + + String returnStr = null; + try { + returnStr = (String)actionClass.getMethod("execute", new Class[0]).invoke(actionInstance, new Object[0]); + } catch (Exception e) { + e.printStackTrace(); + } + + /* + * 3. 通过反射找到对象的所有getter方法(例如 getMessage), 通过反射来调用, 把值和属性形成一个HashMap , 例如 + * {"message": "登录成功"} , 放到View对象的parameters + */ + Map params = new HashMap(); + Method[] methods = actionClass.getDeclaredMethods(); + try { + for(Method method : methods){ + if(method.getModifiers() == Modifier.PUBLIC && method.getName().startsWith("get")){ + String key = method.getName().substring(3,4).toLowerCase() + method.getName().substring(4); + String value = (String)method.invoke(actionInstance, new Object[0]); + params.put(key, value); + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + + /* + * 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + * 放到View对象的jsp字段中。 + */ + View view = new View(); + view.setParameters(params); + Map results = action_result.get(actionName); + view.setJsp(results.get(returnStr)); + return view; + } + + private static void parseConfig() { + String xmlpath = Struts.class.getResource("struts.xml").getFile(); + try { + xmlpath = URLDecoder.decode(xmlpath, "utf-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + SAXBuilder builder = new SAXBuilder(false); + try { + Document doc = builder.build(xmlpath); + Element struts = doc.getRootElement(); + List actionlist = struts.getChildren("action"); + for (Iterator iter = actionlist.iterator(); iter.hasNext();) { + Element action = (Element) iter.next(); + String actionName = action.getAttributeValue("name"); + String actionClass = action.getAttributeValue("class"); + action_class.put(actionName, actionClass); + List resultlist = action.getChildren("result"); + Map result_value = new HashMap(); + for (Iterator iter1 = resultlist.iterator(); iter1.hasNext();) { + Element result = (Element) iter1.next(); + String resultName = result.getAttributeValue("name"); + String resultView = result.getTextTrim(); + result_value.put(resultName, resultView); + } + action_result.put(actionName, result_value); + } + } catch (JDOMException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + System.out.println(action_class); + System.out.println(action_result); + } + + public static void main(String[] args) { + Struts.parseConfig(); + } +} \ No newline at end of file diff --git a/group06/263050006/src/main/java/com/github/chaoswang/learning/java/litestruts/View.java b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/litestruts/View.java new file mode 100644 index 0000000000..ae57b41d32 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/litestruts/View.java @@ -0,0 +1,23 @@ +package com.github.chaoswang.learning.java.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/263050006/src/main/java/com/github/chaoswang/learning/java/litestruts/struts.xml b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/litestruts/struts.xml new file mode 100644 index 0000000000..10671eac25 --- /dev/null +++ b/group06/263050006/src/main/java/com/github/chaoswang/learning/java/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/group06/263050006/src/test/java/com/github/chaoswang/learning/java/array/ArrayUtilTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/array/ArrayUtilTest.java new file mode 100644 index 0000000000..4ebff76195 --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/array/ArrayUtilTest.java @@ -0,0 +1,134 @@ +package com.github.chaoswang.learning.java.array; + +import org.junit.Assert; +import org.junit.Test; + +public class ArrayUtilTest { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + @Test + public void testReverseArray() { + int a[] = {7, 9, 30, 3}; + int reversedA[] = {3, 30, 9, 7}; + int b[] = {7, 9, 30, 3, 4}; + int reversedB[] = {4, 3, 30, 9, 7}; + Assert.assertArrayEquals(reversedA, ArrayUtil.reverseArray(a)); + Assert.assertArrayEquals(reversedB, ArrayUtil.reverseArray(b)); + } + + /** + * 现在有如下的一个数组: 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 + */ + @Test + public void testRemoveZero() { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int newArr[]={1,3,4,5,6,6,5,4,7,6,7,5}; + Assert.assertArrayEquals(newArr, ArrayUtil.removeZero(oldArr)); + } + + /** + * 给定两个已经排序好的整形数组, 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 + */ + @Test + public void testMerge() { + int a1[] = {3, 5, 7,8}; + int a2[] = {4, 5, 6,7}; + int a3[] = {3,4,5,6,7,8}; + Assert.assertArrayEquals(a3, ArrayUtil.merge(a1, a2)); + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + @Test + public void testGrow() { + int oldArray[] = {2,3,6}; + int newArray[] = {2,3,6,0,0,0}; + Assert.assertArrayEquals(newArray, ArrayUtil.grow(oldArray, 3)); + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + @Test + public void testFibonacci() { + int returnA[] = {1,1,2,3,5,8,13}; + int returnB[] = {}; + int returnC[] = {1,1,2,3,5,8,13,21,34}; + int returnD[] = {1,1,2}; + Assert.assertArrayEquals(returnA, ArrayUtil.fibonacci(15)); + Assert.assertArrayEquals(returnB, ArrayUtil.fibonacci(1)); + Assert.assertArrayEquals(returnC, ArrayUtil.fibonacci(34)); + Assert.assertArrayEquals(returnD, ArrayUtil.fibonacci(2)); + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + @Test + public void testGetPrimes() { + int returnC[] = {2,3,5,7,11,13,17,19}; + int returnD[] = {2,3,5,7,11}; + Assert.assertArrayEquals(returnC, ArrayUtil.getPrimes(23)); + Assert.assertArrayEquals(returnD, ArrayUtil.getPrimes(12)); + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + @Test + public void testGetPerfectNumbers() { + int returnC[] = {6,28,496}; + int returnD[] = {6,28,496,8128}; + int returnE[] = {6,28,496,8128,33550336}; + Assert.assertArrayEquals(returnC, ArrayUtil.getPerfectNumbers(496)); + Assert.assertArrayEquals(returnD, ArrayUtil.getPerfectNumbers(8129)); + Assert.assertArrayEquals(returnE, ArrayUtil.getPerfectNumbers(33550337)); + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + @Test + public void testJoin() { + int array[] = {3,8,9}; + Assert.assertEquals("3-8-9", ArrayUtil.join(array,"-")); + Assert.assertEquals("3@$8@$9", ArrayUtil.join(array,"@$")); + } + +} diff --git a/group06/263050006/src/test/java/com/github/chaoswang/learning/java/litestruts/StrutsTest.java b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/litestruts/StrutsTest.java new file mode 100644 index 0000000000..834a4dd390 --- /dev/null +++ b/group06/263050006/src/test/java/com/github/chaoswang/learning/java/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +package com.github.chaoswang.learning.java.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")); + } +} \ No newline at end of file diff --git a/group06/799237637/secondhomework/src/com/liteStructs/LoginAction.java b/group06/799237637/secondhomework/src/com/liteStructs/LoginAction.java new file mode 100644 index 0000000000..12f5362069 --- /dev/null +++ b/group06/799237637/secondhomework/src/com/liteStructs/LoginAction.java @@ -0,0 +1,5 @@ +package com.liteStructs; + +public class LoginAction { + +} diff --git a/group06/799237637/secondhomework/src/com/liteStructs/Structs.java b/group06/799237637/secondhomework/src/com/liteStructs/Structs.java new file mode 100644 index 0000000000..ab36173eaf --- /dev/null +++ b/group06/799237637/secondhomework/src/com/liteStructs/Structs.java @@ -0,0 +1,5 @@ +package com.liteStructs; + +public class Structs { + +} diff --git a/group06/799237637/secondhomework/src/com/liteStructs/View.java b/group06/799237637/secondhomework/src/com/liteStructs/View.java new file mode 100644 index 0000000000..4942fc8c6c --- /dev/null +++ b/group06/799237637/secondhomework/src/com/liteStructs/View.java @@ -0,0 +1,5 @@ +package com.liteStructs; + +public class View { + +} diff --git a/group06/799237637/secondhomework/src/com/liteStructs/struts.xml b/group06/799237637/secondhomework/src/com/liteStructs/struts.xml new file mode 100644 index 0000000000..cc909972cd --- /dev/null +++ b/group06/799237637/secondhomework/src/com/liteStructs/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/group06/799237637/secondhomework/src/secondhomework/MyArrayUtil.java b/group06/799237637/secondhomework/src/secondhomework/MyArrayUtil.java new file mode 100644 index 0000000000..4b5c626cd2 --- /dev/null +++ b/group06/799237637/secondhomework/src/secondhomework/MyArrayUtil.java @@ -0,0 +1,214 @@ +package secondhomework; + +import java.util.ArrayList; +import java.util.Arrays; + +/* + * 实现ArrayUtil + */ +@SuppressWarnings("all") +public class MyArrayUtil { + /** + * 给定一个整形数组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 size =origin.length; + + for(int i=0;i + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group06/949319266/homework/.classpath b/group06/949319266/homework/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group06/949319266/homework/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group06/949319266/homework/.gitignore b/group06/949319266/homework/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group06/949319266/homework/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group06/949319266/homework/.project b/group06/949319266/homework/.project new file mode 100644 index 0000000000..b4bd3f32d4 --- /dev/null +++ b/group06/949319266/homework/.project @@ -0,0 +1,17 @@ + + + homework + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group06/949319266/homework/.settings/org.eclipse.core.resources.prefs b/group06/949319266/homework/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..8822690b9a --- /dev/null +++ b/group06/949319266/homework/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/cn/ecust/Iitestruts/LoginAction.java=UTF-8 +encoding//src/cn/ecust/Iitestruts/Struts.java=UTF-8 +encoding//src/cn/ecust/Iitestruts/StrutsTest.java=UTF-8 diff --git a/group06/949319266/homework/.settings/org.eclipse.jdt.core.prefs b/group06/949319266/homework/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group06/949319266/homework/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +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/group06/949319266/homework/src/cn/ecust/Array/ArrayUtil.java b/group06/949319266/homework/src/cn/ecust/Array/ArrayUtil.java new file mode 100644 index 0000000000..8965b7a1aa --- /dev/null +++ b/group06/949319266/homework/src/cn/ecust/Array/ArrayUtil.java @@ -0,0 +1,201 @@ +package cn.ecust.Array; + +import java.util.ArrayList; +import java.util.Arrays; + +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 tem = 0; + for(int i = 0; i < origin.length/2; i++) { + tem = origin[i]; + origin[i] = origin[origin.length-1-i]; + origin[origin.length-1-i] = tem; + } + + } + + /** + * 现在有如下的一个数组: 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){ + ArrayList arr = new ArrayList(); + for(int i = 0; i < oldArray.length; i++) { + if(oldArray[i] != 0) { + arr.add(oldArray[i]); + } + } + oldArray = null; + for(int i = 0; i= array1.length){ //数组1已经遍历结束 + array3[c++] = array2[b++]; + continue; + } + + if(b >= array2.length){ //数组2已经遍历结束 + array3[c++] = array1[a++]; + continue; + } + + if(array1[a] > array2[b]){ + array3[c++] = array2[b++]; + }else if(array1[a] < array2[b]){ + array3[c++] = array1[a++]; + }else{ + array3[c++] = array1[a++]; + b++; + } + } + array3 = Arrays.copyOf(array3, c); + 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]; + 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){ + ArrayList arr = new ArrayList(); + int i = 1; + int j = 1; + arr.add(i);arr.add(j); + int m=i+j; + while(m arr = new ArrayList(); + for(int i=0;i= result.length) + result = grow(result, result.length); + for(int j = 2; j < i; j++){ + if(i % j == 0) + sum += j; + } + if(sum == i) + result[len++] = sum; + } + result = Arrays.copyOf(result, len); + return result; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + String result = new String(); + int len = 0; + result = new Integer(array[0]).toString(); + for(int i = 1; i < array.length; i++){ + result += seperator; + result += array[i]; + } + return result; + } + +} diff --git a/group06/949319266/homework/src/cn/ecust/Iitestruts/LoginAction.java b/group06/949319266/homework/src/cn/ecust/Iitestruts/LoginAction.java new file mode 100644 index 0000000000..004f428e2b --- /dev/null +++ b/group06/949319266/homework/src/cn/ecust/Iitestruts/LoginAction.java @@ -0,0 +1,39 @@ +package cn.ecust.Iitestruts; + +/** + * 鏉╂瑦妲告稉锟芥稉顏嗘暏閺夈儱鐫嶇粈铏规瑜版洜娈戞稉姘缁紮绱 閸忔湹鑵戦惃鍕暏閹村嘲鎮曢崪灞界槕閻線鍏橀弰顖溾栫紓鏍垳閻ㄥ嫨锟斤拷 + * @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/group06/949319266/homework/src/cn/ecust/Iitestruts/Struts.java b/group06/949319266/homework/src/cn/ecust/Iitestruts/Struts.java new file mode 100644 index 0000000000..27c36c6902 --- /dev/null +++ b/group06/949319266/homework/src/cn/ecust/Iitestruts/Struts.java @@ -0,0 +1,34 @@ +package cn.ecust.Iitestruts; + +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 鐠囪褰囬柊宥囩枂閺傚洣娆truts.xml + + 1. 閺嶈宓乤ctionName閹垫儳鍩岄惄绋款嚠鎼存梻娈慶lass 閿涳拷 娓氬顩oginAction, 闁俺绻冮崣宥呯殸鐎圭偘绶ラ崠鏍电礄閸掓稑缂撶电钖勯敍锟 + 閹圭晝arameters娑擃厾娈戦弫鐗堝祦閿涘矁鐨熼悽銊ヮ嚠鐠烇紕娈憇etter閺傝纭堕敍锟 娓氬顩arameters娑擃厾娈戦弫鐗堝祦閺勶拷 + ("name"="test" , "password"="1234") , + 闁絽姘ㄦ惔鏃囶嚉鐠嬪啰鏁 setName閸滃etPassword閺傝纭 + + 2. 闁俺绻冮崣宥呯殸鐠嬪啰鏁ょ电钖勯惃鍒ectue 閺傝纭堕敍锟 楠炴儼骞忓妤勭箲閸ョ偛锟界》绱濇笟瀣洤"success" + + 3. 闁俺绻冮崣宥呯殸閹垫儳鍩岀电钖勯惃鍕閺堝’etter閺傝纭堕敍鍫滅伐婵★拷 getMessage閿涳拷, + 闁俺绻冮崣宥呯殸閺夈儴鐨熼悽顭掔礉 閹跺﹤锟界厧鎷扮仦鐐达拷褍鑸伴幋鎰娑撶嫥ashMap , 娓氬顩 {"message": "閻ц缍嶉幋鎰"} , + 閺鎯у煂View鐎电钖勯惃鍒綼rameters + + 4. 閺嶈宓乻truts.xml娑擃厾娈 闁板秶鐤,娴犮儱寮積xecute閻ㄥ嫯绻戦崶鐐诧拷纭风礉 绾喖鐣鹃崫顏冪娑撶尐sp閿涳拷 + 閺鎯у煂View鐎电钖勯惃鍒痵p鐎涙顔屾稉顓滐拷锟 + + */ + + return null; + } + +} diff --git a/group06/949319266/homework/src/cn/ecust/Iitestruts/StrutsTest.java b/group06/949319266/homework/src/cn/ecust/Iitestruts/StrutsTest.java new file mode 100644 index 0000000000..10c0d5c6db --- /dev/null +++ b/group06/949319266/homework/src/cn/ecust/Iitestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package cn.ecust.Iitestruts; + +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/group06/949319266/homework/src/cn/ecust/Iitestruts/View.java b/group06/949319266/homework/src/cn/ecust/Iitestruts/View.java new file mode 100644 index 0000000000..dd652dad20 --- /dev/null +++ b/group06/949319266/homework/src/cn/ecust/Iitestruts/View.java @@ -0,0 +1,23 @@ +package cn.ecust.Iitestruts; + +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/group06/949319266/homework/src/cn/ecust/Iitestruts/struts.xml b/group06/949319266/homework/src/cn/ecust/Iitestruts/struts.xml new file mode 100644 index 0000000000..a6cfe43e6c --- /dev/null +++ b/group06/949319266/homework/src/cn/ecust/Iitestruts/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/group06/949319266/homework/src/cn/ecust/Test/ArrayUtilTest.java b/group06/949319266/homework/src/cn/ecust/Test/ArrayUtilTest.java new file mode 100644 index 0000000000..0ac38f0256 --- /dev/null +++ b/group06/949319266/homework/src/cn/ecust/Test/ArrayUtilTest.java @@ -0,0 +1,26 @@ +package cn.ecust.Test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import cn.ecust.Array.ArrayUtil; + +public class ArrayUtilTest { + + @Test + public void test() { + int[] a = {7, 9, 30, 0, 0, 0, 3, 4}; + int[] b = {3,4,5,6,7,8}; + ArrayUtil au = new ArrayUtil(); + au.reverseArray(a); + //au.removeZero(a); + au.merge(a, b); + au.grow(a, 3); + au.fibonacci(25); + au.getPrimes(25); + au.getPerfectNumbers(25); + au.join(a, "-"); + } + +} diff --git "a/group06/949319266/homework/src/\346\226\207\347\253\2402.txt" "b/group06/949319266/homework/src/\346\226\207\347\253\2402.txt" new file mode 100644 index 0000000000..89823c8f5a --- /dev/null +++ "b/group06/949319266/homework/src/\346\226\207\347\253\2402.txt" @@ -0,0 +1 @@ +http://blog.sina.com.cn/s/blog_c20b18280102x3fi.html \ No newline at end of file diff --git a/group07/1058267830/week2/.classpath b/group07/1058267830/week2/.classpath new file mode 100644 index 0000000000..b258db3060 --- /dev/null +++ b/group07/1058267830/week2/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group07/1058267830/week2/.project b/group07/1058267830/week2/.project new file mode 100644 index 0000000000..5b537d7055 --- /dev/null +++ b/group07/1058267830/week2/.project @@ -0,0 +1,17 @@ + + + week2 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group07/1058267830/week2/.settings/org.eclipse.jdt.core.prefs b/group07/1058267830/week2/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group07/1058267830/week2/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +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.7 diff --git a/group07/1058267830/week2/bin/com/coderising/array/ArrayTest.class b/group07/1058267830/week2/bin/com/coderising/array/ArrayTest.class new file mode 100644 index 0000000000..b0d101658f Binary files /dev/null and b/group07/1058267830/week2/bin/com/coderising/array/ArrayTest.class differ diff --git a/group07/1058267830/week2/bin/com/coderising/array/ArrayUtil.class b/group07/1058267830/week2/bin/com/coderising/array/ArrayUtil.class new file mode 100644 index 0000000000..515999a911 Binary files /dev/null and b/group07/1058267830/week2/bin/com/coderising/array/ArrayUtil.class differ diff --git a/group07/1058267830/week2/bin/com/coderising/litestruts/Demo.class b/group07/1058267830/week2/bin/com/coderising/litestruts/Demo.class new file mode 100644 index 0000000000..7d37f20c22 Binary files /dev/null and b/group07/1058267830/week2/bin/com/coderising/litestruts/Demo.class differ diff --git a/group07/1058267830/week2/bin/com/coderising/litestruts/LoginAction.class b/group07/1058267830/week2/bin/com/coderising/litestruts/LoginAction.class new file mode 100644 index 0000000000..794c66e2c7 Binary files /dev/null and b/group07/1058267830/week2/bin/com/coderising/litestruts/LoginAction.class differ diff --git a/group07/1058267830/week2/bin/com/coderising/litestruts/Struts.class b/group07/1058267830/week2/bin/com/coderising/litestruts/Struts.class new file mode 100644 index 0000000000..2e2b15c45f Binary files /dev/null and b/group07/1058267830/week2/bin/com/coderising/litestruts/Struts.class differ diff --git a/group07/1058267830/week2/bin/com/coderising/litestruts/StrutsTest.class b/group07/1058267830/week2/bin/com/coderising/litestruts/StrutsTest.class new file mode 100644 index 0000000000..74b68390bc Binary files /dev/null and b/group07/1058267830/week2/bin/com/coderising/litestruts/StrutsTest.class differ diff --git a/group07/1058267830/week2/bin/com/coderising/litestruts/View.class b/group07/1058267830/week2/bin/com/coderising/litestruts/View.class new file mode 100644 index 0000000000..c28f317aa6 Binary files /dev/null and b/group07/1058267830/week2/bin/com/coderising/litestruts/View.class differ diff --git a/group07/1058267830/week2/bin/com/coderising/litestruts/struts.xml b/group07/1058267830/week2/bin/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group07/1058267830/week2/bin/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 diff --git a/group07/1058267830/week2/lib/dom4j-1.6.1.jar b/group07/1058267830/week2/lib/dom4j-1.6.1.jar new file mode 100644 index 0000000000..c8c4dbb92d Binary files /dev/null and b/group07/1058267830/week2/lib/dom4j-1.6.1.jar differ diff --git a/group07/1058267830/week2/src/com/coderising/array/ArrayTest.java b/group07/1058267830/week2/src/com/coderising/array/ArrayTest.java new file mode 100644 index 0000000000..954ebf54a3 --- /dev/null +++ b/group07/1058267830/week2/src/com/coderising/array/ArrayTest.java @@ -0,0 +1,77 @@ +package com.coderising.array; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ArrayTest { + ArrayUtil util = new ArrayUtil(); + + @Test + public void testReverseArray() { + int[] origin = {1,2,3,4,53,52,4,2,423}; + int[] target = util.reverseArray(origin); + int[] expecteds = {423, 2, 4, 52, 53, 4, 3, 2, 1}; + assertArrayEquals(expecteds, target); + } + + @Test + public void testRemoveZero(){ + int[] oldArray = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] target = util.removeZero(oldArray); + int[] expecteds = {1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5 }; + assertArrayEquals(expecteds, target); + } + + @Test + public void testMerger(){ + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + int[] target = util.merge(a1, a2); + int[] expecteds = {3, 4, 5, 6, 7, 8 }; + assertArrayEquals(expecteds, target); + } + + @Test + public void testGrow(){ + int[] oldArray = {3, 5,7,8}; + int[] target = util.grow(oldArray, 4); + int[] expecteds = {3, 5,7,8, 0, 0, 0,0 }; + assertArrayEquals(expecteds, target); + } + + @Test + public void testFibonacci(){ + int[] target = util.fibonacci(15); + + //int[] expecteds = {1,1}; + int[] expecteds = {1, 1, 2, 3, 5, 8, 13 }; + assertArrayEquals(expecteds, target); + } + + @Test + public void testGetPrimes(){ + int[] target = util.getPrimes(25); + int[] expecteds = {2, 3, 5, 7, 11, 13, 17, 19, 23 }; + assertArrayEquals(expecteds, target); +// for(int i=0; i set = new HashSet(); + for(int i=0; i it = set.iterator(); + while(it.hasNext()){ + target[i++] = it.next(); + } + return target; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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){ + if(oldArray == null) + return null; + if(size == 0) + return oldArray; + if(size < 0) + throw new RuntimeException("鍙傛暟閿欒"); + int length = oldArray.length + size; + int[] target = new int[length]; + System.arraycopy(oldArray, 0, target, 0, oldArray.length); + for(int i=oldArray.length; i< length; i++){ + target[i] = 0; + } + return target; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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) + throw new RuntimeException("鍙傛暟閿欒"); + if(max == 1){ + int[] result = {}; + return result; + } + if(max == 2){ + int[] result = {1, 1}; + return result; + } + List list = new ArrayList(); + list.add(1); + list.add(1); + for(int i=2; ; i++){ + int tmp = list.get(i-2) + list.get(i-1); + if(tmp < max){ + list.add(tmp); + }else{ + break; + } + } + int size = list.size(); + int[] result = new int[size]; + for(int i=0; i= max) + break; + } + int[] result = new int[index]; + System.arraycopy(tmp, 0, result, 0, index); + return result; + } + + List list = new ArrayList(); + list.add(2); + list.add(3); + list.add(5); + list.add(7); + list.add(11); + for(int i=13; i list = new ArrayList(); + for(int input = 2; input list = root.elements("action"); + for(Element action_element : list) { + //鑾峰彇鍏冪礌action鐨勫睘鎬 + if("logout".equals(action_element.attributeValue("name"))){ + System.out.print("action name=" + action_element.attributeValue("name") + " "); + System.out.print("class=" + action_element.attributeValue("class")); + System.out.println(); + + //閬嶅巻鍏冪礌action涓嬫墍鏈夌殑result鍏冪礌 + for(Element rusult_element : (List)action_element.elements("result")) { + //鑾峰彇鍏冪礌result鐨勫睘鎬 + System.out.print("result name=" + rusult_element.attributeValue("name") + " "); + System.out.println("value=" + rusult_element.getText() ); + } + } + } + } + +} diff --git a/group07/1058267830/week2/src/com/coderising/litestruts/LoginAction.java b/group07/1058267830/week2/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group07/1058267830/week2/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; + } +} diff --git a/group07/1058267830/week2/src/com/coderising/litestruts/Struts.java b/group07/1058267830/week2/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..195a63bc8f --- /dev/null +++ b/group07/1058267830/week2/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,64 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + +public class Struts { + + @SuppressWarnings("unchecked") + public static View runAction(String actionName, Map parameters) throws Exception{ + + Class viewClass = Class.forName("com.coderising.litestruts.View"); + View view = (View)viewClass.newInstance(); + + SAXReader saxReader = new SAXReader(); + Document doc = saxReader.read("src/com/coderising/litestruts/struts.xml"); + Element root = doc.getRootElement(); + List list = root.elements("action"); + for(Element action_element : list) { + if(actionName.equals(action_element.attributeValue("name"))){ + String className = action_element.attributeValue("class"); + Class clazz = Class.forName(className); + LoginAction la = (LoginAction)clazz.newInstance(); + Class clazz1 = la.getClass(); + + if(parameters.containsKey("name")){ + Method m1 = clazz1.getDeclaredMethod("setName", String.class); + m1.invoke(la, parameters.get("name")); + } + if(parameters.containsKey("password")){ + Method m2 = clazz1.getDeclaredMethod("setPassword", String.class); + m2.invoke(la, parameters.get("password")); + } + + Method m3 = clazz1.getDeclaredMethod("execute"); + String result = (String)m3.invoke(la); + Map map = new HashMap(); + // 杩欓噷娌℃湁閫氳繃鍙嶅皠锛屽悗缁鏀规鎴愬弽灏 + map.put("name", la.getName()); + map.put("password", la.getPassword()); + map.put("message", la.getMessage()); + + view.setParameters(map); + + + for(Element rusult_element : (List)action_element.elements("result")) { + if(result != null && result.equals(rusult_element.attributeValue("name"))){ + view.setJsp(rusult_element.getText()); + break; + } + } + break; + } + } + return view; + } + +} diff --git a/group07/1058267830/week2/src/com/coderising/litestruts/StrutsTest.java b/group07/1058267830/week2/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..cb1be91034 --- /dev/null +++ b/group07/1058267830/week2/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,41 @@ +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 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/group07/1058267830/week2/src/com/coderising/litestruts/View.java b/group07/1058267830/week2/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group07/1058267830/week2/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/group07/1058267830/week2/src/com/coderising/litestruts/struts.xml b/group07/1058267830/week2/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group07/1058267830/week2/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 diff --git a/group07/1519504320/.idea/libraries/dom4j_1_6_1.xml b/group07/1519504320/.idea/libraries/dom4j_1_6_1.xml new file mode 100644 index 0000000000..933acece89 --- /dev/null +++ b/group07/1519504320/.idea/libraries/dom4j_1_6_1.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/.idea/workspace.xml b/group07/1519504320/.idea/workspace.xml index 7dbdbd8302..d4a1a0f4a8 100644 --- a/group07/1519504320/.idea/workspace.xml +++ b/group07/1519504320/.idea/workspace.xml @@ -22,83 +22,81 @@ - - + + - - - + + + + + - - + + - - + + - - + + - - + + - - - - + + + + - - + + - - + + - - + + - - + + - - - - - - + + - - + + - - + + - - + + - - + + @@ -119,6 +117,9 @@ @@ -153,6 +154,7 @@ + @@ -184,8 +186,26 @@ + + + + + + + + + - @@ -203,21 +223,30 @@ - - + + + + + + + @@ -258,6 +287,52 @@ + + + + + + + + + + + + + + + + + - + + + - - - + + + + + @@ -519,6 +598,17 @@ + + + + + + + + + + + @@ -539,7 +629,7 @@ - + @@ -561,7 +651,17 @@ - + + + + file://$PROJECT_DIR$/src/coderising/litestruts/Struts.java + 44 + + + + @@ -569,11 +669,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - @@ -581,7 +787,6 @@ - @@ -589,12 +794,6 @@ - - - - - - @@ -602,7 +801,6 @@ - @@ -610,14 +808,6 @@ - - - - - - - - @@ -625,7 +815,6 @@ - @@ -633,7 +822,6 @@ - @@ -641,7 +829,6 @@ - @@ -649,12 +836,6 @@ - - - - - - @@ -662,7 +843,6 @@ - @@ -670,7 +850,6 @@ - @@ -678,7 +857,6 @@ - @@ -686,36 +864,27 @@ - - + - - + - - + - - - - - - @@ -723,24 +892,103 @@ - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/1519504320Learning.iml b/group07/1519504320/1519504320Learning.iml index 5034ed029c..7a17e1a748 100644 --- a/group07/1519504320/1519504320Learning.iml +++ b/group07/1519504320/1519504320Learning.iml @@ -8,5 +8,16 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/group07/1519504320/lib/hamcrest-core-1.3.jar b/group07/1519504320/lib/hamcrest-core-1.3.jar new file mode 100644 index 0000000000..9d5fe16e3d Binary files /dev/null and b/group07/1519504320/lib/hamcrest-core-1.3.jar differ diff --git a/group07/1519504320/lib/junit-4.12.jar b/group07/1519504320/lib/junit-4.12.jar new file mode 100644 index 0000000000..3a7fc266c3 Binary files /dev/null and b/group07/1519504320/lib/junit-4.12.jar differ diff --git a/group07/1519504320/src/coderising/array/ArrayUtil.java b/group07/1519504320/src/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..5caa3d54b7 --- /dev/null +++ b/group07/1519504320/src/coderising/array/ArrayUtil.java @@ -0,0 +1,224 @@ +package coderising.array; + +import java.util.Arrays; + +public class ArrayUtil { + + public static void main(String[] args) { + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7, 9, 10}; + ArrayUtil aa = new ArrayUtil(); + int[] o = aa.merge(a1, a2); + for (int i = 0; i < o.length; i++) { + System.out.println(o[i]); + } + + } + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 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 = new int[origin.length]; + for (int i = origin.length - 1; i >= 0; i--) { + temp[origin.length - 1 - i] = origin[i]; + } + for (int i = 0; i < origin.length; i++) { + origin[i] = temp[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[] result; + int zeroNumber = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) { + zeroNumber++; + } + } + result = new int[oldArray.length - zeroNumber]; + for (int i = 0, j = 0; j < result.length; i++) { + if (oldArray[i] != 0) { + result[j] = oldArray[i]; + j++; + } + } + return result; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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 = Arrays.copyOf(array1, array1.length); + outer: + for (int i = 0; i < array2.length; i++) { + for (int j = 0; j < array1.length; j++) { + if (array1[j] == array2[i]) { + continue outer; + } + } + result = Arrays.copyOf(result, result.length + 1); + result[result.length - 1] = array2[i]; + } + + for (int i = 0; i < result.length - 1; i++) { + for (int j = 0; j < result.length - i - 1; j++) { + if (result[j] > result[j+1]) { + int temp = result[j]; + result[j] = result[j+1]; + result[j+1] = temp; + } + } + } + + + 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[] result = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + result[i] = oldArray[i]; + } + for (int i = oldArray.length; i < result.length; i++) { + result[i] = 0; + } + return result; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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[] initial = new int[]{1, 1}; + if (max == 1) { + return new int[1]; + } else { + while (max > initial[initial.length - 1] + initial[initial.length - 2]) { + initial = addOne(initial); + } + } + return initial; + } + + public int[] addOne(int[] current) { + int[] result = Arrays.copyOf(current, current.length + 1); + result[current.length] = result[current.length - 1] + result[current.length - 2]; + return result; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int[] result = new int[1]; + outer: + for (int i = 2; i < max; i++) { + for (int j = 2; j < i; j++) { + if (i % j == 0) { + continue outer; + } + } + if (result.length == 1 && result[0] == 0) { + result[0] = i; + } else { + result = Arrays.copyOf(result, result.length + 1); + result[result.length - 1] = i; + } + } + return result; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int[] result = new int[1]; + for (int i = 1; i < max; i++) { + int sum = 0; + for (int j = 1; j < i; j++) { + if (i % j == 0) { + sum += j; + } + } + if (sum == i) { + if (result.length == 1 && result[0] == 0) { + result[0] = i; + } else { + result = Arrays.copyOf(result, result.length + 1); + result[result.length - 1] = i; + } + } + } + return result; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param + * @return + */ + public String join(int[] array, String seperator) { + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < array.length; i++) { + if (i == array.length - 1) { + sb.append(array[i]); + } else { + sb.append(array[i]); + sb.append(seperator); + } + } + return sb.toString(); + } + + +} \ No newline at end of file diff --git a/group07/1519504320/src/coderising/litestruts/LoginAction.java b/group07/1519504320/src/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..c528df798a --- /dev/null +++ b/group07/1519504320/src/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package 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/group07/1519504320/src/coderising/litestruts/Struts.java b/group07/1519504320/src/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..22b7a291b4 --- /dev/null +++ b/group07/1519504320/src/coderising/litestruts/Struts.java @@ -0,0 +1,106 @@ +package coderising.litestruts; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +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 void main(String[] args) { + runAction("login", null); + } + + public static View runAction(String actionName, Map parameters) { + String className = " "; + View view = new View(); + Map map = new HashMap<>(); + String result = " "; + + SAXReader reader = new SAXReader(); + Document document = null; + try { + document = reader.read(new File("src\\coderising\\litestruts\\struts.xml")); + } catch (DocumentException e) { + e.printStackTrace(); + } + Element root = document.getRootElement(); + List eles = root.elements(); + for (Element e : eles) { + if (e.attribute("name").getValue().equals(actionName)) { + className = e.attribute("class").getValue(); + } + } + + try { + Class clz = Class.forName(className); + Object obj = clz.newInstance(); + Method method = clz.getDeclaredMethod("setName",String.class); + method.invoke(obj, parameters.get("name")); + method = clz.getDeclaredMethod("setPassword",String.class); + method.invoke(obj, parameters.get("password")); + method = clz.getDeclaredMethod("execute"); + result = (String) method.invoke(obj); + method = clz.getDeclaredMethod("getMessage"); + map.put("message", (String) method.invoke(obj)); + method = clz.getDeclaredMethod("getName"); + map.put("name", (String) method.invoke(obj)); + method = clz.getDeclaredMethod("getPassword"); + map.put("password", (String) method.invoke(obj)); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + view.setParameters(map); + for (Element e : eles) { + if (e.attribute("name").getValue().equals(actionName)) { + List ele_subs = e.elements(); + for (Element e_sub : ele_subs) { + if (e_sub.attribute("name").getValue().equals(result)) { + view.setJsp(e_sub.getTextTrim()); + } + } + } + } + + + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + return view; + } + +} diff --git a/group07/1519504320/src/coderising/litestruts/StrutsTest.java b/group07/1519504320/src/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..257f3d5a89 --- /dev/null +++ b/group07/1519504320/src/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package 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() { + + 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/group07/1519504320/src/coderising/litestruts/View.java b/group07/1519504320/src/coderising/litestruts/View.java new file mode 100644 index 0000000000..22fdf877d8 --- /dev/null +++ b/group07/1519504320/src/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package 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/group07/1519504320/src/coderising/litestruts/dom4j-1.6.1.jar b/group07/1519504320/src/coderising/litestruts/dom4j-1.6.1.jar new file mode 100644 index 0000000000..c8c4dbb92d Binary files /dev/null and b/group07/1519504320/src/coderising/litestruts/dom4j-1.6.1.jar differ diff --git a/group07/1519504320/src/coderising/litestruts/struts.xml b/group07/1519504320/src/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..57ad66abd0 --- /dev/null +++ b/group07/1519504320/src/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 diff --git a/group07/178007127/001DataStructure/build.gradle b/group07/178007127/001DataStructure/build.gradle index 670cff0568..8450dd647d 100644 --- a/group07/178007127/001DataStructure/build.gradle +++ b/group07/178007127/001DataStructure/build.gradle @@ -1,9 +1,12 @@ -apply plugin:'java' - -repositories{ - mavenCentral() -} - -dependencies{ - compile group: 'junit', name: 'junit', version: '4.12' +apply plugin:'java' + +repositories{ + mavenCentral() +} + +dependencies{ + compile group: 'junit', name: 'junit', version: '4.12' + + compile group: 'dom4j', name: 'dom4j', version: '1.6.1' + compile group: 'jaxen', name: 'jaxen', version: '1.1.6' } \ No newline at end of file diff --git a/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/array/ArrayUtil.java b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/array/ArrayUtil.java new file mode 100644 index 0000000000..b98e1c1c37 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/array/ArrayUtil.java @@ -0,0 +1,225 @@ +package com.easy.codersing.array; + +import org.junit.Test; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static int[] reverseArray(int[] origin){ + int[] dest =new int[origin.length]; + for(int i=0;iarr[j]){ + int temp=arr[i]; + arr[i]=arr[j]; + arr[j]=temp; + } + } + } + return arr; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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, newArray.length); + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + StringBuilder sb=new StringBuilder(); + + if(max==1){ + return new int[]{}; + }else{ + for(int i=1 ;i<2*max;i++){ + int num = getFibo(i); + if(num parameters) throws Exception { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + SAXReader reader = new SAXReader(); + File file =new File("src/main/java/com/easy/codersing/litestruts/struts.xml"); + Document doc=reader.read(file); + Element el_root=doc.getRootElement(); + Iterator it= el_root.elementIterator(); + while(it.hasNext()){ + Object obj=it.next(); + Element el_action = (Element)obj; + //System.out.println(el_action.attributeValue("name")); + String el_action_name=el_action.attributeValue("name"); + + + if(actionName.equals(el_action_name)){ + String el_action_class=el_action.attributeValue("class"); + Class clazz = Class.forName(el_action_class); + Object actionObj = clazz.newInstance(); + + for(String map_key:parameters.keySet()){ + String set_method_name = "set"+AppUtils.fistLetterToUpper(map_key); + Method set_method =clazz.getMethod(set_method_name, String.class); + set_method.invoke(actionObj,parameters.get(map_key)); + } + + Method execute_method = clazz.getMethod("execute"); + String execute_result = (String)execute_method.invoke(actionObj); + + Method get_message_method = clazz.getMethod("getMessage"); + String get_message_result=(String)get_message_method.invoke(actionObj); + + Map map=new HashMap(); + map.put("message", get_message_result); + + View view =new View(); + view.setParameters(map); + + Iterator it_result = el_action.elementIterator(); + while(it_result.hasNext()){ + Element el_result =(Element)it_result.next(); + if(el_result.attributeValue("name").equals(execute_result)){ + view.setJsp(el_result.getText()); + } + } + + return view; + + + } + } + + return null; + } + + + + +} diff --git a/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/StrutsTest.java b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/StrutsTest.java new file mode 100644 index 0000000000..2d167cce90 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.easy.codersing.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 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/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/TestApp.java b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/TestApp.java new file mode 100644 index 0000000000..df78950609 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/TestApp.java @@ -0,0 +1,85 @@ +package com.easy.codersing.litestruts; + + +import java.io.File; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class TestApp { + public static void test1() throws Exception { + SAXReader reader = new SAXReader(); + File file =new File("src/main/java/com/easy/codersing/litestruts/struts.xml"); + //System.out.println(file.exists()); + Document doc=reader.read(file); + List actions = doc.selectNodes("//*[@name]"); + for(Iterator it=actions.iterator();it.hasNext();){ + Element action = (Element)it.next(); + List list=action.attributes(); + for(Iterator it2=list.iterator();it2.hasNext();){ + Attribute a=(Attribute)it2.next(); + System.out.println(a.getName()+"="+a.getValue()); + } + } + + } + + public static void main(String[] args) throws Exception { + /* Map map=new HashMap<>(); + map.put("name", "test"); + map.put("password", "1234"); + + for (String s : map.keySet()) { + System.out.println(s); + System.out.println(map.get(s)); + }*/ + + + + SAXReader reader = new SAXReader(); + File file =new File("src/main/java/com/easy/codersing/litestruts/struts.xml"); + Document doc=reader.read(file); + Element el_root=doc.getRootElement(); + Iterator it= el_root.elementIterator(); + while(it.hasNext()){ + Object obj=it.next(); + Element el_action = (Element)obj; + //System.out.println(el_action.attributeValue("name")); + //System.out.println(el_action.attributeValue("class")); + + Iterator it_row=el_action.elementIterator(); + if(el_action.attributeValue("name").equals("login")){ + while(it_row.hasNext()){ + Element el_result=(Element)it_row.next(); + System.out.println(el_result.attributeValue("name")); + System.out.println(el_result.getText()); + + } + } + } + + + + } + + public static void test3(String[] args) throws Exception { + Class clazz = Class.forName("com.easy.codersing.litestruts.LoginAction"); + Object obj=clazz.newInstance(); + + Method method1 =clazz.getMethod("setName",String.class); + method1.invoke(obj, "寮犱笁"); + + Method method2 = clazz.getMethod("getName",null); + Object result = method2.invoke(obj); + System.out.println(result); + + } +} diff --git a/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/View.java b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/View.java new file mode 100644 index 0000000000..8c44204849 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/View.java @@ -0,0 +1,23 @@ +package com.easy.codersing.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/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/struts.xml b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/litestruts/struts.xml new file mode 100644 index 0000000000..2d1ab0da83 --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/codersing/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/group07/178007127/001DataStructure/src/main/java/com/easy/core/AppUtils.java b/group07/178007127/001DataStructure/src/main/java/com/easy/core/AppUtils.java new file mode 100644 index 0000000000..e0b216553e --- /dev/null +++ b/group07/178007127/001DataStructure/src/main/java/com/easy/core/AppUtils.java @@ -0,0 +1,13 @@ +package com.easy.core; + +public class AppUtils { + public static String fistLetterToUpper(String words){ + String letter = words.substring(0,1).toUpperCase(); + return letter+words.substring(1,words.length()); + } + + public static void main(String[] args) { + String s="abc"; + System.out.println(fistLetterToUpper(s)); + } +} diff --git a/group07/178007127/001DataStructure/src/test/java/com/easy/codersing/array/TestArrayUtil.java b/group07/178007127/001DataStructure/src/test/java/com/easy/codersing/array/TestArrayUtil.java new file mode 100644 index 0000000000..a0477fb798 --- /dev/null +++ b/group07/178007127/001DataStructure/src/test/java/com/easy/codersing/array/TestArrayUtil.java @@ -0,0 +1,67 @@ +package com.easy.codersing.array; + + +import org.junit.Assert; +import org.junit.Test; + + +public class TestArrayUtil { + @Test + public void test_reverseArray(){ + int[] origin =new int[]{1,2,3}; + int[] dest =ArrayUtil.reverseArray(origin); + Assert.assertArrayEquals(new int[]{3,2,1}, dest); + } + + @Test + public void test_removeZero(){ + int[] origin=new int[]{1,0,2,0,3,0}; + int[] dest = ArrayUtil.removeZero(origin); + Assert.assertArrayEquals(new int[]{1,2,3}, dest); + } + + @Test + public void test_merge(){ + int[] array1=new int[]{4,0,6,2}; + int[] array2=new int[]{7,0,9,2,3}; + int[] array3=ArrayUtil.merge(array1, array2); + //System.out.println(Arrays.toString(array3)); + Assert.assertArrayEquals(new int[]{0, 2, 3, 4, 6, 7, 9}, array3); + } + + @Test + public void test_sort(){ + int[] arr=new int[]{4,6,2}; + arr = ArrayUtil.sort(arr); + //System.out.println(Arrays.toString(arr)); + Assert.assertArrayEquals(new int[]{2,4,6},arr); + } + + + @Test + public void test_getfabonacci(){ + int max=15; + int[] intArr=ArrayUtil.fibonacci(max); + //System.out.println(Arrays.toString(intArr)); + Assert.assertArrayEquals(new int[]{1, 1, 2, 3, 5, 8, 13}, intArr); + } + + @Test + public void test_getPrimes(){ + int[] intArr =ArrayUtil.getPrimes(16); + Assert.assertArrayEquals(new int[]{2,3,5,7,11,13}, intArr); + } + + @Test + public void test_getPerfectNumbers(){ + int[] intArr =ArrayUtil.getPerfectNumbers(10); + Assert.assertArrayEquals(new int[]{6}, intArr); + } + + @Test + public void test_join(){ + int[] intArr=new int[]{1,2,3}; + String s=ArrayUtil.join(intArr, "-"); + Assert.assertEquals("1-2-3", s); + } +} diff --git a/group07/20409287/src/main/java/xdx/homework/first/ArrayList.java b/group07/20409287/src/main/java/xdx/homework/first/ArrayList.java index b7ffc73d07..f8757c9a68 100644 --- a/group07/20409287/src/main/java/xdx/homework/first/ArrayList.java +++ b/group07/20409287/src/main/java/xdx/homework/first/ArrayList.java @@ -32,9 +32,9 @@ public boolean add(E e) { if (++position > size - 1) { grow(); - } else { - elements[position] = e; } + elements[position] = e; + return true; } @@ -169,8 +169,10 @@ public Iterator iterator() { * 鏁扮粍澧為暱 */ private void grow() { - Object[] newElements = new Object[size << 2]; - System.arraycopy(elements, 0, elements, 0, this.size); + + Object[] newElements = new Object[size << 1]; + System.arraycopy(elements, 0, newElements, 0, this.size); + size = size << 1; elements = null; elements = newElements; } diff --git a/group07/20409287/src/main/java/xdx/homework/second/array/ArrayUtil.java b/group07/20409287/src/main/java/xdx/homework/second/array/ArrayUtil.java new file mode 100644 index 0000000000..2fa1237dbb --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/second/array/ArrayUtil.java @@ -0,0 +1,221 @@ +package xdx.homework.second.array; + +import xdx.homework.first.ArrayList; +import xdx.homework.first.List; +import xdx.homework.first.Queue; +import xdx.homework.first.Stack; + +import java.util.Arrays; + +import static java.lang.StrictMath.sqrt; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 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 end = origin.length - 1; + for (int begin = 0; begin < end; begin++, end--) { + // 浜ゆ崲棣栧熬 + origin[begin] = origin[begin] ^ origin[end]; + origin[end] = origin[begin] ^ origin[end]; + origin[begin] = origin[begin] ^ origin[end]; + } + } + + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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[] newArray = new int[oldArray.length]; + int newArrayIndex = 0; + int i = 0; + for (; i < oldArray.length; i++) { + if (oldArray[i] == 0) continue; + newArray[newArrayIndex++] = oldArray[i]; + } + return Arrays.copyOfRange(newArray, 0, newArrayIndex); + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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) { + + // 鍒嗗埆鏀捐繘涓や釜鏍堜腑,鍙栧皬鐨勫嚭鏍 + Queue queue1 = new Queue<>(); + Queue queue2 = new Queue<>(); + ArrayList newArray = new ArrayList<>(); + for (int a : array1) { + queue1.enQueue(a); + } + for (int a : array2) { + queue2.enQueue(a); + } + while (!queue1.isEmpty() && !queue2.isEmpty()) { + if (queue1.getFront() < queue2.getFront()) { + newArray.add(queue1.deQueue()); + } else if (queue1.getFront() > queue2.getFront()) { + newArray.add(queue2.deQueue()); + } else { + newArray.add(queue2.deQueue()); + queue1.deQueue(); + } + } + while (!queue1.isEmpty()) newArray.add(queue1.deQueue()); + while (!queue2.isEmpty()) newArray.add(queue2.deQueue()); + + int[] retArray = new int[newArray.size()]; + for (int i = 0; i < newArray.size(); i++) { + retArray[i] = newArray.get(i); + } + return retArray; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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 first = 1; + int second = 1; + if (max == 0) return new int[]{0}; + if (max == 1) return new int[]{first, second}; + + List fibList = new ArrayList<>(); + fibList.add(first); + fibList.add(second); + int last = first + second; + while (last < max) { + fibList.add(last); + first = second; + second = last; + last = first + second; + } + + return list2array(fibList); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + + if (max < 2) return new int[]{}; + if (max == 2) return new int[]{2}; + + List primeList = new ArrayList<>(); + primeList.add(2); + for (int i = 3; i <= max; i += 2) { + // 鍒ゆ柇i鏄笉鏄礌鏁 + boolean isPrime = true; + for (int j = 2; j <= Math.sqrt(i); j++) { + if (i % j == 0) isPrime = false; + } + if (isPrime) primeList.add(i); + } + + return list2array(primeList); + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + + if (max < 1) return new int[]{}; + + List perfectNums = new ArrayList<>(); + + for (int aPerfectNum = 1; aPerfectNum <= max; aPerfectNum++) { + int sumFactors = 1; + for (int factor = 2; factor <= aPerfectNum / 2; factor++) { + if (aPerfectNum % factor == 0) { + sumFactors += factor; + } + } + if (sumFactors == aPerfectNum) perfectNums.add(aPerfectNum); + } + + return list2array(perfectNums); + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + + StringBuilder stringBuilder = new StringBuilder(); + for (int a : array) { + stringBuilder.append(a).append(seperator); + } + stringBuilder.deleteCharAt(stringBuilder.length() - 1); + return stringBuilder.toString(); + } + + private int[] list2array(List list) { + int[] array = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + array[i] = list.get(i); + } + return array; + } + +} diff --git a/group07/20409287/src/main/java/xdx/homework/second/litestruts/LoginAction.java b/group07/20409287/src/main/java/xdx/homework/second/litestruts/LoginAction.java new file mode 100644 index 0000000000..f59067088b --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/second/litestruts/LoginAction.java @@ -0,0 +1,41 @@ +package xdx.homework.second.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/group07/20409287/src/main/java/xdx/homework/second/litestruts/Struts.java b/group07/20409287/src/main/java/xdx/homework/second/litestruts/Struts.java new file mode 100644 index 0000000000..0ba4f85313 --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/second/litestruts/Struts.java @@ -0,0 +1,149 @@ +package xdx.homework.second.litestruts; + +import org.jcp.xml.dsig.internal.dom.DOMEnvelopedTransform; +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; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Executable; +import java.lang.reflect.Method; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + private static Map actionMap; + + private static Map> actionResultMap; + + private static void init() { + + if (actionMap != null) return; + + actionMap = new HashMap<>(); + actionResultMap = new HashMap<>(); + Document strutsDocument = getDocumentFromFile(); + if (strutsDocument == null) throw new RuntimeException("xml鏂囦欢璇诲彇閿欒,璇锋鏌ユ枃浠惰矾寰勬槸鍚︽纭!"); + + try { + NodeList nodeList = strutsDocument.getFirstChild().getChildNodes(); + for (int i = 0; i < nodeList.getLength(); i++) { + if (nodeList.item(i).getNodeType() != 1) continue; + Element child = (Element) nodeList.item(i); + Class actionClass = Class.forName(child.getAttribute("class")); + actionMap.put(child.getAttribute("name"), (LoginAction) actionClass.newInstance()); + + NodeList resultList = child.getElementsByTagName("result"); + Map params = new HashMap<>(); + for (int j = 0; j < resultList.getLength(); j++) { + Element result = (Element) resultList.item(j); + params.put(result.getAttribute("name"), result.getTextContent()); + } + actionResultMap.put(child.getAttribute("name"), params); + } + + } catch (ClassNotFoundException e) { + System.out.println(e.getMessage()); + throw new RuntimeException("xml鏂囦欢鍐呭鏈夎!"); + } catch (InstantiationException | IllegalAccessException e) { + System.out.println(e.getMessage()); + throw new RuntimeException("鏃犳硶鍒涘缓鎺ュ彛鎴栬呮娊璞$被鐨勫疄渚!"); + } + } + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + // 璇诲彇struts鏂囦欢骞跺垵濮嬪寲鐩稿叧绫 + init(); + + // 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class + LoginAction loginAction = actionMap.get(actionName); + if (loginAction == null) { + System.out.println("鎵句笉鍒拌action: " + actionName); + return null; + } + + View view = new View(); + // 鎹畃arameters涓殑鏁版嵁璋冪敤鐩稿簲鐨勬柟娉 + Class clazz = loginAction.getClass(); + try { + for (String key : parameters.keySet()) { + String value = parameters.get(key); + if (value == null || value.isEmpty()) continue; + PropertyDescriptor descriptor = new PropertyDescriptor(key, clazz); + descriptor.getWriteMethod().invoke(loginAction, value); + } + // 璋冪敤瀵硅薄鐨別xectue鏂规硶锛 骞惰幏寰楄繑鍥炲 + String executeReturn = loginAction.execute(); + // 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶, 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap ,鏀惧埌View瀵硅薄鐨刾arameters + PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz).getPropertyDescriptors(); + HashMap params = new HashMap<>(); + for (PropertyDescriptor pd : pds) { + params.put(pd.getName(), pd.getReadMethod().invoke(loginAction).toString()); + } + view.setParameters(params); + // 纭畾鍝竴涓猨sp + view.setJsp(actionResultMap.get(actionName).get(executeReturn)); + } catch (IntrospectionException e) { + e.printStackTrace(); + return null; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + + return view; + } + + + private static Document getDocumentFromFile() { + + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + Document document = null; + try { + URL fileURL = Struts.class.getResource("struts.xml"); + DocumentBuilder documentBuilder = dbf.newDocumentBuilder(); + document = documentBuilder.parse(new File(fileURL.getFile())); + } catch (ParserConfigurationException e) { + System.out.println("閰嶇疆杞寲寮傚父: " + e.getMessage()); + } catch (SAXException e) { + System.out.println("SAX璇诲彇寮傚父: " + e.getMessage()); + } catch (IOException e) { + System.out.println("IO寮傚父: " + e.getMessage()); + } + return document; + } + +} diff --git a/group07/20409287/src/main/java/xdx/homework/second/litestruts/StrutsTest.java b/group07/20409287/src/main/java/xdx/homework/second/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a56eec5e28 --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/second/litestruts/StrutsTest.java @@ -0,0 +1,42 @@ +package xdx.homework.second.litestruts; + +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.util.HashMap; +import java.util.Map; + + +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/group07/20409287/src/main/java/xdx/homework/second/litestruts/View.java b/group07/20409287/src/main/java/xdx/homework/second/litestruts/View.java new file mode 100644 index 0000000000..0d7f02f86b --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/second/litestruts/View.java @@ -0,0 +1,23 @@ +package xdx.homework.second.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/group07/20409287/src/main/java/xdx/homework/second/litestruts/struts.xml b/group07/20409287/src/main/java/xdx/homework/second/litestruts/struts.xml new file mode 100644 index 0000000000..da4d783426 --- /dev/null +++ b/group07/20409287/src/main/java/xdx/homework/second/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/group07/20409287/src/test/xdx/homework/first/ArrayListTest.java b/group07/20409287/src/test/xdx/homework/first/ArrayListTest.java index 2c71401873..d19cdb3dec 100644 --- a/group07/20409287/src/test/xdx/homework/first/ArrayListTest.java +++ b/group07/20409287/src/test/xdx/homework/first/ArrayListTest.java @@ -38,9 +38,14 @@ public void after() throws Exception { public void testAdd() throws Exception { List testList = new ArrayList<>(); - Assert.assertTrue(testList.add(1)); - Assert.assertTrue(testList.add(2)); - Assert.assertTrue(testList.add(3)); + final int NUM = 1000000; + for (Integer i = 0; i < NUM; i++) { + testList.add(i); + } + for (Integer i = 0; i < NUM; i++) { + Assert.assertEquals(i, testList.get(i)); + } + Assert.assertEquals(NUM, testList.size()); System.out.println(testList.toString()); } diff --git a/group07/20409287/src/test/xdx/homework/second/ArrayUtilTest.java b/group07/20409287/src/test/xdx/homework/second/ArrayUtilTest.java new file mode 100644 index 0000000000..ffafc94f7d --- /dev/null +++ b/group07/20409287/src/test/xdx/homework/second/ArrayUtilTest.java @@ -0,0 +1,157 @@ +package xdx.homework.second; + +import org.junit.Test; +import org.junit.Before; +import org.junit.After; +import org.testng.Assert; +import xdx.homework.second.array.ArrayUtil; + +import java.util.Arrays; + +/** + * ArrayUtil Tester. + * + * @author + * @since
锟斤拷锟斤拷 4, 2017
+ * @version 1.0 + */ +public class ArrayUtilTest { + + ArrayUtil arrayUtil = new ArrayUtil(); + + @Before + public void before() throws Exception { + } + + @After + public void after() throws Exception { + } + + /** + * + * Method: reverseArray(int[] origin) + * + */ + @Test + public void testReverseArray() throws Exception { + + int[] array = {1,2,3,4,5,6,7,8,9}; + System.out.println("===================鏁扮粍鍙嶈浆寮濮==================="); + System.out.println("鍘熸暟缁: " + Arrays.toString(array)); + arrayUtil.reverseArray(array); + System.out.println("鍙嶈浆鍚庣殑鏁扮粍: " + Arrays.toString(array)); + System.out.println("===================鏁扮粍鍙嶈浆缁撴潫===================" + "\n"); + } + + /** + * + * Method: removeZero(int[] oldArray) + * + */ + @Test + public void testRemoveZero() throws Exception { + + System.out.println("===================鏁扮粍娓呴浂寮濮==================="); + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + System.out.println("鍘熸暟缁: " + Arrays.toString(oldArr)); + System.out.println("娓呴浂鍚庣殑鏁扮粍: " + Arrays.toString(arrayUtil.removeZero(oldArr))); + System.out.println("===================鏁扮粍娓呴浂缁撴潫===================" + "\n"); + } + + /** + * + * Method: merge(int[] array1, int[] array2) + * + */ + @Test + public void testMerge() throws Exception { + + System.out.println("===================鏁扮粍鍚堝苟寮濮==================="); + int[] array1 = {1,2,4,5}; + int[] array2 = {3,4,5,6,7,8,9}; + System.out.println("鍘熸暟缁1: " + Arrays.toString(array1)); + System.out.println("鍘熸暟缁2: " + Arrays.toString(array2)); + int[] mergedArray = arrayUtil.merge(array1, array2); + System.out.println("鍚堝苟鍚庣殑鏁扮粍: " + Arrays.toString(mergedArray)); + System.out.println("===================鏁扮粍鍚堝苟缁撴潫===================" + "\n"); + } + + /** + * + * Method: grow(int [] oldArray, int size) + * + */ + @Test + public void testGrow() throws Exception { + + System.out.println("===================鏁扮粍澧為暱寮濮==================="); + int[] array2 = {3,4,5,6,7,8,9}; + final int GROW_SIZE = 5; + int[] growArray = arrayUtil.grow(array2, GROW_SIZE); + Assert.assertEquals(array2.length + GROW_SIZE, growArray.length); + System.out.println("鍘熸暟缁: " + Arrays.toString(array2)); + System.out.println("澧為暱" + GROW_SIZE + "涓崟浣嶅悗鐨勬暟缁: " + Arrays.toString(growArray)); + System.out.println("===================鏁扮粍澧為暱缁撴潫===================" + "\n"); + } + + /** + * + * Method: fibonacci(int max) + * + */ + @Test + public void testFibonacci() throws Exception { + + System.out.println("===================鏂愭尝閭e鏁板垪寮濮==================="); + final int MAX = 10000000; + System.out.println(MAX + "浠ュ唴鐨勬枑娉㈤偅濂戞暟鍒: " + Arrays.toString(arrayUtil.fibonacci(MAX))); + System.out.println("===================鏂愭尝閭e鏁板垪缁撴潫===================" + "\n"); + } + + /** + * + * Method: getPrimes(int max) + * + */ + @Test + public void testGetPrimes() throws Exception { + + System.out.println("===================绱犳暟璁$畻寮濮==================="); + final int MAX = 10000; + System.out.println(MAX + "浠ュ唴鐨勭礌鏁: " + Arrays.toString(arrayUtil.getPrimes(MAX))); + System.out.println("===================绱犳暟璁$畻缁撴潫===================" + "\n"); + } + + /** + * + * Method: getPerfectNumbers(int max) + * + */ + @Test + public void testGetPerfectNumbers() throws Exception { + + System.out.println("===================璁$畻瀹岀編鏁板垪缁撴潫==================="); + final int MAX = 10000; + System.out.println(MAX + "浠ュ唴鐨勫畬鏁板垎鍒槸: " + Arrays.toString(arrayUtil.getPerfectNumbers(MAX))); + System.out.println("===================璁$畻瀹岀編鏁板垪缁撴潫===================" + "\n"); + } + + /** + * + * Method: join(int[] array, String seperator) + * + */ + @Test + public void testJoin() throws Exception { + + System.out.println("===================鏁扮粍鍒嗛殧寮濮==================="); + int[] array2 = {3,4,5,6,7,8,9}; + final String SEP = "-"; + System.out.println("鍘熸暟缁: " + Arrays.toString(array2)); + System.out.println("鍒嗛殧绗: " + SEP); + System.out.println("鍒嗛殧鍚庣殑鏁扮粍: " + arrayUtil.join(array2, SEP)); + System.out.println("===================鏁扮粍鍒嗛殧缁撴潫===================" + "\n"); + } + + +} diff --git a/group07/43819473/homework/pom.xml b/group07/43819473/homework/pom.xml index b4c8775d15..e9da2d1a08 100644 --- a/group07/43819473/homework/pom.xml +++ b/group07/43819473/homework/pom.xml @@ -21,6 +21,16 @@ fastjson 1.2.7 - + + + dom4j + dom4j + 1.6.1 + + + jaxen + jaxen + 1.1.1 + \ No newline at end of file diff --git a/group07/43819473/homework/src/main/java/coderising/array/ArrayUtil.java b/group07/43819473/homework/src/main/java/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..8ef221c5cf --- /dev/null +++ b/group07/43819473/homework/src/main/java/coderising/array/ArrayUtil.java @@ -0,0 +1,238 @@ +package coderising.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + * 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public static int[] reverseArray(int[] origin) { + int[] result = new int[origin.length]; + for (int i = 0; i <= origin.length - 1; i++) { + result[i] = origin[origin.length - 1 - i]; + } + return result; + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 int origin[]={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 origin + * @return + */ + + public static int[] removeZero(int[] origin) { + int[] tempArray = new int[origin.length]; + int j = 0; + for (int i = 0; i <= origin.length - 1; i++) { + if (origin[i] != 0) { + tempArray[j++] = origin[i]; + } + } + + int[] result = new int[j]; + System.arraycopy(tempArray, 0, result, 0, j); + return result; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2) { + if (array1.length == 0) { + return array2; + } + if (array2.length == 0) { + return array1; + } + int[] tempArray = new int[array1.length + array2.length]; + int currentSize = array1.length; + System.arraycopy(array1, 0, tempArray, 0, array1.length); + for (int i = 0; i <= array2.length - 1; i++) { + for (int j = 0; j <= currentSize - 1; j++) { + if (array2[i] == tempArray[j]) { + break; + } else if (array2[i] < tempArray[j]) { + System.arraycopy(tempArray, j, tempArray, j + 1, currentSize - j); + tempArray[j] = array2[i]; + currentSize++; + break; + } + if (j == currentSize - 1) { + tempArray[j + 1] = array2[i]; + currentSize++; + break; + } + } + } + + int[] result = new int[currentSize]; + System.arraycopy(tempArray, 0, result, 0, currentSize); + return result; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + int[] result = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, result, 0, oldArray.length); + return result; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max <= 1) { + return new int[]{}; + } + + int[] temp = new int[max]; + temp[0] = 1; + temp[1] = 1; + + int i = 2; + while (i >= 2) { + int last = temp[i - 1] + temp[i - 2]; + if (last < max) { + temp[i++] = last; + } else { + break; + } + } + + int[] result = new int[i]; + System.arraycopy(temp, 0, result, 0, i); + return result; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + if (max <= 2) { + return new int[]{}; + } else if (max == 3) { + return new int[]{2}; + } else { + int[] temp = new int[max / 2 + 1]; + temp[0] = 2; + int resultSize = 1; + + for (int i = 3; i < max; i += 2) { + boolean isPrime = true; + for (int j = 0; j <= resultSize - 1; j++) { + if (i % temp[j] == 0) { + isPrime = false; + break; + } + } + if (isPrime) { + temp[resultSize++] = i; + } + } + int[] result = new int[resultSize]; + System.arraycopy(temp, 0, result, 0, resultSize); + + return result; + } + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + int[] tempResult = new int[max]; + int resultSize = 0; + + //瀵瑰皬浜巑ax鐨勫奸愪釜寰幆鍒ゆ柇 + for (int i = 2; i < max; i++) { +// System.out.println("==="+i); + int[] factors = new int[max];//鍥犲瓙鏁扮粍 + factors[0] = 1; + int factorSize = 1; + + //鑾峰彇鍥犲瓙 + for (int j = 2; j < i / 2 + 1; j++) { + if (i % j == 0) { + factors[factorSize++] = j; + } + } + + //璁$畻鍥犲瓙鏁扮粍鐨勫拰鏄惁涓庡綋鍓嶅肩浉绛夛紝鐩哥瓑鍒欐斁鍏ョ粨鏋滄暟缁勪腑 + if (factorSize > 1) { + int sumValue = 0; + for (int factorIndex = 0; factorIndex <= factorSize - 1; factorIndex++) { + sumValue += factors[factorIndex]; + } + if (sumValue == i) { + tempResult[resultSize++] = i; + } + } + } + + int[] result = new int[resultSize]; + System.arraycopy(tempResult, 0, result, 0, resultSize); + return result; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public static String join(int[] array, String seperator) { + int size = array.length; + String result = ""; + if (size > 0) { + if (size == 1) { + result = String.valueOf(array[0]); + } else { + for (int i = 0; i < array.length - 1; i++) { + result += String.valueOf(array[i]) + seperator; + } + result += String.valueOf(array[array.length - 1]); + } + } + + return result; + } + +} diff --git a/group07/43819473/homework/src/main/java/coderising/litestruts/LoginAction.java b/group07/43819473/homework/src/main/java/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..c528df798a --- /dev/null +++ b/group07/43819473/homework/src/main/java/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package 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/group07/43819473/homework/src/main/java/coderising/litestruts/Struts.java b/group07/43819473/homework/src/main/java/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..c2b2ccad51 --- /dev/null +++ b/group07/43819473/homework/src/main/java/coderising/litestruts/Struts.java @@ -0,0 +1,95 @@ +package coderising.litestruts; + +import org.dom4j.*; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + + +public class Struts { + + /* + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xecute 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + public static View runAction(String actionName, Map parameters) { + + SAXReader reader = new SAXReader(); + View view = new View(); + + try { + //璇诲彇xml锛岃幏鍙朿lass锛岃幏鍙栧璞 + Document document = reader.read(new File("src\\main\\java\\coderising\\litestruts\\struts.xml")); + Node node = document.selectSingleNode("//struts/action[@name='" + actionName + "']"); + Element element = (Element) node; + String className = element.attribute("class").getText(); + Class clazz = Class.forName(className); + Object object = clazz.newInstance(); + + //璋冪敤set鏂规硶璁剧疆灞炴 + for (Map.Entry entry : parameters.entrySet()) { + String methodName = "set" + entry.getKey().substring(0, 1).toUpperCase() + entry.getKey().substring(1); + Method method = clazz.getMethod(methodName, entry.getValue().getClass()); + method.invoke(object, entry.getValue()); + } + + //璋冪敤exectue 鏂规硶锛 骞惰幏寰楄繑鍥炲 + Method methodExecute = clazz.getMethod("execute"); + String executeResult = methodExecute.invoke(object).toString(); + + //鎵惧埌鎵鏈塯etter锛岃皟鐢ㄥ苟灏嗗兼斁鍒皉esultMap涓 + Map resultMap = new HashMap(); + Method[] getMethods = clazz.getMethods(); + for (Method getMethod : getMethods) { + int index = getMethod.toString().lastIndexOf("."); + String pureName = getMethod.toString().substring(index + 1); + if (pureName.startsWith("get")) { //eg. getName() + String tempStr= pureName.substring(3); //eg. Name() + String attributeName = tempStr.substring(0, 1).toLowerCase() + tempStr.substring(1, tempStr.length() - 2); //eg. name + String attributeResult = getMethod.invoke(object).toString(); + resultMap.put(attributeName, attributeResult); + } + } + + //缁勮杩斿洖瀵硅薄 + Node resultNode = node.selectSingleNode("result[@name='" + executeResult + "']"); + String resultTarget = resultNode.getText(); + view.setJsp(resultTarget).setParameters(resultMap); + + } catch (DocumentException e) { + e.printStackTrace(); + } 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(); + } finally { + return view; + } + } + + public static void main(String[] args) { + + } + +} diff --git a/group07/43819473/homework/src/main/java/coderising/litestruts/StrutsTest.java b/group07/43819473/homework/src/main/java/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..024b5f196f --- /dev/null +++ b/group07/43819473/homework/src/main/java/coderising/litestruts/StrutsTest.java @@ -0,0 +1,38 @@ +package 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() { + + 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/group07/43819473/homework/src/main/java/coderising/litestruts/View.java b/group07/43819473/homework/src/main/java/coderising/litestruts/View.java new file mode 100644 index 0000000000..22fdf877d8 --- /dev/null +++ b/group07/43819473/homework/src/main/java/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package 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/group07/43819473/homework/src/main/java/coderising/litestruts/struts.xml b/group07/43819473/homework/src/main/java/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..57ad66abd0 --- /dev/null +++ b/group07/43819473/homework/src/main/java/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 diff --git a/group07/43819473/homework/src/test/java/coderising/array/ArrayUtilTest.java b/group07/43819473/homework/src/test/java/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..a19eb84269 --- /dev/null +++ b/group07/43819473/homework/src/test/java/coderising/array/ArrayUtilTest.java @@ -0,0 +1,81 @@ +package coderising.array; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.*; + +/** + * Created by zj on 2017/3/4. + */ +public class ArrayUtilTest { + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void reverseArray() throws Exception { + Assert.assertArrayEquals(new int[]{3, 30, 9, 7}, ArrayUtil.reverseArray(new int[]{7, 9, 30, 3})); + Assert.assertArrayEquals(new int[]{6, 3, 30, 9, 7}, ArrayUtil.reverseArray(new int[]{7, 9, 30, 3, 6})); + } + + @Test + public void removeZero() throws Exception { + Assert.assertArrayEquals(new int[]{1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}, + ArrayUtil.removeZero(new int[]{1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5})); + } + + @Test + public void merge() throws Exception { + Assert.assertArrayEquals(new int[]{}, ArrayUtil.merge(new int[]{}, new int[]{})); + Assert.assertArrayEquals(new int[]{1, 2}, ArrayUtil.merge(new int[]{}, new int[]{1, 2})); + Assert.assertArrayEquals(new int[]{1, 2}, ArrayUtil.merge(new int[]{1, 2}, new int[]{})); + Assert.assertArrayEquals(new int[]{3, 4, 5, 6, 7, 8}, ArrayUtil.merge(new int[]{3, 5, 7, 8}, new int[]{4, 5, 6, 7})); + Assert.assertArrayEquals(new int[]{2, 3, 5, 6, 7, 8, 9}, ArrayUtil.merge(new int[]{3, 5, 7, 8}, new int[]{2, 5, 6, 9})); + Assert.assertArrayEquals(new int[]{3, 4, 5, 6, 7, 8}, ArrayUtil.merge(new int[]{4, 5, 6, 7}, new int[]{3, 5, 7, 8})); + Assert.assertArrayEquals(new int[]{2, 3, 5, 6, 7, 8, 9}, ArrayUtil.merge(new int[]{2, 5, 6, 9}, new int[]{3, 5, 7, 8})); + } + + @Test + public void grow() throws Exception { + Assert.assertArrayEquals(new int[]{2, 3, 6, 0, 0, 0}, ArrayUtil.grow(new int[]{2, 3, 6}, 3)); + } + + @Test + public void fibonacci() throws Exception { + Assert.assertArrayEquals(new int[]{}, ArrayUtil.fibonacci(1)); + Assert.assertArrayEquals(new int[]{1, 1}, ArrayUtil.fibonacci(2)); + Assert.assertArrayEquals(new int[]{1, 1, 2, 3, 5, 8, 13}, ArrayUtil.fibonacci(15)); + } + + @Test + public void getPrimes() throws Exception { + Assert.assertArrayEquals(new int[]{}, ArrayUtil.getPrimes(2)); + Assert.assertArrayEquals(new int[]{2}, ArrayUtil.getPrimes(3)); + Assert.assertArrayEquals(new int[]{2, 3}, ArrayUtil.getPrimes(4)); + Assert.assertArrayEquals(new int[]{2, 3, 5, 7, 11, 13, 17, 19}, ArrayUtil.getPrimes(23)); + } + + @Test + public void getPerfectNumbers() throws Exception { + Assert.assertArrayEquals(new int[]{6,28}, ArrayUtil.getPerfectNumbers(100)); +// System.out.println(Arrays.toString(ArrayUtil.getPerfectNumbers(40000000))); + } + + @Test + public void join() throws Exception { + Assert.assertEquals("", ArrayUtil.join(new int[]{}, "-")); + Assert.assertEquals("3", ArrayUtil.join(new int[]{3}, "-")); + Assert.assertEquals("3-8-9", ArrayUtil.join(new int[]{3, 8, 9}, "-")); + } +} \ No newline at end of file diff --git a/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyArrayList.java b/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyArrayList.java new file mode 100644 index 0000000000..fe9f8835f3 --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyArrayList.java @@ -0,0 +1,131 @@ +package com.coding.refactor.List; + +public class MyArrayList implements MyList{ + private int size = 0; + private Object[] dataArray = new Object[0]; + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public void add(T o) { + ensureCapacity(size + 1); + dataArray[size++] = o; + } + + @Override + public void add(int index, T o) { + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException(OutOfBoundsMsg(index)); + } + + ensureCapacity(size + 1); + System.arraycopy(dataArray, index, dataArray, index + 1, size - index); + dataArray[index] = o; + } + + @Override + public boolean contains(Object o) { + for(Object ele : dataArray){ + if(o.equals(ele)){ + return true; + } + } + return false; + } + + @Override + public Object[] toArray() { + Object[] newArray = new Object[size]; + System.arraycopy(dataArray, 0, newArray, 0, size); + return newArray; + } + + @Override + public boolean remove(T o) { + int index = indexOf(o); + if(index < 0){ + return false; + } + System.arraycopy(dataArray, index + 1, dataArray, index, size - 1 - index); + dataArray[--size] = null; + return true; + } + + @Override + public T remove(int index) { + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException(OutOfBoundsMsg(index)); + } + + T ele = (T)dataArray[index]; + System.arraycopy(dataArray, index, dataArray, index+1, size-1-index); + dataArray[--size] = null; + return ele; + } + + @Override + public T get(int index) { + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException(OutOfBoundsMsg(index)); + } + T ele = (T)dataArray[index]; + return ele; + } + + @Override + public T set(int index, T element) { + if(index < 0 || index >= size){ + throw new IndexOutOfBoundsException(OutOfBoundsMsg(index)); + } + dataArray[index] = element; + return element; + } + + @Override + public void clear() { + for(int i=0; i iterator() { + // TODO Auto-generated method stub + return null; + } + + private void ensureCapacity(int minCapacity) { + if(minCapacity > dataArray.length){ + int newCapacity = Math.max(minCapacity, dataArray.length * 2); + Object[] newDataArray = new Object[newCapacity]; + System.arraycopy(dataArray, 0, newDataArray, 0, dataArray.length); + dataArray = newDataArray; + } + + } + + private String OutOfBoundsMsg(int index) { + return "Index: "+index+", Size: "+size; + } + +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyIterator.java b/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyIterator.java new file mode 100644 index 0000000000..04b3691d1c --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyIterator.java @@ -0,0 +1,12 @@ +package com.coding.refactor.List; + +/** + * + * @author nelson + * + */ +public interface MyIterator { + boolean hasNext(); + + T next(); +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyList.java b/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyList.java new file mode 100644 index 0000000000..9daed38086 --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/refactor/List/MyList.java @@ -0,0 +1,35 @@ +package com.coding.refactor.List; + +/** + * + * created by nelson on 2017/03/03 + * + */ +public interface MyList { + + int size(); + + boolean isEmpty(); + + void add(T o); + + void add(int index, T o); + + boolean contains(Object o); + + Object[] toArray(); + + boolean remove(T o); + + T remove(int index); + + T get(int index); + + T set(int index, T element); + + void clear(); + + int indexOf(T o); + + MyIterator iterator(); +} diff --git a/group07/476770768/MyDataStructure/src/com/coding/refactor/List/TestFile.java b/group07/476770768/MyDataStructure/src/com/coding/refactor/List/TestFile.java new file mode 100644 index 0000000000..dbd5317f9c --- /dev/null +++ b/group07/476770768/MyDataStructure/src/com/coding/refactor/List/TestFile.java @@ -0,0 +1,11 @@ +package com.coding.refactor.List; + +public class TestFile { + + public static void main(String[] args) { + int[] a = new int[]{1,2,3,4,5}; + System.arraycopy(a, 1, a, 2, 3); + + } + +} diff --git a/group07/476770768/Week2_HOMEWORK/Coderising/src/com/LinkedList/LinkedListUtil.java b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/LinkedList/LinkedListUtil.java new file mode 100644 index 0000000000..0ada6b7ea8 --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/LinkedList/LinkedListUtil.java @@ -0,0 +1,260 @@ +package com.LinkedList; + +import java.util.Iterator; +import java.util.LinkedList; + +public class LinkedListUtil { + private Node head; + private Node current; + private int size = 0; + + public void add(T o){ + if(head == null){ + head = new Node(o); + current = head; + }else{ + Node tmp = new Node(o); + current.next = tmp; + current = tmp; + } + size++; + } + + public int[] toIntArray(){ + if(size == 0) return null; + int[] intArray = new int[this.size()]; + Node tmp = head; + for(int i=0; i get(int index){ + checkBounds(index); + if (index == 0) { + return head; + } else { + Node pos = findIndexPosition(index); + return pos; + } + } + public Object remove(int index){ + return null; + } + + public void remove(Node n){ + Node former = head; + while(former.next != n) former = former.next; + former.next = n.next; + size--; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + + } + + public void addLast(Object o){ + + } + public Node removeFirst(){ + if(head == null) return null; + Node tmp = head; + head = head.next; + this.size--; + return tmp; + } + public Object removeLast(){ + return null; + } + public Iterator iterator(){ + return null; + } + + public void checkBounds(int index) { + if (index < 0 || index > size - 1) { + // System.out.println("From MyLinkedList: Index out of bounds"); + throw new IndexOutOfBoundsException(OutOfBoundsMsg(index)); + } + } + + public String OutOfBoundsMsg(int index) { + return "Index: " + index + ", Size: " + size; + } + + public Node findIndexPosition(int index) { + Node pos = head; + for (int i = 0; i < index; i++) { + pos = pos.next; + } + return pos; + } + + + private static class Node{ + T data; + Node next; + + public Node(T o){ + this.data = o; + this.next = null; + } + + } + + /** + * 鎶婅閾捐〃閫嗙疆 + * 渚嬪閾捐〃涓 3->7->10 , 閫嗙疆鍚庡彉涓 10->7->3 + */ + public void reverse(){ + if(head == null) return; + Node newHead = head; + head = head.next; + newHead.next = null; + while(head != null){ + Node tmp = head; + head = head.next; + tmp.next = newHead; + newHead = tmp; + } + head = newHead; + } + + /** + * 鍒犻櫎涓涓崟閾捐〃鐨勫墠鍗婇儴鍒 + * 渚嬪锛歭ist = 2->5->7->8 , 鍒犻櫎浠ュ悗鐨勫间负 7->8 + * 濡傛灉list = 2->5->7->8->10 ,鍒犻櫎浠ュ悗鐨勫间负7,8,10 + + */ + public void removeFirstHalf(){ + int midLen = this.size()/2; + for(int i=0; i= size) return; + if(length < 0 ||length >= size) return; + if(i == 0){ + for(int j=0; j startNode = head; + for(int j=0; j pointer = startNode; + while(cnt101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 杩斿洖鐨勭粨鏋滃簲璇ユ槸[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + Iterator it = list.iterator(); + int[] restArray = new int[list.size()]; + int cnt = 0; + while(it.hasNext()){ + int index = it.next(); + restArray[cnt++] = (int)get(index).data; + } + return restArray; + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 浠庡綋鍓嶉摼琛ㄤ腑涓垹闄ゅ湪list涓嚭鐜扮殑鍏冪礌 + + * @param list + */ + + public void subtract(LinkedList list){ + + } + + /** + * 宸茬煡褰撳墠閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 鍒犻櫎琛ㄤ腑鎵鏈夊肩浉鍚岀殑澶氫綑鍏冪礌锛堜娇寰楁搷浣滃悗鐨勭嚎鎬ц〃涓墍鏈夊厓绱犵殑鍊煎潎涓嶇浉鍚岋級 + */ + public void removeDuplicateValues(){ + if(head == null) return; + Node cur = head.next; + Node former = head; + while(cur != null){ + if(former.data.equals(cur.data)){ + remove(cur); + cur = cur.next; + }else{ + former = former.next; + cur = cur.next; + } + } + } + + /** + * 宸茬煡閾捐〃涓殑鍏冪礌浠ュ奸掑鏈夊簭鎺掑垪锛屽苟浠ュ崟閾捐〃浣滃瓨鍌ㄧ粨鏋勩 + * 璇曞啓涓楂樻晥鐨勭畻娉曪紝鍒犻櫎琛ㄤ腑鎵鏈夊煎ぇ浜巑in涓斿皬浜巑ax鐨勫厓绱狅紙鑻ヨ〃涓瓨鍦ㄨ繖鏍风殑鍏冪礌锛 + * @param min + * @param max + */ + public void removeRange(int min, int max){ + if((int)head.data > min){ + head = findMaxLocation(head, max); + }else{ + Node former = head; + while((int)former.data < min && former != null){ + former = former.next; + } + Node behind = findMaxLocation(former.next, max); + former.next = behind; + + } + } + + public Node findMaxLocation(Node n, int max){ + Node tmp = n; + while((int)tmp.data < max && tmp != null){ + tmp = tmp.next; + size--; + } + return tmp; + } + + /** + * 鍋囪褰撳墠閾捐〃鍜屽弬鏁發ist鎸囧畾鐨勯摼琛ㄥ潎浠ュ厓绱犱緷鍊奸掑鏈夊簭鎺掑垪锛堝悓涓琛ㄤ腑鐨勫厓绱犲煎悇涓嶇浉鍚岋級 + * 鐜拌姹傜敓鎴愭柊閾捐〃C锛屽叾鍏冪礌涓哄綋鍓嶉摼琛ㄥ拰list涓厓绱犵殑浜ら泦锛屼笖琛–涓殑鍏冪礌鏈変緷鍊奸掑鏈夊簭鎺掑垪 + * @param list + */ + public LinkedList intersection(LinkedList list){ + if(head == null) return list; + + return null; + } +} diff --git a/group07/476770768/Week2_HOMEWORK/Coderising/src/com/LinkedList/LinkedListUtilTest.java b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/LinkedList/LinkedListUtilTest.java new file mode 100644 index 0000000000..66302fec61 --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/LinkedList/LinkedListUtilTest.java @@ -0,0 +1,139 @@ +package com.LinkedList; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class LinkedListUtilTest { + LinkedListUtil llu; + @Before + public void setUp() throws Exception { + llu = new LinkedListUtil(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReverse() { + llu.reverse(); + assertArrayEquals(null, llu.toIntArray()); + llu.add(3); + llu.add(7); + llu.add(10); + llu.reverse(); + assertArrayEquals(new int[]{10,7,3}, llu.toIntArray()); + } + + @Test + public void testRemoveFirstHalf() { + llu.removeFirstHalf(); + assertArrayEquals(null, llu.toIntArray()); + llu.add(2); + llu.add(5); + llu.add(7); + llu.add(8); + llu.removeFirstHalf(); + assertArrayEquals(new int[]{7,8}, llu.toIntArray()); + llu = new LinkedListUtil(); + llu.add(2); + llu.add(5); + llu.add(7); + llu.add(8); + llu.add(10); + llu.removeFirstHalf(); + assertArrayEquals(new int[]{7,8,10}, llu.toIntArray()); + + } + + @Test + public void testRemoveIntInt() { + llu.add(2); + llu.add(5); + llu.add(7); + llu.add(8); + llu.remove(1, 2); + assertArrayEquals(new int[]{2,8}, llu.toIntArray()); + } + + @Test + public void testGetElements() { + llu.add(11); + llu.add(101); + llu.add(201); + llu.add(301); + llu.add(401); + llu.add(501); + llu.add(601); + llu.add(701); + llu.add(801); + LinkedList l = new LinkedList(); + l.add(1); + l.add(3); + l.add(4); + l.add(6); + assertArrayEquals(new int[]{101,301,401,601}, llu.getElements(l)); + } + + @Test + public void testSubtract() { + fail("Not yet implemented"); + } + + @Test + public void testRemoveDuplicateValues() { + llu.add(1); + llu.add(1); + llu.add(2); + llu.add(3); + llu.add(3); + llu.add(5); + llu.add(6); + llu.add(6); + llu.add(8); + llu.removeDuplicateValues(); + assertArrayEquals(new int[]{1,2,3,5,6,8}, llu.toIntArray()); + + } + + @Test + public void testRemoveRange() { + llu.add(1); + llu.add(1); + llu.add(2); + llu.add(3); + llu.add(4); + llu.add(5); + llu.add(6); + llu.add(6); + llu.add(8); + llu.removeRange(2, 5); + assertArrayEquals(new int[]{1,1,2,5,6,6,8}, llu.toIntArray()); + llu = new LinkedListUtil(); + llu.add(2); + llu.add(2); + llu.add(2); + llu.add(3); + llu.add(4); + llu.add(5); + llu.add(6); + llu.add(6); + llu.add(8); + llu.removeRange(1, 5); + assertArrayEquals(new int[]{5,6,6,8}, llu.toIntArray()); + + } + + @Test + public void testIntersection() { + fail("Not yet implemented"); + } + +} diff --git a/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/LoginAction.java b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/LoginAction.java new file mode 100644 index 0000000000..d18ebabe40 --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/LoginAction.java @@ -0,0 +1,40 @@ +package com.litestruts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author chenran + * + */ +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/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/Struts.java b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/Struts.java new file mode 100644 index 0000000000..1715392c91 --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/Struts.java @@ -0,0 +1,297 @@ +package com.litestruts; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +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.NamedNodeMap; +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) { + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + try { + String path = "./src/com/litestruts/struts.xml"; + Document document = readStruts(path); + NodeList actionList = document.getElementsByTagName("action"); + String actionClassName = getActionClassName(actionName, actionList); + //System.out.println(actionClassName); + Class c = Class.forName(actionClassName); + Object o = c.newInstance(); + String result = executeAction(c, o, actionClassName, parameters); + String message = getMessage(c, o); + String jsp = getJSP(document, result, actionName, actionList); + Map m = new HashMap(); + m.put("message", message); + View view = new View(); + view.setJsp(jsp); + view.setParameters(m); + return view; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } + return null; + } + + /** + * get jsp message + * @param document + * @param result + * @param actionName + * @param actionList + * @return + */ + private static String getJSP(Document document, String result, String actionName, NodeList actionList) { + Node actionNode = getactionNode(actionName, actionList); + NodeList childNodes = actionNode.getChildNodes(); + String jsp = getContent(result, childNodes); + return jsp; + } + + /** + * + * @param result + * @param nodes: nodelist of childNode for action node + * @return + */ + private static String getContent(String result, NodeList nodes) { + String jsp; + if(result.equals("success")){ + jsp = getResultNodeForSuccess(nodes); + }else{ + jsp = getResultNode(result, nodes); + } + return jsp; + } + + /** + * get the certain result node while result is success + * @param nodes + * @return + */ + private static String getResultNodeForSuccess(NodeList nodes) { + for(int i=0; i parameters){ + try { + + setVar(c, o, parameters); + Method m = c.getMethod("execute"); + String message = (String)m.invoke(o); + return message; + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return null; + } + + /** + * set variables of action class instance + * @param c + * @param o + * @param parameters + */ + public static void setVar(Class c, Object o, Map parameters){ + Method[] methods = c.getMethods(); + Iterator> entries = parameters.entrySet().iterator(); + while(entries.hasNext()){ + Map.Entry entry = entries.next(); + String methodName = "set" + entry.getKey(); + for(Method m : methods){ + if(m.getName().equalsIgnoreCase(methodName)){ + try { + m.invoke(o, entry.getValue()); + //System.out.println(m.getName()); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + } +} diff --git a/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/StrutsTest.java b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/StrutsTest.java new file mode 100644 index 0000000000..8c8fdeda8d --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/StrutsTest.java @@ -0,0 +1,38 @@ +package com.litestruts; + +import org.junit.Test; +import java.util.HashMap; +import java.util.Map; +import org.junit.Assert; + +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/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/View.java b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/View.java new file mode 100644 index 0000000000..a7494563ef --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/View.java @@ -0,0 +1,23 @@ +package com.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/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/struts.xml b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/litestruts/struts.xml new file mode 100644 index 0000000000..daef9740c1 --- /dev/null +++ b/group07/476770768/Week2_HOMEWORK/Coderising/src/com/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/group07/562247675/homework/homework-0305/pom.xml b/group07/562247675/homework/homework-0305/pom.xml new file mode 100644 index 0000000000..655bab68b6 --- /dev/null +++ b/group07/562247675/homework/homework-0305/pom.xml @@ -0,0 +1,26 @@ + + + + com.coding2017.group7 + homework + 1.0-SNAPSHOT + + 4.0.0 + + com.coding2017.group7 + homework-0305 + + + junit + junit + + + dom4j + dom4j + + + + \ No newline at end of file diff --git a/group07/562247675/homework/homework-0305/src/main/java/com/coderising/array/MyArrayUtil.java b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/array/MyArrayUtil.java new file mode 100644 index 0000000000..6e60b10eb9 --- /dev/null +++ b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/array/MyArrayUtil.java @@ -0,0 +1,267 @@ +package com.coderising.array; + +import java.util.Arrays; + +public final class MyArrayUtil { + + public static void main(String[] args) { + int[] reverse1 = {7, 9, 30, 3}; + int[] reverse2 = {7, 9, 30, 3, 4}; + reverseArray(reverse1); + reverseArray(reverse2); + System.out.println("reverseArray: " + Arrays.toString(reverse1) + " " + Arrays.toString(reverse2)); + int[] removeZero = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + System.out.println("removeZero: " + Arrays.toString(removeZero(removeZero))); + int[] merge1 = {3, 5, 7, 8}; + int[] merge2 = {4, 5, 6, 7}; + System.out.println("merge: " + Arrays.toString(merge(merge1, merge2))); + System.out.println("merge: " + Arrays.toString(merge(merge2, merge1))); + int[] grow = {2, 3, 6}; + System.out.println("grow: " + Arrays.toString(grow(grow, 3))); + System.out.println("fibonacci: f(15)=" + Arrays.toString(fibonacci(15)) + " f(1)=" + Arrays.toString(fibonacci(1))); + System.out.println("getPrimes: g(23)=" + Arrays.toString(getPrimes(23))); + System.out.println("getPerfectNumbers: g(500)=" + Arrays.toString(getPerfectNumbers(500))); + System.out.println("getPerfectNumbers: j(f(50))=" + join(fibonacci(50), "-")); + + } + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + * 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public static void reverseArray(int[] origin) { + int len = origin.length; + for (int i = 0; i < len / 2; i++) { + int j = len - 1 - i; + int temp = origin[i]; + origin[i] = origin[j]; + origin[j] = 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 static int[] removeZero(int[] oldArray) { + int length = oldArray.length; + // 绌洪棿鎹㈡椂闂村鏉傚害 O(2*n-m) n涓烘暟缁勯暱搴 m涓0鐨勪釜鏁 + int[] src = new int[length]; + int last = 0; + for (int i = 0; i < length; i++) { + int m = oldArray[i]; + if (m != 0) { + src[last++] = m; + } + } + int[] dest = new int[last]; + System.arraycopy(src, 0, dest, 0, last); + return dest; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * + * @param array1 + * @param array2 + * @return + */ + public static int[] merge(int[] array1, int[] array2) { + int[] src = new int[array1.length + array2.length]; + int i = 0, j = 0, k = 0; //i=鏁扮粍1娓告爣 j=鏁扮粍2娓告爣 k=鐩爣鏁扮粍涓嬫爣 + // 鏃堕棿澶嶆潅搴 O(n+m-k) n涓烘暟缁1闀垮害 m涓烘暟缁2闀垮害 k涓洪噸澶嶆暟缁勪釜鏁 + while (i < array1.length && j < array2.length) { + if (array1[i] < array2[j]) { //澶у皬姣旇緝鍏ョ洰鏍囨暟缁 + src[k++] = array1[i++]; + } else if (array1[i] > array2[j]) { + src[k++] = array2[j++]; + } else { //绛夊煎悓鏃剁Щ鍔紝浣嗗彧鍏1涓洰鏍囧 + src[k++] = array1[i++]; + j++; + } + } + // 鍙1涓暟缁勪笅琛ㄨ秺鐣岋紝灏辩洿鎺ユ坊鍔犲彟1涓暟缁勫熬閮ㄦ墍鏈夋湁搴忓 + while (i < array1.length) { + src[k++] = array1[i++]; + } + while (j < array2.length) { + src[k++] = array2[j++]; + } + int[] dest = new int[k]; + System.arraycopy(src, 0, dest, 0, k); + return dest; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + int[] newArr = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArr, 0, oldArray.length); + return newArr; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max < 2) { // 棰濆鍑哄彛 f(1) = [] + return new int[]{}; + } + if (max == 2) { //閫掑綊鍑哄彛 f(2) = [1, 1] + return new int[]{1, 1}; + } + // f(3) = [1,1,2] + // f(5) = f(4) = [1,1,2,3] + // f(8) = f(7) = f(6) = [1,1,2,3,5] + + // 閫掑綊鎷夸笂涓暟鍒楁渶鍚庣殑涓や釜鏁 + int[] src = fibonacci(max - 1); + // 鍒ゆ柇杩欎袱涓暟鐨勫拰鍊兼槸鍚﹀湪max鑼冨洿鍐 + int m = src[src.length - 1] + src[src.length - 2]; + if (m < max) { + int[] dest = new int[src.length + 1]; + System.arraycopy(src, 0, dest, 0, src.length); + dest[src.length] = m; + return dest; + } else { + return src; + } + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + if (max <= 2) { //鐗规畩鍑哄彛 g(1) = [] + return new int[]{}; + } + if (max == 3) { //閫掑綊鍑哄彛 g(3) = [2] + return new int[]{2}; + } + // g[3] = [2] + // g[5] = g[4] = [2,3] + // g[7] = g[6] = [2,3,5] + // g[11] = g[10] = g[9] = g[8] = [2,3,5,7] + + // 閫掑綊鎷夸笂涓暟缁勬渶鍚庣殑绱犳暟 + int[] src = getPrimes(max - 1); + // 鎵句笂涓礌鏁板拰鏈澶у间箣闂寸殑绱犳暟 + int before = src[src.length - 1]; + int next = before; + int start = 1 + before; + int end = max; + for (int num = start; num < end; num++) { + int i = 2; + for (; i <= num / 2; i++) { + if (num % i == 0) { + break; + } + } + if (i > num / 2) { + next = num; + break; + } + } + // 鍒ゆ柇杩欎釜鍊兼槸鍚﹀瓨鍦 + if (next > before) { + int[] dest = new int[src.length + 1]; + System.arraycopy(src, 0, dest, 0, src.length); + dest[src.length] = next; + return dest; + } else { + return src; + } + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + if (max <= 6) { //鐗规畩鍑哄彛 + return new int[]{}; + } + if (max == 7) { //閫掑綊鍑哄彛 + return new int[]{6}; + } + // 閫掑綊鎷夸笂涓暟缁勬渶鍚庣殑瀹屾暟 + int[] src = getPerfectNumbers(max - 1); + // 鎵句笂涓畬鏁板拰鏈澶у间箣闂寸殑瀹屾暟 + int before = src[src.length - 1]; + int next = before; + int start = 1 + before; + int end = max; + for (int num = start; num < end; num++) { + int count = 0; + for (int i = 1; i <= num / 2; i++) { + if (num % i == 0) { + count += i; + } + } + if (count == num) { + next = num; + break; + } + } + // 鍒ゆ柇杩欎釜鍊兼槸鍚﹀瓨鍦 + if (next > before) { + int[] dest = new int[src.length + 1]; + System.arraycopy(src, 0, dest, 0, src.length); + dest[src.length] = next; + return dest; + } else { + return src; + } + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public static String join(int[] array, String seperator) { + StringBuilder sBuilder = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + sBuilder.append(array[i]).append(seperator); + } + if (sBuilder.length() > 0) { + sBuilder.deleteCharAt(sBuilder.length() - 1); + } + return sBuilder.toString(); + } + + +} diff --git a/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/LoginAction.java b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..bf31612b74 --- /dev/null +++ b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,41 @@ +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/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/MyStruts.java b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/MyStruts.java new file mode 100644 index 0000000000..74354e8bce --- /dev/null +++ b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/MyStruts.java @@ -0,0 +1,200 @@ +package com.coderising.litestruts; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + + +public class MyStruts { + + public static View runAction(String actionName, Map parameters) { + MyStrutsActioNode actioNode = strutsActions.get(actionName); + String actionString = actioNode.actionClass; + Class actionClass = null; + Object actionBean = null; + try { + actionClass = Class.forName(actionString); + actionBean = actionClass.newInstance(); + } catch (Exception e) { + e.printStackTrace(); + } + if (actionBean == null) return null; // 娌℃湁榛樿鏋勯犳垨娌℃湁鎵惧埌瀛楄妭鐮佺殑鎯呭喌 + Map setterMethods = getSetterMethods(actionClass); + // #1.璋冪敤action涓殑set鏂规硶璁剧疆鍊 + if (parameters != null && !parameters.isEmpty()) { + for (Map.Entry entry : parameters.entrySet()) { + String fieldName = entry.getKey(); + String fieldValue = entry.getValue(); + Method setterMethod = setterMethods.get(fieldName); + if (setterMethod != null) { + try { + setterMethod.invoke(actionBean, fieldValue); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } + // #2.璋冪敤action鐨別xecute鏂规硶 + Method method = getExecuteMethod(actionClass); + if (method == null) return null; // 鏈0鏄巈xecute鏂规硶 + String jspName = ""; + try { + Object object = method.invoke(actionBean); // 鑾峰彇execute鏂规硶杩斿洖鍊 + if (object instanceof String) { + jspName = (String) object; + + } + } catch (Exception e) { + e.printStackTrace(); + } + // #3.鏌ユ壘execute鏂规硶杩斿洖鍊煎搴旂殑jsp璺緞 + View view = new View(); + String jspValue = actioNode.actionResults.get(jspName).value; + view.setJsp(jspValue); + + // #4.鑾峰彇action涓殑get鏂规硶杩斿洖鍊煎皝瑁呭埌view瀵硅薄涓 + Map getterMethods = getGetterMethods(actionClass); + if (getterMethods != null && !getterMethods.isEmpty()) { + HashMap resultMap = new HashMap(getterMethods.size()); + for (Map.Entry entry : getterMethods.entrySet()) { + String fieldName = entry.getKey(); + Method getterMethod = entry.getValue(); + try { + Object object = getterMethod.invoke(actionBean); + resultMap.put(fieldName, object); + } catch (Exception e) { + e.printStackTrace(); + } + } + view.setParameters(resultMap); + } + return view; + } + + private static Method getExecuteMethod(Class beanClass) { + Method method = null; + try { + method = beanClass.getMethod("execute"); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + return method; + } + + private static Map getGetterMethods(Class beanClass) { + Map resultMap = Collections.emptyMap(); + Map descriptorMap = getPropertyDescriptors(beanClass); + if (descriptorMap != null && !descriptorMap.isEmpty()) { + resultMap = new HashMap(descriptorMap.size() / 2); + for (PropertyDescriptor descriptor : descriptorMap.values()) { + String fieldName = descriptor.getName(); + Method getterMethod = descriptor.getReadMethod(); + resultMap.put(fieldName, getterMethod); + } + } + return resultMap; + } + + private static Map getSetterMethods(Class beanClass) { + Map resultMap = Collections.emptyMap(); + Map descriptorMap = getPropertyDescriptors(beanClass); + if (descriptorMap != null && !descriptorMap.isEmpty()) { + resultMap = new HashMap(descriptorMap.size() / 2); + for (PropertyDescriptor descriptor : descriptorMap.values()) { + String fieldName = descriptor.getName(); + Method setterMethod = descriptor.getWriteMethod(); + resultMap.put(fieldName, setterMethod); + } + } + return resultMap; + } + + private static Map getPropertyDescriptors(Class beanClass) { + Map resultMap = Collections.emptyMap(); + try { + BeanInfo beanInfo = Introspector.getBeanInfo(beanClass); + PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); + resultMap = new HashMap(propertyDescriptors.length); + for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { + String fieldName = propertyDescriptor.getName(); + resultMap.put(fieldName, propertyDescriptor); + } + } catch (IntrospectionException e) { + e.printStackTrace(); + } + return resultMap; + } + + private static Map strutsActions; + + static { + InputStream input = null; + try { + // dom4j瑙f瀽xml鏂囦欢灏佽鍒癑avaBean + input = MyStruts.class.getClassLoader().getResourceAsStream("struts.xml"); + SAXReader saxReader = new SAXReader(); + Document document = saxReader.read(input); + strutsActions = new HashMap(); + Element root = document.getRootElement(); + // 鏍囩 + for (Iterator i = root.elementIterator("action"); i.hasNext(); ) { + Element actionXmlNode = (Element) i.next(); + MyStrutsActioNode actionNode = new MyStrutsActioNode(); + actionNode.actionName = actionXmlNode.attributeValue("name"); + actionNode.actionClass = actionXmlNode.attributeValue("class"); + strutsActions.put(actionNode.actionName, actionNode); + actionNode.actionResults = new HashMap(); + // 鏍囩 + for (Iterator j = actionXmlNode.elementIterator("result"); j.hasNext(); ) { + Element resultXmlNode = (Element) j.next(); + MyActionResultNode resultNode = new MyActionResultNode(); + resultNode.name = resultXmlNode.attributeValue("name"); + resultNode.value = resultXmlNode.getStringValue(); + actionNode.actionResults.put(resultNode.name, resultNode); + } + } + } catch (DocumentException e) { + e.printStackTrace(); + } finally { + if (input != null) { + try { + input.close(); + } catch (IOException e) { + // close quietly.. + } + } + } + } + + public static void main(String[] args) { + + } + + private static class MyStrutsActioNode { + private String actionName; + private String actionClass; + private Map actionResults; + + } + + private static class MyActionResultNode { + private String name; + private String value; + + } + +} diff --git a/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/MyStrutsTest.java b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/MyStrutsTest.java new file mode 100644 index 0000000000..a7e6b63019 --- /dev/null +++ b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/MyStrutsTest.java @@ -0,0 +1,40 @@ +package com.coderising.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + +public class MyStrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + + View view = MyStruts.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 = MyStruts.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/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/View.java b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..ce596da106 --- /dev/null +++ b/group07/562247675/homework/homework-0305/src/main/java/com/coderising/litestruts/View.java @@ -0,0 +1,26 @@ +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/group07/562247675/homework/homework-0305/src/main/resources/struts.xml b/group07/562247675/homework/homework-0305/src/main/resources/struts.xml new file mode 100644 index 0000000000..8a9789665d --- /dev/null +++ b/group07/562247675/homework/homework-0305/src/main/resources/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/group07/562247675/homework/pom.xml b/group07/562247675/homework/pom.xml index 17a2c5f0fd..343c7dbe71 100644 --- a/group07/562247675/homework/pom.xml +++ b/group07/562247675/homework/pom.xml @@ -12,6 +12,7 @@ homework-0226 + homework-0305 pom @@ -22,11 +23,24 @@ + junit junit 4.12 + + + dom4j + dom4j + 1.6.1 + + + + jaxen + jaxen + 1.1.6 + diff --git a/group07/724319952/src/cn/fyl/second/ArrayUtil.java b/group07/724319952/src/cn/fyl/second/ArrayUtil.java new file mode 100644 index 0000000000..a816ff4acd --- /dev/null +++ b/group07/724319952/src/cn/fyl/second/ArrayUtil.java @@ -0,0 +1,235 @@ +package cn.fyl.second; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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 = new int[origin.length]; + + for (int i = 0,j = origin.length - 1; i < origin.length; i++,j--) { + //渚濇灏唎rigin鐨勫艰祴缁欎笌temp鍓嶅悗瀵瑰簲浣嶇疆涓 + temp[i] = origin[j]; + System.out.print(temp[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){ + //淇濆瓨oldArray鏁扮粍鍘0鍚庣殑瀹归噺 + int size = 0; + + for (int i = 0; i < oldArray.length; i++) { + int temp = oldArray[i]; + if(temp != 0){ + size++; + } + } + + //鍒濆鍖栦笌oldArray鏁扮粍鍘0鍚庡閲忎竴鏍风殑鏂版暟缁 + int[] newArray = new int[size]; + + for (int i = 0,j = 0; i < oldArray.length; i++) { + int temp = oldArray[i]; + //灏嗕笉绛変簬0鐨勫艰祴缁欐柊鏁扮粍 + if(temp != 0){ + newArray[j] = temp; + j++; + } + } + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 + * 骞朵笖浠嶇劧鏄湁搴忕殑銆 + * 渚嬪 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 size = array1.length + array2.length; + + int[] newArray = new int[size]; + + //鍏堝皢array1鏁扮粍瀛樺叆temp鏁扮粍 + for (int i = 0; i < array1.length; i++) { + newArray[i] = array1[i]; + } + + //澧0锛岃嫢鐩稿悓鐨勫兼槸鏁扮粍涓掓暟鐨勪簩涓殑鍊硷紝鍒欓渶瑕佷竴涓浂灏嗘渶鍚庝竴涓鐩栨帀 + array2 = grow(array2, 1); + + //灏哸rray1鏁扮粍涓瘡涓涓间緷娆′笌array2鏁扮粍涓瘡涓涓繘琛屾瘮杈冿紝鑻ョ浉鍚岋紝鍒欏皢鐩稿悓鐨勫艰鐩栨帀 + for (int i = 0; i < array1.length; i++) { + for (int j = 0; j < array2.length; j++) { + if(array1[i] == array2[j]){ + for (int k = j; k < array2.length - 1; k++) { + array2[k] = array2[k + 1]; + } + } + } + } + //鍘0 + array2 = removeZero(array2); + + //灏哸rray2鐨勫兼彃鍏ewArray鏁扮粍涓 + for (int i = 0; i < array2.length; i++) { + newArray[array1.length] = array2[i]; + } + + newArray = removeZero(newArray); + + //鍐掓场鎺掑簭 + for (int i = 0; i < newArray.length - 1; i++) { + for (int j = 0; j < newArray.length - 1; j++) { + if(newArray[j] > newArray[j + 1]){ + int temp = newArray[j + 1]; + newArray[j+ 1] = newArray[j]; + newArray[j] = temp; + } + } + } + + return newArray; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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[] array = {1,1,2,3,5,8,13,21}; + int[] newArray = null; + for (int i = 0; i < array.length; i++) { + if(array[i] > max){ + newArray = new int[i]; + System.arraycopy(array, 0, newArray, 0, i); + break; + } + } + return newArray; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + int[] array = {0}; + if(max > 2){ + array[0] = 2; + int k = 1; + for (int n = 3; n < max; n++) { + int i = 2; + + while(i < n){ + //鑻ョ瓑浜0锛屽垯涓嶆槸绱犳暟锛岃烦鍑哄惊鐜 + if(n % i == 0) + break; + + i++; + } + + //濡傛灉i==n鍒欒鏄巒涓嶈兘琚2~n-1鏁撮櫎锛屾槸绱犳暟 + if (i == n) { + //鏁扮粍鑷涓涓閲 + array =grow(array, 1); + //灏嗙礌鏁板姞鍏ユ暟缁 + array[k] = n; + k++; + } + } + return array; + } + else{ + return null; + } + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + + int a =0; + for(int i=2;i> 1); - - for(int i=0; i<1; i++) { - System.out.println("dd"); - } - - Stack stack = new Stack(); - - Queue queue; - TreeNode treeNode; - - List lstint = new ArrayList(); - lstint.add(1); - lstint.add(2); - lstint.add(3); - - // Iterator閬嶅巻涓 - Iterator iterator = lstint.iterator(); - iterator.hasNext(); - while (iterator.hasNext()) - { - int i = (Integer) iterator.next(); - System.out.println(i); - } - - - } -} - \ No newline at end of file diff --git a/group07/752262774/src/com/coderising/array/ArrayUtil.java b/group07/752262774/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..91419d3128 --- /dev/null +++ b/group07/752262774/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,237 @@ +package com.coderising.array; + + +import java.util.ArrayList; + +/** + * Created by yrs on 2017/3/3. + */ +public class ArrayUtil { + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + if(null == origin || 0 == origin.length || 1 == origin.length) { + return; + }else { + for(int i=0; i < (origin.length/2); i++) { + int stmp = origin[i]; + origin[i] = origin[origin.length-1-i]; + origin[origin.length-1-i] = stmp; + } + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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){ + if(null == oldArray || 0 == oldArray.length) { + return oldArray; + } + ArrayList stmp = new ArrayList(); + for(int item: oldArray) { + if(item != 0) + stmp.add(item); + } + int[] newArray = arrayListToIntArray(stmp); + + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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){ + ArrayList arrayList = new ArrayList(); + int i=0,j=0; + while (i arrayList) { + int[] array = new int[arrayList.size()]; + for(int i=0; i arrayList = new ArrayList(); + for(int i=1; i<=Math.sqrt(a); i++) { + if(a % i == 0) { + arrayList.add(i); + arrayList.add(a / i); + } + } + int sum = 0; + for(int item: arrayList) { + sum += item; + } + if(sum-a == a) { + return true; + } + + return false; + } +} + \ No newline at end of file diff --git a/group07/752262774/src/com/coderising/array/ArrayUtilTest.java b/group07/752262774/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..ecad9623b8 --- /dev/null +++ b/group07/752262774/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,95 @@ +package com.coderising.array; + +/** + * Created by yrs on 2017/3/4. + */ +public class ArrayUtilTest { + + @org.junit.Test + public void testReverseArray() throws Exception { + int[] array = new int[5]; + for(int i=0; i<5; i++) { + array[i] = i; + } + ArrayUtil util = new ArrayUtil(); + util.reverseArray(array); + for(int i=0; i<5; i++) { + assert (array[i] == 4-i); + } + } + + @org.junit.Test + public void testRemoveZero() throws Exception { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + ArrayUtil util = new ArrayUtil(); + int[] newArr = util.removeZero(oldArr); + for(int item: newArr) { + System.out.println(item); + assert (item != 0); + } + } + + @org.junit.Test + public void testMerge() throws Exception { + int[] a1 = {3,5,7,8,22}; + int[] a2 = {4,5,6,7,8,9,11}; + int[] a3 = {3,4,5,6,7,8,9,11,22}; + ArrayUtil util = new ArrayUtil(); + int[] merge = util.merge(a1, a2); + for(int i=0;i targetMap = null; + + public static View runAction(String actionName, Map parameters) throws Exception { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + targetMap = getTargetMap(actionName); + if(!targetMap.containsKey(actionName)) { + throw new Exception("this xml file not found" + actionName + "action"); + } + String className = targetMap.get(actionName); + Class classEntity = Class.forName(className); + Object obj = classEntity.newInstance(); + Field[] fields = classEntity.getDeclaredFields(); + + for(Field field: fields) { + field.setAccessible(true); + if(parameters.containsKey(field.getName())) { + field.set(obj, parameters.get(field.getName())); + } + } + + Method execute = classEntity.getMethod("execute"); + String result = (String)execute.invoke(obj); + String jsp = targetMap.get(result); + Field message = classEntity.getDeclaredField("message"); + message.setAccessible(true); + Map parats = new HashMap(); + parats.put("message", (String)message.get(obj)); + + View view = new View(); + view.setJsp(jsp); + view.setParameters(parats); + + return view; + } + + private static Map getTargetMap(String actionName) throws DocumentException { + Map targetMap = new HashMap(); + //鍒涘缓SAXReader瀵硅薄 + SAXReader reader = new SAXReader(); + //璇诲彇鏂囦欢 杞崲鎴怐ocument + Document document = reader.read(new File("src/com/coderising/litestruts/struts.xml")); + //鑾峰彇鏍硅妭鐐瑰厓绱犲璞 + Element root = document.getRootElement(); + List actionList = root.elements("action"); + for(Element action: actionList) { + org.dom4j.Attribute attribute = action.attribute("name"); + if(attribute.getValue().equals("login")){ + targetMap.put(attribute.getValue(), action.attribute("class").getValue()); + List resultList = action.elements("result"); + for(Element result: resultList) { + targetMap.put(result.attribute("name").getValue(),""+result.getData()); + } + break; + } + } + return targetMap; + } + +} diff --git a/group07/752262774/src/com/coderising/litestruts/StrutsTest.java b/group07/752262774/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..70c71c2ab0 --- /dev/null +++ b/group07/752262774/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,53 @@ +package com.coderising.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = null; + try { + view = Struts.runAction(actionName, params); + } catch (Exception e) { + e.printStackTrace(); + } + + 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 = null; + try { + view = Struts.runAction(actionName, params); + } catch (Exception e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group07/752262774/src/com/coderising/litestruts/View.java b/group07/752262774/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group07/752262774/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/group07/752262774/src/com/coderising/litestruts/struts.xml b/group07/752262774/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..6f23f0a83d --- /dev/null +++ b/group07/752262774/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 diff --git a/group07/752262774/2.26/coding/src/main/java/ArrayList.java b/group07/752262774/src/com/coding/basic/ArrayList.java similarity index 99% rename from group07/752262774/2.26/coding/src/main/java/ArrayList.java rename to group07/752262774/src/com/coding/basic/ArrayList.java index a7b0f43aed..59b22d2c53 100644 --- a/group07/752262774/2.26/coding/src/main/java/ArrayList.java +++ b/group07/752262774/src/com/coding/basic/ArrayList.java @@ -1,4 +1,4 @@ -package main.java; +package com.coding.basic; import java.util.Arrays; import java.util.NoSuchElementException; diff --git a/group07/752262774/2.26/coding/src/main/java/BinaryTreeNode.java b/group07/752262774/src/com/coding/basic/BinaryTreeNode.java similarity index 97% rename from group07/752262774/2.26/coding/src/main/java/BinaryTreeNode.java rename to group07/752262774/src/com/coding/basic/BinaryTreeNode.java index cf8ade9193..275a1e66f9 100644 --- a/group07/752262774/2.26/coding/src/main/java/BinaryTreeNode.java +++ b/group07/752262774/src/com/coding/basic/BinaryTreeNode.java @@ -1,4 +1,4 @@ -package main.java; +package com.coding.basic; /** * Created by yrs on 2017/2/25. diff --git a/group07/752262774/2.26/coding/src/main/java/Iterator.java b/group07/752262774/src/com/coding/basic/Iterator.java similarity index 84% rename from group07/752262774/2.26/coding/src/main/java/Iterator.java rename to group07/752262774/src/com/coding/basic/Iterator.java index 74a0b35573..c13e8e3b35 100644 --- a/group07/752262774/2.26/coding/src/main/java/Iterator.java +++ b/group07/752262774/src/com/coding/basic/Iterator.java @@ -1,4 +1,4 @@ -package main.java; +package com.coding.basic; /** * Created by yrs on 2017/2/25. diff --git a/group07/752262774/2.26/coding/src/main/java/LinkedList.java b/group07/752262774/src/com/coding/basic/LinkedList.java similarity index 99% rename from group07/752262774/2.26/coding/src/main/java/LinkedList.java rename to group07/752262774/src/com/coding/basic/LinkedList.java index 4f37c1a31c..60831251d3 100644 --- a/group07/752262774/2.26/coding/src/main/java/LinkedList.java +++ b/group07/752262774/src/com/coding/basic/LinkedList.java @@ -1,4 +1,4 @@ -package main.java; +package com.coding.basic; import java.util.NoSuchElementException; diff --git a/group07/752262774/2.26/coding/src/main/java/List.java b/group07/752262774/src/com/coding/basic/List.java similarity index 90% rename from group07/752262774/2.26/coding/src/main/java/List.java rename to group07/752262774/src/com/coding/basic/List.java index 0f1d979332..55658370ae 100644 --- a/group07/752262774/2.26/coding/src/main/java/List.java +++ b/group07/752262774/src/com/coding/basic/List.java @@ -1,4 +1,4 @@ -package main.java; +package com.coding.basic; /** * Created by yrs on 2017/2/23. diff --git a/group07/752262774/2.26/coding/src/main/java/Queue.java b/group07/752262774/src/com/coding/basic/Queue.java similarity index 94% rename from group07/752262774/2.26/coding/src/main/java/Queue.java rename to group07/752262774/src/com/coding/basic/Queue.java index 59a120f51c..c967b45fa2 100644 --- a/group07/752262774/2.26/coding/src/main/java/Queue.java +++ b/group07/752262774/src/com/coding/basic/Queue.java @@ -1,4 +1,4 @@ -package main.java; +package com.coding.basic; /** * Created by yrs on 2017/2/25. diff --git a/group07/752262774/2.26/coding/src/main/java/Stack.java b/group07/752262774/src/com/coding/basic/Stack.java similarity index 96% rename from group07/752262774/2.26/coding/src/main/java/Stack.java rename to group07/752262774/src/com/coding/basic/Stack.java index efaa2498b9..d47eb07427 100644 --- a/group07/752262774/2.26/coding/src/main/java/Stack.java +++ b/group07/752262774/src/com/coding/basic/Stack.java @@ -1,4 +1,4 @@ -package main.java; +package com.coding.basic; /** * Created by yrs on 2017/2/25. diff --git a/group07/764189149/.classpath b/group07/764189149/.classpath index fb5011632c..1fc7a9a529 100644 --- a/group07/764189149/.classpath +++ b/group07/764189149/.classpath @@ -2,5 +2,9 @@ + + + + diff --git a/group07/764189149/src/com/coderising/download/DownloadThread.java b/group07/764189149/src/com/coderising/download/DownloadThread.java new file mode 100644 index 0000000000..d9514ac5cf --- /dev/null +++ b/group07/764189149/src/com/coderising/download/DownloadThread.java @@ -0,0 +1,38 @@ +package com.coderising.download; + +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + CyclicBarrier barrier ; + String filePath; + + public DownloadThread(CyclicBarrier barrier , Connection conn, int startPos, int endPos , String filePath){ + + this.barrier = barrier; + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.filePath = filePath; + } + public void run(){ + try{ + System.out.println("begin download startPos="+startPos+",endPos="+endPos); + byte[] buffer = conn.read(startPos , endPos); + RandomAccessFile file = new RandomAccessFile(filePath, "rw"); + file.seek(startPos); + file.write(buffer, 0, buffer.length); + file.close(); + barrier.await(); + }catch(Exception e){ + System.out.println("download error:startPos="+startPos+",endPos="+endPos); + } + + } +} diff --git a/group07/764189149/src/com/coderising/download/FileDownloader.java b/group07/764189149/src/com/coderising/download/FileDownloader.java new file mode 100644 index 0000000000..f63b39d0af --- /dev/null +++ b/group07/764189149/src/com/coderising/download/FileDownloader.java @@ -0,0 +1,127 @@ +package com.coderising.download; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.concurrent.CyclicBarrier; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; + + +public class FileDownloader { + + boolean isFinished = false; + + String url; + + DownloadListener listener; + + ConnectionManager cm; + + private static final int THREAD_NUM = 3; + + private static final String BASE_PATH = "F:/download/"; + + + public FileDownloader(String _url) { + this.url = _url; + File baseFile = new File(BASE_PATH); + if(!baseFile.exists()){ + baseFile.mkdirs(); + } + + } + + public void execute(){ + // 鍦ㄨ繖閲屽疄鐜颁綘鐨勪唬鐮侊紝 娉ㄦ剰锛 闇瑕佺敤澶氱嚎绋嬪疄鐜颁笅杞 + // 杩欎釜绫讳緷璧栦簬鍏朵粬鍑犱釜鎺ュ彛, 浣犻渶瑕佸啓杩欏嚑涓帴鍙g殑瀹炵幇浠g爜 + // (1) ConnectionManager , 鍙互鎵撳紑涓涓繛鎺ワ紝閫氳繃Connection鍙互璇诲彇鍏朵腑鐨勪竴娈碉紙鐢╯tartPos, endPos鏉ユ寚瀹氾級 + // (2) DownloadListener, 鐢变簬鏄绾跨▼涓嬭浇锛 璋冪敤杩欎釜绫荤殑瀹㈡埛绔笉鐭ラ亾浠涔堟椂鍊欑粨鏉燂紝鎵浠ヤ綘闇瑕佸疄鐜板綋鎵鏈 + // 绾跨▼閮芥墽琛屽畬浠ュ悗锛 璋冪敤listener鐨刵otifiedFinished鏂规硶锛 杩欐牱瀹㈡埛绔氨鑳芥敹鍒伴氱煡銆 + // 鍏蜂綋鐨勫疄鐜版濊矾锛 + // 1. 闇瑕佽皟鐢–onnectionManager鐨刼pen鏂规硶鎵撳紑杩炴帴锛 鐒跺悗閫氳繃Connection.getContentLength鏂规硶鑾峰緱鏂囦欢鐨勯暱搴 + // 2. 鑷冲皯鍚姩3涓嚎绋嬩笅杞斤紝 娉ㄦ剰姣忎釜绾跨▼闇瑕佸厛璋冪敤ConnectionManager鐨刼pen鏂规硶 + // 鐒跺悗璋冪敤read鏂规硶锛 read鏂规硶涓湁璇诲彇鏂囦欢鐨勫紑濮嬩綅缃拰缁撴潫浣嶇疆鐨勫弬鏁帮紝 杩斿洖鍊兼槸byte[]鏁扮粍 + // 3. 鎶奲yte鏁扮粍鍐欏叆鍒版枃浠朵腑 + // 4. 鎵鏈夌殑绾跨▼閮戒笅杞藉畬鎴愪互鍚庯紝 闇瑕佽皟鐢╨istener鐨刵otifiedFinished鏂规硶 + + // 涓嬮潰鐨勪唬鐮佹槸绀轰緥浠g爜锛 涔熷氨鏄鍙湁涓涓嚎绋嬶紝 浣犻渶瑕佹敼閫犳垚澶氱嚎绋嬬殑銆 + CyclicBarrier barrier = new CyclicBarrier(THREAD_NUM, new Runnable() { + + @Override + public void run() { + listener.notifyFinished(); + } + }); + Connection conn = null; + try { + + conn = cm.open(this.url); + + int length = conn.getContentLength(); + String filePath = BASE_PATH + "download."+getFileType(this.url); + //System.out.println(filePath); + + File file = new File(filePath); + if(!file.exists()){ + try { + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + } + try{ + FileOutputStream fos = new FileOutputStream(file); + fos.write(new byte[length], 0, length);//鍗犱綅 + fos.close(); + } + catch (IOException e) { + System.out.println(e.getMessage()); + } + + int blockSize = (length % THREAD_NUM == 0 ) ? length / THREAD_NUM : (length / THREAD_NUM + 1); + for (int i = 0; i < THREAD_NUM; i++) { + int startPos = i * blockSize; + int endPos = startPos + blockSize - 1; + if(endPos >= length - 1){ + endPos = length - 1; + } + new DownloadThread(barrier , conn, startPos, endPos , filePath).start(); + } + + } catch (ConnectionException e) { + System.out.println(e.getMessage()); + } finally{ + if(conn != null){ + conn.close(); + } + } + + + + + } + + private String getFileType(String url) { + int index = url.lastIndexOf("."); + return url.substring(index + 1 , url.length()); + } + + public void setListener(DownloadListener listener) { + this.listener = listener; + } + + + + public void setConnectionManager(ConnectionManager ucm){ + this.cm = ucm; + } + + public DownloadListener getListener(){ + return this.listener; + } + +} diff --git a/group07/764189149/src/com/coderising/download/FileDownloaderTest.java b/group07/764189149/src/com/coderising/download/FileDownloaderTest.java new file mode 100644 index 0000000000..5e325db7a4 --- /dev/null +++ b/group07/764189149/src/com/coderising/download/FileDownloaderTest.java @@ -0,0 +1,55 @@ +package com.coderising.download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coderising.download.api.ConnectionManager; +import com.coderising.download.api.DownloadListener; +import com.coderising.download.impl.ConnectionManagerImpl; + +public class FileDownloaderTest { + boolean downloadFinished = false; + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testDownload() { + + String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1488796402240&di=8ca9322617d5338cad61232a06f6ed7a&imgtype=0&src=http%3A%2F%2Fjiangsu.china.com.cn%2Fuploadfile%2F2017%2F0212%2F1486868426284307.jpg"; + FileDownloader downloader = new FileDownloader(url); + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + downloader.setListener(new DownloadListener() { + @Override + public void notifyFinished() { + downloadFinished = true; + } + + }); + + + downloader.execute(); + + // 绛夊緟澶氱嚎绋嬩笅杞界▼搴忔墽琛屽畬姣 + while (!downloadFinished) { + try { + System.out.println("杩樻病鏈変笅杞藉畬鎴愶紝浼戠湢浜旂"); + //浼戠湢5绉 + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("涓嬭浇瀹屾垚锛"); + + + + } + +} diff --git a/group07/764189149/src/com/coderising/download/api/Connection.java b/group07/764189149/src/com/coderising/download/api/Connection.java new file mode 100644 index 0000000000..0957eaf7f4 --- /dev/null +++ b/group07/764189149/src/com/coderising/download/api/Connection.java @@ -0,0 +1,23 @@ +package com.coderising.download.api; + +import java.io.IOException; + +public interface Connection { + /** + * 缁欏畾寮濮嬪拰缁撴潫浣嶇疆锛 璇诲彇鏁版嵁锛 杩斿洖鍊兼槸瀛楄妭鏁扮粍 + * @param startPos 寮濮嬩綅缃紝 浠0寮濮 + * @param endPos 缁撴潫浣嶇疆 + * @return + */ + public byte[] read(int startPos,int endPos) throws IOException; + /** + * 寰楀埌鏁版嵁鍐呭鐨勯暱搴 + * @return + */ + public int getContentLength(); + + /** + * 鍏抽棴杩炴帴 + */ + public void close(); +} diff --git a/group07/764189149/src/com/coderising/download/api/ConnectionException.java b/group07/764189149/src/com/coderising/download/api/ConnectionException.java new file mode 100644 index 0000000000..c2a0b14c82 --- /dev/null +++ b/group07/764189149/src/com/coderising/download/api/ConnectionException.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public class ConnectionException extends Exception { + String message; + public ConnectionException(String message){ + super("message:"+message); + this.message = message; + } + +} diff --git a/group07/764189149/src/com/coderising/download/api/ConnectionManager.java b/group07/764189149/src/com/coderising/download/api/ConnectionManager.java new file mode 100644 index 0000000000..ce045393b1 --- /dev/null +++ b/group07/764189149/src/com/coderising/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package com.coderising.download.api; + +public interface ConnectionManager { + /** + * 缁欏畾涓涓猽rl , 鎵撳紑涓涓繛鎺 + * @param url + * @return + */ + public Connection open(String url) throws ConnectionException; +} diff --git a/group07/764189149/src/com/coderising/download/api/DownloadListener.java b/group07/764189149/src/com/coderising/download/api/DownloadListener.java new file mode 100644 index 0000000000..bf9807b307 --- /dev/null +++ b/group07/764189149/src/com/coderising/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package com.coderising.download.api; + +public interface DownloadListener { + public void notifyFinished(); +} diff --git a/group07/764189149/src/com/coderising/download/impl/ConnectionImpl.java b/group07/764189149/src/com/coderising/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..8881230f80 --- /dev/null +++ b/group07/764189149/src/com/coderising/download/impl/ConnectionImpl.java @@ -0,0 +1,40 @@ +package com.coderising.download.impl; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URLConnection; + +import com.coderising.download.api.Connection; + +public class ConnectionImpl implements Connection{ + + private URLConnection connection; + + @Override + public byte[] read(int startPos, int endPos) throws IOException { +// connection.setAllowUserInteraction(true); +// connection.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + InputStream inputstream = connection.getInputStream(); + byte[] buffer = new byte[endPos - startPos + 1]; + inputstream.skip(startPos); + inputstream.read(buffer); + inputstream.close(); + return buffer; + } + + @Override + public int getContentLength(){ + return connection.getContentLength(); + } + + @Override + public void close() { + } + + public void setConnection(URLConnection connection) { + this.connection = connection; + } + + + +} diff --git a/group07/764189149/src/com/coderising/download/impl/ConnectionManagerImpl.java b/group07/764189149/src/com/coderising/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..20dfaaad80 --- /dev/null +++ b/group07/764189149/src/com/coderising/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,30 @@ +package com.coderising.download.impl; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; + +import com.coderising.download.api.Connection; +import com.coderising.download.api.ConnectionException; +import com.coderising.download.api.ConnectionManager; + +public class ConnectionManagerImpl implements ConnectionManager { + + @Override + public Connection open(String url) throws ConnectionException { + URL urlObj; + ConnectionImpl connection = null; + try { + urlObj = new URL(url); + connection = new ConnectionImpl(); + connection.setConnection(urlObj.openConnection()); + } catch (MalformedURLException e) { + throw new ConnectionException("URL鏍煎紡閿欒"); + }catch (IOException e) { + throw new ConnectionException("IO寮傚父"); + } + + return connection; + } + +} diff --git a/group07/764189149/src/com/leijing/coderising/array/ArrayUtil.java b/group07/764189149/src/com/leijing/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..544b788ffb --- /dev/null +++ b/group07/764189149/src/com/leijing/coderising/array/ArrayUtil.java @@ -0,0 +1,279 @@ +package com.leijing.coderising.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +/** + * @author: leijing + * @date: 2017骞3鏈2鏃 涓嬪崍5:30:38 + */ +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin + * @return + * @throws Exception + */ + public void reverseArray(int[] origin) throws Exception{ + if(origin.length < 1){ + throw new Exception("array origin is empty"); + } + int size = origin.length; + int[] copy = Arrays.copyOf(origin, size); + for (int i = 0; i < size; i++) { + origin[size - i - 1] = copy[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 newSize = countNotZero(oldArray);//鍏堣绠楅潪闆跺厓绱犱釜鏁 + int[] newArray = new int[newSize];//鍒涘缓鏂版暟缁 + int newIndex = 0;//鏂版暟缁勭储寮 + int oldIndex = 0;//鏃ф暟缁勭储寮 + while(newIndex < newSize){ + if(oldArray[oldIndex] != 0){ + newArray[newIndex++] = oldArray[oldIndex]; + } + + oldIndex++; + } + + return newArray; + } + + private int countNotZero(int[] oldArray) { + int count = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i] != 0){ + count++; + } + } + return count; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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 size = getSize(array1 , array2); + int[] newArray = new int[size]; + + int minIndex1 = 0; + int minIndex2 = 0; + + for (int i = 0; i < newArray.length; i++) { + + if(minIndex1 < array1.length && minIndex2 == array2.length ){//濡傛灉array2鐨勫厓绱犲彇瀹屼簡锛屽氨鍙朼rray1鐨勫厓绱 + newArray[i] = array1[minIndex1++]; + }else if(minIndex1 == array1.length && minIndex2 < array2.length){//濡傛灉array1鐨勫厓绱犲彇瀹屼簡锛屽氨鍙朼rray2鐨勫厓绱 + newArray[i] = array2[minIndex2++]; + }else{//涓や釜鏁扮粍閮芥病鏈夊彇瀹 + if(array1[minIndex1] < array2[minIndex2]){//绗竴涓暟缁勭殑鍏冪礌灏 + newArray[i] = array1[minIndex1++]; + }else if(array1[minIndex1] == array2[minIndex2]){//鍏冪礌鐩哥瓑锛屼繚鐣欑涓涓暟缁勭殑锛岀浜屼釜鏁扮粍绱㈠紩鍔犱竴 + newArray[i] = array1[minIndex1++]; + minIndex2++; + } + else{//绗簩涓暟缁勫厓绱犲皬 + newArray[i] = array2[minIndex2++]; + } + } + } + + return newArray; + } + + private int getSize(int[] array1 , int[] array2) { + Set set = new HashSet(); + int sameNum = 0; + + for (int i = 0; i < array1.length; i++) { + set.add(array1[i]); + } + + for (int i = 0; i < array2.length; i++) { + if(!set.add(array2[i])){ + sameNum++; + } + } + return array1.length + array2.length - sameNum; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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){ + if(size < 1){ + return oldArray; + } + int[] newArray = new int[oldArray.length + size]; + + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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]; + } + List list = new ArrayList(); + list.add(1); + list.add(1); + + int next = 0; + int i = 2; + while(next < max) { + next = list.get(i - 1) + list.get(i - 2 ); + list.add(next); + i++; + } + int[] array = new int[list.size()]; + list2Array(list , array); + return array; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + List list = new ArrayList(); + for (int i = 2; i < max; i++) { + if(isPrimes(i)){ + list.add(i); + } + } + int[] array = new int[list.size()]; + list2Array(list , array); + return array; + } + + private boolean isPrimes(int num) { + for (int i = 2; i <= num / 2; i++) { + if(num % i == 0){ + return false; + } + } + return true; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + + List list = new ArrayList(); + for (int i = 1; i < max; i++) { + if(isPerfectNum(i)){ + list.add(i); + } + } + int[] array = new int[list.size()]; + list2Array(list , array); + return array; + } + + private void list2Array(List list, int[] array) { + for (int i = 0; i < list.size(); i++) { + array[i] = list.get(i); + } + } + + private boolean isPerfectNum(int num) { + Set list = new HashSet(); + if(num == 1){ + return false; + }else{ + list.add(1); + } + for (int i = 2; i <= num / 2; i++) { + if(num % i == 0){ + list.add(i); + list.add(num / i); + } + } + Iterator iter = list.iterator(); + int sum = 0; + while (iter.hasNext()) { + Integer val = iter.next(); + sum += val; + } + + if(sum == num){ + System.out.println("num:"+num+","+list.toString()); + return true; + } + return false; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + sb.append(array[i]); + if(i < array.length - 1){ + sb.append(seperator); + } + } + return sb.toString(); + } + + public void printArray(int[] array , String msg){ + System.out.print(msg+"["); + for (int i = 0; i < array.length; i++) { + System.out.print(array[i]); + if(i < array.length - 1){ + System.out.print(","); + } + } + System.out.println("]"); + } + + +} diff --git a/group07/764189149/src/com/leijing/coderising/array/ArrayUtilTest.java b/group07/764189149/src/com/leijing/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..235ffb0f15 --- /dev/null +++ b/group07/764189149/src/com/leijing/coderising/array/ArrayUtilTest.java @@ -0,0 +1,77 @@ +package com.leijing.coderising.array; + +import org.junit.Test; + +public class ArrayUtilTest { + private ArrayUtil arrayUtil = new ArrayUtil(); + + @Test + public void testReverseArray() { + int[] origin = {7, 9 , 30, 3 , 18 , 21}; + arrayUtil.printArray(origin,"before:"); + + try { + arrayUtil.reverseArray(origin); + arrayUtil.printArray(origin , "after:"); + } catch (Exception e) { + System.out.println(e); + } + } + + @Test + public void testRemoveZero() { + int[] oldArray={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + arrayUtil.printArray(oldArray , "before:"); + int[] newArray = arrayUtil.removeZero(oldArray); + arrayUtil.printArray(newArray , "after:"); + } + + @Test + public void testMerge() { + int[] array1 = {3, 5, 7,8}; + int[] array2 = {4, 5, 6,7}; + arrayUtil.printArray(array1 , "array1:"); + arrayUtil.printArray(array2 , "array2:"); + int[] newArray = arrayUtil.merge(array1, array2); + arrayUtil.printArray(newArray , "mergre after:"); + } + + @Test + public void testGrow() { + int[] oldArray = {2,3,6}; + int size = 3; + arrayUtil.printArray(oldArray , "grow before:"); + int[] newArray = arrayUtil.grow(oldArray, size); + arrayUtil.printArray(newArray , "grow after:"); + } + + @Test + public void testFibonacci() { + int max = 20; + int[] array = arrayUtil.fibonacci(max); + arrayUtil.printArray(array , max + "浠ュ唴鐨勬墍鏈夋枑娉㈤偅濂戞暟鍒:"); + } + + @Test + public void testGetPrimes() { + int max = 100; + int[] array = arrayUtil.getPrimes(max); + arrayUtil.printArray(array , max + "浠ュ唴鐨勬墍鏈夌礌鏁:"); + } + + @Test + public void testGetPerfectNumbers() { + int max = 1000; + int[] array = arrayUtil.getPerfectNumbers(max); + arrayUtil.printArray(array , max + "浠ュ唴鐨勬墍鏈夊畬鏁:"); + } + + @Test + public void testJoin() { + int[] array = {3,8,9}; + String seperator = "-"; + String result = arrayUtil.join(array, seperator); + System.out.println(result); + } + +} diff --git a/group07/764189149/src/com/leijing/coderising/litestruts/LoginAction.java b/group07/764189149/src/com/leijing/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..833e1464b4 --- /dev/null +++ b/group07/764189149/src/com/leijing/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.leijing.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/group07/764189149/src/com/leijing/coderising/litestruts/Struts.java b/group07/764189149/src/com/leijing/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..fbabd397ed --- /dev/null +++ b/group07/764189149/src/com/leijing/coderising/litestruts/Struts.java @@ -0,0 +1,134 @@ +package com.leijing.coderising.litestruts; + +import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + +/** + * @Description: 瑙f瀽Struts鏂囦欢銆佸鐞哸ction璇锋眰 + * @author: leijing + * @date: 2017骞3鏈3鏃 涓嬪崍3:03:38 + */ +public class Struts { + private static String STRUTS_XML = "struts.xml"; + + private static Map actionsMap = new HashMap(); + private static Map resultMap = new HashMap(); + private Struts(){ + try { + init(); + } catch (Exception e) { + System.out.println("Struts init error:"+e.getMessage()); + } + + } + + /** + * @Description: 纭繚閰嶇疆鐨勬槸闇瑕佺殑鑺傜偣 + * @param xmlName + * @param name + * @return: void + * @author: leijing + * @date: 2017骞3鏈3鏃 涓嬪崍3:20:31 + */ + private void checkTag(String xmlName , String name) throws Exception{ + if(xmlName == null || name == null || !xmlName.equals(name)){ + throw new Exception("xml parse error,"+xmlName+"can not be parsed,"+name+" is required!"); + } + } + private void init() throws Exception{ + InputStream inputStream = this.getClass().getResourceAsStream(STRUTS_XML); + SAXReader reader = new SAXReader();//鍒涘缓SAXReader瀵硅薄 + Document document = reader.read(inputStream); //鐢⊿AXReader瀵硅薄鍔犺浇閰嶇疆鏂囦欢寰楀埌Document瀵硅薄 + Element struts = document.getRootElement();//寰楀埌鏍硅妭鐐 + checkTag(struts.getName(), "struts"); + List actions = struts.elements();//寰楀埌鎵鏈塧ction鑺傜偣 + + for(Iterator it = actions.iterator() ; it.hasNext();){//閬嶅巻action鑺傜偣 + Element action = (Element) it.next(); + checkTag(action.getName(), "action"); + Attribute actionName = action.attribute("name"); + Attribute actionClazz = action.attribute("class"); + actionsMap.put(actionName.getText(), actionClazz.getText()); + System.out.println("add to actionsMap:"+actionName.getText()+":"+actionClazz.getText()); + + List results = action.elements();//寰楀埌閰嶇疆鐨剅esult鑺傜偣 + for(Iterator rit = results.iterator() ; rit.hasNext();){//閬嶅巻result鑺傜偣 + Element result = (Element) rit.next(); + checkTag(result.getName(), "result"); + Attribute resultName = result.attribute("name"); + resultMap.put(actionName.getText()+"_"+resultName.getText() , result.getData().toString()); + System.out.println("add to resultMap: result:"+resultName.getText()+",url:"+result.getData()); + } + } + + + } + + public static View runAction(String actionName, Map parameters) throws Exception { + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + if(!actionsMap.containsKey(actionName)){ + throw new Exception("action "+actionName+"not found!"); + } + + String className = actionsMap.get(actionName); + Class clazz = Class.forName(className); + Object obj = clazz.newInstance(); + Field[] fields = clazz.getDeclaredFields(); + + for (Field field : fields) { + field.setAccessible(true); + if(parameters.containsKey(field.getName())){ + field.set(obj, parameters.get(field.getName())); + } + } + Method execute = clazz.getDeclaredMethod("execute"); + String result = (String) execute.invoke(obj, new Object[]{}); + String jsp = resultMap.get(actionName+"_"+result); + if(null == jsp){ + throw new Exception("jsp config at action:"+actionName +",result:"+ result+" not found !"); + } + Field message = clazz.getDeclaredField("message"); + message.setAccessible(true); + View view = new View(); + view.setJsp(jsp); + Map params = new HashMap(); + params.put("message", message.get(obj)); + view.setParameters(params); + return view; + } + public static Struts getInstance(){ + return StrutsHoldler.INSTANCE; + } + private static class StrutsHoldler{ + private static Struts INSTANCE = new Struts();//struts瀵硅薄搴旇鏄崟渚嬬殑 + } +} diff --git a/group07/764189149/src/com/leijing/coderising/litestruts/StrutsTest.java b/group07/764189149/src/com/leijing/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..787d2bcd28 --- /dev/null +++ b/group07/764189149/src/com/leijing/coderising/litestruts/StrutsTest.java @@ -0,0 +1,44 @@ +package com.leijing.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + + + + + +public class StrutsTest { +private Struts struts = Struts.getInstance();//struts瀵硅薄搴旇鏄崟渚嬬殑 + + @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/group07/764189149/src/com/leijing/coderising/litestruts/View.java b/group07/764189149/src/com/leijing/coderising/litestruts/View.java new file mode 100644 index 0000000000..3411bf9130 --- /dev/null +++ b/group07/764189149/src/com/leijing/coderising/litestruts/View.java @@ -0,0 +1,38 @@ +package com.leijing.coderising.litestruts; + +import java.util.Map; +import java.util.Set; + +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; + } + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("jsp:").append(jsp); + if(null == parameters){ + return sb.toString(); + } + Set keys = parameters.keySet(); + + for (String key : keys) { + sb.append(",").append(key).append(":").append(parameters.get(key)); + } + return sb.toString(); + } +} diff --git a/group07/764189149/src/com/leijing/coderising/litestruts/struts.xml b/group07/764189149/src/com/leijing/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..0ef54148e1 --- /dev/null +++ b/group07/764189149/src/com/leijing/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 diff --git a/group10/1363044717/struts/homework/homework/pom.xml b/group10/1363044717/struts/homework/homework/pom.xml new file mode 100644 index 0000000000..54675f52ac --- /dev/null +++ b/group10/1363044717/struts/homework/homework/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com + code + 1.0 + + + junit + junit + 4.12 + + + dom4j + dom4j + 1.1 + + + + \ No newline at end of file diff --git a/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/array/ArrayUtil.java b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..aee11e5122 --- /dev/null +++ b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/array/ArrayUtil.java @@ -0,0 +1,318 @@ +package com.code.coderising.array; + +import org.junit.Test; +import java.util.Arrays; + +/** + * Created by Mori on 2017/3/2. + */ +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 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, j = n >> 1; i < j; i++) { + int temp = origin[i]; + origin[i] = origin[n - i - 1]; + origin[n - i - 1] = temp; + } + } + + @Test + public void testReverseArray() { + int[] a = {7, 9, 30, 3}; + System.out.println("鍘熸暟缁:" + Arrays.toString(a)); + reverseArray(a); + System.out.println("reverseArray鍚:" + Arrays.toString(a)); + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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) { + Arrays.sort(oldArray); + int[] newArr = null; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArr = new int[oldArray.length - i]; + System.arraycopy(oldArray, i, newArr, 0, oldArray.length - i); + break; + } + } + return newArr; + } + + @Test + public void testRemoveZero() { + int[] a = {7, 9, 0, 30, 0, 0, 3}; + System.out.println("鍘熸暟缁:" + Arrays.toString(a)); + int[] newArr = removeZero(a); + System.out.println("removeZero鍚:" + Arrays.toString(newArr)); + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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 m = array1.length; + int n = array2.length; + int[] newArr = new int[m + n]; + int i = 0, j = 0, k = 0; + for (; i < m && j < n; ) { + if (array1[i] < array2[j]) { + newArr[k++] = array1[i++]; + } else { + newArr[k++] = array2[j++]; + } + + } + while (i < m) { + newArr[k++] = array1[i++]; + } + while (j < n) { + newArr[k++] = array2[j++]; + } + if (newArr.length < 1) + return null; + int slow = 1; + int count = 0; + for (int fast = 1; fast < newArr.length; fast++) { + if (newArr[fast] != newArr[slow - 1]) { + newArr[slow++] = newArr[fast]; + } else { + count++; + } + } + int[] arr = new int[newArr.length - count]; + System.arraycopy(newArr, 0, arr, 0, newArr.length - count); + return arr; + } + + @Test + public void testMerge() { + int[] a1 = {3, 5, 7, 8, 10, 10, 10, 10, 10}; + int[] a2 = {4, 5, 6, 7, 10, 12}; + int[] newArray = merge(a1, a2); + System.out.println("merge:" + Arrays.toString(newArray)); + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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; + } + + @Test + public void testGrow() { + int[] arr = {2, 3, 6}; + int[] newArray = grow(arr, 3); + System.out.println("grow:" + Arrays.toString(newArray)); + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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 a = 1; + int b = 1; + int temp; + StringBuilder str = new StringBuilder("1,1,"); + while (a + b < max) { + temp = b; + b = a + b; + a = temp; + str.append(b + ","); + } + String[] arr = str.toString().split(","); + int[] newArray = new int[arr.length]; + for (int i = 0; i < arr.length; i++) { + newArray[i] = Integer.valueOf(arr[i]); + } + return newArray; + } + + @Test + public void testFibonacci() { + int[] newArray = fibonacci(500); + System.out.println("Fibonacci:" + Arrays.toString(newArray)); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max) { + StringBuilder str = new StringBuilder(); + for (int i = 2; i < max; i++) { + if (i < 4) { + str.append(i + ","); + continue; + } else if (i % 2 == 0) { + continue; + } else if (i < 9) { + str.append(i + ","); + continue; + } else if (i % 3 == 0) { + continue; + } else { + int f = 5; + boolean flag = true; + while (f <= i/f) { + if (i % f == 0) { + flag = false; + break; + } else if (i % (f + 2) == 0) { + flag = false; + break; + } + f += 6; + } + if (flag) { + str.append(i + ","); + continue; + } + } + } + String[] arr = str.toString().split(","); + int[] newArray = new int[arr.length]; + for (int i = 0; i < arr.length; i++) { + newArray[i] = Integer.valueOf(arr[i]); + } + return newArray; + } + + @Test + public void testGetPrimes() { + int[] newArray = getPrimes(5000); + System.out.println("getPrimes:" + Arrays.toString(newArray)); + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + if(max<7){ + return new int[0]; + } + boolean flag = true; + StringBuilder str = new StringBuilder("6,"); + for (int i = 6; i < max; ) { + int sum = 1; + if (i % 3 == 1 && i % 9 == 1) { + //濡傛灉浠8缁撳熬锛岄偅涔堝氨鑲畾鏄互28缁撳熬 + if(!flag){ + if(i%100!=28){ + i += 8; + flag = true; + continue; + } + } + for (int j = 2; j <= i / j; j++) { + if (i % j == 0) { + sum += j; + sum += i / j; + } + } + if (sum == i) { + str.append(i+","); + } + } + if (flag) { + i += 2; + flag = false; + } else { + i += 8; + flag = true; + } + } + String[] arr = str.toString().split(","); + int[] newArray = new int[arr.length]; + for (int i = 0; i < arr.length; i++) { + newArray[i] = Integer.valueOf(arr[i]); + } + return newArray; + } + + @Test + public void testGetPerfectNumbers() { + int[] newArray = getPerfectNumbers(33550337); + System.out.println("getPerfectNumbers:" + Arrays.toString(newArray)); + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + if (array == null) + return "null"; + int n = array.length - 1; + if (n == -1) { + return ""; + } + StringBuilder b = new StringBuilder(); + for (int i = 0; ; i++) { + b.append(array[i]); + if (i == n) { + return b.toString(); + } + b.append(seperator); + } + } + + @Test + public void testJoin() { + int[] a1 = {3, 5, 7}; + System.out.println(join(a1, "-")); + } + +} diff --git a/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/LoginAction.java b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..cd1753941c --- /dev/null +++ b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.code.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/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/Struts.java b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..262b4bca59 --- /dev/null +++ b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/Struts.java @@ -0,0 +1,79 @@ +package com.code.coderising.litestruts; +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + SAXReader reader = new SAXReader(); + View view=new View(); + try { + Document e = reader.read(ClassLoader.getSystemResource("struts.xml")); + //寰楀埌鏍硅妭鐐 + Element struts = e.getRootElement(); + Iterator it = struts.elementIterator(); + while(it.hasNext()) {//action閬嶅巻 + Element action = (Element)it.next(); + // 鎵惧埌 name=actionName 鐨刟ction + List actionAtr = action.attributes(); + if(!actionAtr.get(0).getValue().equals(actionName)) + continue; + String className= actionAtr.get(1).getValue(); + //寰楀埌actionName 瀵瑰簲鐨刢lass + Class classz=Class.forName(className); + Object o=classz.newInstance(); + Field field; + for(Map.Entry entry:parameters.entrySet()){ + field = classz.getDeclaredField(entry.getKey()); + field.setAccessible(true); + field.set(o, entry.getValue()); + } + Method m = classz.getMethod("execute"); + String resultString=(String)m.invoke(o); + Method m2 = classz.getMethod("getMessage"); + String message=(String)m2.invoke(o); + + Map map=new HashMap<>(); + + map.put("message",message); + view.setParameters(map); + it=action.elementIterator(); + //閬嶅巻 + while (it.hasNext()){ + Element result = (Element)it.next(); + String s= result.attribute(0).getValue(); + if(resultString.equals(s)){ + view.setJsp(result.getStringValue()); + return view; + } + } + } + } catch (DocumentException var9) { + var9.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/StrutsTest.java b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..fe414cf1a7 --- /dev/null +++ b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.code.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() { + + 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/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/View.java b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/View.java new file mode 100644 index 0000000000..f55dae45fa --- /dev/null +++ b/group10/1363044717/struts/homework/homework/src/main/java/com/code/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.code.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/group10/1363044717/struts/homework/homework/src/main/resources/struts.xml b/group10/1363044717/struts/homework/homework/src/main/resources/struts.xml new file mode 100644 index 0000000000..b9489dad6d --- /dev/null +++ b/group10/1363044717/struts/homework/homework/src/main/resources/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/group10/1363044717/struts/homework/pom.xml b/group10/1363044717/struts/homework/pom.xml new file mode 100644 index 0000000000..54675f52ac --- /dev/null +++ b/group10/1363044717/struts/homework/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com + code + 1.0 + + + junit + junit + 4.12 + + + dom4j + dom4j + 1.1 + + + + \ No newline at end of file diff --git a/group10/1363044717/struts/homework/src/main/java/com/code/coderising/array/ArrayUtil.java b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..aee11e5122 --- /dev/null +++ b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/array/ArrayUtil.java @@ -0,0 +1,318 @@ +package com.code.coderising.array; + +import org.junit.Test; +import java.util.Arrays; + +/** + * Created by Mori on 2017/3/2. + */ +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 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, j = n >> 1; i < j; i++) { + int temp = origin[i]; + origin[i] = origin[n - i - 1]; + origin[n - i - 1] = temp; + } + } + + @Test + public void testReverseArray() { + int[] a = {7, 9, 30, 3}; + System.out.println("鍘熸暟缁:" + Arrays.toString(a)); + reverseArray(a); + System.out.println("reverseArray鍚:" + Arrays.toString(a)); + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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) { + Arrays.sort(oldArray); + int[] newArr = null; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArr = new int[oldArray.length - i]; + System.arraycopy(oldArray, i, newArr, 0, oldArray.length - i); + break; + } + } + return newArr; + } + + @Test + public void testRemoveZero() { + int[] a = {7, 9, 0, 30, 0, 0, 3}; + System.out.println("鍘熸暟缁:" + Arrays.toString(a)); + int[] newArr = removeZero(a); + System.out.println("removeZero鍚:" + Arrays.toString(newArr)); + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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 m = array1.length; + int n = array2.length; + int[] newArr = new int[m + n]; + int i = 0, j = 0, k = 0; + for (; i < m && j < n; ) { + if (array1[i] < array2[j]) { + newArr[k++] = array1[i++]; + } else { + newArr[k++] = array2[j++]; + } + + } + while (i < m) { + newArr[k++] = array1[i++]; + } + while (j < n) { + newArr[k++] = array2[j++]; + } + if (newArr.length < 1) + return null; + int slow = 1; + int count = 0; + for (int fast = 1; fast < newArr.length; fast++) { + if (newArr[fast] != newArr[slow - 1]) { + newArr[slow++] = newArr[fast]; + } else { + count++; + } + } + int[] arr = new int[newArr.length - count]; + System.arraycopy(newArr, 0, arr, 0, newArr.length - count); + return arr; + } + + @Test + public void testMerge() { + int[] a1 = {3, 5, 7, 8, 10, 10, 10, 10, 10}; + int[] a2 = {4, 5, 6, 7, 10, 12}; + int[] newArray = merge(a1, a2); + System.out.println("merge:" + Arrays.toString(newArray)); + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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; + } + + @Test + public void testGrow() { + int[] arr = {2, 3, 6}; + int[] newArray = grow(arr, 3); + System.out.println("grow:" + Arrays.toString(newArray)); + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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 a = 1; + int b = 1; + int temp; + StringBuilder str = new StringBuilder("1,1,"); + while (a + b < max) { + temp = b; + b = a + b; + a = temp; + str.append(b + ","); + } + String[] arr = str.toString().split(","); + int[] newArray = new int[arr.length]; + for (int i = 0; i < arr.length; i++) { + newArray[i] = Integer.valueOf(arr[i]); + } + return newArray; + } + + @Test + public void testFibonacci() { + int[] newArray = fibonacci(500); + System.out.println("Fibonacci:" + Arrays.toString(newArray)); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max) { + StringBuilder str = new StringBuilder(); + for (int i = 2; i < max; i++) { + if (i < 4) { + str.append(i + ","); + continue; + } else if (i % 2 == 0) { + continue; + } else if (i < 9) { + str.append(i + ","); + continue; + } else if (i % 3 == 0) { + continue; + } else { + int f = 5; + boolean flag = true; + while (f <= i/f) { + if (i % f == 0) { + flag = false; + break; + } else if (i % (f + 2) == 0) { + flag = false; + break; + } + f += 6; + } + if (flag) { + str.append(i + ","); + continue; + } + } + } + String[] arr = str.toString().split(","); + int[] newArray = new int[arr.length]; + for (int i = 0; i < arr.length; i++) { + newArray[i] = Integer.valueOf(arr[i]); + } + return newArray; + } + + @Test + public void testGetPrimes() { + int[] newArray = getPrimes(5000); + System.out.println("getPrimes:" + Arrays.toString(newArray)); + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + if(max<7){ + return new int[0]; + } + boolean flag = true; + StringBuilder str = new StringBuilder("6,"); + for (int i = 6; i < max; ) { + int sum = 1; + if (i % 3 == 1 && i % 9 == 1) { + //濡傛灉浠8缁撳熬锛岄偅涔堝氨鑲畾鏄互28缁撳熬 + if(!flag){ + if(i%100!=28){ + i += 8; + flag = true; + continue; + } + } + for (int j = 2; j <= i / j; j++) { + if (i % j == 0) { + sum += j; + sum += i / j; + } + } + if (sum == i) { + str.append(i+","); + } + } + if (flag) { + i += 2; + flag = false; + } else { + i += 8; + flag = true; + } + } + String[] arr = str.toString().split(","); + int[] newArray = new int[arr.length]; + for (int i = 0; i < arr.length; i++) { + newArray[i] = Integer.valueOf(arr[i]); + } + return newArray; + } + + @Test + public void testGetPerfectNumbers() { + int[] newArray = getPerfectNumbers(33550337); + System.out.println("getPerfectNumbers:" + Arrays.toString(newArray)); + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + if (array == null) + return "null"; + int n = array.length - 1; + if (n == -1) { + return ""; + } + StringBuilder b = new StringBuilder(); + for (int i = 0; ; i++) { + b.append(array[i]); + if (i == n) { + return b.toString(); + } + b.append(seperator); + } + } + + @Test + public void testJoin() { + int[] a1 = {3, 5, 7}; + System.out.println(join(a1, "-")); + } + +} diff --git a/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/LoginAction.java b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..cd1753941c --- /dev/null +++ b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.code.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/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/Struts.java b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..262b4bca59 --- /dev/null +++ b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/Struts.java @@ -0,0 +1,79 @@ +package com.code.coderising.litestruts; +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + SAXReader reader = new SAXReader(); + View view=new View(); + try { + Document e = reader.read(ClassLoader.getSystemResource("struts.xml")); + //寰楀埌鏍硅妭鐐 + Element struts = e.getRootElement(); + Iterator it = struts.elementIterator(); + while(it.hasNext()) {//action閬嶅巻 + Element action = (Element)it.next(); + // 鎵惧埌 name=actionName 鐨刟ction + List actionAtr = action.attributes(); + if(!actionAtr.get(0).getValue().equals(actionName)) + continue; + String className= actionAtr.get(1).getValue(); + //寰楀埌actionName 瀵瑰簲鐨刢lass + Class classz=Class.forName(className); + Object o=classz.newInstance(); + Field field; + for(Map.Entry entry:parameters.entrySet()){ + field = classz.getDeclaredField(entry.getKey()); + field.setAccessible(true); + field.set(o, entry.getValue()); + } + Method m = classz.getMethod("execute"); + String resultString=(String)m.invoke(o); + Method m2 = classz.getMethod("getMessage"); + String message=(String)m2.invoke(o); + + Map map=new HashMap<>(); + + map.put("message",message); + view.setParameters(map); + it=action.elementIterator(); + //閬嶅巻 + while (it.hasNext()){ + Element result = (Element)it.next(); + String s= result.attribute(0).getValue(); + if(resultString.equals(s)){ + view.setJsp(result.getStringValue()); + return view; + } + } + } + } catch (DocumentException var9) { + var9.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/StrutsTest.java b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..fe414cf1a7 --- /dev/null +++ b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.code.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() { + + 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/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/View.java b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/View.java new file mode 100644 index 0000000000..f55dae45fa --- /dev/null +++ b/group10/1363044717/struts/homework/src/main/java/com/code/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package com.code.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/group10/1363044717/struts/homework/src/main/resources/struts.xml b/group10/1363044717/struts/homework/src/main/resources/struts.xml new file mode 100644 index 0000000000..b9489dad6d --- /dev/null +++ b/group10/1363044717/struts/homework/src/main/resources/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/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/ArrayList.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/ArrayList.java" index 620e8ead95..73a0b0ae33 100644 --- "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/ArrayList.java" +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/ArrayList.java" @@ -89,18 +89,15 @@ public boolean isEmpty() { } public Iterator iterator(){ - return new ArrayListIterator(this); + return new ArrayListIterator(); } private class ArrayListIterator implements Iterator{ - private Object [] array; - private int endIndex = 0; + private int endIndex = size - 1; private int index = 0; - public ArrayListIterator(ArrayList list){ - this.array=list.elementData; - this.endIndex = list.size(); + public ArrayListIterator(){ } @Override public boolean hasNext() { @@ -112,7 +109,7 @@ public E next() { if(!this.hasNext()) { throw new NoSuchElementException();//娌℃湁鍏冪礌浜 } else { - return (E)Array.get(this.array, this.index++); + return (E)elementData[this.index++]; } } } diff --git "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/LinkedList.java" "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/LinkedList.java" index 760fec91e9..9a1ae01119 100644 --- "a/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/LinkedList.java" +++ "b/group10/1363044717/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/LinkedList.java" @@ -61,19 +61,16 @@ public E remove(int index) { E e = removeFirst(); return e; } - if (index == size) { + if (index == size - 1) { E e = removeLast(); return e; } Node temp = head; - Node temp2 = null; for (int i = 0; i < index - 1; i++) { temp = temp.next; } E e = (E) temp.next.data; - temp2 = temp.next.next; - temp.next = null; - temp.next = temp2; + temp.next = temp.next.next; size--; return e; } @@ -139,7 +136,7 @@ private class LinkedListIterator implements Iterator{ public LinkedListIterator(LinkedList list){ this.head=list.head; this.tail=list.tail; - this.endIndex = list.size(); + this.endIndex = list.size() - 1; node=head; } @Override diff --git a/group10/205301442/src/com/coding/week2/ArrayUtil.java b/group10/205301442/src/com/coding/week2/ArrayUtil.java new file mode 100644 index 0000000000..b11f5ee71d --- /dev/null +++ b/group10/205301442/src/com/coding/week2/ArrayUtil.java @@ -0,0 +1,199 @@ +package com.coding.week2; + +public class ArrayUtil { + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static int[] reverseArray(int[] origin){ + int j=origin.length-1; + for(int i=0;i=nums.length?nums.length:size; + + System.arraycopy(nums, 0, newNums, 0, length); + return newNums; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + //杩欓噷涓轰粈涔堟槸寮骞虫柟鏍 + public static int[] getPrimes(int max){ + int i=2; + int[] nums = new int[max]; + int index=0; + while(i parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + File f1 = new File("config/struts.xml"); + + File f = new File(f1.getAbsolutePath()); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + try { + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc=builder.parse(f); + Element root = doc.getDocumentElement(); + + Map> actions = new HashMap>(); + if (root.getNodeType() == Node.ELEMENT_NODE) { + NodeList actionList = root.getChildNodes(); + + for (int i = 0; i < actionList.getLength(); i++) { + Node n = actionList.item(i); + if (n.getNodeType() == Node.ELEMENT_NODE) { + NamedNodeMap nnmap = n.getAttributes(); + Map action = new HashMap(); + action.put(nnmap.item(0).getNodeValue(),nnmap.item(1).getNodeValue()); + NodeList result= n.getChildNodes(); + + for(int j = 0;j requestAction=null; + if(actionName!=null){ + requestAction=actions.get(actionName); + }else{ + System.err.println("娌℃湁actionName"); + } + + try { + if(requestAction!=null){ + Class c = Class.forName(requestAction.get(actionName)); + Object co = c.newInstance(); + if("login".equals(actionName)){ + String name = parameters.get("name"); + String password = parameters.get("password"); + Method m1=c.getMethod("setName", String.class); + Method m2=c.getMethod("setPassword", String.class); + Method m3 = c.getMethod("execute"); + m1.invoke(co, name); + m2.invoke(co, password); + String rest = (String)m3.invoke(co); + Method m4 = c.getMethod("getMessage"); + String message = (String)m4.invoke(co); + Map pras =new HashMap(); + pras.put("message", message); + View view = new View(); + view.setJsp(requestAction.get(rest)); + view.setParameters(pras); + return view; + } + }else{ + System.out.println("娌℃湁鎵惧埌瀵瑰簲action"); + } + + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } catch (ParserConfigurationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SAXException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + +} diff --git a/group10/205301442/src/com/coding/week2/litestruts/StrutsTest.java b/group10/205301442/src/com/coding/week2/litestruts/StrutsTest.java new file mode 100644 index 0000000000..99e41bd0f4 --- /dev/null +++ b/group10/205301442/src/com/coding/week2/litestruts/StrutsTest.java @@ -0,0 +1,40 @@ +package com.coding.week2.litestruts; + +import static org.junit.Assert.*; + +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); + System.out.println( view.getJsp()); + 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); + System.out.println( view.getJsp()); + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group10/205301442/src/com/coding/week2/litestruts/View.java b/group10/205301442/src/com/coding/week2/litestruts/View.java new file mode 100644 index 0000000000..bf7bd0d6c8 --- /dev/null +++ b/group10/205301442/src/com/coding/week2/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coding.week2.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/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.classpath" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.classpath" new file mode 100644 index 0000000000..fb565a588d --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.classpath" @@ -0,0 +1,6 @@ + + + + + + diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.project" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.project" new file mode 100644 index 0000000000..34d5612cf4 --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.project" @@ -0,0 +1,17 @@ + + + DataStructure_One + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.settings/org.eclipse.jdt.core.prefs" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.settings/org.eclipse.jdt.core.prefs" new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/.settings/org.eclipse.jdt.core.prefs" @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +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.7 diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/ArrayList.java" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/ArrayList.java" new file mode 100644 index 0000000000..1d3f5bf099 --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/ArrayList.java" @@ -0,0 +1,236 @@ +package list; + +import java.util.Arrays; +import java.util.ConcurrentModificationException; +import java.util.NoSuchElementException; + +public class ArrayList implements List{ + private int size ; + private Object [] elementData; + private static final Object [] EMPTY_ELEMENTDATA ={}; + private static final int DEFAULT_CAPACITY = 10; + private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; + public ArrayList(){ + this.elementData = EMPTY_ELEMENTDATA; + } + public ArrayList(int limit){ + if(limit<0) + throw new IllegalArgumentException("Illegal Capacity: "+limit); + this.elementData = new Object[limit]; + } + /** + * 娣诲姞鍏冪礌 + * @param o 鍏冪礌 + * @return 鏄惁鎴愬姛娣诲姞 + */ + public boolean add(Object o){ + ensureCapacityInternal(size+1); + elementData[size++]=o; + return true; + } + /** + * 娣诲姞鍏冪礌 + * @param index 娣诲姞浣嶇疆 + * @param o 鍏冪礌 + * @return 鏄惁鎴愬姛娣诲姞 + */ + public void add(int index, Object o) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); + } + ensureCapacityInternal(size + 1); + //灏咥鏁扮粍鐨勭殑a浣嶇疆寮濮 澶嶅埗鑷矪鏁扮粍鐨刟+1浣嶇疆杩涜瑕嗙洊--->绛変簬灏咮鏁扮粍a浣嶇疆鍚庨潰鐨勬墍鏈夊厓绱犲悜鍚庣Щ浜嗕竴浣嶃 + System.arraycopy(elementData, index, elementData, index+1, size-index); + elementData[index] = o; + size++; + } + + /** + * 鍒犻櫎鍏冪礌 + * @param o 鍏冪礌 + * @return 鏄惁鎴愬姛鍒犻櫎 + */ + public boolean remove(Object o) { + if (o == null) { + for (int index = 0; index < size; index++) { + if (elementData[index] == null) { + fastRemove(index); + return true; + } + } + } else { + for (int i = 0; i < size; i++) { + if(elementData[i].equals(o)){ + fastRemove(i); + return true; + } + } + } + + return false; + } + /** + * 鍒犻櫎鍏冪礌 + * @param index 闇瑕佸垹闄ょ殑鍏冪礌鐨勪綅缃 + * @return 琚垹闄ょ殑鍏冪礌 + */ + public Object remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } + Object o =elementData[index]; + fastRemove(index); + return o; + } + + + + /** + * 鏌ヨ鍏冪礌 + * @param index 浣嶇疆 + * @return 琚煡璇㈢殑鍏冪礌 + */ + public Object get(int index){ + if(index>=size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + return elementData[index]; + } + + + + + private void fastRemove(int index) { + //闇瑕佺Щ鍔ㄧ殑涓暟 + int move = size-index-1; + System.arraycopy(elementData, index+1, elementData, index, move); + elementData[size]=null; + size--; + } + private void ensureCapacityInternal(int newLength) { + if (elementData == EMPTY_ELEMENTDATA) { + newLength = Math.max(DEFAULT_CAPACITY, newLength); + } + //濡傛灉闀垮害瓒呰繃浜唀lementData鍒嗛厤鐨勫唴瀛樹笂闄 鍒欓渶瑕佺户缁垎閰嶅唴瀛 + if(newLength-elementData.length>0){ + grow(newLength); + } + } + + private void grow(int newLength) { + int oldCapacity = elementData.length; + int newCapacity = (oldCapacity >> 1) + oldCapacity; + if (newCapacity - newLength < 0) { + newCapacity = newLength; + } + // int 涓婇檺锛2147483648 + if (newCapacity > MAX_ARRAY_SIZE) { + newCapacity = newLength > MAX_ARRAY_SIZE ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; + } + // 鎵╁ + elementData = Arrays.copyOf(elementData, newLength); + } + + @Override + public void clear() { + for (int i = 0; i < size; i++) { + elementData[i]=null; + } + size=0; + } + + + @Override + public boolean contains(Object o) { + if (indexOf(o) >= 0) + return true; + return false; + } + + @Override + public int indexOf(Object o) { + if(o==null){ + for (int i = 0; i < size; i++) { + if(elementData[i]==null) + return i; + } + }else{ + for (int i = 0; i < size; i++) { + if(elementData[i].equals(o)) + return i; + } + } + return -1; + } + + @Override + public boolean isEmpty() { + return size==0; + } + + + @Override + public Object set(int index, Object o) { + if(index<0||index>=size) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); + Object oldO=elementData[index]; + elementData[index]=o; + return oldO; + } + @Override + public int size() { + + return size; + } + + @Override + public Iterator iterator() { + return new ArrayIterator(); + } + private class ArrayIterator implements Iterator{ + + int limit=ArrayList.this.size; + int cursor; + int lastRet = -1; + + @Override + public boolean hasNext() { + return cursor < limit; + } + + @Override + public Object next() { + int i = cursor; + if (cursor >= size) { + throw new NoSuchElementException(); + } + Object[] o = ArrayList.this.elementData; + if (cursor > o.length) + throw new ConcurrentModificationException(); + cursor = i + 1; + return o[lastRet = i]; + } + + @Override + public void remove() { + if (lastRet < 0) + throw new IllegalStateException(); + try { + ArrayList.this.remove(lastRet); + cursor = lastRet; + lastRet = -1; + limit--; + } catch (IndexOutOfBoundsException ex) { + throw new ConcurrentModificationException(); + } + } + + } + + + + + + + + +} diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/Iterator.java" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/Iterator.java" new file mode 100644 index 0000000000..142fc4bafe --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/Iterator.java" @@ -0,0 +1,20 @@ +package list; + +public interface Iterator { + /** + * 鏄惁鏈夌涓涓 + * @return + */ + boolean hasNext(); + + /** + * 鑾峰彇涓嬩竴涓 + * @return + */ + Object next(); + + /** + * 鍒犻櫎 + */ + void remove(); +} diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/LinkedList.java" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/LinkedList.java" new file mode 100644 index 0000000000..c311e183c8 --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/LinkedList.java" @@ -0,0 +1,211 @@ +package list; + +public class LinkedList implements List { + + public Node getFirst() { + return first; + } + + public Node getLast() { + return last; + } + + private int size; + private Node first; + private Node last; + + @Override + public void add(int paramInt, Object paramE) { + checkPositionIndex(paramInt); + if (paramInt == size) { + linkLast(paramE); + } else { + linkBefore(paramE, node(paramInt)); + } + + } + + // 瀵绘壘鍒伴渶瑕佹彃鍏ョ殑閭d釜node + Node node(int paramInt) { + if (paramInt < size >> 1) { + Node x = first; + for (int i = 0; i < paramInt; i++) { + x = x.next; + } + return x; + } else { + Node x = last; + for (int i = size - 1; i > paramInt; i--) { + x = x.prev; + } + return x; + } + } + + private void linkBefore(Object o, Node node) { + Node pred = node.prev; + Node newNode = new Node(pred, o, node); + node.prev = newNode; + if (pred == null) { + first = newNode; + } else { + pred.next = newNode; + } + + } + + private void checkPositionIndex(int paramInt) { + if (paramInt < 0 || paramInt > size) { + throw new IndexOutOfBoundsException("Index: " + paramInt + ", Size: " + size); + } + } + + @Override + public boolean add(Object paramE) { + linkLast(paramE); + return true; + } + + private void linkLast(Object paramE) { + Node l = last; + Node newNode = new Node(l, paramE, null); + last = newNode; + if (l == null) { + first = newNode; + } else { + l.next = newNode; + } + size++; + } + + @Override + public void clear() { + for (Node x = first; x != null;) { + Node next = x.next; + x.next = null; + x.prev = null; + x.item = null; + x = next; + } + first = last = null; + size = 0; + } + + @Override + public boolean contains(Object paramObject) { + return false; + } + + @Override + public Object get(int paramInt) { + checkPositionIndex(paramInt); + return node(paramInt).item; + } + + @Override + public int indexOf(Object paramObject) { + return 0; + } + + @Override + public boolean isEmpty() { + return size == 0 ? true : false; + } + + @Override + public Iterator iterator() { + return new LinkedIterator(); + } + + @Override + public Object remove(int paramInt) { + checkPositionIndex(paramInt); + return unlink(node(paramInt)); + } + + private Object unlink(Node node) { + Object item = node.item; + Node prev = node.prev; + Node next = node.next; + if (prev == null) { + first = next; + } else { + prev.next = next; + node.prev = null; + } + if (next == null) { + last = prev; + } else { + next.prev = prev; + node.next = null; + } + node.item = null; + size--; + + return item; + + } + + @Override + public boolean remove(Object paramObject) { + return false; + } + + @Override + public Object set(int paramInt, Object paramE) { + return null; + } + + @Override + public int size() { + return size; + } + + public static class Node { + public Object item; + public Node next; + public Node prev; + + public Node(Node prev, Object item, Node next) { + this.item = item; + this.next = next; + this.prev = prev; + } + + } + + private class LinkedIterator implements Iterator { + + int cursor = 0; + int lastRet = -1; + + @Override + public boolean hasNext() { + return cursor < size(); + } + + @Override + public Object next() { + int i = cursor; + Object node = get(i); + lastRet = i; + cursor = i + 1; + return node; + } + + @Override + public void remove() { + if (lastRet < 0) { + throw new IllegalStateException(); + } + LinkedList.this.remove(lastRet); + if (lastRet < cursor) { + cursor--; + } + lastRet = -1;// 闃叉瀵逛竴涓暟鎹娆emove鎿嶄綔 + + } + + } + +} diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/List.java" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/List.java" new file mode 100644 index 0000000000..05b2c99294 --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/list/List.java" @@ -0,0 +1,32 @@ +package list; + + +public interface List{ + + public abstract void add(int paramInt, Object paramE); + + public abstract boolean add(Object paramE); + + public abstract void clear(); + + public abstract boolean contains(Object paramObject); + + public abstract boolean equals(Object paramObject); + + public abstract Object get(int paramInt); + + public abstract int indexOf(Object paramObject); + + public abstract boolean isEmpty(); + + public abstract Iterator iterator(); + + public abstract Object remove(int paramInt); + + public abstract boolean remove(Object paramObject); + + public abstract Object set(int paramInt, Object paramE); + + public abstract int size(); + +} diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/queue/ListQueue.java" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/queue/ListQueue.java" new file mode 100644 index 0000000000..80c52f2d53 --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/queue/ListQueue.java" @@ -0,0 +1,56 @@ +package queue; + +import list.LinkedList; + +public class ListQueue { + + LinkedList list; + + public ListQueue() { + list = new LinkedList(); + } + + public int size() { + return list.size(); + } + + public boolean isEmpty() { + return list.size() == 0 ? true : false; + } + + public boolean remove(Object o) { + return list.remove(o); + } + + public void clear() { + list.clear(); + } + + public boolean add(Object e) { + return list.add(e); + } + + public boolean offer(Object e) { + return false; + } + + public Object poll() { + if (list.size() == 0) { + return null; + }else{ + Object item = list.getFirst().item; + list.remove(0); + return item; + } + } + + + public Object peek() { + if (list.size() == 0) { + return null; + }else{ + return list.getFirst().item; + } + } + +} diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/stack/Stack.java" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/stack/Stack.java" new file mode 100644 index 0000000000..4bbffc1e0f --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/stack/Stack.java" @@ -0,0 +1,31 @@ +package stack; + +import java.util.NoSuchElementException; + +import list.ArrayList; +import list.List; + +public class Stack { + + List list ; + public Stack(){ + list = new ArrayList(); + } + + public void push(Object o) { + list.add(o); + } + public Object poll(){ + Object remove = list.remove(list.size()-1); + if(remove==null) + throw new NoSuchElementException(); + return remove; + } + + public Object peak(){ + if(list.size()==0) + throw new NoSuchElementException(); + return list.get(list.size()-1); + } + +} diff --git "a/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/tree/BinaryTree.java" "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/tree/BinaryTree.java" new file mode 100644 index 0000000000..420e131537 --- /dev/null +++ "b/group10/3031828378/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232(\346\225\260\346\215\256\347\273\223\346\236\204)/DataStructure_One/src/tree/BinaryTree.java" @@ -0,0 +1,74 @@ +package tree; + +public class BinaryTree { + private TreeNode parent; + + public BinaryTree(){ + parent = new TreeNode(null,null,null); + } + + public void insert(Object o ){ + TreeNode node = new TreeNode(null,o,null); + if(parent.item==null){ + parent= node; + return; + } + insertNode(parent,node); + } + + private void insertNode(TreeNode parentNode, TreeNode newNode) { + if(parentNode.compareTo(newNode)<= 0){ + if(parentNode.right==null){ + parentNode.right = newNode; + }else{ + insertNode(parentNode.right,newNode); + } + + }else{ + if(parentNode.left==null){ + parentNode.left=newNode; + }else{ + insertNode(parentNode.left, newNode); + } + } + } + + public void printTree(){ + printNode(this.parent); + } + + + + + private void printNode(TreeNode node) { + if (node == null) { + System.out.println("node :" + node.item); + printNode(node.left); + printNode(node.right); + } + + } + + + + + class TreeNode implements Comparable{ + Object item; + TreeNode left; + TreeNode right; + TreeNode(TreeNode left,Object item,TreeNode right){ + this.item=item; + this.left =left; + this.right=right; + } + + @Override + public int compareTo(TreeNode o) { + Integer parentItem = (Integer) this.item; + Integer oItem = (Integer) o.item; + + return parentItem.compareTo(oItem); + } + } + +} diff --git a/group10/3314793852/second/.classpath b/group10/3314793852/second/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group10/3314793852/second/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group10/3314793852/second/.gitignore b/group10/3314793852/second/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group10/3314793852/second/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/3314793852/second/.project b/group10/3314793852/second/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group10/3314793852/second/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group10/3314793852/second/.settings/org.eclipse.core.resources.prefs b/group10/3314793852/second/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..5855257b83 --- /dev/null +++ b/group10/3314793852/second/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/com/coderising/litestruts=UTF-8 +encoding//src/com/coderising/litestruts/Struts.java=UTF-8 +encoding/=UTF-8 diff --git a/group10/3314793852/second/.settings/org.eclipse.jdt.core.prefs b/group10/3314793852/second/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group10/3314793852/second/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +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/group10/3314793852/second/src/com/coderising/array/ArrayUtil.java b/group10/3314793852/second/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..5742ea09f9 --- /dev/null +++ b/group10/3314793852/second/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,236 @@ + + package com.coderising.array; + + import java.util.Arrays; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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 size=origin.length-1; + int[] arr=new int[origin.length]; + for(int i=0;iarray1[j]&&array1[i]!=0&&array1[j]!=0){ + int temp; + temp=array1[i]; + array1[i]=array1[j]; + array1[j]=temp; + } + if(array1[i]==array1[j]&&array1[i]!=0&&array1[j]!=0){ + array1[j]=0; + } + } + } + + System.out.println(Arrays.toString(array1)); + + int[] array3=removeZero(array1); //闄ら浂鎿嶄綔銆 + 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[] newArr=new int[oldArray.length+size]; + for(int i=0;i2){ + arr[0]=1; + arr[1]=1; + while(second aMap=new HashMap(); //涓涓敤鏉ュ瓨鍌ㄣ + + private ArrayList> list=new ArrayList>();; //List闆嗗悎鐢ㄦ潵淇濆瓨Map闆嗗悎銆 + + private String currentTag; + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) + throws SAXException { + currentTag =qName; + + if("action".equals(currentTag)){ + key=attributes.getValue(0); //action鐨刵ame灞炴с + value=attributes.getValue(1); //action鐨刢lass灞炴с + + //鍦ㄥ皢灞炴ф垚瀵圭殑淇濆瓨鍒颁竴涓狹ap闆嗗悎涓 + aMap.put(key, value); + + //淇濆瓨鍚庡皢涓棿鍙橀噺鍙樹负null. + key=null; + value=null; + } + + if("result".equals(currentTag)){ + key=attributes.getValue(0); //result鐨刵ame灞炴с + } + + } + + + @Override + public void characters(char[] ch, int start, int length) throws SAXException { + if("result".equals(currentTag)){ + String name=new String(ch,start,length); + value=name; + //灏嗗睘鎬ф垚瀵圭殑淇濆瓨鍒颁竴涓狹ap闆嗗悎涓 + + aMap.put(key, value); + + //淇濆瓨鍚庡皢涓棿鍙橀噺娓呯┖銆 + key=null; + value=null; + currentTag=null; + } + + } + + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + if("action".equals(qName)){ + + list.add(aMap); + + + aMap=new HashMap(); + + + } + + } + + //杩斿洖list闆嗗悎銆 + public ArrayList> getDate(){ + return list; + } + } diff --git a/group10/3314793852/second/src/com/coderising/litestruts/LoginAction.java b/group10/3314793852/second/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group10/3314793852/second/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; + } +} diff --git a/group10/3314793852/second/src/com/coderising/litestruts/SAXGetInfo.java b/group10/3314793852/second/src/com/coderising/litestruts/SAXGetInfo.java new file mode 100644 index 0000000000..52dd35961d --- /dev/null +++ b/group10/3314793852/second/src/com/coderising/litestruts/SAXGetInfo.java @@ -0,0 +1,42 @@ + /* + *璇ョ被鐢ㄤ簬璇诲彇struts.xml鏂囦欢涓殑鏁版嵁锛屽苟鎶婂畠瀛樺偍鍒癆ctionType绫荤殑瀵硅薄涓幓銆 + */ + package com.coderising.litestruts; + + import java.io.File; + import java.io.FileInputStream; + import java.io.IOException; + import java.io.InputStream; + import java.util.ArrayList; + import java.util.HashMap; + + import javax.xml.parsers.ParserConfigurationException; + import javax.xml.parsers.SAXParser; + import javax.xml.parsers.SAXParserFactory; + + import org.xml.sax.InputSource; + import org.xml.sax.SAXException; + import org.xml.sax.XMLReader; + + + + public class SAXGetInfo { + + public ArrayList> getDate() throws SAXException, IOException, ParserConfigurationException{ + //鍒涘缓瑙f瀽宸ュ巶 + SAXParserFactory factory=SAXParserFactory.newInstance(); + + //鍒涘缓瑙f瀽鍣 + SAXParser parser=factory.newSAXParser(); + + //鏂囦欢鍦板潃 + String fileName="E:/CODING2017/Code/coding2017/group10/3314793852/second/src/com/coderising/litestruts/struts.xml"; + //璁剧疆鍐呭澶勭悊鍣 + DealWithInfo handler=new DealWithInfo(); + parser.parse(fileName, handler); + + ArrayList> list=handler.getDate(); + return list; //灏嗕繚瀛樺湪list闆嗗悎涓殑閰嶇疆淇℃伅杩斿洖銆 + } + + } diff --git a/group10/3314793852/second/src/com/coderising/litestruts/Struts.java b/group10/3314793852/second/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..5312535300 --- /dev/null +++ b/group10/3314793852/second/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,165 @@ + + package com.coderising.litestruts; + + import java.io.IOException; + import java.util.ArrayList; + import java.util.HashMap; + import java.util.Iterator; + import java.util.Map; + + import javax.xml.parsers.ParserConfigurationException; + + import org.xml.sax.SAXException; + + import java.lang.reflect.InvocationTargetException; + import java.lang.reflect.Method; + + + + public class Struts { + + public static View runAction(String actionName, Map parameters) { + View view=new View(); + + String execute=null; //execute杩斿洖鍊笺 + String placeOfJsp=null; //JSP鍦板潃銆 + String classname=getClassName(actionName); + + Class classAction = null; + Object obj=null; + try { + classAction=Class.forName(classname); //鏍规嵁绫诲悕鍙嶅皠瀹炰緥鍖朿lass绫汇 + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + //ClassAction绫讳娇鐢╪ewInstance();鏂规硶璋冪敤LoginAction绫荤殑榛樿鏋勯犳柟娉曞垱寤篖oginAction绫汇 + try { + obj=classAction.newInstance(); //Object绫诲瀷鐨勫璞° + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + + //璋冪敤瀵硅薄鐨剆etter鏂规硶,璁剧疆鐢ㄦ埛鍚嶅拰瀵嗙爜锛屽嵆鎶妌ame鍜宲assword鐨勫艰缃埌褰撳墠LoginAction鐨勫璞′腑銆 + try { + setter(obj,"name",parameters.get("name"),String.class); + setter(obj,"password",parameters.get("password"),String.class); + } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException e) { + e.printStackTrace(); + } + + //璋冪敤瀵硅薄鐨別xectue鏂规硶銆 + try{ + Method method=classAction.getMethod("execute"); + execute=(String) method.invoke(obj); + }catch(Exception e){ + e.printStackTrace(); + } + + //璋冪敤瀵硅薄鐨勬墍鏈塯etter鏂规硶,鎶婂艰缃埌view瀵硅薄鐨刾arameters灞炴т腑銆 + Map para=new HashMap(); + try{ + para.put("name", getter(obj,"name")); + para.put("password", getter(obj,"password")); + para.put("message", getter(obj,"message")); + }catch(Exception e){ + e.printStackTrace(); + } + view.setParameters(para); + + //鏍规嵁execute杩斿洖鍊煎拰閰嶇疆锛屽皢JSP鍦板潃璧嬪肩粰View瀵硅薄鐨刯sp鎴愬憳涓 + placeOfJsp=getResultName(actionName,execute); + view.setJsp(placeOfJsp); + + //System.out.println(view.getJsp()+" "+view.getParameters()); + return view; + } + + + /* + * 绗竴涓弬鏁颁负鎿嶄綔瀵硅薄锛岀浜屼釜鍙傛暟涓烘搷浣滅殑鏁版嵁鐨勬暟鎹垚鍛橈紝绗笁涓弬鏁颁负set鐨勫硷紝绗洓涓弬鏁颁负鍙傛暟鐨勭被鍨 + */ + private static void setter(Object obj, String att, Object value, Class type) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + //瀹炰緥鍖栦竴涓猻et鏂规硶 + Method method=obj.getClass().getMethod("set"+initStr(att), type); + method.invoke(obj, value); + } + + /* + * getter鏂规硶 + */ + private static String getter(Object obj,String att) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ + Method method=obj.getClass().getMethod("get"+initStr(att)); + method.invoke(obj); + return (String) method.invoke(obj); + } + + /* + * 鏍规嵁java鐨勫懡鍚嶈鍒欙紝鏁版嵁鎴愬憳鐨勭涓涓崟璇嶉兘瑕佸皬鍐欙紝鍏朵粬鍗曡瘝鐨勯瀛楁瘝澶у啓銆 + * 鎵浠ユ妸set鍜実et鏂规硶鍚庨潰鐨勫崟璇嶇殑棣栧瓧姣嶅ぇ鍐欙紝濡俿etName涓殑N + * 鍜実etName涓殑N. + */ + private static String initStr(String old) { + String str=old.substring(0,1).toUpperCase()+old.substring(1); + //鎶婇瀛楁瘝澶у啓銆 + return str; + } + + public static void main(String args[]){ + String actionName = "login"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + runAction(actionName,params); + } + + //鏌ユ壘action绫,鏌ユ壘result鏃讹紝蹇呴』瑕侀氳繃鏌ユ壘action鎵嶈銆 + private static String getClassName(String actionName){ + String className=getData(actionName).get(actionName); + + return className; + } + + //鏌ユ壘result鏃讹紝蹇呴』瑕侀氳繃鏌ユ壘action鎵嶈銆 + private static String getResultName(String actionName,String result){ + String placeOfJsp=getData(actionName).get(result); + + return placeOfJsp; + } + + //浠庨厤缃俊鎭腑鑾峰彇鏁版嵁,鑾峰彇 + private static HashMap getData(String actionName) { + SAXGetInfo a=new SAXGetInfo(); + ArrayList> x; + HashMap y=null; + Object[] arr=new Object[10]; + int size=0; + + try { + x = a.getDate(); + Iterator it=x.iterator(); + while(it.hasNext()){ + arr[size]=(Object)it.next(); + size++; + + } + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } + if(actionName.equals("login")){ + y=(HashMap) arr[0]; + } + if(actionName.equals("logout")){ + y=(HashMap) arr[1]; + } + return y; + } + + } diff --git a/group10/3314793852/second/src/com/coderising/litestruts/StrutsTest.java b/group10/3314793852/second/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group10/3314793852/second/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group10/3314793852/second/src/com/coderising/litestruts/View.java b/group10/3314793852/second/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group10/3314793852/second/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/group10/3314793852/second/src/com/coderising/litestruts/struts.out.xml b/group10/3314793852/second/src/com/coderising/litestruts/struts.out.xml new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group10/3314793852/second/src/com/coderising/litestruts/struts.xml b/group10/3314793852/second/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..07f80b6476 --- /dev/null +++ b/group10/3314793852/second/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 diff --git a/group10/569420966/struct/pom.xml b/group10/569420966/struct/pom.xml index 2fc5902e7a..bc0c11f1c3 100644 --- a/group10/569420966/struct/pom.xml +++ b/group10/569420966/struct/pom.xml @@ -7,6 +7,14 @@ com.rishy struct 1.0-SNAPSHOT + + + junit + junit + 4.12 + test + + \ No newline at end of file diff --git a/group10/569420966/struct/src/main/java/com/litestruts/LoginAction.java b/group10/569420966/struct/src/main/java/com/litestruts/LoginAction.java new file mode 100644 index 0000000000..b41e849acc --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.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/group10/569420966/struct/src/main/java/com/litestruts/Struts.java b/group10/569420966/struct/src/main/java/com/litestruts/Struts.java new file mode 100644 index 0000000000..875b35d223 --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/litestruts/Struts.java @@ -0,0 +1,147 @@ +package com.litestruts; + +import com.myutil.JavaBeanUtil; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.rmi.server.ExportException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +public class Struts { + + private static Map> xmlInfo = new HashMap<>(); + + public static View runAction(String actionName, Map parameters) { + + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + // 銆0銆 + getInfo(); + + + Map action = xmlInfo.get(actionName); + Class classForName; + View view = new View(); + try { + // 銆1銆 + Object aObj = Class.forName(action.get("class")).newInstance(); + classForName = aObj.getClass(); + Method[] methods = classForName.getMethods(); + for (Method method : methods) { + for (String key : parameters.keySet()) { + if (JavaBeanUtil.getSetMethodName(key).equals(method.getName())) { + Method declaredMethod = classForName.getDeclaredMethod(method.getName(), method.getParameterTypes()); + declaredMethod.invoke(aObj, parameters.get(key)); + } + } + } + // 銆2銆 + Map resultMap = new HashMap<>(); + Method execute = classForName.getDeclaredMethod("execute"); + String invoke = (String) execute.invoke(aObj); + // 銆3銆 + Field[] fields = classForName.getDeclaredFields(); + for (Field field : fields) { + for (Method method : methods) { + if (JavaBeanUtil.getGetMethodName(field.getName()).equals(method.getName())) { + Method declaredMethod = classForName.getDeclaredMethod(method.getName()); + String invoke1 = (String)declaredMethod.invoke(aObj); + resultMap.put(field.getName(), invoke1); + } + } + } + view.setParameters(resultMap); + // 銆4銆 + view.setJsp(action.get(invoke)); + } 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 view; + } + + private static void getInfo() { + try { + File file = new File("D:\\Own\\Code\\Java\\coding2017\\group10\\569420966\\struct\\src\\main\\resources\\struts.xml"); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(file); + NodeList nl = doc.getElementsByTagName("struts"); + + if (nl.getLength() > 0) { + NodeList actions = doc.getElementsByTagName("action"); + if (actions.getLength() > 0) { + Map actionInfo; + for (int i = 0; i < actions.getLength(); i++) { + actionInfo = new HashMap<>(); + NamedNodeMap attributes = actions.item(i).getAttributes(); + for (int j = 0; j < attributes.getLength(); j++) { + attributes.item(j).getNodeName(); + actionInfo.put(attributes.item(j).getNodeName(), attributes.item(j).getNodeValue()); + } + NodeList childNodes = actions.item(i).getChildNodes(); + for (int j = 0; j < childNodes.getLength(); j++) { + if (childNodes.item(j).getNodeType() == 1) { // 鍏冪礌鑺傜偣 + NodeList childNodes1 = childNodes.item(j).getChildNodes(); + NamedNodeMap attributes1 = childNodes.item(j).getAttributes(); + NodeList childNodes2 = childNodes.item(j).getChildNodes(); + actionInfo.put(attributes1.item(0).getNodeValue(), childNodes2.item(0).getNodeValue()); + } + } + if (actionInfo.get("name") != null) { + xmlInfo.put(actionInfo.get("name"), actionInfo); + } + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + getInfo(); + } + +} \ No newline at end of file diff --git a/group10/569420966/struct/src/main/java/com/litestruts/View.java b/group10/569420966/struct/src/main/java/com/litestruts/View.java new file mode 100644 index 0000000000..5567568114 --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/litestruts/View.java @@ -0,0 +1,23 @@ +package com.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/group10/569420966/struct/src/main/java/com/myutil/ArrayUtil.java b/group10/569420966/struct/src/main/java/com/myutil/ArrayUtil.java new file mode 100644 index 0000000000..bf7e794488 --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/ArrayUtil.java @@ -0,0 +1,257 @@ +package com.myutil; + +import java.util.Arrays; + +/** + * 鏁扮粍宸ュ叿绫 + */ +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + * 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * + * @param origin 婧愭暟缁 + */ + public static void reverseArray(int[] origin) { + if (origin == null) { + return; + } + int originLen = origin.length; + int[] copyArray = new int[originLen]; + System.arraycopy(origin, 0, copyArray, 0, originLen); + for (int i = 0; i < originLen; i++) { + origin[i] = copyArray[originLen - 1 - 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 static int[] removeZero(int[] oldArray) { + if (oldArray == null) { + return null; + } + int size = 0; + for (int i : oldArray) { + size += i == 0 ? 0 : 1; + } + int[] newArray = new int[size]; + int newIndex = 0; + for (int old : oldArray) { + if (old != 0) { + newArray[newIndex++] = old; + } + } + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * + * @param array1 绗竴涓暟缁 + * @param array2 绗簩涓暟缁 + * @return 鍚堝苟鍚庣殑鏁扮粍 + */ + + public static int[] merge(int[] array1, int[] array2) { + if (array1 == null || array2 == null) { + return null; + } + int len1 = array1.length; + int len2 = array2.length; + int[] newArray = new int[len1 + len2]; + int size = 0; + int i = 0; + int j = 0; + int k = 0; + while (i < len1 && j < len2) { + if (array1[i] < array2[j]) { + if (k == 0 || newArray[k] != array2[i]) { + newArray[k++] = array1[i++]; + size++; + } else { + i++; + } + } else if (array1[i] > array2[j]) { + if (k == 0 || newArray[k] != array2[j]) { + newArray[k++] = array2[j++]; + size++; + } else { + j++; + } + } else { + if (k == 0 || newArray[k] != array2[i]) { + newArray[k++] = array2[j]; + i++; + j++; + size++; + } else { + i++; + j++; + } + } + } + while (i < len1) { + if (k == 0 || newArray[k] != array1[i]) { + newArray[k++] = array1[i++]; + size++; + } else { + i++; + } + } + while (j < len2) { + if (k == 0 || newArray[k] != array2[j]) { + newArray[k++] = array2[j++]; + size++; + } else { + j++; + } + } + int[] resizeArray = new int[size]; + System.arraycopy(newArray, 0, resizeArray, 0, size); + return resizeArray; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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) { + if (oldArray == null || size <= 0) { + return oldArray; + } + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * + * @param max 鏈澶у + * @return 灏忎簬鏈澶у肩殑fibonacci鏁板垪鐨勬暟缁 + */ + public static int[] fibonacci(int max) { + ArrayList fibonacciList = new ArrayList<>(); + int a1 = 1; + int a2 = 1; + int t; + if (a1 < max) { + fibonacciList.add(a1); + } + while (a2 < max) { + fibonacciList.add(a2); + t = a2; + a2 = a1 + a2; + a1 = t; + } + int[] fibonacciArray = new int[fibonacciList.size()]; + for (int i = 0; i < fibonacciList.size(); i++) { + fibonacciArray[i] = fibonacciList.get(i); + } + return fibonacciArray; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max 鏈澶у + * @return 灏忎簬鏈澶у肩殑鎵鏈夌礌鏁 + */ + public static int[] getPrimes(int max) { + ArrayList primeList = new ArrayList<>(); + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + primeList.add(i); + } + } + int[] primeArray = new int[primeList.size()]; + for (int i = 0; i < primeList.size(); i++) { + primeArray[i] = primeList.get(i); + } + return primeArray; + } + + private static boolean isPrime(int num) { + for (int i = 2; i <= Math.sqrt(num); i++) { + if (num % i == 0) { + return false; + } + } + + return true; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max 鏈澶у + * @return 灏忎簬鏈澶у肩殑鎵鏈夊畬鏁 + */ + public static int[] getPerfectNumbers(int max) { + ArrayList perfectList = new ArrayList<>(); + for (int i = 2; i < max; i++) { + if (isPrefectNumber(i)) { + perfectList.add(i); + } + } + int[] perfectArray = new int[perfectList.size()]; + for (int i = 0; i < perfectList.size(); i++) { + perfectArray[i] = perfectList.get(i); + } + return perfectArray; + } + + private static boolean isPrefectNumber(int num) { + int sum = 0; + for (int i = 1; i < num; i++) { + if (num % i == 0) { + sum += i; + } + } + return num == sum; + } + + /** + * 鐢╯eparator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], separator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * + * @param array 鏁扮粍 + * @param separator 鍒嗛殧绗 + * @return 鐢ㄥ垎闅旂杩炴帴鐨勬暟瀛楀瓧绗︿覆 + */ + public static String join(int[] array, String separator) { + if (array == null || separator == null) { + return ""; + } + StringBuilder sb = new StringBuilder(); + for (int i : array) { + sb.append(i).append(separator); + } + return sb.length() > 0 ? sb.substring(0, sb.length() - 1) : ""; + } + + public static void main(String[] args) { + System.out.println(Arrays.toString(getPerfectNumbers(1000))); + } +} \ No newline at end of file diff --git a/group10/569420966/struct/src/main/java/com/myutil/JavaBeanUtil.java b/group10/569420966/struct/src/main/java/com/myutil/JavaBeanUtil.java new file mode 100644 index 0000000000..614ca57bfb --- /dev/null +++ b/group10/569420966/struct/src/main/java/com/myutil/JavaBeanUtil.java @@ -0,0 +1,35 @@ +package com.myutil; + +import java.util.regex.Pattern; + +/** + * javaBean宸ュ叿 + */ +public class JavaBeanUtil { + public static String getGetMethodName(String attributeName) { + if ((Character.isLowerCase(attributeName.charAt(0)) && Character.isUpperCase(attributeName.charAt(1))) + || (Character.isUpperCase(attributeName.charAt(0)))) { + return "get" + attributeName; + } else if (attributeName.indexOf("is") == 0 && Character.isUpperCase(attributeName.charAt(1))) { + return attributeName; + } else { + char[] chars = attributeName.toCharArray(); + chars[0] -= 32; + return "get" + String.valueOf(chars); + } + } + + public static String getSetMethodName(String attributeName) { + if ((Character.isLowerCase(attributeName.charAt(0)) && Character.isUpperCase(attributeName.charAt(1))) + || (Character.isUpperCase(attributeName.charAt(0)))) { + return "set" + attributeName; + } else if (attributeName.indexOf("is") == 0 && Character.isUpperCase(attributeName.charAt(2))) { + return "set" + attributeName.replace("is", ""); + } else { + char[] chars = attributeName.toCharArray(); + chars[0] -= 32; + return "set" + String.valueOf(chars); + } + } + +} diff --git a/group10/569420966/struct/src/main/resources/struts.xml b/group10/569420966/struct/src/main/resources/struts.xml new file mode 100644 index 0000000000..cdf0277794 --- /dev/null +++ b/group10/569420966/struct/src/main/resources/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/group10/569420966/struct/src/test/java/com/litestruts/test/StrutsTest.java b/group10/569420966/struct/src/test/java/com/litestruts/test/StrutsTest.java new file mode 100644 index 0000000000..4a03024cfc --- /dev/null +++ b/group10/569420966/struct/src/test/java/com/litestruts/test/StrutsTest.java @@ -0,0 +1,45 @@ +package com.litestruts.test; + +import java.util.HashMap; +import java.util.Map; + +import com.litestruts.Struts; +import com.litestruts.View; +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")); + } +} \ No newline at end of file diff --git a/group10/595128841/src/org/le/b/ArrayUtil.java b/group10/595128841/src/org/le/b/ArrayUtil.java new file mode 100644 index 0000000000..29f1b3567a --- /dev/null +++ b/group10/595128841/src/org/le/b/ArrayUtil.java @@ -0,0 +1,197 @@ +/** + * + */ +package org.le.b; + +import java.util.Arrays; + +/** + * @author yue + * @time 2017骞2鏈28鏃 + */ +public class ArrayUtil { + + /** + * @param args + */ + public static void main(String[] args) { + //int[] origin = {7, 9 , 30, 3}; + //int[] n = reverseArray(origin); +// int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; +// int[] n = removeZero(oldArr); +// int[] a1 = {3, 5, 7,8,9,12}; +// int[] a2 = {4, 5, 6,7,8,12,14,5}; +// int[] n =merge(a1, a2); +// int[] n = grow(a1,5); + int[] n = fibonacci(100); + System.out.println(Arrays.toString(n)); + } + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static int[] reverseArray(int[] origin){ + int[] newArry = new int[origin.length]; + int i= 0,j = origin.length; + while(j > 0){ + newArry[i++] = origin[--j]; + } + return newArry; + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 static int[] removeZero(int[] oldArray){ + int[] tempArray = new int[oldArray.length]; + int j = 0; + for(int i = 0; i < oldArray.length;i++){ + if(oldArray[i] > 0){ + tempArray[j++] = oldArray[i]; + } + } + int[] newArray = new int[j]; + System.arraycopy(tempArray, 0, newArray, 0, j); + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){ + int len = array1.length; + int[] newArray = new int[array1.length+array2.length]; + System.arraycopy(array1, 0, newArray, 0, array1.length); + for(int i = 0; i < array2.length; i++){ + boolean flag = true; + for(int j = 0; j < array1.length; j++){ + if(array2[i] == array1[j]){ + flag = false; + } + } + if(flag){ + newArray[len++] = array2[i]; + } + } + int[] aa = new int[len]; + System.arraycopy(newArray, 0, aa, 0, len); + for(int i = 0; i < aa.length; i++){ + for(int j = i+1; j < aa.length; j++){ + if(aa[i] > aa[j]){ + int temp = aa[i]; + aa[i] = aa[j]; + aa[j] = temp; + } + } + } + return aa; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + int[] aa = new int[oldArray.length+size]; + System.arraycopy(oldArray, 0, aa, 0, oldArray.length); + return aa; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * Fn = F(n-1)+F(n-2)(n >= 2) + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if(max <2){ + return new int[0]; + } + int[] temp = new int[8]; + int count = 0; + for(int i = 1;i < max;i ++){ + int len = createFibo(i); + if(len > max) + break; + temp = growInt(temp,count); + temp[count++] = len; + } + int[] res = new int[count]; + System.arraycopy(temp, 0, res, 0, count); + return res; + } + + private static int[] growInt(int[] temp,int count){ + int[] n = temp; + if(count >= temp.length){ + n = new int[temp.length + (temp.length >> 1)]; + System.arraycopy(temp, 0, n, 0, temp.length); + } + return n; + } + + private static int createFibo(int n){ + if(n <= 2){ + return 1; + } + return createFibo(n-1)+createFibo(n-2); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder str = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + str.append(seperator).append(array[i]); + } + return str.substring(1).toString(); + } + +} diff --git a/group10/595128841/src/org/le/b/LoginAction.java b/group10/595128841/src/org/le/b/LoginAction.java new file mode 100644 index 0000000000..d895f6b5e6 --- /dev/null +++ b/group10/595128841/src/org/le/b/LoginAction.java @@ -0,0 +1,35 @@ +package org.le.b; + +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/group10/595128841/src/org/le/b/Struts.java b/group10/595128841/src/org/le/b/Struts.java new file mode 100644 index 0000000000..303286690f --- /dev/null +++ b/group10/595128841/src/org/le/b/Struts.java @@ -0,0 +1,155 @@ +package org.le.b; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.DocumentFactory; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class Struts { + private static Map actionMap; + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + if(actionMap == null){ + actionMap = strutsXmlSax("struts2.xml"); + } + ActionBean actionBean = actionMap.get(actionName); + return processAction(actionBean,parameters); + } + + + private static View processAction(ActionBean actionBean, Map parameters) { + String clazzStr = actionBean.getClazz(); + Map result = actionBean.getResults(); + try { + Class clazz= Class.forName(clazzStr); + Object obj = clazz.newInstance(); + for(String key : parameters.keySet()){ + String name = "set"+(key.charAt(0)+"").toUpperCase()+key.substring(1); + Method method = clazz.getMethod(name, String.class); + method.invoke(obj, parameters.get(key)); + } + Method execute = clazz.getMethod("execute"); + String resultStr = (String)execute.invoke(obj); + String resultJsp = result.get(resultStr); + Map pramMap = new HashMap<>(); + Method meg = clazz.getMethod("getMessage"); + String resultMsg = (String)meg.invoke(obj); + pramMap.put("message", resultMsg); + return new View(resultJsp,pramMap); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } + return null; + } + + + public static Map strutsXmlSax(String path){ + DocumentFactory documentFactory = DocumentFactory.getInstance(); + SAXReader saxReader = new SAXReader(documentFactory); + Document doc = null; + try { + doc = saxReader.read(path); + } catch (DocumentException e) { + e.printStackTrace(); + } + Element rootElement = doc.getRootElement(); + Element pack = rootElement.element("package"); + List actions = pack.elements("action"); + Map actionMap = new HashMap<>(); + for(Element action : actions){ + Attribute name = action.attribute("name"); + Attribute clazz = action.attribute("class"); + List results = action.elements("result"); + Map resMap = new HashMap<>(); + for(Element result : results){ + String key = "success"; + String value = result.getTextTrim(); + Attribute rname = result.attribute("name"); + if(rname != null){ + key = rname.getValue(); + } + resMap.put(key, value); + } + actionMap.put(name.getValue(), new ActionBean(name.getValue(),clazz.getValue(),resMap)); + } + return actionMap; + } + + public static class ActionBean{ + private String name; + private String clazz; + private Map results; + + public ActionBean(String name, String clazz, Map results) { + this.name = name; + this.clazz = clazz; + this.results = results; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClazz() { + return clazz; + } + + public void setClazz(String clazz) { + this.clazz = clazz; + } + + public Map getResults() { + return results; + } + + public void setResults(Map results) { + this.results = results; + } + + } + +} diff --git a/group10/595128841/src/org/le/b/StrutsTest.java b/group10/595128841/src/org/le/b/StrutsTest.java new file mode 100644 index 0000000000..3733a6a777 --- /dev/null +++ b/group10/595128841/src/org/le/b/StrutsTest.java @@ -0,0 +1,38 @@ +package org.le.b; + +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/group10/595128841/src/org/le/b/View.java b/group10/595128841/src/org/le/b/View.java new file mode 100644 index 0000000000..39a9f8a644 --- /dev/null +++ b/group10/595128841/src/org/le/b/View.java @@ -0,0 +1,28 @@ +package org.le.b; + +import java.util.Map; + +public class View { + + private String jsp; + private Map parameters; + + public View(String resultJsp, Map pramMap) { + this.jsp = resultJsp; + this.parameters = pramMap; + } + 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/group10/595128841/src/org/le/ArrayList.java b/group10/595128841/src/org/le/list/ArrayList.java similarity index 100% rename from group10/595128841/src/org/le/ArrayList.java rename to group10/595128841/src/org/le/list/ArrayList.java diff --git a/group10/595128841/src/org/le/LinkedList.java b/group10/595128841/src/org/le/list/LinkedList.java similarity index 100% rename from group10/595128841/src/org/le/LinkedList.java rename to group10/595128841/src/org/le/list/LinkedList.java diff --git a/group10/595128841/src/org/le/List.java b/group10/595128841/src/org/le/list/List.java similarity index 100% rename from group10/595128841/src/org/le/List.java rename to group10/595128841/src/org/le/list/List.java diff --git a/group10/630505243/.classpath b/group10/630505243/.classpath new file mode 100644 index 0000000000..2d7497573f --- /dev/null +++ b/group10/630505243/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group10/630505243/.gitignore b/group10/630505243/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group10/630505243/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/630505243/.project b/group10/630505243/.project new file mode 100644 index 0000000000..f1cd4e602e --- /dev/null +++ b/group10/630505243/.project @@ -0,0 +1,17 @@ + + + 630505243Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group10/630505243/src/com/coderising/array/ArrayUtil.java b/group10/630505243/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..6c251f5fa0 --- /dev/null +++ b/group10/630505243/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,238 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static int[] reverseArray(int[] origin){ + int arrLength = origin.length; + int[] newArray = new int[arrLength]; + //鍒ゆ柇鏁扮粍涓嶄负绌 + if (arrLength > 0) { + for (int i = 0; i < arrLength; i++) { + newArray[i] = origin[arrLength-1-i]; + } + } + return newArray; + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 static int[] removeZero(int[] oldArray){ + int position = 0; + int zeroCount = 0; + int[] rtnArr = null; + int[] newArray = new int[oldArray.length]; + if(oldArray.length>0){ + for(int i=0;i0){ + rtnArr =new int[oldArray.length-zeroCount]; + System.arraycopy(newArray, 0, rtnArr, 0, rtnArr.length); + } + return rtnArr; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){ + //鍘婚噸 + int repeatCount=0; + int len = array1.length-array2.length>0?array2.length:array1.length; + for(int i=0;i1) + return fibonacciImpl(a-1)+fibonacciImpl(a-2); + return -1; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + int[] primes = new int[max]; + int arrCount = 0; + for(int i=1;i0){ + for(int i=0;i parameters) { + View view = new View(); + //璇诲彇閰嶇疆鏂囦欢 + try { + File f = new File("src/com/coderising/litestruts/struts.xml"); + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); + Document doc = docBuilder.parse(f); + + NodeList actionList = doc.getElementsByTagName("action"); + for(int i=0;i> set = parameters.entrySet(); + Map viewparam = new HashMap(); + for(Map.Entry entry:set){ + String key = entry.getKey(); + String value = entry.getValue(); + Method method = clazz.getMethod("set"+key.substring(0,1).toUpperCase()+key.substring(1,key.length()), String.class); + method.invoke(nobj, value); + //viewparam.put(key, value); + } + Method m = clazz.getMethod("execute", null); + String rtnStr = (String) m.invoke(nobj, null); + NodeList resultList = actionElement.getChildNodes(); + for(int j=0;j 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + return view; + } + +} diff --git a/group10/630505243/src/com/coderising/litestruts/StrutsTest.java b/group10/630505243/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group10/630505243/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group10/630505243/src/com/coderising/litestruts/View.java b/group10/630505243/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group10/630505243/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/group10/630505243/src/com/coderising/litestruts/struts.xml b/group10/630505243/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..94b96a89a5 --- /dev/null +++ b/group10/630505243/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 diff --git a/group10/630505243/src/com/coding/basic/ArrayList.java b/group10/630505243/src/com/coding/basic/ArrayList.java index 396dc96ae7..9d32776b61 100644 --- a/group10/630505243/src/com/coding/basic/ArrayList.java +++ b/group10/630505243/src/com/coding/basic/ArrayList.java @@ -21,9 +21,8 @@ public void add(Object o){ } public void add(int index, Object o){ - if(index<=size){ - - if(index=size-1){ + elementData[size] = o; } + }else{ + //新增元素位置处于数组右边界 + Object[] temp = new Object[(size+1)+100]; + System.arraycopy(elementData, 0, temp, 0, size); + elementData = temp; + elementData[index] = o; } + size++; } public Object get(int index){ - if(index<=size){ + if(index<=elementData.length){ return elementData[index]; }else{ return null; @@ -82,8 +84,29 @@ public int size(){ return this.size; } + class ArrayIterator implements Iterator{ + private ArrayList array; + private int position = 0; + public ArrayIterator(ArrayList list){ + this.array = list; + } + @Override + public boolean hasNext() { + if(array.get(position)!=null){ + this.position++; + return true; + }else + return false; + } + @Override + public Object next() { + return array.get(position-1); + } + + } public Iterator iterator(){ - return null; + ArrayIterator aIterator = new ArrayIterator(this); + return aIterator; } } diff --git a/group10/630505243/src/com/coding/basic/ArrayListTest.java b/group10/630505243/src/com/coding/basic/ArrayListTest.java index fdd7cb07f7..bd7f4685dd 100644 --- a/group10/630505243/src/com/coding/basic/ArrayListTest.java +++ b/group10/630505243/src/com/coding/basic/ArrayListTest.java @@ -15,6 +15,7 @@ public class ArrayListTest { @Before public void setUp() throws Exception { list = new ArrayList(); + } @After @@ -26,7 +27,7 @@ public void testAddObject() { list.add(a); list.add(b); list.add(c); - System.out.println(list.size()); + /* System.out.println(list.size()); for(int i=0;i>{ + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode root; + private LinkedList tree = new LinkedList(); + + public Object getData() { + return data; + } + public void setData(T 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(T o){ + BinaryTreeNode currNode = null; + if(root==null){ + currNode = new BinaryTreeNode(); + currNode.data = o; + this.root = currNode; + tree.addFirst(currNode); + }else{ + currNode = findTheParentPosition(root,o); + if(o.compareTo(currNode.data)>0){ + BinaryTreeNode tNode = new BinaryTreeNode(); + tNode.data = o; + tree.add(tNode); + }else{ + BinaryTreeNode tNode = new BinaryTreeNode(); + tNode.data = o; + currNode.left = tNode; + tree.add(tNode); + } + } + return currNode; + } + + private BinaryTreeNode findTheParentPosition(BinaryTreeNode node ,T o){ + if(o.compareTo(node.data) >0){ + if(node.getRight()!=null) + return findTheParentPosition(node.getRight(),o); + else + return node; + }else{ + if(node.getLeft()!=null) + return findTheParentPosition(node.getLeft(),o); + else + return node; + } + } + + public void printTree(){ + if(this.tree!=null){ + Iterator i = tree.iterator(); + while(i.hasNext()){ + System.out.println(i.next()); + } + } + } + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append("[Node data:"+this.data+"]"); + return sb.toString(); + } + + + +} diff --git a/group10/630505243/src/com/coding/basic/BinaryTreeNodeTest.java b/group10/630505243/src/com/coding/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..07cf012318 --- /dev/null +++ b/group10/630505243/src/com/coding/basic/BinaryTreeNodeTest.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class BinaryTreeNodeTest { + BinaryTreeNode root = new BinaryTreeNode(); + @Before + public void setUp() throws Exception { + } + + @Test + public void testInsert() { + root.insert(5); + root.insert(3); + root.insert(8); + + root.printTree(); + } + +} diff --git a/group10/630505243/src/com/coding/basic/Iterator.java b/group10/630505243/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group10/630505243/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group10/630505243/src/com/coding/basic/LinkedList.java b/group10/630505243/src/com/coding/basic/LinkedList.java index 891c6bb034..d519789c36 100644 --- a/group10/630505243/src/com/coding/basic/LinkedList.java +++ b/group10/630505243/src/com/coding/basic/LinkedList.java @@ -74,6 +74,7 @@ public int size(){ public void addFirst(Object o){ Node node = new Node(o,head); this.head = node; + this.last = head; this.size++; } public void addLast(Object o){ @@ -112,8 +113,10 @@ public Object removeLast(){ return orgLast; } } + public Iterator iterator(){ - return null; + LinkedIterator linkIterator = new LinkedIterator(this); + return linkIterator; } @@ -137,7 +140,27 @@ public String toString() { sb.append("]"); return sb.toString(); } - + } + class LinkedIterator implements Iterator{ + private LinkedList list; + private int position=0; + public LinkedIterator(LinkedList list){ + this.list = list; + } + + @Override + public boolean hasNext() { + if(list.get(position)!=null){ + this.position++; + return true; + }else + return false; + } + + @Override + public Object next() { + return list.get(position-1); + } } } diff --git a/group10/630505243/src/com/coding/basic/LinkedListTest.java b/group10/630505243/src/com/coding/basic/LinkedListTest.java index 54937f26c2..dc1108746d 100644 --- a/group10/630505243/src/com/coding/basic/LinkedListTest.java +++ b/group10/630505243/src/com/coding/basic/LinkedListTest.java @@ -94,7 +94,10 @@ public void testRemoveLast() { @Test public void testIterator() { - fail("Not yet implemented"); + Iterator i = lkLst.iterator(); + while(i.hasNext()){ + System.out.println(i.next()); + } } } diff --git a/group10/630505243/src/com/coding/basic/Queue.java b/group10/630505243/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..eb684306d5 --- /dev/null +++ b/group10/630505243/src/com/coding/basic/Queue.java @@ -0,0 +1,46 @@ +package com.coding.basic; + +public class Queue { + private int size = 0; + private Object[] datas; + private int capacity = 10; + public void enQueue(Object o){ + if(datas==null){ + datas = new Object[capacity]; + } + if(this.size>=datas.length){ + Object[] newDatas = new Object[capacity+10]; + System.arraycopy(datas,0,newDatas,0,datas.length); + datas = newDatas; + } + datas[size] = o; + size++; + } + + public Object deQueue(){ + Object o = null; + if(datas!=null && datas.length>0){ + o = datas[0]; + }else{ + System.out.println("队列为空"); + } + for(int i=0;i0){ + return false; + }else{ + return true; + } + } + + public int size(){ + return this.size; + } +} diff --git a/group10/630505243/src/com/coding/basic/QueueTest.java b/group10/630505243/src/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..3658467490 --- /dev/null +++ b/group10/630505243/src/com/coding/basic/QueueTest.java @@ -0,0 +1,46 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class QueueTest { + Queue queue = null; + String str1 = "First"; + String str2 = "Second"; + String str3 = "Third"; + String str4 = "Forth"; + @Before + public void setUp() throws Exception { + queue = new Queue(); + } + + @Test + public void testEnQueue() { + queue.enQueue(str1); + queue.enQueue(str2); + queue.enQueue(str3); + queue.enQueue(str4); + assertEquals(queue.size(),4); + assertFalse(queue.isEmpty()); + } + + @Test + public void testDeQueue() { + queue.enQueue(str1); + queue.enQueue(str2); + queue.enQueue(str3); + queue.enQueue(str4); + assertEquals(str1,(String) queue.deQueue()); + assertEquals(queue.size(),3); + assertEquals(str2,(String) queue.deQueue()); + assertEquals(queue.size(),2); + assertEquals(str3,(String) queue.deQueue()); + assertEquals(queue.size(),1); + assertEquals(str4,(String) queue.deQueue()); + assertEquals(queue.size(),0); + assertFalse(queue.isEmpty()); + } + +} diff --git a/group10/630505243/src/com/coding/basic/Stack.java b/group10/630505243/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..dd0a7c61a2 --- /dev/null +++ b/group10/630505243/src/com/coding/basic/Stack.java @@ -0,0 +1,58 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + + /** + * 把项压入堆栈顶部 + * @param o + */ + public void push(Object o){ + elementData.add(size,o); + size++; + } + + /** + * 移除堆栈顶部的对象,并作为此函数的值,返回该对象 + * @return + */ + public Object pop(){ + if(this.elementData.size()>0){ + Object o = elementData.get(size-1); + elementData.remove(size); + size--; + return o; + }else{ + return null; + } + } + + /** + * 查看堆栈顶部的对象,但不从堆栈中移除它 + * @return + */ + public Object peek(){ + if(size>0){ + return elementData.get(size-1); + }else{ + return null; + } + } + + /** + * 测试堆栈是否为空 + * @return + */ + public boolean isEmpty(){ + return size>0?false:true; + } + + /** + * 获取堆栈大小 + * @return + */ + public int size(){ + return this.size; + } +} diff --git a/group10/630505243/src/com/coding/basic/StackTest.java b/group10/630505243/src/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..9ad98e67b5 --- /dev/null +++ b/group10/630505243/src/com/coding/basic/StackTest.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class StackTest { + + Stack s = null; + String a = "A"; + String b = "B"; + String c = "C"; + @Before + public void setUp() throws Exception { + s = new Stack(); + s.push(a); + //System.out.println(s.peek()); + s.push(b); + //System.out.println(s.peek()); + s.push(c); + //System.out.println(s.peek()); + } + + @Test + public void testPush() { + String d = "D"; + String e = "E"; + s.push(d); + System.out.println(s.peek()); + s.push(e); + System.out.println(s.peek()); + assertEquals(s.size(), 5); + } + + @Test + public void testPop() { + int len = s.size(); + for(int i=0;i + + + diff --git a/group10/875867419/src/com/work/week02/ArrayUtil.java b/group10/875867419/src/com/work/week02/ArrayUtil.java new file mode 100644 index 0000000000..cd22eba6ef --- /dev/null +++ b/group10/875867419/src/com/work/week02/ArrayUtil.java @@ -0,0 +1,198 @@ +package com.work.week02; + +/** + * 第二周数据结构作业 + * @author denghuaijun + * + */ +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 static int[] reverseArray(int[] origin){ + int[] dest = new int[origin.length]; + for(int i=0;i array2[j]){ + dest[k++] = array2[j++]; + continue; + } + } + //只剩array1时 + while(i < array1.length){ + dest[k++] = array1[i++]; + } + //只剩array2时 + while(j < array2.length){ + dest[k++] = array2[j++]; + } + return removeZero(dest); + } + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + int newLength = oldArray.length + size; + int[] newArray = new int[newLength]; + 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 static int[] fibonacci(int max){ + int[] dest = null; + if(max == 1){ + return dest; + } + dest = new int[max+3]; + dest[0] = dest[1] = 1; + for(int i=2; i parameters){ + View view = new View(); + try{ + StrutsXmlDao dao = loadXmlByDom4j(actionName); + view = reflectCreateObj(dao, parameters); + }catch(Exception e) { + e.printStackTrace(); + } + return view; + } + + /** + * 璇诲彇xml鏂囦欢 + * @throws DocumentException + */ + @SuppressWarnings("unchecked") + private static StrutsXmlDao loadXmlByDom4j(String actionName) throws DocumentException{ + StrutsXmlDao dao = new StrutsXmlDao(); + SAXReader reader = new SAXReader(); //鍒涘缓SAXReader瀵硅薄 + Document doc = reader.read(new File("src/com/work/week02/struts.xml")); //鍒涘缓Document瀵硅薄 + Element root = doc.getRootElement(); //鑾峰彇鏍硅妭鐐 + + List list = root.elements(); + for (Element element : list) { + if(element.attributeValue("name").equals(actionName)){ + dao.setActionName(element.attributeValue("name")); + dao.setActionClass(element.attributeValue("class")); + List branchs = element.elements(); + List> actionResult = new ArrayList>(); + for (Element branch : branchs) { + Map map = new HashMap(); + map.put(branch.attributeValue("name"), branch.getTextTrim()); + actionResult.add(map); + } + dao.setActionResult(actionResult); + } + + } + + return dao; + } + + /** + * 閫氳繃鍙嶅皠鍒涘缓鎸囧畾瀵硅薄 + * @throws ClassNotFoundException + * @throws IllegalAccessException + * @throws InstantiationException + * @throws SecurityException + * @throws NoSuchMethodException + * @throws InvocationTargetException + * @throws IllegalArgumentException + */ + private static View reflectCreateObj(StrutsXmlDao dao, Map parameters) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException{ + View view = new View(); + Class clazz = Class.forName(dao.getActionClass()); + //瀹炰緥鍖栧璞 + Object obj = clazz.newInstance(); + //璋冪敤set鏂规硶璁剧疆鍙傛暟 + Set keys = parameters.keySet(); + for (String key : keys) { + String value = parameters.get(key); + Method setMethod = clazz.getDeclaredMethod("set"+key.substring(0, 1).toUpperCase()+key.substring(1), new Class[]{String.class}); + setMethod.invoke(obj, value); + } + + //璋冪敤execute()鏂规硶鎵ц + Method execute = clazz.getMethod("execute"); + Object reuslt = execute.invoke(obj); //execute()鏂规硶杩斿洖鍊 + List> actionResult = dao.getActionResult(); + //鑾峰彇杩斿洖鍊煎搴旂殑璺宠浆jsp + for (Map map : actionResult) { + if(map.containsKey(reuslt)){ + view.setJsp(map.get(reuslt).toString()); + } + } + + Map params = new HashMap(); + //璋冪敤get鏂规硶灏嗗弬鏁板瓨鍏iew涓 + Field[] fields = clazz.getDeclaredFields(); + for (Field field : fields) { + String name = field.getName(); + Method getMethod = clazz.getDeclaredMethod("get"+name.substring(0, 1).toUpperCase()+name.substring(1)); + String value = getMethod.invoke(obj).toString(); + params.put(name, value); + } + view.setParameters(params); + + return view; + } +} diff --git a/group10/875867419/src/com/work/week02/StrutsTest.java b/group10/875867419/src/com/work/week02/StrutsTest.java new file mode 100644 index 0000000000..b2c3ab11a3 --- /dev/null +++ b/group10/875867419/src/com/work/week02/StrutsTest.java @@ -0,0 +1,40 @@ +package com.work.week02; + +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/group10/875867419/src/com/work/week02/StrutsXmlDao.java b/group10/875867419/src/com/work/week02/StrutsXmlDao.java new file mode 100644 index 0000000000..4d355d189a --- /dev/null +++ b/group10/875867419/src/com/work/week02/StrutsXmlDao.java @@ -0,0 +1,49 @@ +package com.work.week02; + +import java.io.Serializable; +import java.util.List; +import java.util.Map; + +public class StrutsXmlDao implements Serializable { + + private static final long serialVersionUID = -3117497421210403602L; + + private String actionName; + private String actionClass; + private List> actionResult; + + public StrutsXmlDao(){ + super(); + } + + public String getActionName() { + return actionName; + } + + public void setActionName(String actionName) { + this.actionName = actionName; + } + + public String getActionClass() { + return actionClass; + } + + public void setActionClass(String actionClass) { + this.actionClass = actionClass; + } + + public List> getActionResult() { + return actionResult; + } + + public void setActionResult(List> actionResult) { + this.actionResult = actionResult; + } + + @Override + public String toString() { + return "StrutsXmlDao [actionName=" + actionName + ", actionClass=" + actionClass + ", actionResult=" + + actionResult + "]"; + } + +} diff --git a/group10/875867419/src/com/work/week02/View.java b/group10/875867419/src/com/work/week02/View.java new file mode 100644 index 0000000000..a4a2830750 --- /dev/null +++ b/group10/875867419/src/com/work/week02/View.java @@ -0,0 +1,30 @@ +package com.work.week02; + + +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; + } + @Override + public String toString() { + return "View [jsp=" + jsp + ", parameters=" + parameters + "]"; + } + +} diff --git a/group10/875867419/src/com/work/week02/struts.xml b/group10/875867419/src/com/work/week02/struts.xml new file mode 100644 index 0000000000..da643f1f65 --- /dev/null +++ b/group10/875867419/src/com/work/week02/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/group10/875867419/src/com/work/week02/\345\215\232\346\226\207\344\275\234\344\270\232\345\234\260\345\235\200.txt" "b/group10/875867419/src/com/work/week02/\345\215\232\346\226\207\344\275\234\344\270\232\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..d1dace46cf --- /dev/null +++ "b/group10/875867419/src/com/work/week02/\345\215\232\346\226\207\344\275\234\344\270\232\345\234\260\345\235\200.txt" @@ -0,0 +1 @@ +本周博文作业链接地址:https://my.oschina.net/u/3080511/blog/851907 \ No newline at end of file diff --git a/group10/904627477/.classpath b/group10/904627477/.classpath new file mode 100644 index 0000000000..e6a94f0acc --- /dev/null +++ b/group10/904627477/.classpath @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/group10/904627477/.gitignore b/group10/904627477/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group10/904627477/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group10/904627477/.project b/group10/904627477/.project new file mode 100644 index 0000000000..9cd9b13ae3 --- /dev/null +++ b/group10/904627477/.project @@ -0,0 +1,23 @@ + + + testGit + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.jdt.core.javanature + + diff --git a/group10/904627477/.settings/org.eclipse.jdt.core.prefs b/group10/904627477/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..bbcbc93486 --- /dev/null +++ b/group10/904627477/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/group10/904627477/.settings/org.eclipse.m2e.core.prefs b/group10/904627477/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000000..14b697b7bb --- /dev/null +++ b/group10/904627477/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/group10/904627477/pom.xml b/group10/904627477/pom.xml new file mode 100644 index 0000000000..2346e64026 --- /dev/null +++ b/group10/904627477/pom.xml @@ -0,0 +1,40 @@ + + 4.0.0 + testGit + testGit + 0.0.1-SNAPSHOT + + + + + + + dom4j + dom4j + 1.6.1 + + + + + + src + + + src + + **/*.java + + + + + + maven-compiler-plugin + 3.1 + + 1.7 + 1.7 + + + + + \ No newline at end of file diff --git a/group10/904627477/src/com/coding/ArrayList.java b/group10/904627477/src/com/coding/ArrayList.java new file mode 100644 index 0000000000..3053e54114 --- /dev/null +++ b/group10/904627477/src/com/coding/ArrayList.java @@ -0,0 +1,107 @@ +package com.coding; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + private void addLength(){ + int len = elementData.length; + Object[] temp = new Object[len+50]; + for(int i=0;isize()){ + throw new IndexOutOfBoundsException(); + } + if(size==elementData.length){ + addLength(); + } + for(int i=size;i>index;i--){ + elementData[i] = elementData[i-1]; + } + elementData[index] = o; + size++; + } + + public Object get(int index){ + if(index<0||index>=size()){ + throw new IndexOutOfBoundsException(); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index<0||index>=size()){ + throw new IndexOutOfBoundsException(); + } + Object temp = elementData[index]; + for (int i = index; i < size-1; i++) { + elementData[i] = elementData[i+1]; + } + elementData[size-1] = null; + size--; + return temp; + } + + public int size(){ + return this.size; + } + + public Iterator iterator(){ + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator{ + + private Object[] array; + private int index; + + public ArrayListIterator(){ + array = new Object[size]; + index = 0; + for (int i = 0; i < size; i++) { + array[i] = elementData[i]; + } + } + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + if(index>=0&&index=array.length){ + throw new NoSuchElementException(); + } + Object temp = array[index]; + index ++; + return temp; + } + + } + + +} diff --git a/group10/904627477/src/com/coding/BinaryTreeNode.java b/group10/904627477/src/com/coding/BinaryTreeNode.java new file mode 100644 index 0000000000..fe50e3d1ae --- /dev/null +++ b/group10/904627477/src/com/coding/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.coding; + +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/group10/904627477/src/com/coding/Iterator.java b/group10/904627477/src/com/coding/Iterator.java new file mode 100644 index 0000000000..d2c1b79572 --- /dev/null +++ b/group10/904627477/src/com/coding/Iterator.java @@ -0,0 +1,7 @@ +package com.coding; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group10/904627477/src/com/coding/LinkedList.java b/group10/904627477/src/com/coding/LinkedList.java new file mode 100644 index 0000000000..f878f75820 --- /dev/null +++ b/group10/904627477/src/com/coding/LinkedList.java @@ -0,0 +1,175 @@ +package com.coding; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + + public void add(Object o){ + addLast(o); + } + + public void add(int index , Object o){ + Node node = new Node(); + node.data = o; + if(index==0){ + addFirst(o); + return ; + } + Node before = getNode(index-1); + Node next = before.next; + before.next = node; + node.next = next; + } + + private Node getLastNode(){ + Node temp = head; + if(head!=null){ + while(true){ + if(temp.next!=null){ + temp = temp.next; + }else{ + break; + } + } + }else{ + throw new NoSuchElementException(); + } + return temp; + } + + private Node getNode(int index){ + if(index<0){ + throw new IndexOutOfBoundsException(); + } + int i = 0; + Node temp = head; + while(true){ + if(temp==null){ + throw new IndexOutOfBoundsException(); + } + if(i==index){ + break; + }else{ + i++; + temp = temp.next; + } + } + return temp; + } + + public Object get(int index){ + Node node = getNode(index); + return node.data; + } + + public Object remove(int index){ + if(index==0){ + removeFirst(); + } + Node before = getNode(index-1); + Node temp = getNode(index); + before.next = temp.next; + return temp.data; + } + + public int size(){ + int size = 0; + Node temp = head; + while(true){ + if(temp==null){ + break; + }else{ + size++; + temp = temp.next; + } + } + return size; + } + + public void addFirst(Object o){ + Node node = new Node(); + node.data = o; + node.next = head; + head = node; + } + + public void addLast(Object o){ + Node node = new Node(); + node.data = o; + if(head==null){ + head = node; + return; + } + Node last = getLastNode(); + last.next = node; + } + public Object removeFirst(){ + if(head == null){ + throw new NoSuchElementException(); + } + Object obj = head.data; + head = head.next; + return obj; + } + public Object removeLast(){ + if(head == null){ + throw new NoSuchElementException(); + } + if(head.next == null){ + return removeFirst(); + } + Node before = head; + Node temp = head.next; + while(true){ + if(temp.next==null){ + break; + }else{ + before = temp; + temp = temp.next; + } + } + before.next = null; + return temp.data; + } + + public Iterator iterator(){ + return new LinkedIterator(); + } + + + private static class Node{ + Object data; + Node next; + } + + private class LinkedIterator implements Iterator{ + + private Node node; + + public LinkedIterator(){ + node = head; + } + + @Override + public boolean hasNext() { + if(node!=null){ + return true; + } + return false; + } + + @Override + public Object next() { + if(node==null){ + throw new NoSuchElementException(); + }else{ + Object obj = node.data; + node = node.next; + return obj; + } + } + + } +} diff --git a/group10/904627477/src/com/coding/List.java b/group10/904627477/src/com/coding/List.java new file mode 100644 index 0000000000..5e78eced11 --- /dev/null +++ b/group10/904627477/src/com/coding/List.java @@ -0,0 +1,9 @@ +package com.coding; + +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/group10/904627477/src/com/coding/Queue.java b/group10/904627477/src/com/coding/Queue.java new file mode 100644 index 0000000000..8fd01bef96 --- /dev/null +++ b/group10/904627477/src/com/coding/Queue.java @@ -0,0 +1,28 @@ +package com.coding; + +public class Queue { + + private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o){ + elementData.add(o); + } + + public Object deQueue(){ + if(elementData.size()==0){ + return null; + } + return elementData.remove(0); + } + + public boolean isEmpty(){ + if(elementData.size()==0){ + return true; + } + return false; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group10/904627477/src/com/coding/Stack.java b/group10/904627477/src/com/coding/Stack.java new file mode 100644 index 0000000000..3f8b319607 --- /dev/null +++ b/group10/904627477/src/com/coding/Stack.java @@ -0,0 +1,36 @@ +package com.coding; + +import java.util.EmptyStackException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + if(elementData.size()==0){ + throw new EmptyStackException(); + } + Object obj = elementData.remove(size()-1); + return obj; + } + + public Object peek(){ + if(elementData.size()==0){ + throw new EmptyStackException(); + } + Object obj = elementData.get(size()-1); + return obj; + } + public boolean isEmpty(){ + if(elementData.size()==0){ + return true; + } + return false; + } + public int size(){ + return elementData.size(); + } +} diff --git a/group10/904627477/src/com/coding/array/ArrayUtil.java b/group10/904627477/src/com/coding/array/ArrayUtil.java new file mode 100644 index 0000000000..37f230812a --- /dev/null +++ b/group10/904627477/src/com/coding/array/ArrayUtil.java @@ -0,0 +1,215 @@ +package com.coding.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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[len-1-i]; + origin[len-1-i] = origin[i]; + origin[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[] tempArr = new int[oldArray.length]; + int size = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i]!=0){ + tempArr[size] = oldArray[i]; + size++; + } + } + int[] newArr = new int[size]; + System.arraycopy(tempArr, 0, newArr, 0, size); + return newArr; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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[] arr3 = new int[array1.length+array2.length]; + int len1 = array1.length; + int len2 = array2.length; + int i=0,j=0,k=0; + while(true){ + if(iarray2[j]){ + arr3[k++]=array2[j++]; + }else{ + arr3[k++] = array1[i++]; + j++; + } + }else if(i>=len1&&j=len2){ + arr3[k++]=array1[i++]; + }else{ + break; + } + } + int[] newArr = new int[k]; + System.arraycopy(arr3, 0, newArr, 0, k); + return newArr; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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){ + if(size<0){ + throw new IllegalArgumentException(); + } + int[] newArr = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArr[i] = oldArray[i]; + } + return newArr; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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){ + throw new IllegalArgumentException(); + } + int[] arr = new int[0]; + int temp = 1; + for (int i = 0; max > temp; i++) { + arr = grow(arr, 1); + arr[arr.length-1] = temp; + temp = getFibonacci(i+1); + } + return arr; + } + + public int getFibonacci(int n){ + if(n==0||n==1){ + return 1; + }else{ + return getFibonacci(n-1)+getFibonacci(n-2); + } + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + //瀵瑰ぇ浜1鐨勮嚜鐒舵暟n锛屽鏋滅敤2鍒 寮鏍瑰彿n 涔嬮棿鐨勬墍鏈夋暣鏁板幓闄わ紝鍧囨棤娉曟暣闄わ紝鍒檔涓鸿川鏁 + public int[] getPrimes(int max){ + int[] arr = new int[0]; + if(max<2){ + return arr; + } + for(int i=2;i1; + } + for(int i=2;i<=Math.sqrt(n);i++){ + if(n%i==0){ + return false; + } + } + return true; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] arr = new int[0]; + if(max<1){ + return arr; + } + for (int i = 0; i < max; i++) { + if(isPerfectNumber(i)){ + arr = grow(arr, 1); + arr[arr.length-1] = i; + } + } + return arr; + } + + public boolean isPerfectNumber(int n){ + if(n<=1){ + return false; + } + int result = 1; + for (int i = 2; i <= Math.sqrt(n); i++) { + if(n%i==0){ + result = result + i + n/i; + } + } + if(result==n){ + return true; + }else{ + return false; + } + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + String result = ""; + for (int i = 0; i < array.length; i++) { + result = result + array[i] + seperator; + } + int index = result.lastIndexOf(seperator); + return result.substring(0, index); + } + + +} diff --git a/group10/904627477/src/com/coding/litestruts/LoginAction.java b/group10/904627477/src/com/coding/litestruts/LoginAction.java new file mode 100644 index 0000000000..ec16031fee --- /dev/null +++ b/group10/904627477/src/com/coding/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coding.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/group10/904627477/src/com/coding/litestruts/ReflectUtil.java b/group10/904627477/src/com/coding/litestruts/ReflectUtil.java new file mode 100644 index 0000000000..789275aa02 --- /dev/null +++ b/group10/904627477/src/com/coding/litestruts/ReflectUtil.java @@ -0,0 +1,145 @@ +package com.coding.litestruts; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.regex.Pattern; + +public class ReflectUtil { + + + public static Object exectue(Object o,String methodName){ + Object result = null; + try { + Method m = o.getClass().getMethod(methodName); + result = m.invoke(o); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return result; + } + + public static Object getObject(String className, Map parameters){ + Object action = null; + try { + action = Class.forName(className).newInstance(); + if(parameters==null){ + return action; + } + Iterator ite = parameters.keySet().iterator(); + while(ite.hasNext()){ + String name = ite.next(); + String value = parameters.get(name); + setAttribute(action, name, value); + } + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return action; + } + + /** + * + * @param o + * @param name + * @param value + */ + public static void setAttribute(Object o,String name,String value){ + try { + Class c = o.getClass(); + String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); + Class attType = c.getDeclaredField(name).getType(); + Method m = c.getMethod(methodName, attType); + m.invoke(o, typeCase(value, attType)); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + + public static Map getAttributes(Object o){ + Map param = new HashMap(); + Class c = o.getClass(); + Field[] attrs = c.getDeclaredFields(); + for (Field att : attrs) { + String name = att.getName(); + String methodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1); + Object reslut = exectue(o, methodName); + if(reslut!=null){ + param.put(name, reslut); + } + } + return param; + } + + /** + * 灏哠ting绫诲瀷鏁版嵁杞崲鎴愭寚瀹氱被鍨嬫暟鎹,鏆傛敮鎸佺畝鍗曞拰甯歌绫诲瀷 + * @param oldValue 闇瑕佽浆鎹㈢殑鏁版嵁鍊 + * @param type 鐩爣绫诲瀷 + * @return 杩斿洖杞崲鍚庣殑鏁版嵁 + */ + public static Object typeCase(String oldValue,Class type){ + Object value = null; + String typeName = type.getName(); + if("int".equals(typeName)){ + value = Integer.parseInt(oldValue); + }else if("float".equals(typeName)){ + value = Float.parseFloat(oldValue); + }else if("boolean".equals(typeName)){ + value = Boolean.parseBoolean(oldValue); + }else if("long".equals(typeName)){ + value = Long.parseLong(oldValue); + }else if("byte".equals(typeName)){ + value = Byte.parseByte(oldValue); + }else if("double".equals(typeName)){ + value = Double.parseDouble(oldValue); + }else if("short".equals(typeName)){ + value = Short.parseShort(oldValue); + }else if("char".equals(typeName)){ + value = oldValue.charAt(0); + }else if("java.util.Date".equals(typeName)){ + try { + if(Pattern.matches("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{3}", oldValue)){ + value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").parse(oldValue); + }else if(Pattern.matches("[0-9]{4}-[0-9]{2}-[0-9]{2}", oldValue)){ + value = new SimpleDateFormat("yyyy-MM-dd").parse(oldValue); + }else{ + value = null; + } + } catch (ParseException e) { + e.printStackTrace(); + } + }else if("java.lang.String".equals(typeName)){ + value = oldValue; + }else{ + throw new ClassCastException(); + } + return value; + } +} diff --git a/group10/904627477/src/com/coding/litestruts/Struts.java b/group10/904627477/src/com/coding/litestruts/Struts.java new file mode 100644 index 0000000000..eb68fa9441 --- /dev/null +++ b/group10/904627477/src/com/coding/litestruts/Struts.java @@ -0,0 +1,106 @@ +package com.coding.litestruts; + +import java.util.List; +import java.util.Map; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + + +public class Struts { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + public static View runAction(String actionName, Map parameters) { + if(actionName==null){ + return null; + } + View view = new View(); + String path = getStrutsXMLPath(); + Element actionEle = getActionElement(path, actionName); + if(actionEle==null){ + return null; + } + String className = actionEle.attributeValue("class"); + Object action = ReflectUtil.getObject(className, parameters); + if(action==null){ + return null; + } + String methodName = actionEle.attributeValue("method"); + methodName = methodName==null?"execute":methodName; + Object reslut = ReflectUtil.exectue(action, methodName); + String jsp = getElementJsp(actionEle, reslut!=null?reslut.toString():null); + view.setJsp(jsp); + view.setParameters(ReflectUtil.getAttributes(action)); + return view; + } + + private static String getStrutsXMLPath(){ + String path = Struts.class.getResource("").getPath()+"struts.xml"; + path = path.substring(1); + return path; + } + + @SuppressWarnings("unchecked") + public static Element getActionElement(String path,String actionName){ + if(path==null||actionName==null){ + return null; + } + Element actionEle = null; + try { + SAXReader read = new SAXReader(); + Document doc = read.read(path); + Element root = doc.getRootElement(); + List actions = root.elements("action"); + for (Element element : actions) { + String name = element.attributeValue("name"); + if(actionName.equals(name)){ + actionEle = element; + break; + } + } + } catch (SecurityException e) { + e.printStackTrace(); + } catch (DocumentException e) { + e.printStackTrace(); + } + return actionEle; + } + + @SuppressWarnings("unchecked") + public static String getElementJsp(Element actionEle, String reslut) { + String jsp = null; + if(reslut!=null){ + List results = actionEle.elements("result"); + for (Element reslutEle : results) { + String resName = reslutEle.attributeValue("name"); + resName = resName==null?"success":resName; + if(reslut.equals(resName)){ + jsp = reslutEle.getText().trim(); + } + } + } + return jsp; + } + +} diff --git a/group10/904627477/src/com/coding/litestruts/StrutsTest.java b/group10/904627477/src/com/coding/litestruts/StrutsTest.java new file mode 100644 index 0000000000..fe43a16de8 --- /dev/null +++ b/group10/904627477/src/com/coding/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coding.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/group10/904627477/src/com/coding/litestruts/View.java b/group10/904627477/src/com/coding/litestruts/View.java new file mode 100644 index 0000000000..9ee1d76628 --- /dev/null +++ b/group10/904627477/src/com/coding/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coding.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/group10/904627477/src/com/coding/litestruts/struts.xml b/group10/904627477/src/com/coding/litestruts/struts.xml new file mode 100644 index 0000000000..b494d6b52f --- /dev/null +++ b/group10/904627477/src/com/coding/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/group10/904627477/src/com/coding/test/ArrayListTest.java b/group10/904627477/src/com/coding/test/ArrayListTest.java new file mode 100644 index 0000000000..d65a692351 --- /dev/null +++ b/group10/904627477/src/com/coding/test/ArrayListTest.java @@ -0,0 +1,71 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coding.ArrayList; +import com.coding.Iterator; + +public class ArrayListTest { + + private static ArrayList list = new ArrayList(); + + @Before + public void setUp() throws Exception { + list = new ArrayList(); + list.add("1111"); + list.add("2222"); + list.add("3333"); + } + + @After + public void tearDown() throws Exception { + list = null; + } + + @Test + public void testAddObject() { + list.add("4444"); + assertEquals("4444", list.get(3)); + assertEquals(4, list.size()); + } + + @Test + public void testAddIntObject() { + list.add(1, "4444"); + assertEquals("4444", list.get(1)); + assertEquals(4, list.size()); + assertEquals("2222", list.get(2)); + } + + @Test + public void testGet() { + assertEquals("1111", list.get(0)); + } + + @Test + public void testRemove() { + Object sss = list.remove(1); + assertEquals(2, list.size()); + assertEquals("2222", sss); + } + + @Test + public void testSize() { + assertEquals(3, list.size()); + } + + @Test + public void testIterator() { + Iterator it = list.iterator(); + assertEquals(true, it.hasNext()); + assertEquals("1111", it.next()); + assertEquals("2222", it.next()); + assertEquals("3333", it.next()); + assertEquals(false, it.hasNext()); + } + +} diff --git a/group10/904627477/src/com/coding/test/ArrayUtilTest.java b/group10/904627477/src/com/coding/test/ArrayUtilTest.java new file mode 100644 index 0000000000..f6bf45f1cc --- /dev/null +++ b/group10/904627477/src/com/coding/test/ArrayUtilTest.java @@ -0,0 +1,128 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coding.array.ArrayUtil; + +public class ArrayUtilTest { + + private ArrayUtil arrayUtil; + + @Before + public void setUp() throws Exception { + this.arrayUtil = new ArrayUtil(); + } + + @After + public void tearDown() throws Exception { + this.arrayUtil = null; + } + + @Test + public void testReverseArray() { + int[] origin1 = {7, 9 , 30, 3}; + int[] origin2 = {7, 9, 30, 3, 4}; + this.arrayUtil.reverseArray(origin1); + assertEquals(3, origin1[0]); + assertEquals(30, origin1[1]); + assertEquals(9, origin1[2]); + assertEquals(7, origin1[3]); + this.arrayUtil.reverseArray(origin2); + assertEquals(4, origin2[0]); + assertEquals(3, origin2[1]); + assertEquals(30, origin2[2]); + assertEquals(9, origin2[3]); + assertEquals(7, origin2[4]); + } + + @Test + public void testRemoveZero() { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] newArr = this.arrayUtil.removeZero(oldArr); + assertEquals(12, newArr.length); + assertEquals(6, newArr[4]); + int oldArr1[]={0,0,0,0}; + int[] newArr1 = this.arrayUtil.removeZero(oldArr1); + assertEquals(0, newArr1.length); + } + + @Test + public void testMerge() { + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + //[3,4,5,6,7,8] + int[] a3 = this.arrayUtil.merge(a1, a2); + assertEquals(6, a3.length); + assertEquals(3, a3[0]); + assertEquals(8, a3[5]); + int[] arr1 = {3, 5, 7}; + int[] arr2 = {9,12,15,19}; + int[] arr3 = this.arrayUtil.merge(arr1, arr2); + int[] arr4 = this.arrayUtil.merge(arr2, arr1); + assertEquals(7, arr3.length); + assertEquals(3, arr3[0]); + assertEquals(19, arr3[6]); + //assertEquals(arr3, arr4); + assertEquals(7, arr4.length); + assertEquals(3, arr4[0]); + assertEquals(19, arr4[6]); + int[] a = {}; + int[] arr = this.arrayUtil.merge(a, a1); + assertEquals(4, arr.length); + assertEquals(3, arr[0]); + assertEquals(8, arr[3]); + } + + @Test + public void testGrow() { + int[] a1 = {2,3,6}; + int[] a2 = this.arrayUtil.grow(a1, 3); + assertEquals(6, a2.length); + assertEquals(3, a1[1]); + assertEquals(0, a2[3]); + int[] a3 = this.arrayUtil.grow(a1, 0); + assertEquals(3, a3.length); + int[] a4 = this.arrayUtil.grow(new int[0], 3); + assertEquals(3, a4.length); + } + + @Test + public void testFibonacci() { + int[] arr = this.arrayUtil.fibonacci(15); + assertEquals(7, arr.length); + assertEquals(13, arr[6]); + int[] arr1 = this.arrayUtil.fibonacci(1); + assertEquals(0, arr1.length); + } + + @Test + public void testGetPrimes() { + int[] arr = this.arrayUtil.getPrimes(23); + assertEquals(8, arr.length); + assertEquals(true, arr[arr.length-1]<23); + int[] arr1 = this.arrayUtil.getPrimes(2); + assertEquals(0, arr1.length); + } + + @Test + public void testGetPerfectNumbers() { + //6 28 496 8128 33550336 + int[] arr = this.arrayUtil.getPerfectNumbers(10000); + assertEquals(4, arr.length); + assertEquals(8128, arr[3]); + } + + @Test + public void testJoin() { + int[] array= {3,8,9}; + String str = this.arrayUtil.join(array, "-"); + assertEquals("3-8-9", str); + String str1 = this.arrayUtil.join(array, "9"); + assertEquals("39899", str1); + } + +} diff --git a/group10/904627477/src/com/coding/test/LinkedListTest.java b/group10/904627477/src/com/coding/test/LinkedListTest.java new file mode 100644 index 0000000000..af48a6fb0b --- /dev/null +++ b/group10/904627477/src/com/coding/test/LinkedListTest.java @@ -0,0 +1,118 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coding.Iterator; +import com.coding.LinkedList; + +public class LinkedListTest { + + private static LinkedList link; + + @Before + public void setUp() throws Exception { + link = new LinkedList(); + link.add("111"); + link.add("222"); + link.add("333"); + } + + @After + public void tearDown() throws Exception { + link = null; + } + + @Test + public void testAddObject() { + link.add("444"); + assertEquals(4, link.size()); + assertEquals("444", link.get(3)); + link = new LinkedList(); + link.add("000"); + assertEquals(1, link.size()); + assertEquals("000", link.get(0)); + } + + @Test + public void testAddIntObject() { + link.add(2,"444"); + assertEquals(4, link.size()); + assertEquals("444", link.get(2)); + assertEquals("333", link.get(3)); + assertEquals("222", link.get(1)); + link = new LinkedList(); + link.add(0,"000"); + assertEquals(1, link.size()); + assertEquals("000", link.get(0)); + } + + @Test + public void testGet() { + assertEquals("222", link.get(1)); + } + + @Test + public void testRemove() { + Object obj = link.remove(1); + assertEquals("222", obj); + assertEquals("333", link.get(1)); + assertEquals(2, link.size()); + } + + @Test + public void testSize() { + assertEquals(3, link.size()); + } + + @Test + public void testAddFirst() { + link.addFirst("000"); + assertEquals(4, link.size()); + assertEquals("000", link.get(0)); + link = new LinkedList(); + link.addFirst("000"); + assertEquals(1, link.size()); + assertEquals("000", link.get(0)); + } + + @Test + public void testAddLast() { + link.addLast("444"); + assertEquals(4, link.size()); + assertEquals("444", link.get(3)); + link = new LinkedList(); + link.addLast("444"); + assertEquals(1, link.size()); + assertEquals("444", link.get(0)); + } + + @Test + public void testRemoveFirst() { + Object obj = link.removeFirst(); + assertEquals("111", obj); + assertEquals(2, link.size()); + assertEquals("222", link.get(0)); + } + + @Test + public void testRemoveLast() { + Object obj = link.removeLast(); + assertEquals(2, link.size()); + assertEquals("333", obj); + } + + @Test + public void testIterator() { + Iterator it = link.iterator(); + assertEquals(true, it.hasNext()); + assertEquals("111", it.next()); + assertEquals("222", it.next()); + assertEquals("333", it.next()); + assertEquals(false, it.hasNext()); + } + +} diff --git a/group10/904627477/src/com/coding/test/QueueTest.java b/group10/904627477/src/com/coding/test/QueueTest.java new file mode 100644 index 0000000000..779120c0fb --- /dev/null +++ b/group10/904627477/src/com/coding/test/QueueTest.java @@ -0,0 +1,53 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coding.Queue; + +public class QueueTest { + + private static Queue queue; + + @Before + public void setUp() throws Exception { + queue = new Queue(); + queue.enQueue("111"); + queue.enQueue("222"); + queue.enQueue("333"); + } + + @After + public void tearDown() throws Exception { + queue = null; + } + + @Test + public void testEnQueue() { + queue.enQueue("444"); + assertEquals(4, queue.size()); + } + + @Test + public void testDeQueue() { + Object obj = queue.deQueue(); + assertEquals(2, queue.size()); + assertEquals("111",obj); + } + + @Test + public void testIsEmpty() { + assertEquals(false, queue.isEmpty()); + queue = new Queue(); + assertEquals(true, queue.isEmpty()); + } + + @Test + public void testSize() { + assertEquals(3, queue.size()); + } + +} diff --git a/group10/904627477/src/com/coding/test/StackTest.java b/group10/904627477/src/com/coding/test/StackTest.java new file mode 100644 index 0000000000..38de36c9d9 --- /dev/null +++ b/group10/904627477/src/com/coding/test/StackTest.java @@ -0,0 +1,61 @@ +package com.coding.test; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.coding.Stack; + +public class StackTest { + + private static Stack stack; + + @Before + public void setUp() throws Exception { + stack = new Stack(); + stack.push("111"); + stack.push("222"); + stack.push("333"); + } + + @After + public void tearDown() throws Exception { + stack = null; + } + + @Test + public void testPush() { + stack.push("444"); + assertEquals(4, stack.size()); + assertEquals("444", stack.pop()); + } + + @Test + public void testPop() { + Object obj = stack.pop(); + assertEquals("333", obj); + assertEquals(2, stack.size()); + } + + @Test + public void testPeek() { + Object obj = stack.peek(); + assertEquals("333", obj); + assertEquals(3, stack.size()); + } + + @Test + public void testIsEmpty() { + assertEquals(false, stack.isEmpty()); + stack = new Stack(); + assertEquals(true, stack.isEmpty()); + } + + @Test + public void testSize() { + assertEquals(3,stack.size()); + } + +} diff --git a/group10/904627477/src/com/coding/test/Test.java b/group10/904627477/src/com/coding/test/Test.java new file mode 100644 index 0000000000..c8024a8c97 --- /dev/null +++ b/group10/904627477/src/com/coding/test/Test.java @@ -0,0 +1,29 @@ +package com.coding.test; + + + + + +public class Test { + + public static void main(String[] args) { + /*int[] origin = {7, 9 , 30, 3}; + ArrayUtil au = new ArrayUtil(); + au.reverseArray(origin); + for (int i : origin) { + System.out.println(i); + } + */ +/* int a = 5; + System.out.println(5/2); + int[] a1 = new int[0]; + System.out.println(a1.length); + System.out.println(Math.sqrt(2));*/ +// int[] a2 = new ArrayUtil().getPerfectNumbers(100); +// for (int i = 0; i < a2.length; i++) { +// System.out.println(a2[i]); +// } + System.out.println(Test.class.getResource("").getPath()); + } + +} diff --git a/group15/1500_369516660/.classpath b/group11/1059156023/Array/.classpath similarity index 100% rename from group15/1500_369516660/.classpath rename to group11/1059156023/Array/.classpath diff --git a/group11/1059156023/Array/.gitignore b/group11/1059156023/Array/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group11/1059156023/Array/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group11/1059156023/Array/.project b/group11/1059156023/Array/.project new file mode 100644 index 0000000000..1b5c14fe3f --- /dev/null +++ b/group11/1059156023/Array/.project @@ -0,0 +1,17 @@ + + + Array + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group11/1059156023/Array/src/com/coding/basic/ArrayUtil.java b/group11/1059156023/Array/src/com/coding/basic/ArrayUtil.java new file mode 100644 index 0000000000..2b47957623 --- /dev/null +++ b/group11/1059156023/Array/src/com/coding/basic/ArrayUtil.java @@ -0,0 +1,158 @@ +package com.coding.basic; + +import java.util.Arrays; +import java.util.HashSet; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + for (int i = 0; i < origin.length/2; i++) {//鍓嶅悗浜ゆ崲鍏冪礌 + int temp = origin[i]; + origin[i] = origin[origin.length-i-1]; + origin[origin.length-i-1]= temp; + } + System.out.println(Arrays.toString(origin));//杈撳嚭鏁扮粍 + + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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[] arr = new int[oldArray.length]; + int count = 0; + for(int i=0;i set1 = new HashSet(Arrays.asList(array1));//浠rray1寤虹珛闆嗗悎 + HashSet set2 = new HashSet(Arrays.asList(array2)); + set1.addAll(set2);//姹傚苟闆 + Integer[] arr = set1.toArray(new Integer[set1.size()]);//鑾峰彇骞堕泦鍚庣殑鏁扮粍 + Arrays.sort(arr);//鏁扮粍鎺掑簭 + for(int i=0;i + + + + + diff --git a/group11/1059156023/dataStructure/.gitignore b/group11/1059156023/dataStructure/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group11/1059156023/dataStructure/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group11/1059156023/dataStructure/.project b/group11/1059156023/dataStructure/.project new file mode 100644 index 0000000000..35bb23a0fc --- /dev/null +++ b/group11/1059156023/dataStructure/.project @@ -0,0 +1,17 @@ + + + dataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group11/1059156023/dataStructure/src/com/coding/basic/ArrayList.java b/group11/1059156023/dataStructure/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..c853dcaea3 --- /dev/null +++ b/group11/1059156023/dataStructure/src/com/coding/basic/ArrayList.java @@ -0,0 +1,71 @@ +package com.coding.basic; + +import java.util.List; + +public class ArrayList implements List{ + private int size; + + //璁剧疆涓涓粯璁ゅ閲忥紝褰撹皟鐢ㄩ粯璁ゆ瀯閫犲嚱鏁板疄渚嬪寲鏁扮粍鍚庯紝闇瑕佹墿瀹规椂鍙敤 + private static final int DEFAULT_CAPACITY=10; + + //Integer.MAX_VALUE:2147483647锛孧AX_ARRAY_SIZE锛2147483639 + private static final int MAX_ARRAY_SIZE=Integer.MAX_VALUE-8; + + private Object[] elementData; + + //瀹氫箟涓涓粯璁や负绌虹殑鏁扮粍锛屼緵榛樿鏋勯犲嚱鏁颁娇鐢 + private static final Object[] EMPTY_ELEMENTDATA={}; + + //瀹氫箟榛樿鏋勯犲嚱鏁,瀹炰緥鍖栦负绌烘暟缁 + public ArrayList(){ + this.elementData=EMPTY_ELEMENTDATA; + } + + //瀹氫箟涓涓湁鍙傜殑鏋勯犲嚱鏁 + public ArrayList(int initialCapacity){ + if(initialCapacity<0) + throw new IllegalArgumentException("Illegal Capacity:"+initialCapacity); + this.elementData = new Object[initialCapacity]; + } + + //瀹氫箟add(Object o)鏂规硶,榛樿鍦ㄦ暟缁勬湯灏炬坊鍔 + public boolean add(Object o){ + //瑕佹坊鍔犱竴涓暟锛屾墍浠ョ敤ensureCapacityInternal()鍒ゆ柇size+1涓殑鏁帮紝鏁扮粍鏄惁鏀惧緱涓 + ensureCapacityInternal(size+1); + elementData[size++]=o; + return true; + } + + private void ensureCapacityInternal(int minCapacity) { + if(elementData == EMPTY_ELEMENTDATA) + minCapacity = DEFAULT_CAPACITY; + + //濡傛灉闇瑕佹墿瀹癸紝鍒欒皟鐢╣row() + if(minCapacity-elementData.length>0) + grow(minCapacity); + } + + private void grow(int minCapacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity+(oldCapacity>>1); + + //鍘熷闀垮害鏄0鏃讹紝鍗冲師鏉ユ槸绌烘暟缁勬椂 + if(newCapacity-minCapacity<0) + newCapacity = minCapacity; + + //濡傛灉鏂扮殑瀹归噺瓒呰繃浜嗘暟缁勬渶澶у閲忥紝灏辫皟鐢╤ugeCapacity()鎶婅兘缁欑殑鏈澶у閲忕粰瀹 + if(newCapacity-MAX_ARRAY_SIZE>0) + newCapacity = hugeCapacity(minCapacity); + + + } + + private static int hugeCapacity(int minCapacity) { + if (minCapacity<0) { + throw new OutOfMemoryError(); //鎶涘嚭鍐呭瓨婧㈠嚭寮傚父 + } + //濡傛灉minCapacity姣擬AX_ARRAY_SIZE澶э紝鍒欒繑鍥瀒nt绫诲瀷鎵鑳借〃绀虹殑鏈澶у硷紝鍚﹀垯杩斿洖MAX_ARRAY_SIZE + return (minCapacity>MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; + } + +} diff --git a/group11/1059156023/struts/.classpath b/group11/1059156023/struts/.classpath new file mode 100644 index 0000000000..400cc1471c --- /dev/null +++ b/group11/1059156023/struts/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group11/1059156023/struts/.gitignore b/group11/1059156023/struts/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group11/1059156023/struts/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group11/1059156023/struts/.project b/group11/1059156023/struts/.project new file mode 100644 index 0000000000..2c00c2049a --- /dev/null +++ b/group11/1059156023/struts/.project @@ -0,0 +1,17 @@ + + + struts + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group11/1059156023/struts/src/com/coding/basic/LoginAction.java b/group11/1059156023/struts/src/com/coding/basic/LoginAction.java new file mode 100644 index 0000000000..23683995f2 --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/LoginAction.java @@ -0,0 +1,39 @@ +package com.coding.basic; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @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/group11/1059156023/struts/src/com/coding/basic/Struts.java b/group11/1059156023/struts/src/com/coding/basic/Struts.java new file mode 100644 index 0000000000..a0d26a207b --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/Struts.java @@ -0,0 +1,59 @@ +package com.coding.basic; + +import java.io.File; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.experimental.theories.Theories; + +import com.sun.corba.se.impl.orbutil.graph.Node; +import com.sun.org.apache.bcel.internal.classfile.Attribute; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) throws DocumentException { + //鍒涘缓SAXReader瀵硅薄 + SAXReader reader = new SAXReader(); + //璇诲彇鏂囦欢 杞崲鎴怐ocument + Document document = reader.read(new File("src/com/coding/basic/struts.xml")); + //鑾峰彇鏍硅妭鐐瑰厓绱犲璞 + Element root = document.getRootElement(); + //閬嶅巻鏍硅妭鐐 + listNodes(root); + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + return null; + } + + public static void listNodes(Element node) { + String name; + List list = node.attributes(); + + } + +} diff --git a/group11/1059156023/struts/src/com/coding/basic/StrutsTest.java b/group11/1059156023/struts/src/com/coding/basic/StrutsTest.java new file mode 100644 index 0000000000..5bac52805d --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/StrutsTest.java @@ -0,0 +1,51 @@ +package com.coding.basic; + +import java.util.HashMap; +import java.util.Map; + +import org.dom4j.DocumentException; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class StrutsTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @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/group11/1059156023/struts/src/com/coding/basic/View.java b/group11/1059156023/struts/src/com/coding/basic/View.java new file mode 100644 index 0000000000..9e479018c8 --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/View.java @@ -0,0 +1,23 @@ +package com.coding.basic; + +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/group11/1059156023/struts/src/com/coding/basic/struts.xml b/group11/1059156023/struts/src/com/coding/basic/struts.xml new file mode 100644 index 0000000000..b3b576c15f --- /dev/null +++ b/group11/1059156023/struts/src/com/coding/basic/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/group11/1178243325/DataStructure/build.gradle b/group11/1178243325/DataStructure/build.gradle index 5c3694d8ec..9c6bc859e6 100644 --- a/group11/1178243325/DataStructure/build.gradle +++ b/group11/1178243325/DataStructure/build.gradle @@ -1,10 +1,20 @@ + apply plugin: 'java' apply plugin: 'eclipse' jar { + from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }} manifest { - attributes 'Main-Class' : 'com.coding.Main' + attributes 'Main-Class' : 'com.Main' } } +repositories { + mavenCentral() +} + +dependencies { + compile 'junit:junit:4.12' + compile 'dom4j:dom4j:1.6.1' +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/Main.java b/group11/1178243325/DataStructure/src/main/java/com/Main.java new file mode 100644 index 0000000000..f5e5a36ebd --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/Main.java @@ -0,0 +1,56 @@ +package com; + +import java.util.*; +import com.coderising.litestruts.*; +import com.coderising.array.*; +public class Main { + public static void main(String[] args) { + int[] array = {1, 2, 3, 4, 5}; + System.out.print("reverseArray娴嬭瘯:"); + ArrayUtil.reverseArray(array); + for (int i : array) + System.out.print(i + " "); + System.out.print("\nremoveZero娴嬭瘯:"); + + int[] oldArray = {1, 3, 4, 5, 0, 0, 8 , 0, 9}; + oldArray = ArrayUtil.removeZero(oldArray); + for (int i : oldArray) { + System.out.print(i + " "); + } + + System.out.print("\nmerge娴嬭瘯:"); + int[] a1 = {3, 5,8}; + int[] a2 = {4, 5, 6,7}; + int[] arrays = ArrayUtil.merge(a1, a2); + for (int i : arrays) + System.out.print(i + " "); + + System.out.print("\ngrow娴嬭瘯:"); + + int[] growArray = ArrayUtil.grow(a1, 5); + for (int i : growArray) + System.out.print(i + " "); + + System.out.print("\nfibonacci娴嬭瘯"); + int[] fArray = ArrayUtil.fibonacci(1); + System.out.print(fArray); + System.out.println(); + fArray = ArrayUtil.fibonacci(15); + for (int i : fArray) + System.out.print(i + " "); + System.out.print("\ngetPrimes娴嬭瘯:"); + int[] primesArray = ArrayUtil.getPrimes(23); + for (int i : primesArray) + System.out.print(i + " "); + System.out.print("\ngetPerfectNumbers娴嬭瘯:"); + int[] pArray = ArrayUtil.getPerfectNumbers(100); + for (int i : pArray) + System.out.print(i + " "); + System.out.print("\njoin娴嬭瘯:"); + int[] jArray = new int[]{2, 3, 8}; + System.out.print(ArrayUtil.join(jArray, "-")); + Map map = new HashMap<>(); + Struts.runAction("login", map); + + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/array/ArrayUtil.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..f94d5d01c4 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/array/ArrayUtil.java @@ -0,0 +1,229 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static void reverseArray(int[] origin){ + if (origin == null) { + return; + } + + int length = origin.length; + int[] temp = new int[length]; + for (int i = 0; i < length; i++) + temp[i] = origin[i]; + for (int i = length - 1, j = 0; i >= 0 && j < length; i--, j++) + origin[j] = temp[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 static int[] removeZero(int[] oldArray){ + if (oldArray == null) { + return null; + } + + int zeroCount = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) + zeroCount++; + } + int[] newArray = new int[oldArray.length-zeroCount]; + for (int i = 0, j = 0; i < oldArray.length && j < newArray.length; i++) { + if (oldArray[i] != 0) { + newArray[j] = oldArray[i]; + j++; + } + } + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){ + if (array1 == null && array2 == null) + return null; + int index1 = 0, index2 = 0; + int[] array3 = new int[array1.length + array2.length]; + int index = 0; + while (index1 != array1.length && index2 != array2.length) { + if (array1[index1] < array2[index2]) { + array3[index++] = array1[index1++]; + } else if (array1[index1] > array2[index2]) { + array3[index++] = array2[index2++]; + } else if (array1[index1] == array2[index2]){ + array3[index++] = array1[index1++]; + index2++; + } + } + + if (index1 == array1.length && index2 != array2.length) { + for (int i = index2; i < array2.length; i++) + array3[index++] = array2[i]; + } else if (index2 == array2.length && index1 != array1.length) { + for (int i = index1; i < array1.length; i++) { + array3[index++] = array1[i]; + } + } + + int[] newArray = new int[index]; + for (int i = 0; i < newArray.length; i++) + newArray[i] = array3[i]; + return newArray; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + if (size <= 0) + return null; + int[] newArray = new int[oldArray.length + size]; + for (int i = 0; i < oldArray.length; i++) { + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if (max < 1) + return null; + if (max == 1) + return null; + int[] array = new int[max]; + int i = 0; + int value = fibonaccis(i+1); + while ( value < max) { + array[i++] = value; + value = fibonaccis(i+1); + } + int[] newArray = new int[i]; + for (int j = 0; j < newArray.length; j++) { + newArray[j] = array[j]; + } + return newArray; + } + + private static int fibonaccis(int n) { + if (n <=0) + return 0; + if (n == 1 || n ==2 ) + return 1; + return fibonaccis(n-1)+fibonaccis(n-2); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + if (max <= 1) { + return null; + } + int[] array = new int[max]; + int index = 0; + for (int i = 2; i < max; i++) { + if (i == 2 || i == 3 || i == 5 || i == 7) + array[index++] = i; + if (i%2 !=0 && i%3 != 0 && i%5 != 0 && i%7 != 0) + array[index++] = i; + } + int[] newArray = new int[index]; + for (int i = 0; i < newArray.length; i++) { + newArray[i] = array[i]; + } + + return newArray; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + if (max <= 0) + return null; + int[] array = new int[max]; + int index = 0; + for (int i = 1; i < max; i++) { + if (isPerfectNumber(i)) + array[index++] = i; + } + + int[] newArray = new int[index]; + for (int i = 0; i < newArray.length; i++) + newArray[i] = array[i]; + + return newArray; + } + + private static boolean isPerfectNumber(int n) { + int sum = 0; + int i = 1; + while (i < n) { + if (n%i == 0) + sum += i; + i++; + } + if (sum == n) + return true; + return false; + } + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + if (array == null) + return null; + StringBuilder str = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + if (i == array.length-1) + str.append(array[i]); + else + str.append(array[i] + seperator); + } + return str.toString(); + } + + +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp new file mode 100644 index 0000000000..1f45a5f25e Binary files /dev/null and b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/.Struts.java.swp differ diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/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; + } +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..b3bd421435 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,62 @@ +package com.coderising.litestruts; + +import java.util.Map; +import java.util.HashMap; +import java.lang.reflect.Method; +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml*/ + /* + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + try { + String targetClassName = XmlUtil.parseXML("struts.xml", actionName); + Class targetClass = Class.forName(targetClassName); + + Method setName = targetClass.getMethod("setName", String.class); + Method setPassword = targetClass.getMethod("setPassword", String.class); + Object object = targetClass.newInstance(); + + setName.invoke(object, parameters.get("name")); + setPassword.invoke(object, parameters.get("password")); + + Method execute = targetClass.getMethod("execute"); + String result = (String)execute.invoke(object); + + Method getMessage = targetClass.getMethod("getMessage"); + String message = (String)getMessage.invoke(object); + + Map params = new HashMap(); + params.put("message", message); + String jspUrl = XmlUtil.getJspUrl("struts.xml", actionName, result); + View view = new View(); + view.setJsp(jspUrl); + view.setParameters(params); + return view; + + } catch (Exception e) { + e.printStackTrace(); + } + + + return null; + } + +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/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/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java new file mode 100644 index 0000000000..d200452cc8 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/XmlUtil.java @@ -0,0 +1,59 @@ +package com.coderising.litestruts; + +import java.io.*; +import java.util.*; +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.dom4j.io.XMLWriter; +public class XmlUtil { + + public static String parseXML(String filePath, String actionName) { + try { + File file = new File(filePath); + SAXReader reader = new SAXReader(); + Document doc = reader.read(file); + Element root = doc.getRootElement(); + for (Iterator iter = root.elementIterator("action"); iter.hasNext();) { + Element element = (Element)iter.next(); + Attribute nameAttr = element.attribute("name"); + if (nameAttr.getValue().equals(actionName)) { + Attribute classAttr = element.attribute("class"); + return classAttr.getValue(); + } + } + } catch (Exception e) { + e.printStackTrace(); + System.out.println("parse error"); + } + return null; + } + + public static String getJspUrl(String filePath, String actionName, String resultName) { + try { + File file = new File(filePath); + SAXReader reader = new SAXReader(); + Document doc = reader.read(file); + Element root = doc.getRootElement(); + for (Iterator iter = root.elementIterator("action"); iter.hasNext();) { + Element element = (Element)iter.next(); + Attribute nameAttr = element.attribute("name"); + if (nameAttr.getValue().equals(actionName)) { + for (Iterator ite = element.elementIterator("result"); ite.hasNext();) { + Element ele = (Element)ite.next(); + Attribute resultAttr = ele.attribute("name"); + if (resultAttr.getValue().equals(resultName)) { + return ele.getText(); + } + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + +} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..ae0ce37fd8 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/java/com/coderising/litestruts/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/Main.java b/group11/1178243325/DataStructure/src/main/java/com/coding/Main.java deleted file mode 100644 index 31d2f2f277..0000000000 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/Main.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.coding; - -import com.coding.basic.*; -public class Main { - public static void main(String[] args) { - ArrayList list = new ArrayList(); - - list.add(0, "2xxx"); - list.add(1, "we"); - list.add(2, "sss"); - list.add("xing"); - list.remove(2); - System.out.println(list.get(2)); - Iterator iterator = list.iterator(); - while(iterator.hasNext()) { - System.out.println(iterator.next()); - } - System.out.println(list.size()); - - LinkedList llist = new LinkedList(); - llist.add("hu"); - llist.add("zhao"); - llist.add(2,"xing"); - llist.addFirst("韬獞鐧介┈"); - llist.addLast("寰峰窞灏忚佽檸"); - llist.add(5, "sf"); - llist.remove(5); - llist.removeFirst(); - llist.removeLast(); - for (int i = 2; i >=0; i--) - System.out.print(llist.get(i)); - System.out.println(llist.size()); - - Iterator literator = llist.iterator(); - while(literator.hasNext()) { - System.out.println(literator.next()); - } - - Stack stack = new Stack(); - stack.push(1); - stack.push(2); - stack.push(3); - stack.push(4); - System.out.println(stack.peek()); - while(!stack.isEmpty()) - System.out.println(stack.pop()); - - Queue queue = new Queue(); - queue.enQueue(1); - queue.enQueue(2); - queue.enQueue(3); - System.out.println(queue.size()); - while (!queue.isEmpty()) { - System.out.println(queue.deQueue()); - } - - } -} diff --git a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java index 266eff3d56..1cf38aee30 100644 --- a/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java +++ b/group11/1178243325/DataStructure/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -9,24 +9,25 @@ public class BinaryTreeNode { 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/group11/1178243325/DataStructure/src/main/resources/struts.xml b/group11/1178243325/DataStructure/src/main/resources/struts.xml new file mode 100644 index 0000000000..ae0ce37fd8 --- /dev/null +++ b/group11/1178243325/DataStructure/src/main/resources/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group11/1178243325/DataStructure/struts.xml b/group11/1178243325/DataStructure/struts.xml new file mode 100644 index 0000000000..0582b7d4ea --- /dev/null +++ b/group11/1178243325/DataStructure/struts.xml @@ -0,0 +1,11 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + diff --git a/group11/171535320/ArrayUtil.java b/group11/171535320/ArrayUtil.java new file mode 100644 index 0000000000..6b6cb818d0 --- /dev/null +++ b/group11/171535320/ArrayUtil.java @@ -0,0 +1,211 @@ +import sun.security.util.Length; + +import java.util.ArrayList; +import java.util.InputMismatchException; +import java.util.List; + +/** + * Created by dengdechao on 2017/2/27. + */ +public class ArrayUtil { + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin + * @return + */ + + public static void reverseArray(int[] origin){ + if(origin == null) { + return ; + } + int i = 0; + int j = origin.length - 1; + while(i != j) { + int temp = origin[i]; + origin[i] = origin[j]; + origin[j] = temp; + i++; + j--; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 static int[] removeZero(int[] oldArray){ + if(oldArray == null) { + return null; + } + int i = 0; + int j = oldArray.length - 1; + + while(i != j) { + if(oldArray[i] != 0) { + i++; + continue; + } + if(oldArray[j] == 0) { + j--; + continue; + } + int temp = oldArray[i]; + oldArray[i] = oldArray[j]; + oldArray[j] = temp; + } + int[] array = new int[i]; + for(int n = 0; n < i; ++n) { + array[n] = oldArray[n]; + } + return array; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){ + int[] result = new int[array1.length + array2.length]; + int len1 = 0; + int len2 = 0; + int i = 0; + while(len1 != array1.length || len2 != array2.length) { + if(len1 < array1.length && len2 < array2.length) { + if(array1[len1] < array2[len2]) { + result[i++] = array1[len1++]; + } else if(array1[len1] > array2[len2]) { + result[i++] = array2[len2++]; + } else { + result[i++] = array1[len1]; + len1++; + len2++; + } + } else if(len1 < array1.length){ + result[i++] = array1[len1++]; + } else { + result[i++] = array2[len2++]; + } + + } + int[] last = new int[i]; + System.arraycopy(result,0,last,0,i); + return last; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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 len = oldArray.length + size; + int[] result = new int[len]; + for(int i = 0; i < oldArray.length; ++i) { + result[i] = oldArray[i]; + } + return result; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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){ + return null; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + List list = new ArrayList(); + + for(int i = 2; i <= max; ++i) { + int temp = 2; + while(temp < i) { + if(i % temp == 0) { + break; + } + ++temp; + } + if(i == temp) { + list.add(i); + } + } + + int[] result = new int[list.size()]; + + for(int i = 0; i < list.size(); ++i) { + result[i] = list.get(i); + } + return result; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + List list = new ArrayList(); + + for(int j = 0; j < max; ++j) { + int temp = 0; + for(int i = 0; i < j / 2 + 1; ++i) { + if(j % i == 0) { + temp += i; + } + } + if(temp == j) { + list.add(j); + } + } + + int[] result = new int[list.size()]; + + for(int i = 0; i < list.size(); ++i) { + result[i] = list.get(i); + } + return result; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder str = new StringBuilder(); + for(int i = 0; i < array.length; ++i) { + str.append(array[i]); + if(i + 1 < array.length) { + str.append(seperator); + } + } + String s = str.toString(); + return s; + } +} diff --git a/group11/171535320/ArrayList.java b/group11/171535320/DataStruct/ArrayList.java similarity index 98% rename from group11/171535320/ArrayList.java rename to group11/171535320/DataStruct/ArrayList.java index e277814cc9..4340deecf4 100644 --- a/group11/171535320/ArrayList.java +++ b/group11/171535320/DataStruct/ArrayList.java @@ -1,3 +1,5 @@ +package DataStruct; + import java.util.Arrays; import java.util.Objects; diff --git a/group11/171535320/BinaryTreeNode.java b/group11/171535320/DataStruct/BinaryTreeNode.java similarity index 97% rename from group11/171535320/BinaryTreeNode.java rename to group11/171535320/DataStruct/BinaryTreeNode.java index 3c8d543355..e0cc417a43 100644 --- a/group11/171535320/BinaryTreeNode.java +++ b/group11/171535320/DataStruct/BinaryTreeNode.java @@ -1,3 +1,5 @@ +package DataStruct; + public class BinaryTreeNode { private Node root = null; diff --git a/group11/171535320/DataStruct/Iterator.java b/group11/171535320/DataStruct/Iterator.java new file mode 100644 index 0000000000..012d28806a --- /dev/null +++ b/group11/171535320/DataStruct/Iterator.java @@ -0,0 +1,12 @@ +package DataStruct; + +/** + * Created by dengdechao on 2017/2/27. + */ + + public interface Iterator { + public boolean hasNext(); + public Object next(); + + } + diff --git a/group11/171535320/LinkedList.java b/group11/171535320/DataStruct/LinkedList.java similarity index 98% rename from group11/171535320/LinkedList.java rename to group11/171535320/DataStruct/LinkedList.java index 43bb74e516..24a99466ca 100644 --- a/group11/171535320/LinkedList.java +++ b/group11/171535320/DataStruct/LinkedList.java @@ -1,4 +1,6 @@ -import java.util.Objects; +package DataStruct; + +import DataStruct.List; public class LinkedList implements List { diff --git a/group11/171535320/DataStruct/List.java b/group11/171535320/DataStruct/List.java new file mode 100644 index 0000000000..125407436c --- /dev/null +++ b/group11/171535320/DataStruct/List.java @@ -0,0 +1,9 @@ +package DataStruct; + +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/group11/252308879/dataStructure/pom.xml b/group11/252308879/dataStructure/pom.xml index 4b756e4f0d..0bf21adba4 100644 --- a/group11/252308879/dataStructure/pom.xml +++ b/group11/252308879/dataStructure/pom.xml @@ -21,5 +21,15 @@ 4.12 test + + junit + junit + RELEASE + + + dom4j + dom4j + 1.6.1 + diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/array/ArrayUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/array/ArrayUtil.java new file mode 100644 index 0000000000..82d7073a0f --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/array/ArrayUtil.java @@ -0,0 +1,282 @@ +package org.pan.coding2017.array; + +import java.util.Arrays; + +/** + * Created by QiPan on 2017/2/27. + */ +public class ArrayUtil { + + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + * 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public static void reverseArray(int[] origin) { + + // 濡傛灉鏄痭ull, 鎴栬呴暱搴﹀皬浜庣瓑浜1锛 鐩存帴杩斿洖 + if (origin == null || origin.length <= 1) { + return; + } + for (int i = 0; i < origin.length / 2; i++) { + int tmp = origin[i]; + origin[i] = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = tmp; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 static int[] removeZero(int[] oldArray) { + + if (oldArray == null) { + return oldArray; + } + int[] newArray = null; + int count = 0; //缁熻琚Щ鍑虹殑鏁扮粍鐨勪釜鏁 + for (int i = 0; i < oldArray.length; i++) { + int num = oldArray[i]; + if (num == 0) { + count++; + System.arraycopy(oldArray, i + 1, oldArray, i, oldArray.length - i - 1); + i--; + } + } + if (count == 0) { + newArray = oldArray; + } else { + newArray = new int[oldArray.length - count]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length - count); + } + return newArray; + } + + /** + * 涓嶇敤JavaAPI鏉ュ仛 + * + * @param oldArray + * @return + */ + public static int[] removeZero_2(int[] oldArray) { + + if (oldArray == null) { + return oldArray; + } + int count = 0; + for (int num : oldArray) { + if (num == 0) { + count++; + } + } + int[] newArray = new int[oldArray.length - count]; + for (int i = 0, j = 0; i < oldArray.length; i++, j++) { + int num = oldArray[i]; + if (num == 0) { + j--; + } else { + newArray[j] = num; + } + } + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * + * @param array1 + * @param array2 + * @return + */ + public static int[] merge(int[] array1, int[] array2) { + //鍏堝垵濮嬪寲涓涓猘rray3锛屼絾涓嶆槸鏈鍚庤繑鍥炵殑鏁扮粍 + int[] array3 = new int[array1.length + array2.length]; + int i = 0, j = 0; + int array3Size = 0; // 缁熻瀹為檯涓婂悎骞舵暟缁勫悗鐨勫ぇ灏 + while (i < array1.length && j < array2.length) { + if (array1[i] < array2[j]) {// 濡傛灉array1涓厓绱犲皬锛屽垯鎻掑叆鍒癮rray3涓 + array3[array3Size++] = array1[i]; + ++i; + } else if (array1[i] > array2[j]) {//濡傛灉array2涓厓绱犲皬锛屽垯鎻掑叆鍒癮rray3涓 + array3[array3Size++] = array2[j]; + ++j; + } else {//鍚﹀垯闅忎究鎻掑叆涓涓,浣嗘槸璁℃暟瑕佸悓鏃跺姞1 + array3[array3Size++] = array1[i]; + ++i; + ++j; + } + } + + if (i == array1.length) { //濡傛灉array1涓叏閮ㄥ惊鐜畬姣曚簡锛岄偅涔堥渶瑕佸幓澶勭悊array2涓墿浣欑殑鍏冪礌 + for (int n = j; n < array2.length; n++) { + array3[array3Size++] = array2[n]; + } + } else if (j == array2.length) {// 濡傛灉array2涓叏閮ㄥ惊鐜畬姣,閭d箞闇瑕佸幓澶勭悊array1涓墿浣欑殑鍏冪礌 + for (int n = i; n < array1.length; n++) { + array3[array3Size++] = array1[n]; + } + } + int[] returnResultArray = new int[array3Size]; + System.arraycopy(array3, 0, returnResultArray, 0, + array3Size); + return returnResultArray; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + if (oldArray == null) { + oldArray = new int[size]; + } + oldArray = Arrays.copyOf(oldArray, oldArray.length + size); + return oldArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max <= 1) { + return new int[0]; + } + int[] arrays = new int[max / 2]; + int firstNum = 1; //绗竴涓暟瀛 + int secondNum = 1; // 绗簩涓暟瀛 + int arraySize = 0; + arrays[arraySize++] = 1; // 鍒濆鍖栫涓浣 + while (secondNum < max) { + arrays[arraySize++] = secondNum; + int tmpNum = secondNum; // 淇濆瓨绗簩涓暟锛屽緱浼氶渶瑕佷粯缁欑涓涓暟 + secondNum = firstNum + secondNum; // 涓哄墠涓や釜鏁颁箣鍜 + firstNum = tmpNum; // 绗竴涓暟锛屽悗绉 + } + return Arrays.copyOf(arrays, arraySize); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + int[] returnResultArray = new int[max + 1]; + int arraySize = 0; + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + returnResultArray[arraySize++] = i; + } + + } + if (arraySize == returnResultArray.length) { + return returnResultArray; + } + return Arrays.copyOf(returnResultArray, arraySize); + } + + private static boolean isPrime(final int number) { + if (number < 2) { + return false; + } + // 鍥犱负涓嶅彲鑳藉皢涓涓暟闄や笌鎵鏈夊皬浜庡畠鐨勬暟瀛楋紝鍙妫鏌ュ埌N鐨勫钩鏂规牴灏卞ソ浜嗐 + // 浣嗙洿鎺ュ紑鏍瑰彿杩樻湁涓簿搴︾殑闂銆傝繖涓彲鑳戒細浜х敓璇樊銆 绱㈡у皢鍒ゆ柇鏉′欢鍐欐垚 i*i<=number + for (int i = 2; i * i <= number; i++) { + if (number % i == 0) {//鏌ョ湅鎵鏈夊皬浜巒umber骞虫柟鏍圭殑鏁帮紝鑳藉琚暣闄 + return false; + } + } + // 濡傛灉涓涓兘娌℃湁锛岄偅涔堝氨鏄礌鏁 + return true; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + int[] array = new int[max]; + int arraySize = 0; + for (int n = 0; n < max; n++) { + int fac,// 琚櫎鐨勫洜瀛 + sum,// 鐢ㄦ潵缁熻鍥犲瓙涔嬪拰 + num;// 闄ゆ暟鐨勫洜瀛愶紝涓棿鍙橀噺 + for (sum = 1, num = n, fac = 2; fac < num; fac++) { + + if (n % fac == 0) {// 濡傛灉浣欐暟涓0锛岄偅涔堣鏄庢湁鍥犲瓙 + sum += fac; // 缁熻鍥犲瓙鍜 + num = n / fac; // num=绛変簬闄ゆ暟鐨勬渶澶у洜瀛 + if (num == fac) // 濡傛灉鏈澶у拰鏈灏忕浉绛夎烦鍑哄惊鐜 + break; + sum += num; // 鍐嶇粺璁″洜瀛 + } + } + + if (sum == n) { //鍥犲瓙鍜屼笌鏁存暟鐩哥瓑锛岄偅涔堝氨鏄竴涓畬缇庢暟 + if (n != 1) { + System.out.println(n + "鏄竴涓畬鍏ㄦ暟锛屽叾鍥犲瓙涓:"); + } + for (fac = 1; fac < n; fac++) { + if (n % fac == 0) {// 鍒楀嚭鎵鏈夌殑鍥犲瓙 + System.out.print(fac + " "); + } + } + System.out.println(); + array[arraySize++] = n; // 鏀惧埌鏁扮粍涓 + } + } + return Arrays.copyOf(array, arraySize); + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public static String join(int[] array, String seperator) { + if (array == null) { + return null; + } + StringBuilder str = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + if (i == array.length - 1) { + str.append(array[i]); + continue; + } + str.append(array[i]).append(seperator); + } + return str.toString(); + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/ArrayList.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/ArrayList.java similarity index 99% rename from group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/ArrayList.java rename to group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/ArrayList.java index 3d2a685f35..73dd4b7a5f 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/ArrayList.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/ArrayList.java @@ -1,4 +1,4 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; import java.util.Arrays; import java.util.NoSuchElementException; diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/BinaryTreeNode.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/BinaryTreeNode.java similarity index 98% rename from group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/BinaryTreeNode.java rename to group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/BinaryTreeNode.java index 3725e5c71b..80f7f78751 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/BinaryTreeNode.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/BinaryTreeNode.java @@ -1,4 +1,4 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; /** * Created by QiPan on 2017/2/23. diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Iterator.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Iterator.java similarity index 80% rename from group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Iterator.java rename to group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Iterator.java index 94dc84dfdc..3d1849f1a0 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Iterator.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Iterator.java @@ -1,4 +1,4 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; /** * Created by QiPan on 2017/2/23. diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/LinkedList.java similarity index 98% rename from group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java rename to group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/LinkedList.java index e83de27c11..015ac3d59d 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/LinkedList.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/LinkedList.java @@ -1,4 +1,4 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; /** * Created by QiPan on 2017/2/23. diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/List.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/List.java similarity index 90% rename from group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/List.java rename to group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/List.java index 15c9d9d3be..daa8253313 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/List.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/List.java @@ -1,4 +1,4 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; /** diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Queue.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Queue.java similarity index 96% rename from group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Queue.java rename to group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Queue.java index d51695b148..af478b4288 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Queue.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Queue.java @@ -1,4 +1,4 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; /** * Created by QiPan on 2017/2/23. diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack.java similarity index 97% rename from group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack.java rename to group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack.java index 3233954cf8..918db8f70d 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack.java @@ -1,4 +1,4 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; /** * Created by QiPan on 2017/2/23. diff --git a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack2.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack2.java similarity index 95% rename from group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack2.java rename to group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack2.java index 2769c72485..2a056b8bbf 100644 --- a/group11/252308879/dataStructure/src/main/java/org/apn/coding2017/basic/Stack2.java +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/basic/Stack2.java @@ -1,4 +1,4 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; /** * Created by Pan on 2017/2/25. diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/LoginAction.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/LoginAction.java new file mode 100644 index 0000000000..f38cbcb084 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/LoginAction.java @@ -0,0 +1,39 @@ +package org.pan.coding2017.parsingXML; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @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/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/Struts.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/Struts.java new file mode 100644 index 0000000000..90c5443c23 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/Struts.java @@ -0,0 +1,110 @@ +package org.pan.coding2017.parsingXML; + +import org.pan.coding2017.utils.JaxpDomUtil; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + try { + Document document = JaxpDomUtil.getDocument(); + NodeList actionNodeList = document.getElementsByTagName("action"); + for (int i = 0; i < actionNodeList.getLength(); i++) { + NamedNodeMap attributes = actionNodeList.item(i).getAttributes(); + String methodName = attributes.getNamedItem("name").getTextContent(); + if (!actionName.equals(methodName)) { + continue; + } + // 鑾峰彇鍏ㄧ被鍚嶅璞★紝鍙嶅皠鍒涘缓瀵硅薄 + String className = attributes.getNamedItem("class").getTextContent(); + Class actionClass = Class.forName(className); + + // 鑾峰彇鍙嶅皠鐨勬柟娉曞悕绉, 鍥犱负鏄痯ublic淇グ鎵浠ョ敤鐨勬槸getMethod锛岃幏鍙栫鏈夋柟娉曢渶瑕佺敤 getDeclaredMethod + Method setName = actionClass.getMethod("setName", String.class); + Method setPassword = actionClass.getMethod("setPassword", String.class); + // 鍒涘缓瀵硅薄 + Object actionObject = actionClass.newInstance(); + + // 璋冪敤鍙嶅皠鐨剆etter鏂规硶,缁欏弬鏁拌祴鍊 + setName.invoke(actionObject, parameters.get("name")); + setPassword.invoke(actionObject, parameters.get("password")); + + // 鑾峰彇execute鏂规硶 + Method execute = actionClass.getMethod("execute"); + // 杩斿洖缁撴灉 + String result = (String) execute.invoke(actionObject); + + // 鑾峰彇getMessage鏂规硶 + Method getMessage = actionClass.getMethod("getMessage"); + String message = (String) getMessage.invoke(actionObject); + // 鍒涘缓涓涓狹ap 鐢ㄦ潵鏀剧疆鍦 View涓 + Map params = new HashMap(); + params.put("message", message); + + // 鑾峰彇杩斿洖鐨凧SP璺緞锛岃繖涓渶瑕佹瘮杈價esult鑺傜偣鐨刵ame灞炴 + //鑾峰彇action鐨勫瓙鑺傜偣 + NodeList resultNodes = actionNodeList.item(i).getChildNodes(); + String viewUrl = ""; + for (int n = 0; n < resultNodes.getLength(); n++) { + Node item = resultNodes.item(n); + NamedNodeMap resultAttributes = item.getAttributes(); + if (resultAttributes == null) { + continue; + } + String name = resultAttributes.getNamedItem("name").getTextContent(); + if (result.equals(name)) { + viewUrl = item.getTextContent(); + break; + } + } + View view = new View(); + view.setJsp(viewUrl); + view.setParameters(params); + return view; + } + + } 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/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/StrutsTest.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/StrutsTest.java new file mode 100644 index 0000000000..ccdacc514c --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/StrutsTest.java @@ -0,0 +1,43 @@ +package org.pan.coding2017.parsingXML; + +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/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/View.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/View.java new file mode 100644 index 0000000000..3271ab4ed1 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/parsingXML/View.java @@ -0,0 +1,23 @@ +package org.pan.coding2017.parsingXML; + +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/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java new file mode 100644 index 0000000000..f1679f6bef --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/Dom4JUtil.java @@ -0,0 +1,41 @@ +package org.pan.coding2017.utils; + +import java.io.FileWriter; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.io.OutputFormat; +import org.dom4j.io.SAXReader; +import org.dom4j.io.XMLWriter; + +public class Dom4JUtil { + + public static Document getDocument(String xmlPath) { + + try { + //鍒涘缓瑙f瀽鍣 + SAXReader saxReader = new SAXReader(); + //寰楀埌Documment + Document document = saxReader.read(xmlPath); + return document; + } catch (DocumentException e) { + e.printStackTrace(); + } + + return null; + } + + public static void xmlWrite(Document document,String xmlPath){ + + try { + OutputFormat format = OutputFormat.createPrettyPrint(); + XMLWriter xmlWriter = new XMLWriter(new FileWriter(xmlPath),format); + xmlWriter.write(document); + xmlWriter.close(); + } catch (Exception e) { + e.printStackTrace(); + } + + + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpDomUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpDomUtil.java new file mode 100644 index 0000000000..d8fe537e35 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpDomUtil.java @@ -0,0 +1,152 @@ +package org.pan.coding2017.utils; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class JaxpDomUtil { + public static final String XMLPATH = JaxpDomUtil.class.getClassLoader().getResource("struts.xml").getPath(); + + /** + * 閫氳繃 瑙f瀽鍣 鑾峰彇鍒 Document + * @return + */ + public static Document getDocument() { + try { + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory + .newInstance(); + DocumentBuilder documentBuilder = documentBuilderFactory + .newDocumentBuilder(); + Document document = documentBuilder.parse(XMLPATH); + return document; + } catch (Exception e) { + e.printStackTrace(); + + } + return null; + } + + /** + * 鍥炲啓 XML 鏂规硶 + * @param document + */ + public static void tranFormMethod(Document document) { + try { + TransformerFactory transformerFactory = TransformerFactory + .newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + transformer.transform(new DOMSource(document), new StreamResult( + XMLPATH)); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 閫掑綊璋冪敤 鑾峰彇鎵鏈夌殑鍏冪礌 骞舵墦鍗板厓绱犲悕绉 + * @param node + */ + private static void listElement(Node node) { + // 鍒ゆ柇鏄厓绱犵被鍨嬫椂鎵嶆墦鍗 + if (node.getNodeType() == Node.ELEMENT_NODE) { + System.out.println(node.getNodeName()); + } + + // 鑾峰緱涓灞傚瓙鑺傜偣 + NodeList nodelist = node.getChildNodes(); + for (int i = 0; i < nodelist.getLength(); i++) { + // 寰楀埌姣忎竴涓瓙鑺傜偣 + Node nodeChild = nodelist.item(i); + + // 閫掑綊璋冪敤 + listElement(nodeChild); + } + + } + + /** + * 鑾峰彇鎵鏈夌殑 鍏冪礌鍚嶇О + */ + public static void getListElement() { + + Document document = JaxpDomUtil.getDocument(); + listElement(document); + } + + /** + * 鍒犻櫎nan鑺傜偣 + */ + public static void delSex() { + + Document document = JaxpDomUtil.getDocument(); + + // 鑾峰彇鍏冪礌 + Node nodeSex = document.getElementsByTagName("sex").item(0); + + // 寰楀埌鐖惰妭鐐 + Node parent = nodeSex.getParentNode(); + + // 閫氳繃鐖惰妭鐐瑰垹闄 + parent.removeChild(nodeSex); + + // 鍥炲啓XML + JaxpDomUtil.tranFormMethod(document); + + } + + /** + * 淇敼 sex 鏍囩鐨 鍐呭涓簄v + */ + public static void modifySex() { + Document document = JaxpDomUtil.getDocument(); + Node nodeSex = document.getElementsByTagName("sex").item(0); + nodeSex.setTextContent("nv"); + JaxpDomUtil.tranFormMethod(document); + + } + + /** + * 涓虹涓涓猵1 澧炲姞 nv + */ + public static void addSex(){ + Document document = JaxpDomUtil.getDocument(); + Node p1Node = document.getElementsByTagName("p1").item(0); + + //閫氳繃 Document 鍒涘缓 Element + Element sexElement = document.createElement("sex"); + sexElement.setTextContent("nv"); + p1Node.appendChild(sexElement); + JaxpDomUtil.tranFormMethod(document); + + } + + /** + * 鏌ヨxml涓涓涓猲ame鍏冪礌鐨勫 + */ + public static void selectSin(){ + Document document = JaxpDomUtil.getDocument(); + Node nameNode = document.getElementsByTagName("name").item(0); + String name = nameNode.getTextContent(); + System.out.println(name); + } + + /** + * 鏌ヨ鎵鏈塶ame鍏冪礌鐨勫 + */ + public static void selectAll(){ + Document document = JaxpDomUtil.getDocument(); + NodeList nodeList = document.getElementsByTagName("name"); + for (int i = 0; i < nodeList.getLength(); i++) { + System.out.println(nodeList.item(i).getTextContent()); + } + } + +} diff --git a/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpSAXPUtil.java b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpSAXPUtil.java new file mode 100644 index 0000000000..8756c8ab95 --- /dev/null +++ b/group11/252308879/dataStructure/src/main/java/org/pan/coding2017/utils/JaxpSAXPUtil.java @@ -0,0 +1,89 @@ +package org.pan.coding2017.utils; + +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +public class JaxpSAXPUtil { + public static void main(String[] args) { + /* + * 1銆佸垱寤鸿В鏋愬櫒宸ュ巶 + * 2銆佸垱寤鸿В鏋愬櫒 + * 3銆佹墽琛 parse 鏂规硶 + * + * 4銆佽嚜宸卞垱寤轰竴涓被銆佺户鎵緿efaultHandler + * 5銆侀噸鍐欑被閲岄潰鐨勪笁涓柟娉 + */ + + try { + SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); + SAXParser saxParser = saxParserFactory.newSAXParser(); + saxParser.parse("src/p1.xml",new MyDeafultHandler2() ); + } catch (Exception e) { + e.printStackTrace(); + } + + + } + +} + +class MyDeafultHandler1 extends DefaultHandler{ + + @Override + public void startElement(String uri, String localName, String qName, + Attributes attributes) throws SAXException { + System.out.println("<"+qName+">"); + } + + @Override + public void characters(char[] ch, int start, int length) + throws SAXException { + System.out.println(new String(ch,start,length)); + } + + @Override + public void endElement(String uri, String localName, String qName) + throws SAXException { + System.out.println("<"+qName+"/>"); + } + +} + +//瀹炵幇鑾峰彇鎵鏈夌殑name鍏冪礌鐨勫 +class MyDeafultHandler2 extends DefaultHandler{ + + boolean flag = false; + int index = 1; + + @Override + public void startElement(String uri, String localName, String qName, + Attributes attributes) throws SAXException { + //鍒ゆ柇qName 鏄惁涓 name 鍏冪礌 + if("name".equals(qName) && index == 2){ + flag = true; + } + } + + @Override + public void characters(char[] ch, int start, int length) + throws SAXException { + if(flag == true){ + System.out.println(new String(ch, start, length)); + } + } + + @Override + public void endElement(String uri, String localName, String qName) + throws SAXException { + if("name".equals(qName)){ + flag = false; + index++ ; + } + } + + +} diff --git a/group11/252308879/dataStructure/src/main/resources/struts.xml b/group11/252308879/dataStructure/src/main/resources/struts.xml new file mode 100644 index 0000000000..35830922ba --- /dev/null +++ b/group11/252308879/dataStructure/src/main/resources/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/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/TestJavaUtilArrayList.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/TestJavaUtilArrayList.java similarity index 94% rename from group11/252308879/dataStructure/src/test/java/org/apn/coding2017/TestJavaUtilArrayList.java rename to group11/252308879/dataStructure/src/test/java/org/pan/coding2017/TestJavaUtilArrayList.java index 1b38998253..281a5bf07b 100644 --- a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/TestJavaUtilArrayList.java +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/TestJavaUtilArrayList.java @@ -1,4 +1,4 @@ -package org.apn.coding2017; +package org.pan.coding2017; import org.junit.Assert; import org.junit.Test; diff --git a/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/array/ArrayUtilTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/array/ArrayUtilTest.java new file mode 100644 index 0000000000..29ab6bf6dc --- /dev/null +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/array/ArrayUtilTest.java @@ -0,0 +1,89 @@ +package org.pan.coding2017.array; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by QiPan on 2017/2/27. + */ +public class ArrayUtilTest { + + @Test + public void removeZero() throws Exception { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + System.out.println("removeZero 绉婚櫎0涔嬪墠锛 "+ Arrays.toString(oldArr)); + int[] newArrays = ArrayUtil.removeZero(oldArr); + System.out.println("removeZero 绉婚櫎0涔嬪悗锛 "+ Arrays.toString(newArrays)); + } + + @Test + public void removeZero_2() throws Exception { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + System.out.println("removeZero_2 绉婚櫎0涔嬪墠锛 "+ Arrays.toString(oldArr)); + int[] newArrays = ArrayUtil.removeZero_2(oldArr); + System.out.println("removeZero_2 绉婚櫎0涔嬪悗锛 "+ Arrays.toString(newArrays)); + } + + @Test + public void reverseArray() throws Exception { + int[] array = new int[]{7, 9 , 30, 3}; + int[] array2 = new int[] {7, 9, 30, 3, 4}; + System.out.println("缃崲鍓: " + Arrays.toString(array)); + ArrayUtil.reverseArray(array); + System.out.println("缃崲鍚: "+ Arrays.toString(array)); + System.out.println("缃崲鍓: " + Arrays.toString(array2)); + ArrayUtil.reverseArray(array2); + System.out.println("缃崲鍚: "+ Arrays.toString(array2)); + } + + @Test + public void merge() throws Exception { + int[] a1 = {3, 5, 7,8}, a2 = {4, 5, 6,7}; + //鍒 a3 涓篬3,4,5,6,7,8] + int[] merge = ArrayUtil.merge(a1, a2); + System.out.println(Arrays.toString(merge)); + } + + @Test + public void grow() throws Exception { + int[] oldArray = {2,3,6} ; + int size = 3; + System.out.println("grow 涔嬪墠锛"+ Arrays.toString(oldArray)); + int[] newArrays = ArrayUtil.grow(oldArray, size); + System.out.println("grow 涔嬪悗锛"+ Arrays.toString(newArrays)); + + } + + @Test + public void fibonacci() throws Exception { + //max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + int[] fibonacci = ArrayUtil.fibonacci(988); + System.out.println(Arrays.toString(fibonacci)); + } + + @Test + public void getPrimes() throws Exception { + //渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + int[] primes = ArrayUtil.getPrimes(23); + System.out.println(Arrays.toString(primes)); + } + + @Test + public void getPerfectNumbers() throws Exception { + int[] primes = ArrayUtil.getPerfectNumbers(10000); + System.out.println(Arrays.toString(primes)); + } + + @Test + public void join() throws Exception { + int [] array= {3,8,9}; + String seperator = "-"; + String result = ArrayUtil.join(array, seperator); + System.out.println(result); + } + + + + +} \ No newline at end of file diff --git a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/ArrayListTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/ArrayListTest.java similarity index 95% rename from group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/ArrayListTest.java rename to group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/ArrayListTest.java index a52647b7df..f93876522a 100644 --- a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/ArrayListTest.java +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/ArrayListTest.java @@ -1,10 +1,8 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; - /** * Created by Pan on 2017/2/26. */ diff --git a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/BinaryTreeNodeTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/BinaryTreeNodeTest.java similarity index 91% rename from group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/BinaryTreeNodeTest.java rename to group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/BinaryTreeNodeTest.java index 8b15597ed2..4c3c01cd73 100644 --- a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/BinaryTreeNodeTest.java +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/BinaryTreeNodeTest.java @@ -1,10 +1,8 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; - /** * Created by Pan on 2017/2/26. */ diff --git a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/LinkedListTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/LinkedListTest.java similarity index 96% rename from group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/LinkedListTest.java rename to group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/LinkedListTest.java index f932e49cf0..d10e2a1d48 100644 --- a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/LinkedListTest.java +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/LinkedListTest.java @@ -1,10 +1,8 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; - /** * Created by Pan on 2017/2/26. */ diff --git a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/QueueTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/QueueTest.java similarity index 91% rename from group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/QueueTest.java rename to group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/QueueTest.java index 0d52d8585f..c720e7d95e 100644 --- a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/QueueTest.java +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/QueueTest.java @@ -1,10 +1,8 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; - /** * Created by Pan on 2017/2/26. */ diff --git a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/StackTest.java b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/StackTest.java similarity index 91% rename from group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/StackTest.java rename to group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/StackTest.java index f1798f8329..df85b797d4 100644 --- a/group11/252308879/dataStructure/src/test/java/org/apn/coding2017/basic/StackTest.java +++ b/group11/252308879/dataStructure/src/test/java/org/pan/coding2017/basic/StackTest.java @@ -1,10 +1,8 @@ -package org.apn.coding2017.basic; +package org.pan.coding2017.basic; import org.junit.Before; import org.junit.Test; -import static org.junit.Assert.*; - /** * Created by Pan on 2017/2/26. */ diff --git a/group11/283091182/src/com/coderising/array/ArrayUtil.java b/group11/283091182/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..93c43d9500 --- /dev/null +++ b/group11/283091182/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,303 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + if(origin==null||origin.length==0){ + throw new RuntimeException("invalid array argument!"); + } + if(origin.length>1){ + int temp; + for(int i=0;iarray2[pos2]){ + array3[pos3]=array2[pos2]; + pos2++; + pos3++; + }else if(array1[pos1]3){ + result[pos]=2; + pos++; + } + if(max>4){ + result[pos]=3; + pos++; + } + for(int i=4;i0){ + sb.append(seperator); + } + sb.append(i); + } + return sb.toString(); + } + + private void printArray(String msg,int[] array){ + System.out.print(msg); + for(int i=0;i resultMap= new HashMap(); + public static final String CONST_ACTION = "action"; + public static final String CONST_NAME = "name"; + public static final String CONST_CLASS = "class"; + public static final String CONST_RESULT = "result"; + + private String actionName; + private String actionClass; + + public Action(){}; + + public Action(String actionName,String actionClass){ + this.actionName = actionName; + this.actionClass = actionClass; + } + + public void setActionResultJsp(String result,String dispatcherJsp){ + this.resultMap.put(result, dispatcherJsp); + } + + public String getActionResultJsp(String result){ + return this.resultMap.get(result); + } + + + public String getActionName() { + return actionName; + } + + public void setActionName(String actionName) { + this.actionName = actionName; + } + + public String getActionClass() { + return actionClass; + } + + public void setActionClass(String actionClass) { + this.actionClass = actionClass; + } + + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + + @Override + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append(CONST_ACTION).append(":").append(this.actionName).append(","); + sb.append(CONST_CLASS).append(":").append(this.actionClass).append(","); + sb.append("ResultMap").append(":").append(this.resultMap.toString()); + return sb.toString(); + } +} diff --git a/group11/283091182/src/com/coderising/litestruts/LoginAction.java b/group11/283091182/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group11/283091182/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; + } +} diff --git a/group11/283091182/src/com/coderising/litestruts/Struts.java b/group11/283091182/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..dbe4691938 --- /dev/null +++ b/group11/283091182/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,229 @@ +package com.coderising.litestruts; + +import java.beans.BeanInfo; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Constructor; +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.NamedNodeMap; +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) { + + /* + * + * 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + * + * 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + * 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 ("name"="test" , + * "password"="1234") , 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + * + * 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + * + * 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 + * {"message": "鐧诲綍鎴愬姛"} , 鏀惧埌View瀵硅薄鐨刾arameters + * + * 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + * 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + * + */ + + //0. Load Structs.xml and return a map + Map actionMap = loadActionMap(); + + //1.Find and initialize action instance to actionName provided + Action action = actionMap.get(actionName); + Object instance = initializeActionInstance(action.getActionClass(),parameters); + + //2.invoke the execute method and get the result + String result = executeAction(instance); + + //3.Extract info for view + View view = new View(); + view.setParameters(extractInfo(instance)); + + //4.set dispatcher jsp according to execution result + view.setJsp(action.getActionResultJsp(result)); + + return view; + } + + private static Map loadActionMap() { + try { + + InputStream is = Struts.class.getResourceAsStream("struts.xml"); + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.parse(is); + Element struct = document.getDocumentElement(); + + return getActions(struct); + + } catch (IOException e) { + throw new RuntimeException("Error Reading Configuration XML",e); + } catch (ParserConfigurationException e) { + throw new RuntimeException("Error Parsing Configuration XML",e); + } catch (SAXException e) { + throw new RuntimeException("Error Parsing Configuration XML",e); + } + } + + /** + * Parse the XML and construct ActionName:Action map + * + * @param Element struct, root of the struct xml + * @return Map + */ + private static Map getActions(Element struct) { + + Map map = new HashMap(); + + NodeList nl = struct.getElementsByTagName(Action.CONST_ACTION); + + for (int i = 0; i < nl.getLength(); i++) + { + Node actionNode = nl.item(i); + // Get action name and corresponding action class from property + String actionClass = getAttribute(actionNode,Action.CONST_CLASS); + String actionName = getAttribute(actionNode,Action.CONST_NAME); + + Action action = new Action(actionName, actionClass); + + // get results under action + NodeList childNodes = actionNode.getChildNodes(); + + for (int j = 0; j < childNodes.getLength(); j++) + { + Node result = childNodes.item(j); + //Only accept if Node Type is element and Node name is "result" + if ((result.getNodeType() == result.ELEMENT_NODE) + && (result.getNodeName() == Action.CONST_RESULT)) + { + String resultName = getAttribute(result,Action.CONST_NAME); + String dispatcherJsp = result.getTextContent(); + action.setActionResultJsp(resultName, dispatcherJsp); + } + } + map.put(action.getActionName(), action); + } + System.out.println(map); + return map; + } + /** + * Get property from given node + * @param node + * @param key + * @return attribute as String + */ + private static String getAttribute(Node node,String key){ + NamedNodeMap map = node.getAttributes(); + Node attriNode = map.getNamedItem(key); + if(attriNode!=null && attriNode.getNodeType()==Node.ATTRIBUTE_NODE){ + return attriNode.getNodeValue(); + } + return null; + } + + /** + * Initialize instance from given class name and parameters map + * @param actionClass + * @param parameters + * @return instance of specified class + */ + private static Object initializeActionInstance(String actionClass,Map parameters){ + try { + Class clazz= Class.forName(actionClass); + //Instantiate by calling constructor + Constructor constructor = clazz.getConstructor(); + + constructor.setAccessible(true); + Object instance = constructor.newInstance(new Object[]{}); + + //Check class propertes with instrospector + BeanInfo beanInfo = Introspector.getBeanInfo(clazz); + PropertyDescriptor[] props = beanInfo.getPropertyDescriptors(); + + for(PropertyDescriptor prop:props){ + String propName = prop.getName(); + Method propSetter = prop.getWriteMethod(); + //If there is a setter for the property and also there is a value in parameter map + //then invoke the setter method to set the values + if(propSetter!=null && parameters.containsKey(propName)) + { + propSetter.invoke(instance, parameters.get(propName)); + } + } + + return instance; + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Error initializing instance: ClassName="+actionClass,e); + } + } + + + /** + * Invoke the "execute" method from the action instance + * @param Action instance + * @return execute result as String + */ + private static String executeAction(Object instance){ + Class clazz = instance.getClass(); + try { + //exepct no argument for execute method + Method execute = clazz.getMethod("execute", new Class[0]); + return (String)execute.invoke(instance, new Object[0]); + } catch (Exception e) { + throw new RuntimeException("Error executing action,class name="+clazz.getCanonicalName()); + } + } + + + /** + * Extracting Bean info by calling the getting method in the Action instance + * @param instance + * @return map + */ + private static Map extractInfo(Object instance){ + Map map = new HashMap(); + Class clazz = instance.getClass(); + try{ + Method[] methods = clazz.getMethods(); + for(Method method:methods) + { + String methodName = method.getName(); + if(methodName.startsWith("get")&&method.getParameterTypes().length==0) + { + Object methodReturn = method.invoke(instance, new Object[0]); + //construct the properties name by getter method name,first character toLower case + String propName = methodName.replaceFirst("get", ""); + char[] propNameCharArr = propName.toCharArray(); + propNameCharArr[0]=Character.toLowerCase(propNameCharArr[0]); + + map.put(String.valueOf(propNameCharArr), methodReturn); + } + } + + }catch(Exception e){ + throw new RuntimeException("Error extracting info from Action Insance,class="+clazz.getCanonicalName(),e); + } + return map; + } + +} diff --git a/group11/283091182/src/com/coderising/litestruts/StrutsTest.java b/group11/283091182/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group11/283091182/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group11/283091182/src/com/coderising/litestruts/View.java b/group11/283091182/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group11/283091182/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/group11/283091182/src/com/coderising/litestruts/struts.xml b/group11/283091182/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..4c6eeabbd4 --- /dev/null +++ b/group11/283091182/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 diff --git a/group11/283091182/src/com/coding/basic/ArrayListTest.java b/group11/283091182/src/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..7807fa831e --- /dev/null +++ b/group11/283091182/src/com/coding/basic/ArrayListTest.java @@ -0,0 +1,142 @@ +/** + * + */ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * @author Administrator + * + */ +public class ArrayListTest { + + private ArrayList al; + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + System.out.println("SetUp"); + al= new ArrayList(); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + System.out.println("TearDown"); + al = null; + } + + /** + * Test method for {@link com.coding.basic.ArrayList#add(java.lang.Object)}. + */ + @Test + public final void testAddObject() { + al.add("aaa"); + al.add("bbb"); + al.add("ccc"); + assertEquals("aaa",al.get(0)); + assertEquals("bbb",al.get(1)); + assertEquals("ccc",al.get(2)); + assertEquals(3,al.size()); + } + + /** + * Test method for {@link com.coding.basic.ArrayList#add(int, java.lang.Object)}. + */ + @Test + public final void testAddIntObject() { + al.add("aaa"); + al.add(0,"bbb"); + al.add(1,"ccc"); + assertEquals("bbb",al.get(0)); + assertEquals("ccc",al.get(1)); + assertEquals("aaa",al.get(2)); + assertEquals(3,al.size()); + } + /** + * Test method for {@link com.coding.basic.ArrayList#add(int, java.lang.Object)}. + */ + @Test(expected=IndexOutOfBoundsException.class) + public final void testAddIntObjectWithException1() { + al.add(-1, "aaa"); + } + /** + * Test method for {@link com.coding.basic.ArrayList#add(int, java.lang.Object)}. + */ + @Test(expected=IndexOutOfBoundsException.class) + public final void testAddIntObjectWithException2() { + al.add("aaa"); + al.add(1,"bbb"); + } + + /** + * Test method for {@link com.coding.basic.ArrayList#get(int)}. + */ + @Test + public final void testGet() { + fail("Not yet implemented"); // TODO + } + /** + * Test method for {@link com.coding.basic.ArrayList#get(int)}. + */ + @Test + public final void testGetWithException1() { + fail("Not yet implemented"); // TODO + } + /** + * Test method for {@link com.coding.basic.ArrayList#get(int)}. + */ + @Test + public final void testGetWithException2() { + fail("Not yet implemented"); // TODO + } + + /** + * Test method for {@link com.coding.basic.ArrayList#remove(int)}. + */ + @Test + public final void testRemove() { + fail("Not yet implemented"); // TODO + } + + /** + * Test method for {@link com.coding.basic.ArrayList#size()}. + */ + @Test + public final void testSize() { + fail("Not yet implemented"); // TODO + } + + /** + * Test method for {@link com.coding.basic.ArrayList#iterator()}. + */ + @Test + public final void testIterator() { + fail("Not yet implemented"); // TODO + } + +} diff --git a/group11/395443277/data_structure/.gitignore b/group11/395443277/.gitignore similarity index 100% rename from group11/395443277/data_structure/.gitignore rename to group11/395443277/.gitignore diff --git a/group11/395443277/src/com/coderising/array/ArrayUtil.java b/group11/395443277/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..d73df85b05 --- /dev/null +++ b/group11/395443277/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,254 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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 first = 0; + int last = origin.length - 1; + + while (first < last) { + int temp; + temp = origin[first]; + origin[first] = origin[last]; + origin[last] = temp; + + first++; + last--; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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){ + // count how many zeros + int zeroCount = 0; + int len= oldArray.length; + + if (len==0) { + return new int[]{}; + } + + for (int i=0; i parameters) { + + /* + * + * 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + * + * 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + * 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 ("name"="test" , + * "password"="1234") , 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + * + * 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + * + * 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 + * {"message": "鐧诲綍鎴愬姛"} , 鏀惧埌View瀵硅薄鐨刾arameters + * + * 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + * 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + * + */ + + // create a new DocumentBuilderFactory + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + + try { + // use the factory to create a documentbuilder + DocumentBuilder builder = factory.newDocumentBuilder(); + InputStream istream = Struts.class.getResourceAsStream("struts.xml"); + Document doc = builder.parse(istream); + + // find jsp page based on the result from execute result + Element element = doc.getDocumentElement(); + NodeList actionNodeList = element.getElementsByTagName("action"); + + for (int i = 0; i < actionNodeList.getLength(); i++) { + NamedNodeMap actionNodeAttr = actionNodeList.item(i).getAttributes(); + + if (actionNodeAttr.getNamedItem("name").getTextContent().equals(actionName)) { + String className = actionNodeAttr.getNamedItem("class").getTextContent(); + + Class cls = Class.forName(className); + Object obj = cls.newInstance(); + + // set name and password + Method setName = cls.getDeclaredMethod("setName", String.class); + Method setPassword = cls.getDeclaredMethod("setPassword", String.class); + setName.invoke(obj, parameters.get("name")); + setPassword.invoke(obj, parameters.get("password")); + + // execute + Method execute = cls.getDeclaredMethod("execute"); + String executeResult = (String) execute.invoke(obj); + + // get message and jsp + Method getMessage = cls.getDeclaredMethod("getMessage"); + String msg = (String) getMessage.invoke(obj); + Map params = new HashMap(); + params.put("message",msg); + + // check result nodes + NodeList resultNodes = actionNodeList.item(i).getChildNodes(); + + String jsp = ""; + for (int j=0; j viewCls = Class.forName("com.coderising.litestruts.View"); + View viewObj = (View) viewCls.newInstance(); + Method setParameters = viewCls.getDeclaredMethod("setParameters", Map.class); + setParameters.invoke(viewObj, params); + Method setJsp = viewCls.getDeclaredMethod("setJsp", String.class); + setJsp.invoke(viewObj, jsp); + + return viewObj; + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + +} diff --git a/group11/395443277/src/com/coderising/litestruts/StrutsTest.java b/group11/395443277/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group11/395443277/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group11/395443277/src/com/coderising/litestruts/View.java b/group11/395443277/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group11/395443277/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/group11/395443277/src/com/coderising/litestruts/struts.xml b/group11/395443277/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..7f8d558286 --- /dev/null +++ b/group11/395443277/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 diff --git a/group11/395443277/data_structure/src/com/coding/basic/ArrayList.java b/group11/395443277/src/com/coding/basic/ArrayList.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/ArrayList.java rename to group11/395443277/src/com/coding/basic/ArrayList.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/ArrayListTest.java b/group11/395443277/src/com/coding/basic/ArrayListTest.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/ArrayListTest.java rename to group11/395443277/src/com/coding/basic/ArrayListTest.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/BinaryTreeNode.java b/group11/395443277/src/com/coding/basic/BinaryTreeNode.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/BinaryTreeNode.java rename to group11/395443277/src/com/coding/basic/BinaryTreeNode.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/BinaryTreeNodeTest.java b/group11/395443277/src/com/coding/basic/BinaryTreeNodeTest.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/BinaryTreeNodeTest.java rename to group11/395443277/src/com/coding/basic/BinaryTreeNodeTest.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/Iterator.java b/group11/395443277/src/com/coding/basic/Iterator.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/Iterator.java rename to group11/395443277/src/com/coding/basic/Iterator.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/LinkedList.java b/group11/395443277/src/com/coding/basic/LinkedList.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/LinkedList.java rename to group11/395443277/src/com/coding/basic/LinkedList.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/LinkedListTest.java b/group11/395443277/src/com/coding/basic/LinkedListTest.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/LinkedListTest.java rename to group11/395443277/src/com/coding/basic/LinkedListTest.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/List.java b/group11/395443277/src/com/coding/basic/List.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/List.java rename to group11/395443277/src/com/coding/basic/List.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/Queue.java b/group11/395443277/src/com/coding/basic/Queue.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/Queue.java rename to group11/395443277/src/com/coding/basic/Queue.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/QueueTest.java b/group11/395443277/src/com/coding/basic/QueueTest.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/QueueTest.java rename to group11/395443277/src/com/coding/basic/QueueTest.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/Stack.java b/group11/395443277/src/com/coding/basic/Stack.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/Stack.java rename to group11/395443277/src/com/coding/basic/Stack.java diff --git a/group11/395443277/data_structure/src/com/coding/basic/StackTest.java b/group11/395443277/src/com/coding/basic/StackTest.java similarity index 100% rename from group11/395443277/data_structure/src/com/coding/basic/StackTest.java rename to group11/395443277/src/com/coding/basic/StackTest.java diff --git a/group11/542194147/myDataStructure/.classpath b/group11/542194147/myDataStructure/.classpath new file mode 100644 index 0000000000..ddd63c6d08 --- /dev/null +++ b/group11/542194147/myDataStructure/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group11/542194147/myDataStructure/.gitignore b/group11/542194147/myDataStructure/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group11/542194147/myDataStructure/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group11/542194147/myDataStructure/.project b/group11/542194147/myDataStructure/.project new file mode 100644 index 0000000000..56bfc21f79 --- /dev/null +++ b/group11/542194147/myDataStructure/.project @@ -0,0 +1,17 @@ + + + myDataStructure + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group11/542194147/myDataStructure/src/com/coderising/array/ArrayUtil.java b/group11/542194147/myDataStructure/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..7512dac816 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,208 @@ +package com.coderising.array; + +import java.util.Arrays; + +/** + * Array闆嗗悎宸ュ叿绫 + * @author 灏忔懇鎵 + * + */ +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public int[] reverseArray(int[] origin){ + if(origin.length==0){ + throw new RuntimeException("鏁扮粍涓虹┖"); + } + int[]exchange=new int[origin.length]; + for(int i=origin.length-1;i>=0;i--){ + exchange[origin.length-1-i]=origin[i]; + } + return exchange; + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 exchange[]={}; + for(int i=0;i parameters) { + + /* + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + View view=new View(); + try { + SAXReader sr=new SAXReader(); + Document document= sr.read(new File("src/com/coderising/litestruts/struts.xml")); + Element root=document.getRootElement(); + List actions=root.elements("action"); + for(Iterator it=actions.iterator();it.hasNext();){ + Element action =it.next(); + if(action.attribute("name").getText().equals(actionName)){ + LoginAction loginAction=(LoginAction)Class.forName( + action.attribute("class").getText()).newInstance(); + loginAction.setName(parameters.get("name")); + loginAction.setPassword(parameters.get("password")); + String loginMsg=loginAction.execute(); + if(loginMsg.equals("success")){ + Listresults=action.elements("result"); + for(it=results.iterator();it.hasNext();){ + Element result=it.next(); + if(result.attribute("name").getText().equals("success")){ + createView(view, loginAction, result); + } + } + } + if(loginMsg.equals("fail")){ + Listresults=action.elements("result"); + for(it=results.iterator();it.hasNext();){ + Element result=it.next(); + if(result.attribute("name").getText().equals("fail")){ + createView(view, loginAction, result); + } + } + } + if(loginMsg.equals("error")){ + Listresults=action.elements("result"); + for(it=results.iterator();it.hasNext();){ + Element result=it.next(); + if(result.attribute("name").getText().equals("error")){ + createView(view, loginAction, result); + } + } + } + } + } + } catch (Exception e) { + System.out.println("have exception:"+e); + e.printStackTrace(); + } + return view; + } + + private static void createView(View view, LoginAction loginAction, Element result) { + Map msgMap=new HashMap(); + msgMap.put("message", loginAction.getMessage()); + view.setParameters(msgMap); + view.setJsp(result.getText()); + } + +} diff --git a/group11/542194147/myDataStructure/src/com/coderising/litestruts/StrutsTest.java b/group11/542194147/myDataStructure/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..7fa9e9a4e5 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coderising/litestruts/StrutsTest.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 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/group11/542194147/myDataStructure/src/com/coderising/litestruts/View.java b/group11/542194147/myDataStructure/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..610ce0d092 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coderising/litestruts/View.java @@ -0,0 +1,24 @@ +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/group11/542194147/myDataStructure/src/com/coderising/litestruts/struts.xml b/group11/542194147/myDataStructure/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group11/542194147/myDataStructure/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 diff --git a/group11/542194147/myDataStructure/src/com/coding/basic/MyArrayList.java b/group11/542194147/myDataStructure/src/com/coding/basic/MyArrayList.java new file mode 100644 index 0000000000..fbede89fa1 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coding/basic/MyArrayList.java @@ -0,0 +1,133 @@ +package com.coding.basic; + +import java.util.Iterator; + +public class MyArrayList implements MyList { + + /** + * 鏁扮粍榛樿澶у皬 + */ + private static final int DEFAULT_SIZE = 10; + /** + * 鍌ㄥ瓨鍏冪礌鐨勬暟缁 + */ + private Object[] elementData =null; + /** + * 鏁扮粍澶у皬鎸囬拡; + */ + private int capacity; + /** + * 褰撳墠娓告爣 + */ + private int current; + + public MyArrayList(int size){ + if(size<0){ + throw new RuntimeException("澶у皬涓嶈兘灏忎簬0"); + }else{ + this.elementData= new Object[size]; + this.current=0; + this.capacity=size; + } + } + + public MyArrayList(){ + this(DEFAULT_SIZE); + } + + @Override + public void add(Object o) { + isOverSize();//鍒ゆ柇鏁扮粍瀹归噺鏄惁婊¤冻锛屼笉婊¤冻澧炲姞绌洪棿 + this.elementData[current]=o; + this.current++; + } + + @Override + public void add(int index, Object o) { + isOverSize();//鍒ゆ柇鏁扮粍瀹归噺鏄惁婊¤冻锛屼笉婊¤冻澧炲姞绌洪棿 + isOutOfBoundIndex(index);//鍒ゆ柇鏁扮粍涓嬫爣鏄惁瓒婄晫 + System.arraycopy(elementData, index, elementData, index+1, this.elementData.length-index); + this.current++; + } + + @Override + public Object get(int index) { + isOutOfBoundIndex(index);//鍒ゆ柇鏁扮粍涓嬫爣鏄惁瓒婄晫 + return this.elementData[index]; + } + + @Override + public Object remove(int index) { + isOutOfBoundIndex(index);//鍒ゆ柇鏁扮粍涓嬫爣鏄惁瓒婄晫 + Object o=this.elementData[index]; + if(this.elementData.length>index+1){ + System.arraycopy(elementData, index+1, elementData, index,this.elementData.length-index-1); + } + this.elementData[this.elementData.length-1]=null; + return o; + } + + public Iterator iterator(){ + return new MyArrayListIterator(); + } + + @Override + public int size() { + return this.elementData.length; + } + + /** + * 鍒ゆ柇鏁扮粍瀹归噺鏄惁婊¤冻锛屼笉婊¤冻澧炲姞绌洪棿 + */ + private void isOverSize() { + if(this.current==this.capacity){ + this.capacity+=MyArrayList.DEFAULT_SIZE; + } + Object[]newElementData=new Object[this.capacity]; + for(int i=0;ithis.capacity||index<0){ + throw new RuntimeException("鏁扮粍涓嬫爣瓒婄晫"); + } + } + + /** + * MyArrayList鐨勮凯浠e櫒 + * @author 灏忔懇鎵 + * + */ + private class MyArrayListIterator implements Iterator{ + + private int current=0; + + @Override + public boolean hasNext() { + return currentsize||index<0){//妫鏌ヤ笅鏍囨槸鍚﹁秺鐣 + throw new RuntimeException("涓嬫爣瓒婄晫"); + } + if(index==this.size){ + addLast(o);//鎻掑埌闃熷熬 + }else{ + Node l= node(index); + addBeforeNode(o,l);//鎻掑埌鎸囧畾涓嬫爣鑺傜偣涔嬪墠 + } + } + + @Override + public Object get(int index) { + if (index >= size || index < 0) { + throw new RuntimeException("涓嬫爣瓒婄晫"); + } + return node(index).data; + } + + @Override + public Object remove(int index) { + if (index >= size || index < 0) { + throw new RuntimeException("涓嬫爣瓒婄晫"); + } + Node l=node(index); + Node prevNode=l.prev; + Node nextNode=l.next; + if(prevNode==null){ + head=nextNode; + }else{ + prevNode.next=nextNode; + + } + if(nextNode==null){ + last=prevNode; + }else{ + nextNode.prev=prevNode; + } + l.prev =null; + l.next=null; + l.data=null; + return l.data; + } + + @Override + public int size() { + return size; + } + + /** + * 鑾峰彇瀵瑰簲鑺傜偣鐨勪笅鏍 + * @param element + * @return + */ + public int indexOf(Object element) { + Node current = head; + int count = 0; + while (current != null) { + if (element != null) { + if (element.equals(current.data)) { + return count; + } + }else{ + if (current.data == null) { + return count; + } + } + count ++; + current = current.next; + } + return -1; + } + + /** + * 娣诲姞鍒板搴斾笅鏍囩殑鑺傜偣涔嬪墠 + * @param o + * @param theNode + */ + public void addBeforeNode(Object o,Node theNode){ + Node prevNode=theNode.prev; + Node newNode= new Node(o,theNode,prevNode); + theNode.prev=newNode; + if(null==prevNode){ + this.head=newNode; + }else{ + prevNode.next=newNode; + } + size++; + } + + /** + * 榛樿娣诲姞鍒伴槦灏 + * @param o + */ + public void addLast(Object o){ + Node l=this.last; + Node node= new Node(o,null,l); + if(null!=l){ + l.next=node; + }else{ + this.head=node; + } + size++; + } + + /** + * 鏌ユ壘瀵瑰簲涓嬫爣鐨勮妭鐐瑰苟杩斿洖 + * @param index + * @return + */ + private Node node(int index){ + if(index<(this.size>>1)){ + Node current=head; + for(int i=0;iindex;i--){ + current=current.prev; + } + return current; + } + } + + public Iterator iterator(){ + return new MyLinkedListIterator(); + } + + private class MyLinkedListIterator implements Iterator{ + private Node current=head; + + @Override + public boolean hasNext() { + + return current!=last; + } + + @Override + public Object next() { + if(hasNext()==false){ + throw new RuntimeException("涓嶅瓨鍦ㄥ搴斿厓绱"); + } + Object o=current.data; + current=current.next; + return o; + } + + @Override + public void remove() { + int index=MyLinkedList.this.indexOf(current); + MyLinkedList.this.remove(index); + } + + } + /** + * 鍙屽悜閾捐〃 + * @author 灏忔懇鎵 + * + */ + private static class Node{ + Object data; + Node next; + Node prev; + public Node(Object d,Node n,Node p){ + this.data=d; + this.next=n; + this.prev=p; + } + } + +} diff --git a/group11/542194147/myDataStructure/src/com/coding/basic/MyList.java b/group11/542194147/myDataStructure/src/com/coding/basic/MyList.java new file mode 100644 index 0000000000..73f331beec --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coding/basic/MyList.java @@ -0,0 +1,34 @@ +package com.coding.basic; + +public interface MyList { + + /** + * 鍚戦泦鍚堜腑娣诲姞鍏冪礌 + * @param o 浠绘剰绫诲瀷鍏冪礌 + */ + public void add(Object o); + /** + * 鍚戦泦鍚堜腑娣诲姞鍏冪礌 + * @param index 鎸囧畾浣嶇疆涓嬫爣 + * @param o 浠绘剰绫诲瀷鍏冪礌 + */ + public void add(int index, Object o); + /** + * 鑾峰彇瀵瑰簲涓嬫爣鐨勫厓绱 + * @param index 涓嬫爣 + * @return 瀵瑰簲涓嬫爣鐨勫厓绱 + */ + public Object get(int index); + /** + * 绉婚櫎瀵瑰簲涓嬫爣鐨勫厓绱 + * @param index 涓嬫爣 + * @return + */ + public Object remove(int index); + /** + * 鑾峰彇闆嗗悎鐨勫ぇ灏 + * @return 澶у皬鏁板 + */ + public int size(); + +} diff --git a/group11/542194147/myDataStructure/src/com/coding/basic/MyQueue.java b/group11/542194147/myDataStructure/src/com/coding/basic/MyQueue.java new file mode 100644 index 0000000000..c7fba907d4 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coding/basic/MyQueue.java @@ -0,0 +1,19 @@ +package com.coding.basic; + +public class MyQueue { + + public void enQueue(Object o){ + } + + public Object deQueue(){ + return null; + } + + public boolean isEmpty(){ + return false; + } + + public int size(){ + return -1; + } +} diff --git a/group11/542194147/myDataStructure/src/com/coding/basic/MyStack.java b/group11/542194147/myDataStructure/src/com/coding/basic/MyStack.java new file mode 100644 index 0000000000..705b3ec7e0 --- /dev/null +++ b/group11/542194147/myDataStructure/src/com/coding/basic/MyStack.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +import java.util.ArrayList; + +public class MyStack { + + 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/group11/729245768/DataStructure/src/main/coding_170302/ArrayUtil.java b/group11/729245768/DataStructure/src/main/coding_170302/ArrayUtil.java new file mode 100644 index 0000000000..db8d8bef49 --- /dev/null +++ b/group11/729245768/DataStructure/src/main/coding_170302/ArrayUtil.java @@ -0,0 +1,211 @@ +package main.coding_170302; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + for(int i=0,j=origin.length-1;i list = new ArrayList<>(); + for(int i=6;i<=max;i++){ + if(isPerfectNumbers(i)){ + list.add(i); + } + } + int[] array = new int[list.size()]; + for(int i=0;i list = new ArrayList<>(); + int end =(int) Math.sqrt(element);// + for(int i=2;i<=end;){ + if(element%i==0){ + list.add(i); + element=element/i; + }else{ + i++; + } + } + int sum =1; + for(Integer i:list){ + sum+=i; + } + if(element==sum){ + return true; + }else{ + return false; + } + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for(int i=0;i parameters) throws DocumentException { + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + View view = new View(); + SAXReader saxReader = new SAXReader(); + Document document = saxReader.read(new File("DataStructure/src/main/coding_170302/struts.xml")); + Element root = document.getRootElement();//鑾峰彇鏍瑰厓绱 + List listOfAction = root.elements("action");//鑾峰彇action瀛愬厓绱 + int i=0; + for(;i> inputs = parameters.entrySet(); + //鎵цsetName鍜宻etPassword + for(Map.Entry input:inputs){ + String key = input.getKey(); + String value = input.getValue(); + String methodName = "set"+Character.toUpperCase(key.charAt(0))+key.substring(1); + Method method = c.getDeclaredMethod(methodName,String.class); + method.invoke(o,value); + } + //鎵цexecute鏂规硶锛屽苟杩斿洖message + Method execute = c.getDeclaredMethod("execute"); + String returnMessage = (String)execute.invoke(o); + + Method[] methods = c.getDeclaredMethods(); + //鑾峰彇鎵鏈塯etter鏂规硶锛屽苟涓斿皢鍊兼斁鍒皏iew鐨刾arameters涓 + Map paraMap = new HashMap<>(); + for(int j=0;j 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + // 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + List resultOfElements = action.elements("result"); + for(int k=0;k + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group11/729245768/DataStructure/tests/main/coding_170302/ArrayUtilTest.java b/group11/729245768/DataStructure/tests/main/coding_170302/ArrayUtilTest.java new file mode 100644 index 0000000000..4d5cc38728 --- /dev/null +++ b/group11/729245768/DataStructure/tests/main/coding_170302/ArrayUtilTest.java @@ -0,0 +1,78 @@ +package main.coding_170302; + +import junit.framework.TestCase; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by peter on 2017/3/2. + */ +public class ArrayUtilTest extends TestCase { + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testReverseArray() throws Exception { + int[] array1 = {1,2,3,4,5,6,7}; + int[] array2 = {7,6,5,4,3,2,1}; + ArrayUtil util = new ArrayUtil(); + util.reverseArray(array1); + Assert.assertArrayEquals(array1,array2); + } + + @Test + public void testRemoveZero() throws Exception { + int[] array1 = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] arry2 = {1,3,4,5,6,6,5,4,7,6,7,5}; + Assert.assertArrayEquals(new ArrayUtil().removeZero(array1),arry2); +} + + @Test + public void testMerge() throws Exception { + int[] array1 = {3,5,7,8}; + int[] array2 = {4,5,6,7}; + int[] array = {3,4,5,6,7,8}; + Assert.assertArrayEquals(array,new ArrayUtil().merge(array1,array2)); + } + + @Test + public void testGrow() throws Exception { + int[] array1 ={2,3,6}; + int[] array2 = {2,3,6,0,0,0}; + Assert.assertArrayEquals(array2,new ArrayUtil().grow(array1,3)); + } + + @Test + public void testFibonacci() throws Exception { + int[] array1 = {1,1,2,3,5,8,13}; + Assert.assertArrayEquals(array1,new ArrayUtil().fibonacci(15)); + } + + @Test + public void testGetPrimes() throws Exception { + int[] array1 = {2,3,5,7,11,13,17,19}; + Assert.assertArrayEquals(array1,new ArrayUtil().getPrimes(23)); + } + + @Test + public void testGetPerfectNumbers() throws Exception { + int[] array1 = {6}; + Assert.assertArrayEquals(array1,new ArrayUtil().getPerfectNumbers(10)); + } + + @Test + public void testJoin() throws Exception { + int[] array1 = {3,8,9}; + Assert.assertEquals("3-8-9",new ArrayUtil().join(array1,"-")); + } + +} \ No newline at end of file diff --git a/group11/729245768/DataStructure/tests/main/coding_170302/StrutsTest.java b/group11/729245768/DataStructure/tests/main/coding_170302/StrutsTest.java new file mode 100644 index 0000000000..309bdfc9ba --- /dev/null +++ b/group11/729245768/DataStructure/tests/main/coding_170302/StrutsTest.java @@ -0,0 +1,42 @@ +package main.coding_170302; + +import junit.framework.TestCase; +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by peter on 2017/3/3. + */ +public class StrutsTest extends TestCase { + @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")); + } + +} \ No newline at end of file diff --git a/group11/996108220/.classpath b/group11/996108220/.classpath index d171cd4c12..11cc9c31cb 100644 --- a/group11/996108220/.classpath +++ b/group11/996108220/.classpath @@ -2,5 +2,7 @@ + + diff --git a/group11/996108220/src/com/coderising/array/ArrayUtil.java b/group11/996108220/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..0637eb921c --- /dev/null +++ b/group11/996108220/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,194 @@ +package com.coderising.array; + +import com.coding.basic.ArrayList; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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 mid=origin.length%2==1?origin.length/2:origin.length/2-1; + for (int i = 0; i <= mid; i++) { + int vlaue=origin[i]; + origin[i]=origin[origin.length-1-i]; + origin[origin.length-1-i]=vlaue; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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){ + ArrayList list=new ArrayList(); + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i]!=0) { + list.add(oldArray[i]); + } + } + int[] array=new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + array[i]=(int) list.get(i); + } + + return array; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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[] array=new int[array1.length+array2.length]; + int ptr1=0;int ptr2=0;int index=0; + while (ptr1!=array1.length&&ptr2!=array2.length) { + array[index++]=array1[ptr1]>array2[ptr2]?array2[ptr2++]:array1[ptr1++]; + } + if (ptr1==array1.length) { + for (int i = ptr2; i < array2.length; i++)array[index++]=array2[i]; + } + else { + for (int i = ptr1; i < array1.length; i++)array[index++]=array1[i]; + } + return array; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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 [] array=new int[oldArray.length+size]; + for (int i = 0; i < oldArray.length; i++) { + array[i]=oldArray[i]; + } + return array; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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; + } + else if (max==2) { + return new int[]{1,1}; + } + else { + ArrayList list=new ArrayList(); + list.add(1); + list.add(1); + int next=2; + while (next messageToresult=null; + public String getActionName() { + return actionName; + } + public void setActionName(String actionName) { + this.actionName = actionName; + } + public String getClazzName() { + return clazzName; + } + public void setClazzName(String clazzName) { + this.clazzName = clazzName; + } + public HashMap getMessageToresult() { + return messageToresult; + } + public void setMessageToresult(HashMap messageToresult) { + this.messageToresult = messageToresult; + } + public ActionConfig(String actionName, String clazzName, + HashMap messageToresult) { + super(); + this.actionName = actionName; + this.clazzName = clazzName; + this.messageToresult = messageToresult; + } + + +} diff --git a/group11/996108220/src/com/coderising/litestruts/LoginAction.java b/group11/996108220/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..01b51fdd88 --- /dev/null +++ b/group11/996108220/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,40 @@ +package com.coderising.litestruts; + + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author liuxin + * + */ +public class LoginAction implements Action{ + 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/group11/996108220/src/com/coderising/litestruts/LogoutAction.java b/group11/996108220/src/com/coderising/litestruts/LogoutAction.java new file mode 100644 index 0000000000..8043fae56e --- /dev/null +++ b/group11/996108220/src/com/coderising/litestruts/LogoutAction.java @@ -0,0 +1,35 @@ +package com.coderising.litestruts; + +public class LogoutAction implements Action{ + 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 = "logout successful"; + return "success"; + } + this.message = "logout failed,please try again"; + return "error"; + } + + 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/group11/996108220/src/com/coderising/litestruts/Struts.java b/group11/996108220/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..8f961f2175 --- /dev/null +++ b/group11/996108220/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,167 @@ +package com.coderising.litestruts; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import com.coderising.litestruts.Action; + + + +/* + +0. 璇诲彇閰嶇疆鏂囦欢struts.xml + +1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 +鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 +("name"="test" , "password"="1234") , +閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + +2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + +3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, +閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , +鏀惧埌View瀵硅薄鐨刾arameters + +4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 +鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + +*/ + +public class Struts { + //strut.xml鏂囦欢鎵鍦ㄧ殑璺緞 + public static final String dir="/mycoding2017/group11/996108220/src/com/coderising/litestruts/struts.xml"; + + /** + * 鐢ㄦ埛鎻愪緵action鍔ㄤ綔锛屼互鍙婄敤鎴峰悕鍜屽瘑鐮侊紝瀵瑰簲杩斿洖view瑙嗗浘 + * @param actionName 鐧诲叆鐧诲嚭 + * @param parameters 鐢ㄦ埛鍚嶅瘑鐮 + */ + public static View runAction(String actionName, Map parameters) throws Exception { + ActionConfig actionConfig=getActionConfig(actionName); + Action action=createAction(actionConfig.getClazzName(),parameters); + String message=getActionMessage(action); + View view=updaView(getALL(action), actionConfig, message); + return view; + } + + /** + * 姝ラ0锛氳鍙栭厤缃枃浠讹紝灏嗘枃浠朵腑鐨刟ction鐢熸垚ActionDao + * @param actionName浼犲叆action鐨勫悕瀛 + */ + private static ActionConfig getActionConfig(String name) throws Exception { + // 鐢熸垚涓涓狣om瑙f瀽鍣 + DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + // 瑙f瀽XML鏂囦欢 + Document document = builder.parse(dir); + NodeList actions = document.getElementsByTagName("action"); + int j = 0;String actionName="";String clazzName=""; + for (; j < actions.getLength(); j++) { + actionName= actions.item(j).getAttributes().getNamedItem("name").getNodeValue(); + if (actionName.equals(name)) { + clazzName=actions.item(j).getAttributes().getNamedItem("class").getNodeValue(); + break; + } + } + if (actions.item(0).getNodeType() == Node.ELEMENT_NODE) { + + Element action =(Element) actions.item(j); + NodeList results =action.getElementsByTagName("result"); + HashMap map=new HashMap(); + for (int i = 0; i < results.getLength(); i++) { + String nameString=results.item(i).getAttributes().getNamedItem("name").getNodeValue(); + String pageString=results.item(i).getTextContent(); + map.put(nameString, pageString); + } + return new ActionConfig(actionName, clazzName, map); + } + return null; + } + /** + * 姝ラ1锛氬弽灏勫垱寤篴ction鐨勫璞★紝灏唍ame鍜宲assword璧嬪 + * @param clazzName + * @return + */ + private static Action createAction(String clazzName,Map parameters) throws Exception { + Class clazz=Class.forName(clazzName); + Object action = clazz.newInstance() ; + Method nameSetter = action.getClass().getMethod("setName", String.class); + nameSetter .invoke(action, parameters.get("name")); + Method passwordSetter = action.getClass().getMethod("setPassword", String.class); + passwordSetter.invoke(action, parameters.get("password")); + return (Action) action; + } + /** + * 姝ラ2锛氬弽灏勮繍琛宔xecute鏂规硶锛岃幏寰梞essage + * @param action + * @return message + */ + private static String getActionMessage (Action action) throws Exception { + + return (String) action.getClass().getMethod("execute").invoke(action); + + } + /** + * 姝ラ3锛氬皢action涓璯et鏂规硶涓巊et鍒扮殑鍊肩殑鏄犲皠鍏崇郴璁板綍鍒皏iew閲岀殑Parameters琛ㄤ腑 + * @param action + * @return view + */ + private static View getALL(Action action) throws Exception { + HashMap map=new HashMap<>(); + Method nameGetter = action.getClass().getMethod("getName"); + map.put("name", (String) nameGetter.invoke(action)); + Method passwordGetter = action.getClass().getMethod("getPassword"); + map.put("password", (String) passwordGetter.invoke(action)); + Method MessageGetter = action.getClass().getMethod("getMessage"); + map.put("message", (String) MessageGetter.invoke(action)); + View view=new View(); + view.setParameters(map); + return view; + } + /** + * 姝ラ4锛氬皢execute鑾峰緱鐨刴essage鏌ユ壘Struts閰嶇疆鏂囦欢灏嗗搴旂殑椤甸潰璁板綍鍒皏iew涓 + * @param view + * @param actionConfig + * @param message + * @return + */ + private static View updaView(View view,ActionConfig actionConfig,String message) { + return view.setJsp(actionConfig.getMessageToresult().get(message)); + } +// public static void main(String[] args) { +// DocumentBuilder builder; +// try { +// builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); +// Document doc = builder.parse(dir); +// NodeList beans = doc.getElementsByTagName("action"); +// for (int j = 0; j < beans.getLength(); j++) { +// System.out.println(beans.item(j).getAttributes().getNamedItem("name").getNodeValue()); +// System.out.println(beans.item(j).getAttributes().getNamedItem("class").getNodeValue()); +// } +// if (beans.item(0).getNodeType() == Node.ELEMENT_NODE) { +// +// Element action =(Element) beans.item(0); +// NodeList results =action.getElementsByTagName("result"); +// HashMap map=new HashMap(); +// System.out.println(results.getLength()); +// for (int i = 0; i < results.getLength(); i++) { +// System.out.println(results.item(i).getAttributes().getNamedItem("name").getNodeValue()); +// System.out.println(results.item(i).getTextContent()); +// } +// } +// +//// NamedNodeMap name = beans.item(0).getAttributes(); +// +// } catch (Exception e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } + + +// } + +} diff --git a/group11/996108220/src/com/coderising/litestruts/StrutsTest.java b/group11/996108220/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..af5f82d275 --- /dev/null +++ b/group11/996108220/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,63 @@ +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 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")); + } + @Test + public void testLogoutActionSuccess() throws Exception { + + String actionName = "logout"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + Assert.assertEquals("/jsp/welcome.jsp", view.getJsp()); + Assert.assertEquals("logout successful", view.getParameters().get("message")); + } + + @Test + public void testLogoutActionFailed() throws Exception { + String actionName = "logout"; + Map params = new HashMap(); + params.put("name","test"); + params.put("password","123456"); //瀵嗙爜鍜岄璁剧殑涓嶄竴鑷 + + View view = Struts.runAction(actionName,params); + Assert.assertEquals("/jsp/error.jsp", view.getJsp()); + Assert.assertEquals("logout failed,please try again", view.getParameters().get("message")); + } +} diff --git a/group11/996108220/src/com/coderising/litestruts/View.java b/group11/996108220/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group11/996108220/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/group11/996108220/src/com/coderising/litestruts/struts.xml b/group11/996108220/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..246b3595ad --- /dev/null +++ b/group11/996108220/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 diff --git a/group11/996108220/src/com/coding/basic/ArrayList.java b/group11/996108220/src/com/coding/basic/ArrayList.java index 6fe727645b..2445d52ae5 100644 --- a/group11/996108220/src/com/coding/basic/ArrayList.java +++ b/group11/996108220/src/com/coding/basic/ArrayList.java @@ -1,5 +1,6 @@ package com.coding.basic; + public class ArrayList implements List { private int size = 0; @@ -94,6 +95,13 @@ public Object next() { } } + public Object[] toArray() { + Object[] array= new Object[size]; + for (int i = 0; i < elementData.length; i++) { + array[i]=elementData[i]; + } + return array; + } public void grow(Object[] elementData2){ int[] elementData=new int[elementData2.length+elementData2.length/2]; System.arraycopy(elementData2,0,elementData,0,elementData2.length); diff --git a/group15/1500_369516660/.gitignore b/group15/1500_369516660/.gitignore deleted file mode 100644 index 5e56e040ec..0000000000 --- a/group15/1500_369516660/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/bin diff --git a/group15/1500_369516660/src/com/arrayList/basic/SimpleArrayList.java b/group15/1500_369516660/src/com/arrayList/basic/SimpleArrayList.java deleted file mode 100644 index d2f3af6329..0000000000 --- a/group15/1500_369516660/src/com/arrayList/basic/SimpleArrayList.java +++ /dev/null @@ -1,155 +0,0 @@ -package com.arrayList.basic; - -import java.util.Arrays; - -/** - * - * @author Jodie - * - */ -public class SimpleArrayList implements List { - - private final static int default_num = 10;//创建数组时,没有定义大小,默认为10 - private Object[] elementData; - private int size = 0;//定义数组的大小,真实数组的大小 - - public SimpleArrayList(){ - this(default_num); - } - - public SimpleArrayList(int size) { - if(size<0){//判断定义数组的大小是否小于0 - throw new IllegalArgumentException(); - }else{ - elementData = new Object[size]; - } - } - -/** - * 重写增加函数的方法1 - */ - @Override - public void add(Object o) { - //判断是否需要扩容 - ifSpaceEnougn(size+1); - elementData[size++]=o; - } - -/** - * 重写增加函数的方法2 - */ - @Override - public void add(int index, Object o) { - checkIfOut(index);//是否越界 - ifSpaceEnougn(size+1);//是否要扩容 - System.arraycopy(elementData, index, elementData, index + 1, size-index);//将index的元素及以后的元素向后移一位 - } -/** - * 得到指定下标的数据 - */ - @Override - public Object get(int index) { - checkIfOut(index); - return elementData[index]; - } -/** - * 删除指定下标的数据 - */ - @Override - public Object remove(int index) { - Object value = get(index); - int numRemove = size - index - 1; - if(numRemove > 0){ - System.arraycopy(elementData, index+1, elementData, index, size - index);//集体向前进一位 - } - elementData[--size] = null; - return value; - } -/** - * 删除指定的数据 - */ - @Override - public String remove(Object o) { - if(contains(o)){ - remove(indexOf(o)); - return "删除成功"; - }else{ - return "要删除的数据不在数组中,删除失败"; - } - } -/** - * 判断是否需要扩容 - * @param size - */ - private void ifSpaceEnougn(int size) { - if(size>default_num){ - exlicitSpace(size); - } - if(size<0){//当size超过Integer.MAX_VALUE时,会变为负数 - throw new OutOfMemoryError(); - } - } -/** - * 数组扩容方法 - * @param - */ - private void exlicitSpace(int size) { - final int max_arrayLength = Integer.MAX_VALUE-8; - int newLength = elementData.length*2;//一次性扩容为原来的两倍,避免频繁的扩容 - if(newLength - size<0){ - newLength = size; - } - if(newLength > max_arrayLength){//避免扩容后的大小超过最大值 - newLength = (size > max_arrayLength ? Integer.MAX_VALUE : max_arrayLength); - } - elementData = Arrays.copyOf(elementData, newLength); - } - -/** - * 判断是否越界 - * @param index - */ - private void checkIfOut(int index) { - if(index<0 || index>size){ - throw new IndexOutOfBoundsException(); - } - } - -/** - * 找到指定的数的下标 - * @param o - * @return - */ -private int indexOf(Object o) { - if(o!=null){ - for(int i=0;i= 0; - } - - @Override - public int size() { - return size; - } - - -} diff --git a/group15/1500_369516660/src/com/arrayList/basic/SimpleLinkedList.java b/group15/1500_369516660/src/com/arrayList/basic/SimpleLinkedList.java deleted file mode 100644 index 646c2ab656..0000000000 --- a/group15/1500_369516660/src/com/arrayList/basic/SimpleLinkedList.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.arrayList.basic; - -public class SimpleLinkedList implements List { - - private int size;//数组的大小 - private Node head; - - @Override - public void add(Object o) { - - } - - @Override - public void add(int index, Object o) { - - } - - @Override - public Object get(int index) { - return null; - } - - @Override - public Object remove(int index) { - return null; - } - - @Override - public String remove(Object o) { - return null; - } - - @Override - public int size() { - return 0; - } - -} diff --git a/group15/1501_2535894075/2017code/src/arraylist.java b/group15/1501_2535894075/2017code/src/arraylist.java deleted file mode 100644 index 01e3be56e7..0000000000 --- a/group15/1501_2535894075/2017code/src/arraylist.java +++ /dev/null @@ -1,252 +0,0 @@ -package firstweek; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.ListIterator; - -public class arraylist implements List { - ArrayList q=new ArrayList(); - - private static final int InitialValue=10; - - private static final Object[] Empty_Elementdata={}; - - private Object[] elementData = new Object[100]; - - private int size = 0; - - public boolean add(Object o){ - if(elementData==Empty_Elementdata){ - //设置为初始化容量为10的数组 - elementData[0]=o; - } - if(size==elementData.length){ - //copy数组并增大50% - int oldLength=elementData.length; - int newLength=oldLength+(oldLength>>1); - if(newLength-oldLength>0){ - elementData=Arrays.copyOf(elementData, newLength); - } - } - elementData[size++]=o; - return true; - - } - public void add(int index, Object o){ - if(elementData==Empty_Elementdata){ - elementData[0]=o; - System.out.print("创建了一个新的只有该元素的数组"); - return ; - } - if(size==elementData.length){ - int oldLength=elementData.length; - int newLength=oldLength+(oldLength>>1); - if(newLength-oldLength>0){ - System.arraycopy(elementData, index, elementData,index+1 ,size-index); - } - } - elementData[index]=o; - size++; - } - - public Object get(int index){ - rangecheck(index); - return elementData[index]; - } - - public Object remove(int index){ - rangecheck(index); - Object oldValue=elementData(index); - int numMove=size-index-1; - if(numMove>0){ - System.arraycopy(elementData, index+1, elementData, index, numMove); - } - elementData[--size]=null; - return true; - } - public boolean removeObject(Object o){ - if(o==null){ - for(int index=0;index elementData(int index) { - // TODO Auto-generated method stub - return null; - } - public int size(){ - return size; - } - @Override - public boolean isEmpty() { - // TODO Auto-generated method stub - return size==0; - } - @Override - public boolean contains(Object o) { - // TODO Auto-generated method stub - return 0>=indexOf(o); - } - @Override - public Iterator iterator() { - // TODO Auto-generated method stub - //不会 - return null; - } - @Override - public Object[] toArray() { - // TODO Auto-generated method stub - return Arrays.copyOf(elementData,size); - } - @Override - public Object[] toArray(Object[] a) { - - // TODO Auto-generated method stub - return null; - } - @Override - public boolean remove(Object o) { - // TODO Auto-generated method stub - if(o==null){ - for(int i=0;isize||index<0){ - System.out.println("查询超出范围。"); - return null; - } - elementData[index]=element; - return null; - } - @Override - public int indexOf(Object o) { - // TODO Auto-generated method stub - if(o==null){ - for(int i=0;i= this.size) - throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); - } - private String outOfBoundsMsg(int index) { - return "Index: "+index+", Size: "+this.size; - } - private void fastremove(int index){ - int numMoved=size-index-1; - if(numMoved>0){ - System.arraycopy(elementData, index+1, elementData, index, numMoved); - } - elementData[--size]=null; - } - -} diff --git a/group15/1501_2535894075/2017code/src/com/coderising/array/ArrayUtil.java b/group15/1501_2535894075/2017code/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..8d5a83d85d --- /dev/null +++ b/group15/1501_2535894075/2017code/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,190 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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[] o){ + + int length=o.length; + int half=length/2; + if(length==0||length==1){ + return; + } + for(int i=0;i parameters) { + + try{ + File f=new File("src\\com\\coderising\\litestruts\\struts.xml");//鎵惧埌xml鏂囦欢 + DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();//寰楀埌dom瑙f瀽鍣ㄥ伐鍘傚疄渚 + DocumentBuilder builder =factory.newDocumentBuilder();//浠庡伐鍘備腑寰楀埌DOM瑙f瀽鍣 + Document doc=builder.parse(f);//瑙f瀽鏂囦欢寰楀埌docunment + Element root=doc.getDocumentElement();//寰楀埌xml鏂囨。鐨勬牴鑺傜偣 + NodeList books=root.getChildNodes();//寰楀埌姣忎竴涓妭鐐 + if(books!=null){//濡傛灉涓嶄负绌鸿妭鐐 + for(int i=0;i 鍒 + String name=book.getAttributes().getNamedItem("name").getNodeValue();//鑾峰彇閲岀殑 灞炴у + String classname=book.getAttributes().getNamedItem("class").getNodeValue(); + if(actionName.equals(name)){//濡傛灉浼犺繘鏉ョ殑鍊肩瓑浜 灞炴ame鐨勫 + Class localclass=Class.forName(classname);//鏍规嵁class灞炴х殑鍊煎垱寤篶lass + Object object =localclass.newInstance();//鍒涘缓class瀹炰緥 + for(Entry entry :parameters.entrySet()){//寰幆閬嶅巻浼犺繘鏉ョ殑鍙傛暟 + String methodname=new StringBuffer("set").append(entry.getKey().substring(0,1).toUpperCase()).append(entry.getKey().substring(1)).toString();//鏍规嵁鍙傛暟鏉ョ敓鎴愮浉搴旂殑set鍑芥暟 + Method method=localclass.getMethod(methodname,entry.getKey().getClass()); + method.invoke(object, entry.getValue());//鎵ц鍑芥暟 + } + Method methods=localclass.getMethod("execute"); + String returnvalue=(String)methods.invoke(object); + Map map=new HashMap(); + String namepara=(String) getter(object,"name"); + String passwordpara=(String)getter(object,"password"); + String message=(String)getter(object,"message"); + map.put("name",namepara); + map.put("password", passwordpara); + map.put("message",message); + View view= new View(); + view.setParameters(map); + String jsp = null; + if(returnvalue.equals(" ")){ + + } + NodeList nl = doc.getElementsByTagName("result"); + for(int w=0;w 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + return null; + } + public static Object getter(Object object,String toGet){ + Method target=null; + Object result=null; + try{ + target=object.getClass().getMethod("get"+upperFirst(toGet), null); + result=target.invoke(object); + return result; + }catch(Exception e){ + e.printStackTrace(); + } + return result; + } + public static String upperFirst(String toUpper){ + return toUpper.substring(0,1).toUpperCase()+toUpper.substring(1); + } +} diff --git a/group15/1501_2535894075/2017code/src/com/coderising/litestruts/StrutsTest.java b/group15/1501_2535894075/2017code/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group15/1501_2535894075/2017code/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group15/1501_2535894075/2017code/src/com/coderising/litestruts/View.java b/group15/1501_2535894075/2017code/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group15/1501_2535894075/2017code/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/1501_2535894075/2017code/src/com/coderising/litestruts/struts.xml b/group15/1501_2535894075/2017code/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..d60b234c2b --- /dev/null +++ b/group15/1501_2535894075/2017code/src/com/coderising/litestruts/struts.xml @@ -0,0 +1,21 @@ + + + + + + + /jsp/homepage.jsp + + /jsp/showLogin.jsp + + + + + + /jsp/welcome.jsp + + /jsp/error.jsp + + + + \ No newline at end of file diff --git a/group20/423184723/src/com/coding/basic/ArrayList.java b/group15/1501_2535894075/2017code/src/com/coding/basic/ArrayList.java similarity index 100% rename from group20/423184723/src/com/coding/basic/ArrayList.java rename to group15/1501_2535894075/2017code/src/com/coding/basic/ArrayList.java diff --git a/group20/423184723/src/com/coding/basic/BinaryTreeNode.java b/group15/1501_2535894075/2017code/src/com/coding/basic/BinaryTreeNode.java similarity index 100% rename from group20/423184723/src/com/coding/basic/BinaryTreeNode.java rename to group15/1501_2535894075/2017code/src/com/coding/basic/BinaryTreeNode.java diff --git a/group15/1514_616019420/Iterator.java b/group15/1501_2535894075/2017code/src/com/coding/basic/Iterator.java similarity index 100% rename from group15/1514_616019420/Iterator.java rename to group15/1501_2535894075/2017code/src/com/coding/basic/Iterator.java diff --git a/group20/423184723/src/com/coding/basic/LinkedList.java b/group15/1501_2535894075/2017code/src/com/coding/basic/LinkedList.java similarity index 100% rename from group20/423184723/src/com/coding/basic/LinkedList.java rename to group15/1501_2535894075/2017code/src/com/coding/basic/LinkedList.java diff --git a/group15/1514_616019420/List.java b/group15/1501_2535894075/2017code/src/com/coding/basic/List.java similarity index 100% rename from group15/1514_616019420/List.java rename to group15/1501_2535894075/2017code/src/com/coding/basic/List.java diff --git a/group20/423184723/src/com/coding/basic/Queue.java b/group15/1501_2535894075/2017code/src/com/coding/basic/Queue.java similarity index 100% rename from group20/423184723/src/com/coding/basic/Queue.java rename to group15/1501_2535894075/2017code/src/com/coding/basic/Queue.java diff --git a/group20/423184723/src/com/coding/basic/Stack.java b/group15/1501_2535894075/2017code/src/com/coding/basic/Stack.java similarity index 100% rename from group20/423184723/src/com/coding/basic/Stack.java rename to group15/1501_2535894075/2017code/src/com/coding/basic/Stack.java diff --git a/group15/1501_2535894075/2017code/src/linkedlist.java b/group15/1501_2535894075/2017code/src/linkedlist.java deleted file mode 100644 index 04a99762e5..0000000000 --- a/group15/1501_2535894075/2017code/src/linkedlist.java +++ /dev/null @@ -1,194 +0,0 @@ -package firstweek; - - -import java.util.NoSuchElementException; - -public class linkedlist { - private int size=0; - - private node first; - - private node next; - - private node last; - - public linkedlist(){ - - } - - private void linkFirst(E e){ - final node f=first; - final node newnode=new node(null,e,f); - first=newnode; - if(f==null){ - last=f; - }else{ - f.pre=newnode; - } - size++; - } - - void linkLast(E e){ - final node l=last; - final node newnode=new node(l,e,null); - last=newnode; - if(last==null){ - last=l; - }else{ - l.next=newnode; - } - } - - void linkbefore(E e,node nodes ){ - final node pre=nodes.pre; - final node newnode=new node<>(pre,e,nodes); - nodes.pre=newnode; - if(pre==null){ - first=newnode; - }else{ - pre.next=newnode; - } - size++; - } - - private E unlinkFirst(node f){ - //删除第一个非空节点 确定f为第一个节点并非空 - final E item=f.item; - final node next=f.next; - f.item=null; - f.next=null;//这样是为了gc来回收 - first=next; - if(next==null){ - last=null; - }else{ - next.pre=null; - } - size--; - return item; - } - private E unlinkLast(node l){ - //删除最后一个非空节点 确定l为最后节点且非空 - final E item=l.item; - final node pre=l.pre; - l.item=null; - l.pre=null; - last=pre; - if(pre==null){ - first=null; - }else{ - pre.next=null; - } - size--; - return item; - } - - E unlinekd(node e){ - final node pre=e.pre; - final node next=e.next; - if(size==0){ - System.out.println("链表为空 无需删除"); - return e.item; - } - if(e==last){ - if(size==1){ - last=null; - first=null; - size--; - return e.item; - } - last=pre; - pre.next=null; - size--; - return e.item; - } - if(e==first){ - if(size==1){ - first=null; - last=null; - size--; - return e.item; - } - size--; - first=next; - next.pre=null; - return e.item; - } - pre.next=next; - next.pre=pre; - size--; - return e.item; - } - - public E getFirst(){ - final node f=first; - if(f==null){ - throw new NoSuchElementException(); - } - return f.item; - } - - public E getLast(){ - final node l=last; - if(l==null){ - throw new NoSuchElementException(); - } - return l.item; - } - public E removeFirst(){ - final node f=first; - if(f==null){ - throw new NoSuchElementException(); - } - return unlinkFirst(f); - } - public E removeLast(){ - final node l=last; - if(l==null){ - throw new NoSuchElementException(); - } - return unlinkLast(l); - } - public void addLast(E e){ - linkLast(e); - } - public void addFirst(E e){ - linkFirst(e); - } - public int size(){ - return size; - } - public boolean add(E e){ - linkLast(e); - return true; - } - public int indexof(Object o){ - int index=0; - if(o==null){ - for(node x=first;x!=null;x=x.next){ - if(x.item==null){ - return index; - } - index++; - } - }else{ - for(node x=first;x!=null;x=x.next){ - if(o.equals(x.item)){ - return index; - } - index++; - } - } - return -1; - } -} - -class node{ - E item; - node pre; - node next; - node(node pre,E item,node next){ - this.pre=pre; - this.next=next; - this.item=item; - } -} diff --git a/group15/1501_2535894075/2017code/src/queue.java b/group15/1501_2535894075/2017code/src/queue.java deleted file mode 100644 index 8a9646a2ea..0000000000 --- a/group15/1501_2535894075/2017code/src/queue.java +++ /dev/null @@ -1,115 +0,0 @@ -package firstweek; -import java.util.AbstractList; -import java.util.Iterator; -import java.util.List; - -public class queue extends AbstractList implements List ,java.io.Serializable { - - private static final long serialVersionUID = 6203363761107460505L; - - // 指向队头 - private transient Entry front; - - private transient int size ; - // 指向队尾 - private transient Entry rear; - - public Iterator singleListIterator() { - return new QueueIterator(); - } - - public queue() { - this.front = this.rear = null; - } - - @Override - public boolean add(E e) { - Entry newData = new Entry(e, null); - if (this.rear == null) { - this.rear = newData; - this.front = this.rear; - } else { - Entry preElement = this.front; - while (preElement.next != null) { - preElement = preElement.next; - } - preElement.next = newData; - } - size ++; - return true; - } - - /** - * 队尾加入元素 - * @param e - * @return - */ - public boolean append(E e) { - return add(e); - } - - /** - * 取队头数据 - */ - @Override - public E get(int index) { - return this.front.element; - } - - public E getFrist() { - return get(0); - } - - /** - * 出队列 - * @return - */ - public E delete() { - E result = null; - Entry entry = this.front; - if (entry != null) { - result = entry.element; - this.front = entry.next; - entry = null; - } - size --; - return result; - } - - /** - * 队长度 - */ - @Override - public int size() { - return size; - } - - private static class Entry { - E element; - Entry next; - public Entry(E element, Entry next) { - this.element = element; - this.next = next; - } - } - - private class QueueIterator implements Iterator { - private Entry itFront = front; - @Override - public boolean hasNext() { - return itFront != null; - } - - @Override - public E next() { - E result = itFront.element ; - itFront = itFront.next; - return result; - } - - @Override - public void remove() { - throw new UnsupportedOperationException("can not remove"); - } - } -} \ No newline at end of file diff --git a/group15/1501_2535894075/2017code/src/stack.java b/group15/1501_2535894075/2017code/src/stack.java deleted file mode 100644 index 64cf55168b..0000000000 --- a/group15/1501_2535894075/2017code/src/stack.java +++ /dev/null @@ -1,71 +0,0 @@ -package firstweek; - -import java.util.Arrays; - -public class stack { - //Define capacity constant:CAPACITY - private static final int CAPACITY = 1024; - //Define capacity - private static int capacity; - //Define the top position of stack - //top = -1 meaning that the stack empty - private static int top = -1; - //Basic Object class array - Object[] array; - //Initialize the capacity of stack - public stack() { - this.capacity = CAPACITY; - array = new Object[capacity]; - } - - //Get the size of stack - public int getSize(){ - if(isEmpty()){ - return 0; - }else{ - return top + 1; - } - } - - //Get whether stack is empty - public boolean isEmpty(){ - return (top < 0); - } - - //Get the top element of stack - public Object top() { - - if(isEmpty()){ - System.out.println("Stack is empty"); - } - return array[top]; - - } - - //Push element to stack - public void push(Object element) throws Exception{ - if(getSize()== CAPACITY){ - throw new Exception("Stack is full"); - } - array[++ top] = element; - } - - //Pop element from stack - public Object pop() { - if(isEmpty()){ - System.out.println("Stack is empty"); - } - return array[top --]; - } - - - public String getAllElements() { - String[] arr = new String[top + 1]; - if(!isEmpty()){ - for(int i = 0;i < getSize();i ++){ - arr[i] = (String)array[i]; - } - } - return Arrays.toString(arr); - } -} 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 = [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鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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; + //鏈夌浉鍚岄」涓簍rue锛屾病鏈変负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; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪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 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬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; + + } + + /** + * 鐢╯eperator 鎶婃暟缁 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; + } + + //鐢熸垚鏂愭尝閭e鏁板垪 + 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;// 鑳借鏁撮櫎鐨勯櫎鏁板垯琚姞鍒皌emp涓 + } + } + 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, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + 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); + //鍒ゆ柇杩斿洖鐨刯sp + List elementresult = thiselement.elements(); + for (Element element:elementresult){ + if (element.attribute("name").getValue().equals(result)) { + view.setJsp(element.getStringValue()); + break; + } + } + // 鑾峰緱getter鏂规硶,骞惰缃甈arameters + 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 diff --git a/group15/1503_1311822904/week2/src/ArrayUtil.java b/group15/1503_1311822904/week2/src/ArrayUtil.java new file mode 100644 index 0000000000..5bd2784183 --- /dev/null +++ b/group15/1503_1311822904/week2/src/ArrayUtil.java @@ -0,0 +1,216 @@ +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static void reverseArray(int[] origin){ + int length=origin.length; + int[] reverse=new int[length]; + int i=0; + for(;i-1;i--){ + origin[i]=reverse[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 static int[] removeZero(int[] oldArray){ + int length=oldArray.length; + int[] index=new int[length]; + int i=0; + int j=0; + for(;i= 0 && arr[j] > temp; j --){ + arr[j+1] = arr[j]; + } + arr[j+1] = temp; + } + } + //鍘婚噸鍚 + int j=0; + for(int i = 1;i parameters) { + SAXReader reader = new SAXReader(); + View view=new View(); + try { + File f= new File("src/struts.xml");//E:\mygit\coding2017\liuxin\src\com\coderising\litestruts\struts.xml + Document e = reader.read(f); + //寰楀埌鏍硅妭鐐 + Element struts = e.getRootElement(); + Iterator it = struts.elementIterator(); + + while(it.hasNext()) {//action閬嶅巻 + Element action = (Element)it.next(); + // 鎵惧埌 name=actionName 鐨刟ction + List actionAtr = action.attributes(); + if(!actionAtr.get(0).getValue().equals(actionName)) + continue; + String className= actionAtr.get(1).getValue(); + //寰楀埌actionName 瀵瑰簲鐨刢lass + Class aClass=Class.forName(className); + Object o=aClass.newInstance(); + Field field; + for(Map.Entry entry:parameters.entrySet()){ + field = aClass.getDeclaredField( entry.getKey()); + field.setAccessible(true); + field.set(o, entry.getValue()); + } + + Method m = aClass.getMethod("execute"); + String resultString=(String)m.invoke(o); + Method m2 = aClass.getMethod("getMessage"); + String message=(String)m2.invoke(o); + + Map map=new HashMap<>(); + + map.put("message",message); + view.setParameters(map); + it=action.elementIterator(); + //閬嶅巻 + while (it.hasNext()){ + Element result = (Element)it.next(); + String s= result.attribute(0).getValue(); + if(resultString.equals(s)){ + view.setJsp(result.getStringValue()); + return view; + } + + } + + //Element results = (Element)it.next(); + //List results = action.attributes(); + + } + } catch (DocumentException var9) { + var9.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + return null; + } + +} diff --git a/group15/1503_1311822904/week2/test/ArrayUtilTest.java b/group15/1503_1311822904/week2/test/ArrayUtilTest.java new file mode 100644 index 0000000000..8ba10c75f7 --- /dev/null +++ b/group15/1503_1311822904/week2/test/ArrayUtilTest.java @@ -0,0 +1,114 @@ +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +/** +* ArrayUtil Tester. +* +* @author +* @since
锟斤拷锟斤拷 1, 2017
+* @version 1.0 +*/ +public class ArrayUtilTest { + int[] a={0,1,2,3,4,5,6,7,8,9}; +@Before +public void before() throws Exception { +} + +@After +public void after() throws Exception { +} + +/** +* +* Method: reverseArray(int[] origin) +* +*/ +@Test +public void testReverseArray() throws Exception { + + ArrayUtil.reverseArray(a); + System.out.println(Arrays.toString(a)); + +} + +/** +* +* Method: removeZero(int[] oldArray) +* +*/ +@Test +public void testRemoveZero() throws Exception { + int[] a={0,1,0,3,4,0,0,7,8,0,10}; + System.out.println(Arrays.toString(ArrayUtil.removeZero(a))); +} + +/** +* +* Method: merge(int[] array1, int[] array2) +* +*/ +@Test +public void testMerge() throws Exception { + int[] a={0,1,0,3,4,-90,0,0,7,8,0,10}; + int[] b={-90,0,10}; + System.out.println(Arrays.toString(ArrayUtil.merge(a,b))); + +//TODO: Test goes here... +} + +/** +* +* Method: grow(int [] oldArray, int size) +* +*/ +@Test +public void testGrow() throws Exception { + int[] b= ArrayUtil.grow(a,4); + System.out.println(Arrays.toString(b)); +} + +/** +* +* Method: fibonacci(int max) +* +*/ +@Test +public void testFibonacci() throws Exception { + System.out.println(Arrays.toString(ArrayUtil.fibonacci(2))); +} + +/** +* +* Method: getPrimes(int max) +* +*/ +@Test +public void testGetPrimes() throws Exception { + System.out.println(Arrays.toString(ArrayUtil.getPrimes(99))); +} + +/** +* +* Method: getPerfectNumbers(int max) +* +*/ +@Test +public void testGetPerfectNumbers() throws Exception { + System.out.println(Arrays.toString(ArrayUtil.getPerfectNumbers(99))); +} + +/** +* +* Method: join(int[] array, String seperator) +* +*/ +@Test +public void testJoin() throws Exception { + System.out.println(ArrayUtil.join(a,"@")); +} + + +} diff --git a/group15/1506_1084478979/1506_1084478979/.classpath b/group15/1506_1084478979/1506_1084478979/.classpath index fceb4801b5..373dce4005 100644 --- a/group15/1506_1084478979/1506_1084478979/.classpath +++ b/group15/1506_1084478979/1506_1084478979/.classpath @@ -2,5 +2,6 @@ + diff --git a/group15/1506_1084478979/1506_1084478979/.settings/org.eclipse.core.resources.prefs b/group15/1506_1084478979/1506_1084478979/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..ac8e1d1ea4 --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/banshee/WorkTwo=GBK +encoding//src/banshee/WorkTwo/ArrayUtil.java=UTF-8 +encoding/=UTF-8 diff --git a/group15/1506_1084478979/1506_1084478979/RemoteSystemsTempFiles/.project b/group15/1506_1084478979/1506_1084478979/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group15/1506_1084478979/1506_1084478979/src/RemoteSystemsTempFiles/.project b/group15/1506_1084478979/1506_1084478979/src/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/src/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group15/1506_1084478979/1506_1084478979/src/banshee/ArrayList.java b/group15/1506_1084478979/1506_1084478979/src/banshee/ArrayList.java index d3afe067a7..5cc79a3457 100644 --- a/group15/1506_1084478979/1506_1084478979/src/banshee/ArrayList.java +++ b/group15/1506_1084478979/1506_1084478979/src/banshee/ArrayList.java @@ -40,16 +40,16 @@ public int size(){ return -1; } - public Iterator iterator(){ - //TODO - //不会。。。 - return null; - } +// public Iterator iterator(){ +// //TODO +// //锟斤拷锟结。锟斤拷锟斤拷 +// return null; +// } private void rangeCheck( int index) { if (index >= size || index < 0){ - throw new IndexOutOfBoundsException("指定的index超过界限"); + throw new IndexOutOfBoundsException("指锟斤拷锟斤拷index锟斤拷锟斤拷锟斤拷锟斤拷"); } } @@ -57,7 +57,7 @@ private void rangeCheck( int index) { public void ensureCapacity(int minCapacity) { int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { - //计算新的容量大小,为当前容量的1.5倍 + //锟斤拷锟斤拷锟铰碉拷锟斤拷锟斤拷锟斤拷小锟斤拷为锟斤拷前锟斤拷锟斤拷锟斤拷1.5锟斤拷 int newCapacity = (oldCapacity * 3) / 2 + 1; if (newCapacity < minCapacity) newCapacity = minCapacity; diff --git a/group15/1506_1084478979/1506_1084478979/src/banshee/LinkedList.java b/group15/1506_1084478979/1506_1084478979/src/banshee/LinkedList.java index f5fe5b5a31..9462e7fbae 100644 --- a/group15/1506_1084478979/1506_1084478979/src/banshee/LinkedList.java +++ b/group15/1506_1084478979/1506_1084478979/src/banshee/LinkedList.java @@ -2,7 +2,8 @@ import java.util.NoSuchElementException; -public class LinkedList implements List { + +public class LinkedList { private Node head; private Node last; @@ -74,11 +75,11 @@ public Object removeLast(){ size--; return e; } - public Iterator iterator(){ - //TODO - //不会... - return null; - } +// public Iterator iterator(){ +// //TODO +// //锟斤拷锟斤拷... +// return null; +// } private static class Node{ @@ -119,7 +120,7 @@ private void linkBrfore(Object element , Node spNode ){ private void rangeCheck(int index) { if (index > size || index < 0) { - throw new IndexOutOfBoundsException("指定的index超过界限"); + throw new IndexOutOfBoundsException("指锟斤拷锟斤拷index锟斤拷锟斤拷锟斤拷锟斤拷"); } } diff --git a/group15/1506_1084478979/1506_1084478979/src/banshee/RemoteSystemsTempFiles/.project b/group15/1506_1084478979/1506_1084478979/src/banshee/RemoteSystemsTempFiles/.project new file mode 100644 index 0000000000..5447a64fa9 --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/src/banshee/RemoteSystemsTempFiles/.project @@ -0,0 +1,12 @@ + + + RemoteSystemsTempFiles + + + + + + + org.eclipse.rse.ui.remoteSystemsTempNature + + diff --git a/group15/1506_1084478979/1506_1084478979/src/banshee/WorkTwo/ArrayUtil.java b/group15/1506_1084478979/1506_1084478979/src/banshee/WorkTwo/ArrayUtil.java new file mode 100644 index 0000000000..3bac4a642c --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/src/banshee/WorkTwo/ArrayUtil.java @@ -0,0 +1,165 @@ +package banshee.WorkTwo; + +import java.util.Arrays; +import java.util.ArrayList; + + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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 j = origin.length - 1; + int temp; + for(int i = 0; i < origin.length /2; i++,j--){ + temp = origin[j]; + origin[j] = origin[i]; + origin[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[] newArr = new int[oldArray.length]; + int j = 0; + for (int i = 0; i < newArr.length; i++) { + if (oldArray[i] != 0){ + newArr[j] = oldArray[i]; + j++; + } + } + + return Arrays.copyOfRange(newArr, 0, j) ; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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 i = 0,j = 0, k = 0; + int[] array3 = new int[array1.length + array2.length]; + + while (i < array1.length && j < array2.length) { + if (array1[i] < array2[j]) { + array3[k++] = array1[i++]; + } else if (array1[i] > array2[j]) { + array3[k++] = array2[j++]; + }else { + array3[k++] = array1[i++]; + j++; + } + } + while (i < array1.length) { + array3[k++] = array1[i++]; + } + while (j < array2.length) { + array3[k++] = array2[j++]; + } + return Arrays.copyOf(array3, k); + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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 newCapacity = oldArray.length + size; + int[] newArr = Arrays.copyOf(oldArray, newCapacity); + return newArr; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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){ + ArrayList list = new ArrayList(); + if (max == 1) { + return new int[0]; + }else{ + list.add(1); + list.add(1); + int temp = 0, a = 1,b = 1; + while (a + b < max) { + list.add(a + b); + temp = a; + a = b; + b = temp + b; + } + } + int[] arr = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + + arr[i] = (Integer) list.get(i); + + } + return arr; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for (int element : array) { + sb.append(element).append(seperator); + } + sb.setLength(sb.length() - 1); + return sb.toString(); + } + + + + +} diff --git a/group15/1506_1084478979/1506_1084478979/src/banshee/WorkTwo/ArrayUtilTest.java b/group15/1506_1084478979/1506_1084478979/src/banshee/WorkTwo/ArrayUtilTest.java new file mode 100644 index 0000000000..4b0c7cd328 --- /dev/null +++ b/group15/1506_1084478979/1506_1084478979/src/banshee/WorkTwo/ArrayUtilTest.java @@ -0,0 +1,72 @@ +package banshee.WorkTwo; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ArrayUtilTest { + + private ArrayUtil util; + + @Before + public void setUp() throws Exception { + util = new ArrayUtil(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReverseArray() throws Exception { + int[] origin1 = {7, 9 , 30, 3}; + int[] origin2 = {7, 9, 30, 3, 4}; + util.reverseArray(origin1); + util.reverseArray(origin2); + assertArrayEquals(new int[]{3, 30, 9,7}, origin1); + assertArrayEquals(new int[]{4,3, 30 , 9,7}, origin2); + } + + @Test + public void testRemoveZero() throws Exception { + int oldArr[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + assertArrayEquals(new int[]{1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}, util.removeZero(oldArr)); + } + + @Test + public void testMerge() throws Exception{ + int[] arr1 = new int[]{3, 5, 7, 8}; + int[] arr2 = new int[]{4, 5, 6, 7}; + assertArrayEquals(new int[]{3, 4, 5, 6, 7, 8}, util.merge(arr1, arr2)); + } + + @Test + public void testGrow() throws Exception{ + int[] old = new int[]{2, 3, 6}; + assertArrayEquals(util.grow(old, 3), new int[]{2, 3, 6, 0, 0, 0}); + } + + @Test + public void testFibonacci() throws Exception { + assertArrayEquals(new int[]{1, 1, 2, 3, 5, 8, 13}, util.fibonacci(15)); + } + + @Test + public void testGetPrimes() { + fail("Not yet implemented"); + } + + @Test + public void testGetPerfectNumbers() { + fail("Not yet implemented"); + } + + @Test + public void testJoin() throws Exception { + int[] arr = new int[]{3, 8, 9}; + assertEquals("3-8-9", util.join(arr, "-")); + } + +} diff --git a/group15/1507_977996067/src/task2/README.md b/group15/1507_977996067/src/task2/README.md new file mode 100644 index 0000000000..8c99b55977 --- /dev/null +++ b/group15/1507_977996067/src/task2/README.md @@ -0,0 +1,3 @@ +## 3.5 浣滀笟鏂囦欢澶 +1. ArrayUtil +2. struts \ No newline at end of file diff --git a/group15/1507_977996067/src/task2/array/ArrayUtil.java b/group15/1507_977996067/src/task2/array/ArrayUtil.java new file mode 100644 index 0000000000..21cb4286c7 --- /dev/null +++ b/group15/1507_977996067/src/task2/array/ArrayUtil.java @@ -0,0 +1,215 @@ +package task2.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 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 length = origin.length; + int[] temp = new int[length]; + for (int i = 0; i < length; i++) { + temp[i] = origin[length - i - 1]; + } + System.arraycopy(temp, 0, origin, 0, length); + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 length = oldArray.length; + for (int i = 0; i < length; i++) { + int element = oldArray[i]; + if (element != 0) { + oldArray[count] = element; + count++; + } + } + return Arrays.copyOf(oldArray, count); + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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 firstPos = 0; + int secondPos = 0; + int firstLength = array1.length; + int secondLength = array2.length; + int[] tempArray = new int[firstLength + secondLength]; + int tempArrayPos = 0; +/* 姣旇緝涓や釜鏁扮粍绗竴涓厓绱犵殑澶у皬,灏忕殑鍏冪礌鏀惧埌tempArray涓笖鏁扮粍绱㈠紩鍔犱竴,鐒跺悗缁х画姣旇緝. + 鐩村埌鏈変竴涓暟缁勯亶鍘嗗畬,鎶婂彟澶栦竴涓暟缁勫墿涓嬬殑鍏冪礌渚濇鏀惧埌tempArray涓*/ + while (firstPos < firstLength && secondPos < secondLength) { + int firstElement = array1[firstPos]; + int secondElement = array2[secondPos]; + if (firstElement < secondElement) { + tempArray[tempArrayPos++] = array1[firstPos++]; + } else if (firstElement > secondElement) { + tempArray[tempArrayPos++] = array2[secondPos++]; + } else { + tempArray[tempArrayPos++] = array1[firstPos++]; + secondPos++; + } + } + if (firstPos == firstLength && secondPos < secondLength) { + System.arraycopy(array2, secondPos, tempArray, tempArrayPos, secondLength - secondPos); + tempArrayPos += secondLength - secondPos; + } else if (secondPos == secondLength && firstPos < firstLength) { + System.arraycopy(array1, firstPos, tempArray, tempArrayPos, firstLength - firstPos); + tempArrayPos += firstLength - firstPos; + } + return Arrays.copyOf(tempArray, tempArrayPos); + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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[] temp = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, temp, 0, oldArray.length); + return temp; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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 = new int[max + 1]; + result[0] = 1; + result[1] = 1; + return getFibonacci(result, 2, max); + } + + private int[] getFibonacci(int[] arr, int index, int max) { + if (arr[index - 1] >= max) { + return Arrays.copyOf(arr, index - 1); + } + arr[index] = arr[index - 1] + arr[index - 2]; + return getFibonacci(arr, ++index, max); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + if (max == 2) + return new int[]{2}; + else if (max >= 3 && max <= 4) + return new int[]{2, 3}; + else if (max > 4) { + int[] arr = new int[max]; + arr[0] = 2; + arr[1] = 3; + int pos = 2; + for (int i = 5; i < max; i++) { + //鍘绘帀鑳借2銆3鏁撮櫎鐨勬暟:6n+2 6n+3 6n+4 + if ((i + 1) % 6 == 0 || (i - 1) % 6 == 0) { + if (isPrime(i)) { + arr[pos++] = i; + } + } + } + return Arrays.copyOf(arr, pos); + } + return null; + } + + private boolean isPrime(int number) { + //鍘绘帀鍋舵暟 + if (number % 2 == 0) + return false; + //鍙兘琚嚜韬暣闄ょ殑涓虹礌鏁 + for (int i = 2; i < number; i++) { + if (number % i == 0) { + return false; + } + } + return true; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int[] arr = new int[max]; + int pos = 0; + for (int i = 4; i < max; i++) { + //鍘绘帀绱犳暟 + if (!isPrime(i)) { + List list = new ArrayList<>(); + for (int j = 1; j < i; j++) { + if (i % j == 0) + list.add(j); + } + if (i == list.stream().reduce(0, (a, b) -> a + b)) + arr[pos++] = i; + } + } + return Arrays.copyOf(arr, pos); + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param separator + * @return + */ + public String join(int[] array, String separator) { + StringBuilder sb = new StringBuilder(); + for (int element : array) { + sb.append(element).append(separator); + } + sb.setLength(sb.length() - 1); + return sb.toString(); + } + +} \ No newline at end of file diff --git a/group15/1507_977996067/src/task2/array/ArrayUtilTest.java b/group15/1507_977996067/src/task2/array/ArrayUtilTest.java new file mode 100644 index 0000000000..8a38dd59ce --- /dev/null +++ b/group15/1507_977996067/src/task2/array/ArrayUtilTest.java @@ -0,0 +1,75 @@ +package task2.array; + +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.*; + +public class ArrayUtilTest { + + private ArrayUtil util; + + @Before + public void pre() { + util = new ArrayUtil(); + } + + @Test + public void reverseArray() throws Exception { + int[] i = {7, 9, 40, 30, 3}; + int[] ii = {7, 9, 40, 30, 3, 11}; + util.reverseArray(i); + util.reverseArray(ii); + assertArrayEquals(new int[]{3, 30, 40, 9, 7}, i); + assertArrayEquals(new int[]{11, 3, 30, 40, 9, 7}, ii); + } + + @Test + public void removeZero() throws Exception { + int oldArr[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + assertArrayEquals(new int[]{1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5}, util.removeZero(oldArr)); + } + + @Test + public void merge() throws Exception { + int[] arr1 = new int[]{3, 5, 7, 8}; + int[] arr2 = new int[]{4, 5, 6, 7}; + assertArrayEquals(new int[]{3, 4, 5, 6, 7, 8}, util.merge(arr1, arr2)); + } + + @Test + public void grow() throws Exception { + int[] old = new int[]{2, 3, 6}; + assertArrayEquals(util.grow(old, 3), new int[]{2, 3, 6, 0, 0, 0}); + } + + @Test + public void fibonacci() throws Exception { + assertArrayEquals(new int[]{1, 1, 2, 3, 5, 8, 13}, util.fibonacci(15)); + } + + @Test + public void getPrimes() throws Exception { + int[] primes = util.getPrimes(33); + assertArrayEquals(new int[]{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}, primes); + } + + @Test + public void getPerfectNumbers() throws Exception { + assertArrayEquals(new int[]{6,28,496,8128}, util.getPerfectNumbers(10000)); + } + + @Test + public void join() throws Exception { + int[] arr = new int[]{3, 8, 9}; + assertEquals("3-8-9", util.join(arr, "-")); + } + + private void printArray(int[] arr) { + Arrays.stream(arr).forEach(System.out::println); + } + + +} \ No newline at end of file diff --git a/group15/1507_977996067/src/task2/litestruts/LogOutAction.java b/group15/1507_977996067/src/task2/litestruts/LogOutAction.java new file mode 100644 index 0000000000..b62d25ecc9 --- /dev/null +++ b/group15/1507_977996067/src/task2/litestruts/LogOutAction.java @@ -0,0 +1,34 @@ +package task2.litestruts; + +public class LogOutAction { + 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 = "logout successful"; + return "success"; + } + this.message = "logout failed,please check your user/pwd"; + return "error"; + } + + 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/group15/1507_977996067/src/task2/litestruts/LoginAction.java b/group15/1507_977996067/src/task2/litestruts/LoginAction.java new file mode 100644 index 0000000000..abde57860a --- /dev/null +++ b/group15/1507_977996067/src/task2/litestruts/LoginAction.java @@ -0,0 +1,34 @@ +package task2.litestruts; + +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/group15/1507_977996067/src/task2/litestruts/Struts.java b/group15/1507_977996067/src/task2/litestruts/Struts.java new file mode 100644 index 0000000000..c2c8a61c3a --- /dev/null +++ b/group15/1507_977996067/src/task2/litestruts/Struts.java @@ -0,0 +1,80 @@ +package task2.litestruts; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + +// 0. 璇诲彇閰嶇疆鏂囦欢struts.xml,寮曠敤Jsoup + Document document = null; + try { + document = Jsoup.connect("http://my.977996067.cn/2017_2/struts.xml").get(); + } catch (IOException e) { + System.err.println("xml瑙f瀽澶辫触"); + } +// 鑾峰彇鎵鏈夌殑action鏍囩 + Elements actions = document != null ? document.select("action") : null; + if (actions == null) + return null; + for (Element actionElement : actions) { + String name = actionElement.attr("name"); +// 鍙嶅皠action + if (actionName.equals(name)) { + try { + Class actionClass = Class.forName(actionElement.attr("class")); + Object o = actionClass.getConstructor().newInstance(); + parameters.forEach((k, v) -> { + try { + Field field = actionClass.getDeclaredField(k); + field.setAccessible(true); + //璧嬪 + field.set(o, v); + } catch (Exception e) { + e.printStackTrace(); + } + }); +// 鎵цexecute鏂规硶,鑾峰彇杩斿洖鍊 + Method method = actionClass.getMethod("execute"); + String returnValue = (String) method.invoke(o); + View view = new View(); + Map map = new HashMap(); + Arrays.stream(actionClass.getMethods()) + .forEach(((Method m) -> { + try { + String methodName = m.getName(); + if (methodName.startsWith("get")) + map.put(methodName.substring(methodName.indexOf("get") + 3).toLowerCase(), m.invoke(o)); + } catch (Exception e) { + e.printStackTrace(); + } + })); + view.setParameters(map); +// 鑾峰彇杩斿洖瑙嗗浘鍚 + Elements children = actionElement.children().select("result"); + for (Element aChildren : children) { + if (returnValue.equals(aChildren.attr("name"))) { + view.setJsp(aChildren.text()); + } + } + System.out.println(view); + return view; + } catch (Exception e) { + e.printStackTrace(); + } + } + } + return null; + } +} diff --git a/group15/1507_977996067/src/task2/litestruts/StrutsTest.java b/group15/1507_977996067/src/task2/litestruts/StrutsTest.java new file mode 100644 index 0000000000..ec7d992cf2 --- /dev/null +++ b/group15/1507_977996067/src/task2/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +package task2.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/group15/1507_977996067/src/task2/litestruts/View.java b/group15/1507_977996067/src/task2/litestruts/View.java new file mode 100644 index 0000000000..667068ef37 --- /dev/null +++ b/group15/1507_977996067/src/task2/litestruts/View.java @@ -0,0 +1,33 @@ +package task2.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; + } + + @Override + public String toString() { + return "View{" + "jsp='" + jsp + '\'' + + ", parameters=" + parameters + + '}'; + } +} \ No newline at end of file diff --git a/group15/1507_977996067/src/task2/litestruts/struts.xml b/group15/1507_977996067/src/task2/litestruts/struts.xml new file mode 100644 index 0000000000..8716d96c5a --- /dev/null +++ b/group15/1507_977996067/src/task2/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/group15/1510_739253131/src/com/bruce/homework0305/array/ArrayUtil.java b/group15/1510_739253131/src/com/bruce/homework0305/array/ArrayUtil.java new file mode 100644 index 0000000000..85276ce4da --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/array/ArrayUtil.java @@ -0,0 +1,232 @@ +package com.bruce.homework0305.array; + +import com.bruce.homework0226.LinkedListV00; + +import java.util.*; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public int[] reverseArray(int[] origin){ + if(origin != null && origin.length > 0) { + int temp; + for (int i = 0; i < origin.length / 2; i++) { + temp = origin[i]; + origin[i] = origin[origin.length-1 - i]; + origin[origin.length-1 - i] = temp; + } + } + return origin; + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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[] result = null; + if(oldArray != null && oldArray.length >= 0){ + int index = 0; + LinkedList linkedList = new LinkedList(); + for(int i = 0;i list = new LinkedList<>(); + getElementFromArray(array1,list); + getElementFromArray(array2,list); + Collections.sort(list); + int[] result = new int[list.size()]; + for(int n = 0;n Integer.MAX_VALUE) { + return result;//鎶涘嚭寮傚父 + } else if (max == 1) { + result = new int[0]; + } else { + ArrayList list = new ArrayList(); + list.add(1); + list.add(1); + for (int i = 2;i max){ + break; + } + list.add(i,(list.get(i-2)+list.get(i-1))); + } + result = new int[list.size()]; + for (int j=0;j Integer.MAX_VALUE) { + return result;//鎶涘嚭寮傚父 + } else if(max == 1){ + result = new int[0]; + }else { + ArrayList list = new ArrayList<>(); + for (int i=2 ; i<=max ; i++) { + if(isPrimes(i)){ + list.add(i); + } + } + result = new int[list.size()]; + for(int m = 0 ; m Integer.MAX_VALUE){ + return result; + } else { + LinkedList list = new LinkedList<>(); + for(int i = 1 ; i < max ; i++){ + if (isPerfectNumber(i)) { + list.add(i); + } + } + result = new int[list.size()]; + for (int i = 0 ; i < list.size() ; i++) { + result[i] = list.get(i); + } + } + return result; + } + + //鍒ゆ柇涓涓暟鏄惁鏄畬鏁帮紝true锛氭槸瀹屾暟锛沠alse锛氫笉鏄畬鏁般 + public boolean isPerfectNumber(int value){ + int sum = 0; + for(int i = 1 ; i < value ; i++){ + if(value % i == 0){ + sum += i; + } + } + return sum == value; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuffer sb = new StringBuffer(); + if (array != null && array.length >= 1) { + for (int i = 0 ; i < array.length ; i++) { + sb.append(array[i]); + if (i < array.length-1) { + sb.append(seperator); + } + } + } + return sb.toString(); + } + + +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/array/JuintArrayUtil.java b/group15/1510_739253131/src/com/bruce/homework0305/array/JuintArrayUtil.java new file mode 100644 index 0000000000..eb5487e76d --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/array/JuintArrayUtil.java @@ -0,0 +1,84 @@ +package com.bruce.homework0305.array; + +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by Bruce.Jiao on 2017/3/2. + */ +public class JuintArrayUtil extends TestCase { + + @Test + public void testReverse(){ + ArrayUtil au = new ArrayUtil(); + int[] demo0 = {}; + int[] demo1 = {6}; + int[] demo = {7, 9, 30, 3, 4, 6}; + int[] nullArray = au.reverseArray(null); + int[] reverseArray0 = au.reverseArray(demo0); + int[] reverseArray1 = au.reverseArray(demo1); + int[] reverseArray = au.reverseArray(demo); + System.out.println(Arrays.toString(nullArray)); + System.out.println(Arrays.toString(reverseArray0)); + System.out.println(Arrays.toString(reverseArray1)); + System.out.println(Arrays.toString(reverseArray)); + } + + @Test + public void testRemoveZero(){ + ArrayUtil au = new ArrayUtil(); + int[] one = {0}; + int[] many = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + System.out.println(Arrays.toString(au.removeZero(one))); + System.out.println(Arrays.toString(au.removeZero(many))); + System.out.println(Arrays.toString(au.removeZero(null))); + System.out.println(Arrays.toString(au.removeZero(new int[0]))); + } + + @Test + public void testMerge(){ + ArrayUtil au = new ArrayUtil(); + int[] arr1 = {3,4,5,6,7,8,9}; + int[] arr2 = {1,3,5,6,7,9,10,12,13}; + int[] arr3 = null; + int[] arr4 = new int[0]; + System.out.println(Arrays.toString(au.merge(arr1,arr2))); + System.out.println(Arrays.toString(au.merge(arr1,arr3))); + System.out.println(Arrays.toString(au.merge(arr2,arr4))); + } + + @Test + public void testGrow(){ + ArrayUtil au = new ArrayUtil(); + int[] arr = {3,4,5,6,7,8,9}; + System.out.println(Arrays.toString(au.grow(arr,5))); + } + + @Test + public void testFibonacci(){ + ArrayUtil au = new ArrayUtil(); + System.out.println(Arrays.toString(au.fibonacci(15))); + } + + @Test + public void testPrimes(){ + ArrayUtil au = new ArrayUtil(); + System.out.println(Arrays.toString(au.getPrimes(23))); + } + + @Test + public void testPerfectNumbers(){ + ArrayUtil au = new ArrayUtil(); + System.out.println(Arrays.toString(au.getPerfectNumbers(23))); + } + + @Test + public void testJoin(){ + ArrayUtil au = new ArrayUtil(); + int[] array = {1,6,8,8,8,8,8,8,8,8,8,}; + System.out.println(au.join(array,"-")); + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/mystruts/LoginAction.java b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/LoginAction.java new file mode 100644 index 0000000000..4b509b2102 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.bruce.homework0305.mystruts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @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/group15/1510_739253131/src/com/bruce/homework0305/mystruts/Struts.java b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/Struts.java new file mode 100644 index 0000000000..ab54fd0625 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/Struts.java @@ -0,0 +1,81 @@ +package com.bruce.homework0305.mystruts; + +import com.sun.deploy.util.StringUtils; +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.Test; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) throws DocumentException, + ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { + + /* + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + SAXReader reader = new SAXReader(); + Document document = reader.read(new File("src/com/bruce/homework0305/mystruts/struts.xml")); + Element root = document.getRootElement(); + Iterator actions = root.elementIterator("action"); + HashMap map = new HashMap<>(); + View view = new View(); + while(actions.hasNext()){ + Element next = actions.next(); + List attributes = next.attributes(); + Attribute action_name = next.attribute("name"); + if(actionName.equals(action_name.getValue())){ + //鎵惧埌name="login"鐨刟ction,鎷垮埌鍏禼lass璺緞 + Attribute aClass = next.attribute("class"); + //閫氳繃鍙嶅皠鎷垮埌LoginAction绫 + Class clazz = Class.forName(aClass.getValue()); + LoginAction login = (LoginAction) clazz.newInstance(); + //浠巔arameters涓嬁鍒版墍鏈夌殑key锛岄氳繃杩欎簺key鎷垮埌瀵瑰簲鐨勫硷紝骞朵笖浼犲叆LoginAction瀵瑰簲鐨剆etter鏂规硶 + Set keys = parameters.keySet();parameters.entrySet(); + for(String key : keys){ + //棣栧瓧姣嶅ぇ鍐欙紝鎷垮埌setter鏂规硶,骞跺皢parameters涓搴旇key鐨剉alue鎷垮嚭鏉ワ紝鍙嶅皠浼犲叆鐩稿簲鏂规硶 + String setter = "set"+ key.substring(0,1).toUpperCase()+key.substring(1,key.length()); + Method method = clazz.getMethod(setter,String.class); + method.invoke(login,parameters.get(key)); + } + //鍙嶅皠鎷垮埌execute鏂规硶锛屽苟鎷垮埌鎵ц缁撴灉 + Method execute = clazz.getMethod("execute"); + String result = (String) execute.invoke(login); + //鍙嶅皠鎷垮埌getMessage鏂规硶,缁撴灉浠ap鏍煎紡淇濆瓨锛屽苟淇濆瓨鍦╲iew瀵硅薄涓 + Method getMessage = clazz.getMethod("getMessage"); + String message = (String)getMessage.invoke(login); + map.put("message",message); + view.setParameters(map); + //鏍规嵁execute鏂规硶鐨勬墽琛岀粨鏋滐紝鎵惧埌鐩稿叧result鐨刯sp璺緞锛屽皢璇ヨ矾寰勪繚瀛樺湪view瀵硅薄涓 + Iterator results = next.elementIterator("result"); + while(results.hasNext()){ + Element element = results.next(); + if(result.equals(element.attribute("name").getValue())){ + view.setJsp(element.getText()); + break; + } + } + } + } + return view; + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/mystruts/StrutsTest.java b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/StrutsTest.java new file mode 100644 index 0000000000..e54809e4b6 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/StrutsTest.java @@ -0,0 +1,61 @@ +package com.bruce.homework0305.mystruts; + +import org.dom4j.DocumentException; +import org.junit.Assert; +import org.junit.Test; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() { + try { + 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")); + } 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(); + } + } + + @Test + public void testLoginActionFailed() { + try { + 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")); + } 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(); + } + } +} diff --git a/group15/1510_739253131/src/com/bruce/homework0305/mystruts/View.java b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/View.java new file mode 100644 index 0000000000..47abd77a37 --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/View.java @@ -0,0 +1,23 @@ +package com.bruce.homework0305.mystruts; + +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/1510_739253131/src/com/bruce/homework0305/mystruts/struts.xml b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/struts.xml new file mode 100644 index 0000000000..41c7be3d2f --- /dev/null +++ b/group15/1510_739253131/src/com/bruce/homework0305/mystruts/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/group15/1511_714512544/.idea/workspace.xml b/group15/1511_714512544/.idea/workspace.xml index e9a1aea025..cb4932867a 100644 --- a/group15/1511_714512544/.idea/workspace.xml +++ b/group15/1511_714512544/.idea/workspace.xml @@ -1,9 +1,7 @@ - - - + @@ -76,7 +93,7 @@ - + @@ -89,8 +106,9 @@ - + + @@ -116,6 +134,10 @@ @@ -130,11 +152,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + - + - - + + + + - - + + + + + + + + + + + + + - - + - - + - - + - @@ -770,7 +823,28 @@ - + + + + + + + + + + + + + + + + + + + + + + 1487829266287 @@ -828,77 +902,149 @@ - - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + + + + + + + + - - - - - + - - - - - - + + + + + + + + + + + + - - @@ -933,12 +1079,14 @@ - + + @@ -947,29 +1095,6 @@ - - - - - - - - - - - - - - - - - - - - - - - @@ -1012,13 +1137,6 @@ - - - - - - - @@ -1061,6 +1179,71 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1068,143 +1251,231 @@ - + - - + + - + - - - + + - + - - + + - + - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - + - + - - + + - - - - - - - - - - - - + + - + - - - + + - + - - + + + - + - - - + + - + - - - - - - + + - - - + + + + + + + + + - + - - - + + + + + + - + - - - + + + + + + - - + + + + + + + + + + + + + + + + + + diff --git a/group15/1511_714512544/1511_714512544.iml b/group15/1511_714512544/1511_714512544.iml index 3ab15b1867..1868349e70 100644 --- a/group15/1511_714512544/1511_714512544.iml +++ b/group15/1511_714512544/1511_714512544.iml @@ -10,7 +10,16 @@ - + + + + + + + + + + diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coderising/array/ArrayUtil.class b/group15/1511_714512544/out/production/1511_714512544/com/coderising/array/ArrayUtil.class new file mode 100644 index 0000000000..e84a8b4b40 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coderising/array/ArrayUtil.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coderising/array/ArrayUtilTest.class b/group15/1511_714512544/out/production/1511_714512544/com/coderising/array/ArrayUtilTest.class new file mode 100644 index 0000000000..f7b0cd1b22 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coderising/array/ArrayUtilTest.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/LoginAction.class b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/LoginAction.class new file mode 100644 index 0000000000..d781f45ce9 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/LoginAction.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/Struts.class b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/Struts.class new file mode 100644 index 0000000000..02b3ad4da2 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/Struts.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/StrutsTest.class b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/StrutsTest.class new file mode 100644 index 0000000000..ab32147b2a Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/StrutsTest.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/View.class b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/View.class new file mode 100644 index 0000000000..e7a5112144 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/View.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/struts.xml b/group15/1511_714512544/out/production/1511_714512544/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..07f80b6476 --- /dev/null +++ b/group15/1511_714512544/out/production/1511_714512544/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 diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList$1.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList$1.class new file mode 100644 index 0000000000..c4306e1489 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList$1.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList$ListIterator.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList$ListIterator.class new file mode 100644 index 0000000000..1d423f2cc8 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList$ListIterator.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList.class new file mode 100644 index 0000000000..b3a53ec4c1 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/ArrayList.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/BinarySearchTree.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/BinarySearchTree.class new file mode 100644 index 0000000000..5998f97032 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/BinarySearchTree.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/BinarySearchTreeNode.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/BinarySearchTreeNode.class new file mode 100644 index 0000000000..2fbda2a306 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/BinarySearchTreeNode.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Iterator.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Iterator.class new file mode 100644 index 0000000000..16c3f1aded Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Iterator.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$1.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$1.class new file mode 100644 index 0000000000..648241c965 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$1.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$ListIterator.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$ListIterator.class new file mode 100644 index 0000000000..4097ca6433 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$ListIterator.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$Node.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$Node.class new file mode 100644 index 0000000000..466c28ba4f Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList$Node.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList.class new file mode 100644 index 0000000000..7ac1a0fa80 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/LinkedList.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/List.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/List.class new file mode 100644 index 0000000000..82055051ce Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/List.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Queue.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Queue.class new file mode 100644 index 0000000000..60e7cea803 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Queue.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Stack.class b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Stack.class new file mode 100644 index 0000000000..c309e6c5e7 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/com/coding/basic/Stack.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/ArrayListTest.class b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/ArrayListTest.class new file mode 100644 index 0000000000..435d12baf4 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/ArrayListTest.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/BinarySearchTreeTest.class b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/BinarySearchTreeTest.class new file mode 100644 index 0000000000..167f83c32c Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/BinarySearchTreeTest.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/LinkedListTest.class b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/LinkedListTest.class new file mode 100644 index 0000000000..2b2fb7b768 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/LinkedListTest.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/QueueTest.class b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/QueueTest.class new file mode 100644 index 0000000000..fcd2281a17 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/QueueTest.class differ diff --git a/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/StackTest.class b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/StackTest.class new file mode 100644 index 0000000000..1b08c84113 Binary files /dev/null and b/group15/1511_714512544/out/production/1511_714512544/test/com/coding/basic/StackTest.class differ diff --git a/group15/1511_714512544/src/com/coderising/array/ArrayUtil.java b/group15/1511_714512544/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..cd48c18e32 --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,183 @@ +package com.coderising.array; + +import java.util.Arrays; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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; + int temp = 0; + for (int i = 0; i < len/2; i++) { + 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[] temp = new int[oldArray.length]; + int index = 0; + for (int i : oldArray) { + if(i != 0){ + temp[index++] = i; + } + } + if(index == 0){ + return new int[]{}; + } + return Arrays.copyOf(temp, index); + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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[] array3 = new int[array1.length+array2.length]; + for (int i = 0; i < array1.length; i++) { + array3[i] = array1[i]; + } + int index = array1.length; + for (int i = 0; i < array2.length; i++) { + boolean flag = true; //a2鍏冪礌涓嶅湪a3涓 + for (int j = 0; j < index; j++) { + if(array3[j] == array2[i]){ + flag = false; + } + } + if(flag){ //a2鍏冪礌涓嶅湪a3涓 + array3[index++] = array2[i]; + } + } + int[] temp = Arrays.copyOf(array3, index); + Arrays.sort(temp); + return temp; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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){ + return Arrays.copyOf(oldArray, oldArray.length+size); + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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[]{}; //max=1锛岃繑鍥炵┖鏁扮粍 + int[] arr = new int[max+1]; + arr[0] = 1; + arr[1] = 1; + + int sum = 2; + for (int i = 2; i < arr.length; i++) { + arr[i] = arr[i-1] + arr[i-2]; + if(arr[i] >= max){ + break; + } + sum ++; + } + return Arrays.copyOf(arr, sum); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max<=2) return new int[]{}; + + int[] temp = new int[max]; + int index = 0; + for (int i = 2; i < max; i++) { + boolean flag = true; + for (int j = 2; j <= Math.sqrt(i); j++) { + if(i % j == 0){ + flag = false; + } + } + if(flag){temp[index++] = i;} + } + return Arrays.copyOf(temp, index); + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if(max <= 2) return new int[]{}; + + int[] temp = new int[max]; + int index = 0; + for (int i = 2; i < max; i++) { + int sum = 0; + for (int j = 1; j <= Math.sqrt(i); j++) { + if(j == 1) { + sum += 1; + }else{ + if(i % j == 0){ + sum += j + i/j; + } + } + } + if(sum == i) temp[index++] = i; + } + return Arrays.copyOf(temp, index); + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator){ + if(array.length == 0){ + return ""; + } + StringBuilder sb = new StringBuilder(); + for (int i : array) { + sb.append(i).append(seperator); + } + String temp = sb.toString(); + return temp.substring(0,temp.length()-1); + } + + +} \ No newline at end of file diff --git a/group15/1511_714512544/src/com/coderising/array/ArrayUtilTest.java b/group15/1511_714512544/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..939c5c7ac6 --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,76 @@ +package com.coderising.array; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.*; + +/** + * ArrayList娴嬭瘯 + */ +public class ArrayUtilTest { + private ArrayUtil util = null; + + @Before + public void setUp() throws Exception { + util = new ArrayUtil(); + } + + @After + public void tearDown() throws Exception { + util = null; + } + + @Test + public void reverseArray() throws Exception { + int[] arr = new int[]{7, 9, 30, 3, 4}; + util.reverseArray(arr); + assertArrayEquals(new int[]{4,3, 30 , 9,7}, arr); + } + + @Test + public void removeZero() throws Exception { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] newArr = util.removeZero(oldArr); + assertArrayEquals(new int[]{1,3,4,5,6,6,5,4,7,6,7,5}, newArr); + } + + @Test + public void merge() throws Exception { + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + assertArrayEquals(new int[]{3,4,5,6,7,8}, util.merge(a1, a2)); + } + + @Test + public void grow() throws Exception { + int[] oldArray = {2,3,6}; + int[] newArr = util.grow(oldArray, 3); + assertArrayEquals(new int[]{2,3,6,0,0,0}, newArr); + } + + @Test + public void fibonacci() throws Exception { + assertArrayEquals(new int[]{1,1,2,3,5,8,13}, util.fibonacci(15) ); + } + + @Test + public void getPrimes() throws Exception { + assertArrayEquals(new int[]{2,3,5,7,11,13,17,19}, util.getPrimes(23)); + } + + @Test + public void getPerfectNumbers() throws Exception { + assertArrayEquals(new int[]{6} , util.getPerfectNumbers(7)); + } + + @Test + public void join() throws Exception { + int[] array= {3,8,9}; + assertEquals("3-8-9" ,util.join(array, "-")); + } + +} \ No newline at end of file diff --git a/group15/1511_714512544/src/com/coderising/litestruts/LoginAction.java b/group15/1511_714512544/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group15/1511_714512544/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; + } +} diff --git a/group15/1511_714512544/src/com/coderising/litestruts/Struts.java b/group15/1511_714512544/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..8822339c00 --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,113 @@ +package com.coderising.litestruts; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +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") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + + try { + //0璇诲彇閰嶇疆鏂囦欢struts.xml + SAXReader reader = new SAXReader(); + Document doc = reader.read(new File("src/com/coderising/litestruts/struts.xml")); + Element root = doc.getRootElement(); //鏍瑰厓绱 + Map action = new HashMap<>(); + Map loginResult = new HashMap<>(); + Map logoutResult = new HashMap<>(); + java.util.Iterator iterator = root.elementIterator("action"); + while(iterator.hasNext()){ + Element actionNode = (Element) iterator.next(); + String key = actionNode.attributeValue("name"); + String value = actionNode.attributeValue("class"); + action.put(key,value); + java.util.Iterator it = actionNode.elementIterator("result"); + while(it.hasNext()){ + Element resultNode = (Element) it.next(); + String k = resultNode.attributeValue("name"); + String v = resultNode.getText(); + if(key.equals("login")){ + loginResult.put(k,v); + }else { + logoutResult.put(k,v); + } + } + } + + //1 + String className = action.get(actionName); //鑾峰彇绫诲悕 + Object o = Class.forName(className).newInstance(); //鍒涘缓瀵硅薄 + if(o instanceof LoginAction){ + LoginAction loginAction = (LoginAction) o; + Set> set = parameters.entrySet(); + for (Map.Entry en : set) { + if(en.getKey().equals("name")){ + loginAction.setName(en.getValue()); + }else if(en.getKey().equals("password")){ + loginAction.setPassword(en.getValue()); + } + } + + //2 + Class clazz = (Class) loginAction.getClass(); + Method m = clazz.getDeclaredMethod("execute",null); + m.setAccessible(true); + String message = (String) m.invoke(loginAction,null); + + //3 + View view = new View(); + Map params = new HashMap(); + Method[] ms = clazz.getDeclaredMethods(); + for (Method method : ms) { + method.setAccessible(true); + if(method.getName().equals("getName")){ + String value = (String) method.invoke(loginAction,null); + params.put("name",value); + }else if(method.getName().equals("getPassword")){ + String value = (String) method.invoke(loginAction,null); + params.put("password",value); + }else if(method.getName().equals("getMessage")){ + String value = (String) method.invoke(loginAction,null); + params.put("message",value); + } + } + view.setParameters(params); + + //4 + String jsp = loginResult.get(message); + view.setJsp(jsp); + + return view; + }else { + return null; + } + } catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } + } + +} diff --git a/group15/1511_714512544/src/com/coderising/litestruts/StrutsTest.java b/group15/1511_714512544/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..d28d29c0e2 --- /dev/null +++ b/group15/1511_714512544/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,38 @@ +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() { + 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/group15/1511_714512544/src/com/coderising/litestruts/View.java b/group15/1511_714512544/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group15/1511_714512544/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/1511_714512544/src/com/coderising/litestruts/struts.xml b/group15/1511_714512544/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..07f80b6476 --- /dev/null +++ b/group15/1511_714512544/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 diff --git a/group15/1511_714512544/src/com/coding/basic/BinarySearchTree.java b/group15/1511_714512544/src/com/coding/basic/BinarySearchTree.java index 936960abab..7f30ecd124 100644 --- a/group15/1511_714512544/src/com/coding/basic/BinarySearchTree.java +++ b/group15/1511_714512544/src/com/coding/basic/BinarySearchTree.java @@ -1,5 +1,7 @@ package com.coding.basic; +import edu.princeton.cs.algs4.BinarySearch; + import java.util.Stack; /** @@ -43,15 +45,19 @@ public BinarySearchTreeNode insert(T data){ if(current.getLeft() != null){ current = current.getLeft(); }else { - current.setLeft(new BinarySearchTreeNode(data)); - return current.getLeft(); + BinarySearchTreeNode child = new BinarySearchTreeNode(data); + current.setLeft(child); + child.setParent(current); + return child; } }else {//褰撳墠鑺傜偣鏁版嵁澶т簬root if(current.getRight() != null){ current = current.getRight(); }else { - current.setRight(new BinarySearchTreeNode(data)); - return current.getRight(); + BinarySearchTreeNode child = new BinarySearchTreeNode(data); + current.setRight(child ); + child.setParent(current); + return child; } } } @@ -230,9 +236,83 @@ public void postOrderWithoutRecursion(){ } } - //鎸夊眰閬嶅巻锛屾瘡灞備粠宸﹀埌鍙宠緭鍑 - /*public void TraversalByLayer(){ + //鍒犻櫎鏌愪釜鑺傜偣n + public void delete(BinarySearchTreeNode n){ + BinarySearchTreeNode p = n.getParent(); //鑺傜偣鐨勭埗鑺傜偣 + BinarySearchTreeNode child; //鑺傜偣鐨勫瓙鑺傜偣 + + //璇ヨ妭鐐规病鏈変换浣曞瓙鑺傜偣銆// 鍙跺瓙缁撶偣锛岀洿鎺ュ垹闄ゅ嵆鍙傝鑰冭檻寰呭垹闄ょ粨鐐规槸root鐨勬儏鍐点 + if(n.getLeft()==null && n.getRight()==null){ + //璇ヨ妭鐐规槸鏍硅妭鐐 + if(n == root){ + root = null; + return ; + } + //闈炴牴鑺傜偣 + if(n == p.getLeft()){ + p.setLeft(null); + }else if(n == p.getRight()){ + p.setRight(null); + } + } + + // 鍐呴儴缁撶偣锛屾妸瀹冪殑鍚庣户鐨勫兼嫹杩涙潵锛岀劧鍚庨掑綊鍒犻櫎瀹冪殑鍚庣户銆 + else if(n.getLeft()!=null && n.getRight()!=null){ + BinarySearchTreeNode next = successor(n); //鎵惧埌n鐨勪腑搴忓悗缁ц妭鐐 + n.setData(next.getData()); + delete(next); //涓簭鍚庣户鑺傜偣 + } + + //鍙湁涓涓瀛愮殑缁撶偣锛屾妸瀹冪殑瀛╁瓙浜ょ粰瀹冪殑鐖剁粨鐐瑰嵆鍙 + else { + if(n.getLeft() != null){ //寰楀埌瀛愯妭鐐 + child = n.getLeft(); + }else { + child = n.getRight(); + } + + if(n == root){ // n鏄牴鑺傜偣鐨勬儏鍐 + child.setParent(null); + root = child; + return; + } + //闈炴牴鑺傜偣 + if(n == p.getLeft()){ + p.setLeft(child); + child.setParent(p); + }else{ + p.setRight(child); + child.setParent(p); + } + + } + } + + //鎵惧埌n鐨勪腑搴忓悗缁ц妭鐐 + public BinarySearchTreeNode successor(BinarySearchTreeNode n){ + if( n == null) return null; + if( n.getRight() == null ) return null; + return findMin(n.getRight()); + } + + //鏌ユ壘n鏍戠殑鏈灏忓 + public BinarySearchTreeNode findMin(BinarySearchTreeNode n){ + BinarySearchTreeNode current = n; + while(current.getLeft() != null){ + current = current.getLeft(); + } + return current; + } + + //鏌ユ壘n鏍戠殑鏈澶у + public BinarySearchTreeNode findMax(BinarySearchTreeNode n){ + BinarySearchTreeNode current = n; + while(current.getRight() != null){ + current = current.getRight(); + } + return current; + } + + - }*/ - } diff --git a/group15/1511_714512544/src/com/coding/basic/BinarySearchTreeNode.java b/group15/1511_714512544/src/com/coding/basic/BinarySearchTreeNode.java index b4e94ff7b6..323a040832 100644 --- a/group15/1511_714512544/src/com/coding/basic/BinarySearchTreeNode.java +++ b/group15/1511_714512544/src/com/coding/basic/BinarySearchTreeNode.java @@ -5,6 +5,7 @@ */ public class BinarySearchTreeNode{ private T data; + private BinarySearchTreeNode parent; //鐖惰妭鐐 private BinarySearchTreeNode left; private BinarySearchTreeNode right; private int state; //閫掑綊鐘舵(闈為掑綊閬嶅巻琛ㄧず涓涓妭鐐硅繍琛屽埌鐨勭姸鎬) @@ -13,6 +14,7 @@ public BinarySearchTreeNode(T data) { this.data = data; this.left = null; this.right = null; + this.parent = null; } public T getData() { @@ -46,4 +48,11 @@ public int getState() { public void setState(int state) { this.state = state; } + public BinarySearchTreeNode getParent() { + return parent; + } + + public void setParent(BinarySearchTreeNode parent) { + this.parent = parent; + } } diff --git "a/group15/1511_714512544/\346\226\207\347\253\240\345\234\260\345\235\200.md" "b/group15/1511_714512544/\346\226\207\347\253\240\345\234\260\345\235\200.md" index f6f4c26b37..3a34a9a3fb 100644 --- "a/group15/1511_714512544/\346\226\207\347\253\240\345\234\260\345\235\200.md" +++ "b/group15/1511_714512544/\346\226\207\347\253\240\345\234\260\345\235\200.md" @@ -1 +1,3 @@ (1)浠嬬粛CPU锛屽唴瀛橈紝纭洏锛屾寚浠や互鍙婁粬浠箣闂寸殑鍏崇郴鐨勬枃绔犲湴鍧锛歨ttp://www.jianshu.com/p/f86ca5072c5d + +(2)绋嬪簭鐨勬満鍣ㄥ強琛ㄧず锛 http://www.jianshu.com/p/1eed6fe682cd diff --git a/group15/1513_121469918/HomeWork01/.settings/org.eclipse.core.resources.prefs b/group15/1513_121469918/HomeWork01/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..bcb09d4d64 --- /dev/null +++ b/group15/1513_121469918/HomeWork01/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,8 @@ +eclipse.preferences.version=1 +encoding//src/coding/ArrayList.java=GBK +encoding//src/coding/BinaryTreeNode.java=GBK +encoding//src/coding/Iterator.java=GBK +encoding//src/coding/LinkedList.java=GBK +encoding//src/coding/List.java=GBK +encoding//src/coding/Queue.java=GBK +encoding//src/coding/Stack.java=GBK diff --git a/group15/1513_121469918/HomeWork20170305/.classpath b/group15/1513_121469918/HomeWork20170305/.classpath new file mode 100644 index 0000000000..a851141e9a --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/group15/1513_121469918/HomeWork20170305/.project b/group15/1513_121469918/HomeWork20170305/.project new file mode 100644 index 0000000000..0e4fe1fc91 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/.project @@ -0,0 +1,17 @@ + + + HomeWork20170305 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group15/1513_121469918/HomeWork20170305/.settings/org.eclipse.core.resources.prefs b/group15/1513_121469918/HomeWork20170305/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..67f156c482 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,9 @@ +eclipse.preferences.version=1 +encoding//src/com/coderising/array/ArrayUtil.java=UTF-8 +encoding//src/com/coding/basic/ArrayList.java=GBK +encoding//src/com/coding/basic/BinaryTreeNode.java=GBK +encoding//src/com/coding/basic/Iterator.java=GBK +encoding//src/com/coding/basic/LinkedList.java=GBK +encoding//src/com/coding/basic/List.java=GBK +encoding//src/com/coding/basic/Queue.java=GBK +encoding//src/com/coding/basic/Stack.java=GBK diff --git a/group15/1513_121469918/HomeWork20170305/.settings/org.eclipse.jdt.core.prefs b/group15/1513_121469918/HomeWork20170305/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..7341ab1683 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +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.7 diff --git a/group15/1513_121469918/HomeWork20170305/bin/com/coderising/litestruts/struts.xml b/group15/1513_121469918/HomeWork20170305/bin/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..a7cb57e188 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/bin/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 diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coderising/array/ArrayUtil.java b/group15/1513_121469918/HomeWork20170305/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..985ca47709 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,281 @@ +package com.coderising.array; + + +import java.util.Arrays; +import java.util.TreeSet; +import com.coding.basic.ArrayList; +import com.coding.basic.Iterator; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 渚嬪锛 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 count = 0; + + // 鍒涘缓涓涓复鏃舵暟缁勮娌℃湁闆剁殑鏃ф暟缁 + int[] temp = new int[oldArray.length - count]; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) { + // 濡傛灉鍊间负0锛岀粺璁°佽烦杩囦笉鍔犲叆鏂版暟缁 + count++; + continue; + } else { + temp[i - count] = oldArray[i]; + } + } + // 瀹氫箟杩斿洖鏁扮粍鐨勯暱搴 + int len = oldArray.length - count; + int[] resultArray = new int[len]; + System.arraycopy(temp, 0, resultArray, 0, len); + return resultArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 渚嬪 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 = array1; + // 鍘婚櫎閲嶅鍏冪礌 + for (int i = 0; i < array2.length; i++) { + boolean sameVal = false; + for (int j = 0; j < array1.length; j++) { + if (array1[j] == array2[i]) { + sameVal = true; + break; + } + } + if(sameVal == false){ + result = grow(result, 1); + result[result.length-1] = array2[i]; + } + } + //鍐掓场鎺掑簭 + for (int i = 0; i < result.length-1; i++) { + for (int j = i+1; j < result.length; j++) { + if(result[i]>result[j]){ + int temp = result[i]; + result[i] = result[j]; + result[j] =temp; + } + } + } + return result; +*/ + int len1=0,len2=0;//arr1闀垮害len1 + ArrayList list = new ArrayList(); + for (int k=0;k < array1.length+array2.length; k++) { + //濡傛灉涓や釜鏁扮粍閮借繕鏈夊厓绱 + if(len1array2[len2]){ + list.add(array2[len2]); + len2++; + }else{ + list.add(array1[len1]); + len1++; + len2++; + } + }else if(len1==array1.length && len2 < array2.length){ + //濡傛灉鏁扮粍1娌℃湁鍏冪礌,骞朵笖2鏈夊厓绱 + list.add(array2[len2]); + len2++; + }else if(len2==array2.length && len1 < array1.length){ + //鏁扮粍2娌℃湁鍏冪礌锛屽苟涓1鏈夊厓绱 + list.add(array1[len1]); + len1++; + }else{ + break; + } + } + //list杞暟缁 + int[] result = new int[list.size()]; + Iterator it = list.iterator(); + int index = 0; + while(it.hasNext()){ + result[index++] = ((Integer)it.next()).intValue(); + } + 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[] resultArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, resultArray, 0, oldArray.length); + return resultArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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]; + } else { + int[] temp = new int[max]; + temp[0] = 1; + temp[1] = 1; + // 瀹氫箟杩斿洖鏁扮粍鐨勯暱搴﹀彉閲 + int len = 2; + for (int i = 2; i < max; i++) { + int last = temp[i - 1] + temp[i - 2]; + if (last >= max) { + break; + } else { + temp[i] = last; + len = i + 1; + } + } + return Arrays.copyOf(temp, len); + } + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + + if (max <= 1) { + return new int[0]; + } else { + // 鍒涘缓涓存椂鏁扮粍 + int[] temp = new int[max]; + int count = 0; + // 浠庨浂寮濮嬮亶鍘嗗埌max锛屽鏋滄湁鏄礌鏁板氨鍔犲叆涓存椂鏁扮粍銆 + for (int i = 0; i < max; i++) { + if (isPrimes(i)) { + temp[i - count] = i; + } else { + count++; + } + } + // max -1 -count鏄渶鍚庝竴涓厓绱犵储寮 + int len = max - count; + int[] resultArray = Arrays.copyOf(temp, len); + return resultArray; + } + } + + boolean isPrimes(int x) { + if (x <= 1) { + return false; + } else { + for (int i = 2; i < x; i++) { + if (x % i == 0) { + return false; + } + } + } + return true; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + if (max < 1) { + return new int[0]; + } else { + ArrayList array = new ArrayList(); + for (int i = 1; i <= max; i++) { + if (isPerfectNumber(i)) { + array.add(i); + } + } + int[] result = new int[array.size()]; + Iterator it = array.iterator(); + int index = 0; + while (it.hasNext()) { + result[index] = ((Integer) it.next()).intValue(); + index++; + } + return result; + } + } + + boolean isPerfectNumber(int x) { + if (x < 1) { + return false; + } else { + int count = 0; + for (int i = 1; i < x; i++) { + if (x % i == 0) { + count += i; + } + } + if (x == count) { + return true; + } + return false; + } + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 渚嬪array= [3,8,9], seperator = "-" 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < array.length; i++) { + if(i == array.length-1){ + sb.append(String.valueOf(array[i])); + break; + } + sb.append(String.valueOf(array[i])+seperator); + } + String result = sb.toString(); + return result; + } + +} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/LoginAction.java b/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/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; + } +} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/Struts.java b/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..a0dd465d25 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,116 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + + + + //璇诲彇閰嶇疆鏂囦欢 + try { + Document doc =new SAXReader().read(new File("./src/com/coderising/litestruts/struts.xml")); + //鑾峰彇XML涓殑action鏍囩 + Element loginName = (Element)doc.selectSingleNode("//action[1]"); + Element logoutName = (Element)doc.selectSingleNode("//action[2]"); + //鍒ゆ柇action鐨刵ame灞炴у唴瀹 + if(actionName.equals(loginName.attributeValue("name"))){ + //浼犲叆鐨刟ctionName鍐呭涓簂ogin鍒欒繘琛宭ogin鎿嶄綔 + + //鑾峰彇class璺緞 + String s = loginName.attributeValue("class"); + Class c = Class.forName(s); + Constructor con = c.getConstructor(); + //瀹炰緥鍖朙oginAction + LoginAction login =(LoginAction) con.newInstance(); + + //閫氳繃parameters鑾峰彇name锛宲assword + String executeName = parameters.get("name"); + String executePassword = parameters.get("password"); + + //鑾峰彇 setter 鏂规硶,骞惰皟鐢 + Method m = c.getMethod("setName", String.class); + m.invoke(login, executeName); + m =c.getMethod("setPassword", String.class); + m.invoke(login, executePassword); + + //璋冪敤LoginAction 鐨別xectue 鏂规硶 exectueResult鍊间负锛氬笎鍙峰瘑鐮佹纭繑鍥瀞uccess锛屽弽涔嬩负fail + m = c.getMethod("execute"); + String exectueResult = (String)m.invoke(login); + + //鑾峰彇 getMessage 鏂规硶 + m = c.getMethod("getMessage"); + String message =(String) m.invoke(login); + m = c.getMethod("getName"); + String name =(String) m.invoke(login); + m = c.getMethod("getPassword"); + String password =(String) m.invoke(login); + + //鍒涘缓HashMap,灏嗙櫥褰曟搷浣滆繑鍥炵殑3涓彉閲忔斁鍒癕ap + HashMap hm = new HashMap(); + hm.put("message", message); + hm.put("name", name); + hm.put("password", password); + + //鍒涘缓View瀵硅薄 + View view = new View(); + view.setParameters(hm); + + //璇诲彇struts.xml涓殑 閰嶇疆 + List list =(List) doc.selectNodes("//action[@name = 'login']/result"); + for(Element e : list){ + String resultName = e.attributeValue("name"); + //exectue杩斿洖鍊兼瘮瀵筙ML涓媟esult 瀵瑰簲鐨勫艰繑鍥炲搴旂殑jsp + if(resultName.equals(exectueResult)){ + //灏嗗搴旂殑jsp 鏀惧埌view瀵硅薄涓 + view.setJsp(e.getText()); + } + } + return view; + + }else if(actionName.equals(logoutName.attributeValue("name"))){ + //actionName鏄痩ogout鍒欒繘琛宭ogout鎿嶄綔 + } + + } catch (Exception e) { + // TODO: handle exception + e.printStackTrace(); + } + + return null; + } + +} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/StrutsTest.java b/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/View.java b/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/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/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/struts.xml b/group15/1513_121469918/HomeWork20170305/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..a7cb57e188 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/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 diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/ArrayList.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..951b1ce0e9 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/ArrayList.java @@ -0,0 +1,118 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + int len = size + 1; + // 判断list的长度是否大于容器长度 + if (len > elementData.length) { + // 创建新容器 + Object[] newElemDate = new Object[elementData.length + 1]; + // 复制旧容器所有元素到新容器 + System.arraycopy(elementData, 0, newElemDate, 0, elementData.length); + elementData = newElemDate; + } + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + // 检查是否越界 + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + // 插入元素到末尾直接调用add方法 + if (index == size) { + add(o); + } else { + // 创建新容器 + Object[] newElemData = new Object[elementData.length + 1]; + // 复制index以前的所有元素到新容器 + System.arraycopy(elementData, 0, newElemData, 0, index); + newElemData[index] = o; + // 复制index 及以后的元素到新容器 + System.arraycopy(elementData, index, newElemData, index + 1, size - index); + + elementData = newElemData; + size++; + } + } + + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + return elementData[index]; + } + + public Object remove(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + Object removeElement = elementData[index]; + //不是最后一个元素的索引值才需要操作 + if(index != (size-1)){ + // 创建新容器 + Object[] newElemData = new Object[elementData.length]; + // 复制index以前的所有元素到新容器 + System.arraycopy(elementData, 0, newElemData, 0, index); + // 复制index 以后的元素到新容器 + System.arraycopy(elementData, index+1, newElemData, index, size - index -1); + } + //最后一个元素的索引值直接减短list长度 + size--; + return removeElement; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new MyIterator(this); + } + + private class MyIterator implements Iterator { + private int poi = -1; + private ArrayList array = null; + + private MyIterator(ArrayList array) { + this.array = array; + } + + @Override + public boolean hasNext() { + return (poi + 1) < array.size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + poi++; + if (poi >= array.size) { + poi--; + throw new IndexOutOfBoundsException(); + } + + return array.get(poi); + } + + @Override + public Object remove() { + // TODO Auto-generated method stub + if (poi < 0) { + throw new NoSuchElementException(); + } + Object val = array.remove(poi); + poi--; + return val; + } + + } +} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/BinaryTreeNode.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..b3a1ee65b6 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,53 @@ +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) { + // 判断当前节点有无元素 + if (data == null) { + setData(o); + } else { + Integer i = (Integer) o; + // 当前节点有数据则判断左右节点 + if (i.compareTo((Integer) data) == -1) { + if(right == null) + right = new BinaryTreeNode(); + return right.insert(i); + } else if (i.compareTo((Integer) data) == 1) { + if(left == null) + left = new BinaryTreeNode(); + return left.insert(i); + } + return null; + } + return null; + } + +} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Iterator.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..f5cf74673d --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + public Object remove(); +} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/LinkedList.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..d8f759fdcb --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/LinkedList.java @@ -0,0 +1,170 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + private Node head; + private int size; + + public void add(Object o) { + // 判断头是否有数据 + if (head == null) { + head = new Node(o, null); + } else { + Node newNode = head; + while (newNode.next != null) { + newNode = newNode.next; + } + newNode.next = new Node(o, null); + } + size++; + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + Node node = head; + if (index != 0) { + // 不是第一个索引值就找到索引值的前面一个节点 + for (int i = 1; i < index; i++) { + node = node.next; + } + Node newNode = new Node(o, node.next); + node.next = newNode; + size++; + } else { + // 第一个索引值就将头节点指向它 + Node newNode = new Node(o, head); + head = newNode; + size++; + } + } + + public Object get(int index) { + indexCheck(index); + Node node = head; + for (int i = 1; i <= index; i++) { + node = node.next; + } + return node.data; + } + + public Object remove(int index) { + indexCheck(index); + + Node node = head; + Node removeNode; + if (index == 0) { + //删除第一个节点就把头节点指向原本的第二个节点 + removeNode = head; + head = head.next; + } else { + //找到索引值的前一个节点 + for (int i = 1; i < index; i++) { + node = node.next; + } + removeNode = node.next; + //前一个节点指针,指向被删除节点所指向的节点 + node.next = removeNode.next; + } + size--; + return removeNode.data; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o, head.next); + head.next = newNode; + size++; + } + + public void addLast(Object o) { + add(o); + } + + public Object removeFirst() { + //没有元素就抛异常 + if (size <= 0) { + throw new IndexOutOfBoundsException("size:" + size); + } + Object val = head.data; + head = head.next; + size--; + return val; + } + + public Object removeLast() { + if (size <= 0) { + throw new IndexOutOfBoundsException("size:" + size); + } + Node node = head; + while (node.next != null) { + node = node.next; + } + Object val = node.data; + node = null; + size--; + return val; + } + + public Iterator iterator() { + return new MyIterator(this); + } + + private class MyIterator implements Iterator{ + private int poi = -1; + private LinkedList list ; + private MyIterator(LinkedList list) { + this.list= list; + } + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return (poi + 1) < list.size; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + poi++; + if (poi >= list.size) { + poi--; + throw new IndexOutOfBoundsException(); + } + + return list.get(poi); + } + + @Override + public Object remove() { + // TODO Auto-generated method stub + if (poi < 0) { + throw new NoSuchElementException(); + } + Object val = list.removeLast(); + poi--; + return val; + } + + } + + private void indexCheck(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("index:" + index + "size:" + size); + } + } + + private static class Node { + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/group16/542087872/src/com/coding/basic/List.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/List.java similarity index 100% rename from group16/542087872/src/com/coding/basic/List.java rename to group15/1513_121469918/HomeWork20170305/src/com/coding/basic/List.java diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Queue.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..ea25224bd2 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Queue.java @@ -0,0 +1,35 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class Queue { + private int size; + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.addLast(o);; + size++; + } + + public Object deQueue(){ + if(size<=0){ + throw new NoSuchElementException(); + } + Object val = list.removeFirst(); + size--; + return val; + } + + public boolean isEmpty(){ + boolean flag = false; + if(size >= 0){ + flag = true; + } + return flag; + } + + public int size(){ + return size; + } + +} diff --git a/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Stack.java b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..9a28ee4d36 --- /dev/null +++ b/group15/1513_121469918/HomeWork20170305/src/com/coding/basic/Stack.java @@ -0,0 +1,41 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size; + + public void push(Object o){ + elementData.add(o); + size++; + } + + public Object pop(){ + if(size<=0){ + throw new NoSuchElementException(); + } + int len = size-1; + Object val = elementData.remove(len); + size--; + return val; + } + + public Object peek(){ + if(size<=0){ + throw new NoSuchElementException(); + } + int len = size-1; + return elementData.get(len); + } + public boolean isEmpty(){ + boolean flag = false; + if(size >= 0){ + flag = true; + } + return flag; + } + public int size(){ + return size; + } +} diff --git a/group15/1514_616019420/.classpath b/group15/1514_616019420/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group15/1514_616019420/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group15/1514_616019420/.gitignore b/group15/1514_616019420/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group15/1514_616019420/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group15/1514_616019420/.project b/group15/1514_616019420/.project new file mode 100644 index 0000000000..fab8d7f04c --- /dev/null +++ b/group15/1514_616019420/.project @@ -0,0 +1,17 @@ + + + 2017Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group15/1514_616019420/src/com/coderising/array/ArrayUtil.java b/group15/1514_616019420/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..d636e46186 --- /dev/null +++ b/group15/1514_616019420/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,205 @@ +package com.coderising.array; + +import java.lang.reflect.Array; +import java.util.*; +import com.coding.basic.ArrayList; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] 濡傛灉 a = + * [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public int[] reverseArray(int[] origin) { + + if (origin != null && origin.length > 0) { + int size = origin.length; + int[] intarray = new int[size]; + for (int i = 0; i < size; i++) { + intarray[i] = origin[size - 1 - i]; + } + origin = intarray; + + } else if (origin != null && origin.length == 0) { + + } else { + throw new NullPointerException(); + } + return origin; + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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) { + + if (oldArray != null) { + int[] intarry = new int[oldArray.length]; + int x = 0; + int y = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + intarry[y] = oldArray[i]; + y++; + } else { + x++; + } + } + int[] newarray = new int[y]; + System.arraycopy(intarry, 0, newarray, 0, y); + return newarray; + } else { + throw new NullPointerException(); + } + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 渚嬪 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 count = array2.length; + array2 = grow(array2, array1.length); + System.arraycopy(array1, 0, array2, count, array1.length); + Arrays.sort(array2); + int[] array3 = new int[array2.length]; + array3[0] = array2[0]; + int x = 0; + for (int i = 1; i < array2.length; i++) { + if (array2[i] != array3[x]) { + array3[x + 1] = array2[i]; + x++; + } + } + + int[] array4 = new int[x + 1]; + System.arraycopy(array3, 0, array4, 0, x + 1); + return array4; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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[] array, int max) { + int[] array0 = new int[array.length]; + int x = 0; + int y = 0; + for (int i = 0; i < array.length; i++) { + if (array[i] < max) { + array0[y] = array[i]; + y++; + } else { + x++; + } + } + int[] array1 = new int[y]; + return Arrays.copyOf(array0, y); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + + + if(max<=2) + {return new int[]{}; + + } + int[] temp = new int[max]; + int index = 0; + for (int i = 2; i < max; i++) { + boolean flag = true; + for (int j = 2; j <= Math.sqrt(i); j++) { + if(i % j == 0){ + flag = false; + } + } + if(flag){temp[index++] = i;} + } + return Arrays.copyOf(temp, index); + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + + if(max <= 2) return new int[]{}; + + int[] array = new int[max]; + int index = 0; + for (int i = 2; i < max; i++) { + int x = 0; + for (int j = 1; j <= Math.sqrt(i); j++) { + if(j == 1) { + x += 1; + }else{ + if(i % j == 0){ + x += j + i/j; + } + } + } + if(x == i) array[index++] = i; + } + return Arrays.copyOf(array, index); + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 渚嬪array= [3,8,9], seperator = "-" 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + if(array.length == 0){ + return ""; + } + StringBuilder sb = new StringBuilder(); + for (int i : array) { + sb.append(i).append(seperator); + } + String temp = sb.toString(); + return temp.substring(0,temp.length()-1); + } + +} diff --git a/group15/1514_616019420/src/com/coderising/array/ArrayUtilTest.java b/group15/1514_616019420/src/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..0f244f272a --- /dev/null +++ b/group15/1514_616019420/src/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,110 @@ +package com.coderising.array; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ArrayUtilTest { + + private ArrayUtil u; + int[] array; + + @Before + public void setUp() throws Exception { + + u = new ArrayUtil(); + + array = new int[100]; + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testReverseArray() { + fail("Not yet implemented"); + for (int i = 0; i < 100; i++) { + array[i] = i; + + } + array = u.reverseArray(array); + System.out.println("testReverseArray:" + Arrays.toString(array)); + + } + + @Test + public void testRemoveZero() { + fail("Not yet implemented"); + for (int i = 0; i < 100; i++) { + if (i < 50) { + array[i] = 0; + } else { + array[i] = i; + } + } + array = u.removeZero(array); + System.out.println("testRemoveZero:" + Arrays.toString(array)); + } + + @Test + public void testMerge() { + //fail("Not yet implemented"); + int[] intarray={0,1,2,3,4,5,6,7}; + int[] intarray0={10,11,12,13,14,15,16,17,0,3}; + int[] intarray1=u.merge(intarray, intarray0); + System.out.println("testMerge:" + Arrays.toString(intarray1)); + + } + + @Test + public void testGrow() { + //fail("Not yet implemented"); + int[] intarray={0,1,2,3,4,5,6,7}; + int[] intarray0={10,11,12,13,14,15,16,17,0,3}; + int[] intarray1=u.grow(intarray, 10); + System.out.println("testGrow:" + Arrays.toString(intarray1)); + + } + + @Test + public void testFibonacci() { + //fail("Not yet implemented"); + int[] intarray={0,1,2,3,4,5,6,7}; + int[] intarray0={10,11,12,13,14,15,16,17,0,3}; + int[] intarray1=u.fibonacci(intarray0, 17); + System.out.println("testFibonacci:" + Arrays.toString(intarray1)); + } + + @Test + public void testGetPrimes() { + //fail("Not yet implemented");int[] intarray={0,1,2,3,4,5,6,7}; + int[] intarray0={10,11,12,13,14,15,16,17,0,3}; + int[] intarray1=u.getPrimes(10); + System.out.println("testGetPrimes:" + Arrays.toString(intarray1)); + + } + + @Test + public void testGetPerfectNumbers() { + // fail("Not yet implemented"); + int[] intarray0={10,11,12,13,14,15,16,17,0,3}; + int[] intarray1=u.getPerfectNumbers(10); + System.out.println("testGetPerfectNumbers:" + Arrays.toString(intarray1)); + } + + @Test + public void testJoin() { + // fail("Not yet implemented"); + int[] intarray0={10,11,12,13,14,15,16,17,0,3}; + String str=u.join(intarray0, "++"); + System.out.println("testJoin:" + str); + } + +} diff --git a/group15/1514_616019420/src/com/coderising/litestruts/LoginAction.java b/group15/1514_616019420/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group15/1514_616019420/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; + } +} diff --git a/group15/1514_616019420/src/com/coderising/litestruts/Struts.java b/group15/1514_616019420/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..9081b94e78 --- /dev/null +++ b/group15/1514_616019420/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,113 @@ +package com.coderising.litestruts; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.File; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +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") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + + try { + //0璇诲彇閰嶇疆鏂囦欢struts.xml + SAXReader saxReader = new SAXReader(); + Document document = saxReader.read(new File("src/com/coderising/litestruts/struts.xml")); + Element rootElement = document.getRootElement(); //鏍瑰厓绱 + Map actionMap = new HashMap<>(); + Map loginResult = new HashMap<>(); + Map logoutResult = new HashMap<>(); + java.util.Iterator iterator = rootElement.elementIterator("action"); + while(iterator.hasNext()){ + Element actionNode = (Element) iterator.next(); + String key = actionNode.attributeValue("name"); + String value = actionNode.attributeValue("class"); + action.put(key,value); + java.util.Iterator it = actionNode.elementIterator("result"); + while(it.hasNext()){ + Element resultNode = (Element) it.next(); + String k = resultNode.attributeValue("name"); + String v = resultNode.getText(); + if(key.equals("login")){ + loginResult.put(k,v); + }else { + logoutResult.put(k,v); + } + } + } + + //1 + String className = actionMap.get(actionName); //鑾峰彇绫诲悕 + Object o = Class.forName(className).newInstance(); //鍒涘缓瀵硅薄 + if(o instanceof LoginAction){ + LoginAction loginAction = (LoginAction) o; + Set> set = parameters.entrySet(); + for (Map.Entry en : set) { + if(en.getKey().equals("name")){ + loginAction.setName(en.getValue()); + }else if(en.getKey().equals("password")){ + loginAction.setPassword(en.getValue()); + } + } + + //2 + Class clazz = (Class) loginAction.getClass(); + Method m = clazz.getDeclaredMethod("execute",null); + m.setAccessible(true); + String message = (String) m.invoke(loginAction,null); + + //3 + View view = new View(); + Map params = new HashMap(); + Method[] ms = clazz.getDeclaredMethods(); + for (Method method : ms) { + method.setAccessible(true); + if(method.getName().equals("getName")){ + String value = (String) method.invoke(loginAction,null); + params.put("name",value); + }else if(method.getName().equals("getPassword")){ + String value = (String) method.invoke(loginAction,null); + params.put("password",value); + }else if(method.getName().equals("getMessage")){ + String value = (String) method.invoke(loginAction,null); + params.put("message",value); + } + } + view.setParameters(params); + + //4 + String jsp = loginResult.get(message); + view.setJsp(jsp); + + return view; + }else { + return null; + } + } catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } + } + +} diff --git a/group15/1514_616019420/src/com/coderising/litestruts/StrutsTest.java b/group15/1514_616019420/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group15/1514_616019420/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group15/1514_616019420/src/com/coderising/litestruts/View.java b/group15/1514_616019420/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group15/1514_616019420/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/1514_616019420/src/com/coderising/litestruts/struts.xml b/group15/1514_616019420/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..dd598a3664 --- /dev/null +++ b/group15/1514_616019420/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 diff --git a/group15/1514_616019420/ArrayList.java b/group15/1514_616019420/src/com/coding/basic/ArrayList.java similarity index 100% rename from group15/1514_616019420/ArrayList.java rename to group15/1514_616019420/src/com/coding/basic/ArrayList.java diff --git a/group15/1514_616019420/BinaryTreeNode.java b/group15/1514_616019420/src/com/coding/basic/BinaryTreeNode.java similarity index 100% rename from group15/1514_616019420/BinaryTreeNode.java rename to group15/1514_616019420/src/com/coding/basic/BinaryTreeNode.java diff --git a/group16/542087872/src/com/coding/basic/Iterator.java b/group15/1514_616019420/src/com/coding/basic/Iterator.java similarity index 100% rename from group16/542087872/src/com/coding/basic/Iterator.java rename to group15/1514_616019420/src/com/coding/basic/Iterator.java diff --git a/group15/1514_616019420/LinkedList.java b/group15/1514_616019420/src/com/coding/basic/LinkedList.java similarity index 100% rename from group15/1514_616019420/LinkedList.java rename to group15/1514_616019420/src/com/coding/basic/LinkedList.java diff --git a/group20/423184723/src/com/coding/basic/List.java b/group15/1514_616019420/src/com/coding/basic/List.java similarity index 100% rename from group20/423184723/src/com/coding/basic/List.java rename to group15/1514_616019420/src/com/coding/basic/List.java diff --git a/group15/1514_616019420/Queue.java b/group15/1514_616019420/src/com/coding/basic/Queue.java similarity index 100% rename from group15/1514_616019420/Queue.java rename to group15/1514_616019420/src/com/coding/basic/Queue.java diff --git a/group15/1514_616019420/Stack.java b/group15/1514_616019420/src/com/coding/basic/Stack.java similarity index 100% rename from group15/1514_616019420/Stack.java rename to group15/1514_616019420/src/com/coding/basic/Stack.java diff --git a/group15/1514_616019420/src/com/coding/basic/readme.txt b/group15/1514_616019420/src/com/coding/basic/readme.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/group15/1515_337959725/.classpath b/group15/1515_337959725/.classpath index d171cd4c12..6a4228528e 100644 --- a/group15/1515_337959725/.classpath +++ b/group15/1515_337959725/.classpath @@ -1,6 +1,7 @@ - + + diff --git a/group15/1515_337959725/.project b/group15/1515_337959725/.project index 8c7c8dd0f7..6730933705 100644 --- a/group15/1515_337959725/.project +++ b/group15/1515_337959725/.project @@ -1,6 +1,6 @@ - BasicTest + coding0305 diff --git a/group15/1515_337959725/.settings/org.eclipse.jdt.core.prefs b/group15/1515_337959725/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..060c5ee3d2 --- /dev/null +++ b/group15/1515_337959725/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.5 +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.5 diff --git a/group15/1515_337959725/src/com/coderising/array/ArrayUtil.java b/group15/1515_337959725/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..3273e77f7b --- /dev/null +++ b/group15/1515_337959725/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,184 @@ +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; + int length=origin.length; + for(int i=0;iarray2[j]){ + array3[k]=array1[j]; + j++; + k++; + } + } + 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]; + int i; + for(i=0;i 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 builderFactory = DocumentBuilderFactory.newInstance(); + View view = null; + try { + DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder(); + File f = new File("E:/gitProject/coding2017/group15/1515_337959725/src/com/coderising/litestruts/struts.xml"); + Document document = documentBuilder.parse(f); + NodeList actionList = document.getElementsByTagName("action"); + Node node = null; + String className=""; + for(int i=0;i 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/group15/1515_337959725/src/com/coderising/litestruts/View.java b/group15/1515_337959725/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group15/1515_337959725/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/1515_337959725/src/com/coderising/litestruts/struts.xml b/group15/1515_337959725/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..6f23f0a83d --- /dev/null +++ b/group15/1515_337959725/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 diff --git a/group15/1517_279137987/src/com/coderising/array/ArrayUtil.java b/group15/1517_279137987/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..443184b555 --- /dev/null +++ b/group15/1517_279137987/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,226 @@ +package com.coderising.array; + +import java.util.Arrays; + +import my.collection.linear.MyArrayList; + +public class ArrayUtil { + + /** + * 1缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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; + int len = origin.length; + for(int i=0; i array2[j]){ + retTmp[k++] = array2[j++]; + }else{ + j++; + sameCount++; + } + } + //insert remainder array + while(i < array1.length){ + retTmp[k++] = array1[i++]; + } + while(j < array2.length){ + retTmp[k++] = array2[j++]; + } + int[] ret = new int[retTmp.length - sameCount]; + if(sameCount > 0){ + System.arraycopy(retTmp, 0, ret, 0, retTmp.length - sameCount); + } + return ret; + } + + /** + * 4鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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; i0; i--){ + //get divide numbers + MyArrayList divideNumArrayList = new MyArrayList(10); + for(int j=1; j parameters) { + View view = new View(); + /* + * 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + * */ + SAXReader reader = new SAXReader(); + try { + Document document = reader.read(new File("src/com/coderising/litestruts/struts.xml")); + Element root = document.getRootElement(); + Iterator iter = root.elementIterator(); + while(iter.hasNext()){ + Element secondNode = (Element) iter.next(); + String nameStr = secondNode.attributeValue("name"); + String classStr = secondNode.attributeValue("class"); + + /* + * 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + */ + if(nameStr.equals(actionName)){ + Class cls = Class.forName(classStr); + Object obj = cls.newInstance(); + Method mtd1 = cls.getDeclaredMethod("setName", String.class); + mtd1.invoke(obj, parameters.get("name")); + + Method mtd2 = cls.getDeclaredMethod("setPassword", new Class[]{String.class}); + mtd2.invoke(obj, parameters.get("password")); + + /* + * 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + * */ + Method execute = cls.getDeclaredMethod("execute"); + String runStatus = (String) execute.invoke(obj); + + /* + * 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + */ + Method mtd3 = cls.getDeclaredMethod("getMessage"); + String getMes = (String) mtd3.invoke(obj); + Map params = new HashMap(); + params.put("message",getMes); + + /* + * 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + Iterator iterSecond = secondNode.elementIterator(); + while(iterSecond.hasNext()){ + Element thirdNode = (Element) iterSecond.next(); + String resultNameStr = thirdNode.attributeValue("name"); + String pageStr = thirdNode.getText(); + + if(runStatus.equals(resultNameStr)){ + view.setJsp(pageStr); + view.setParameters(params); + break; + } + } + } + } + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return view; + } +} diff --git a/group15/1517_279137987/src/com/coderising/litestruts/StrutsTest.java b/group15/1517_279137987/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..9f20bbfd59 --- /dev/null +++ b/group15/1517_279137987/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +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() { + + 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/group15/1517_279137987/src/com/coderising/litestruts/View.java b/group15/1517_279137987/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group15/1517_279137987/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/1517_279137987/src/com/coderising/litestruts/struts.xml b/group15/1517_279137987/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..ea46090bc9 --- /dev/null +++ b/group15/1517_279137987/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 diff --git a/group15/1517_279137987/my/collection/linear/MyArrayList.java b/group15/1517_279137987/src/my/collection/linear/MyArrayList.java similarity index 77% rename from group15/1517_279137987/my/collection/linear/MyArrayList.java rename to group15/1517_279137987/src/my/collection/linear/MyArrayList.java index 380c552539..d84b583dcb 100644 --- a/group15/1517_279137987/my/collection/linear/MyArrayList.java +++ b/group15/1517_279137987/src/my/collection/linear/MyArrayList.java @@ -70,4 +70,35 @@ public String toString(){ System.arraycopy(elementData, 0, largerElement, 0, elementData.length); elementData = largerElement; }*/ + + public MyIterator myIterator(){ + return new MyArrayListIterator(this); + } + + private class MyArrayListIterator implements MyIterator{ + @SuppressWarnings("unused") + MyArrayList list = null; + int pos = 0; + + private MyArrayListIterator(MyArrayList list){ + this.list = list; + } + + public boolean hasNext() { + if(++pos > size){ + return false; + }else{ + return true; + } + } + + public Object next() { + return get(pos); + } + + public Object remove(){ //? + return MyArrayList.this.remove(this.pos); + } + + } } diff --git a/group15/1517_279137987/src/my/collection/linear/MyBinaryTreeNode.java b/group15/1517_279137987/src/my/collection/linear/MyBinaryTreeNode.java new file mode 100644 index 0000000000..8b52c6f838 --- /dev/null +++ b/group15/1517_279137987/src/my/collection/linear/MyBinaryTreeNode.java @@ -0,0 +1,83 @@ +package my.collection.linear; + +public class MyBinaryTreeNode implements Comparable{ + + private Object data; + private MyBinaryTreeNode left; + private MyBinaryTreeNode right; + + public Object getData() { + return data; + } + public void setData(Object data) { + this.data = data; + } + public MyBinaryTreeNode getLeft() { + return left; + } + public void setLeft(MyBinaryTreeNode left) { + this.left = left; + } + public MyBinaryTreeNode getRight() { + return right; + } + public void setRight(MyBinaryTreeNode right) { + this.right = right; + } + + public MyBinaryTreeNode insert(Object o){ + //cast to MyBinaryTreeNode + MyBinaryTreeNode newNode = new MyBinaryTreeNode(); + newNode.setData(o); + newNode.setLeft(null); + newNode.setRight(null); + + //insert to current node + if(data == null){ + this.setData(o); + this.setLeft(null); + this.setRight(null); + }else{ + //insert to left child + if(compareTo(o) == -1){ + if(this.getLeft() == null){ + this.setLeft(newNode); + }else{ + this.data = this.getLeft().data; + this.setLeft(this.getLeft().getLeft()); + this.setRight(this.getRight().getRight()); + insert(o); + } + //insert to right child + }else if(compareTo(o) == 1){ + if(this.getRight() == null){ + this.setRight(newNode); + }else{ + this.data = this.getLeft().data; + this.setLeft(this.getLeft().getLeft()); + this.setRight(this.getRight().getRight()); + insert(o); + } + //can't insert node which has same data. + }else{ + + } + } + return newNode; + } + + public int compareTo(Object o) { + int compareFlag = 0; + if(o instanceof Integer){ + if(Integer.valueOf(o.toString()) < Integer.valueOf(data.toString())){ + compareFlag = -1; + }else if(Integer.valueOf(o.toString()) > Integer.valueOf(data.toString())){ + compareFlag = 1; + }else{ + compareFlag = 0; + } + } + return compareFlag; + } + +} diff --git a/group15/1517_279137987/my/collection/linear/MyIterator.java b/group15/1517_279137987/src/my/collection/linear/MyIterator.java similarity index 100% rename from group15/1517_279137987/my/collection/linear/MyIterator.java rename to group15/1517_279137987/src/my/collection/linear/MyIterator.java diff --git a/group15/1517_279137987/my/collection/linear/MyLinkedList.java b/group15/1517_279137987/src/my/collection/linear/MyLinkedList.java similarity index 100% rename from group15/1517_279137987/my/collection/linear/MyLinkedList.java rename to group15/1517_279137987/src/my/collection/linear/MyLinkedList.java diff --git a/group15/1517_279137987/my/collection/linear/MyList.java b/group15/1517_279137987/src/my/collection/linear/MyList.java similarity index 100% rename from group15/1517_279137987/my/collection/linear/MyList.java rename to group15/1517_279137987/src/my/collection/linear/MyList.java diff --git a/group15/1517_279137987/my/collection/linear/MyQueue.java b/group15/1517_279137987/src/my/collection/linear/MyQueue.java similarity index 100% rename from group15/1517_279137987/my/collection/linear/MyQueue.java rename to group15/1517_279137987/src/my/collection/linear/MyQueue.java diff --git a/group15/1517_279137987/my/collection/linear/MyStack.java b/group15/1517_279137987/src/my/collection/linear/MyStack.java similarity index 100% rename from group15/1517_279137987/my/collection/linear/MyStack.java rename to group15/1517_279137987/src/my/collection/linear/MyStack.java diff --git a/group15/1517_279137987/my/collection/linearTest/MyArrayListTest.java b/group15/1517_279137987/src/my/collection/linearTest/MyArrayListTest.java similarity index 100% rename from group15/1517_279137987/my/collection/linearTest/MyArrayListTest.java rename to group15/1517_279137987/src/my/collection/linearTest/MyArrayListTest.java diff --git a/group15/1517_279137987/src/my/collection/linearTest/MyBinaryTreeNodeTest.java b/group15/1517_279137987/src/my/collection/linearTest/MyBinaryTreeNodeTest.java new file mode 100644 index 0000000000..17a6df5e5a --- /dev/null +++ b/group15/1517_279137987/src/my/collection/linearTest/MyBinaryTreeNodeTest.java @@ -0,0 +1,24 @@ +package my.collection.linearTest; + +import my.collection.linear.MyBinaryTreeNode; + +public class MyBinaryTreeNodeTest { + + public static void main(String[] args) { + MyBinaryTreeNode myTree = new MyBinaryTreeNode(); + + myTree.insert(new Integer(8)); + myTree.insert(new Integer(12)); + myTree.insert(new Integer(6)); + myTree.insert(new Integer(3)); + myTree.insert(new Integer(7)); + + System.out.println(myTree.getData().toString()); //8 + System.out.println(myTree.getLeft().getData().toString()); //6 + System.out.println(myTree.getLeft().getData().toString()); //3 + System.out.println(myTree.getRight().getData().toString()); //12 + + + } + +} diff --git a/group15/1517_279137987/my/collection/linearTest/MyLinkedListTest.java b/group15/1517_279137987/src/my/collection/linearTest/MyLinkedListTest.java similarity index 100% rename from group15/1517_279137987/my/collection/linearTest/MyLinkedListTest.java rename to group15/1517_279137987/src/my/collection/linearTest/MyLinkedListTest.java diff --git a/group15/1517_279137987/my/collection/linearTest/MyQueueTest.java b/group15/1517_279137987/src/my/collection/linearTest/MyQueueTest.java similarity index 100% rename from group15/1517_279137987/my/collection/linearTest/MyQueueTest.java rename to group15/1517_279137987/src/my/collection/linearTest/MyQueueTest.java diff --git a/group15/1517_279137987/my/collection/linearTest/MyStackTest.java b/group15/1517_279137987/src/my/collection/linearTest/MyStackTest.java similarity index 100% rename from group15/1517_279137987/my/collection/linearTest/MyStackTest.java rename to group15/1517_279137987/src/my/collection/linearTest/MyStackTest.java diff --git a/group15/1517_279137987/src/my/collection/linearTestJUnit/MyArrayListTest.java b/group15/1517_279137987/src/my/collection/linearTestJUnit/MyArrayListTest.java new file mode 100644 index 0000000000..2216d2c830 --- /dev/null +++ b/group15/1517_279137987/src/my/collection/linearTestJUnit/MyArrayListTest.java @@ -0,0 +1,109 @@ +package my.collection.linearTestJUnit; + +import my.collection.linear.MyArrayList; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class MyArrayListTest { + + MyArrayList mal = new MyArrayList(5); + + @Before + public void setUp() throws Exception { + for(int i=0; i0){ + for(int i=0;i=array1.length&&b=array2.length&&am)list.add(n); + } + int[] arr = new int[list.size()]; + for(int i=0;i + + + + + diff --git a/group15/1519_137845093/.gitignore b/group15/1519_137845093/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group15/1519_137845093/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group15/1519_137845093/.project b/group15/1519_137845093/.project new file mode 100644 index 0000000000..7371704aff --- /dev/null +++ b/group15/1519_137845093/.project @@ -0,0 +1,17 @@ + + + helloworld + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group15/1519_137845093/3.5_homework/array/ArrayUtil.java b/group15/1519_137845093/3.5_homework/array/ArrayUtil.java new file mode 100644 index 0000000000..e8660ed7ce --- /dev/null +++ b/group15/1519_137845093/3.5_homework/array/ArrayUtil.java @@ -0,0 +1,210 @@ +package com.coderising.array; +import java.util.*; +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + if(origin.length > 0 && origin != null){ + int temp; + for (int i = 0; i < origin.length / 2; i++) { + temp = origin[i]; + origin[i] = origin[origin.length-1 - i]; + origin[origin.length-1 - i] = temp; + } + } + else { + throw new IndexOutOfBoundsException("鍘熸暟缁勬湁閿" ); + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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){ + if(oldArray.length == 0){ + return oldArray; + } + int index = 0; + for(int i =0; i< oldArray.length; i++){ + if(oldArray[index] != 0){ + oldArray[index] = oldArray[i]; + index++; + } + } + return Arrays.copyOf(oldArray, index); + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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 length1 = array1.length; + int length2 = array2.length; + int newArrayLength = length1 + length2; + int[] result = new int[newArrayLength]; + int i = 0, j = 0, k = 0; //i:鐢ㄤ簬鏍囩ず1鏁扮粍 j锛氱敤鏉ユ爣绀2鏁扮粍 k锛氱敤鏉ユ爣绀轰紶鍏ョ殑鏁扮粍 + + while (i < length1 && j < length2) { + /* 鍘婚噸澶嶅厓绱 */ + if (array1[i] < array2[j]) { + result[k++] = array1[i++]; + } else if (array1[i] == array2[j]) { + result[k++] = array1[i]; + //鍦ㄦ煇涓綅缃笂2涓肩浉绛夌殑璇濓紝鍙栧摢涓兘涓鏍凤紝 + // 鐒跺悗杩欎釜鐩哥瓑鐨勪綅缃殑2涓奸兘鐩存帴鍚戝悗绉诲姩1锛岀户缁瘮杈 + j++; + i++; + } else { + result[k++] = array2[j++]; + } + } + /* 鍚庨潰while寰幆鏄敤鏉ヤ繚璇佷袱涓暟缁勬瘮杈冨畬涔嬪悗鍓╀笅鐨勪竴涓暟缁勯噷鐨勫厓绱犺兘椤哄埄浼犲叆缁撴灉鏁扮粍 */ + while (i < length1) { + result[k++] = array1[i++]; + } + + while (j < length2) { + result[k++] = array2[j++]; + } + return Arrays.copyOf(result, k); + } + + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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[] newArr = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArr, 0, oldArray.length); + return newArr; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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[] array = new int[]{}; + for(int i=1; getfb(i) k) { + if (i % k == 0) + break; + k++; + } + //鎵╁ + prime = grow(prime, 1); + prime[j++] = i; + } + return prime; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] array = new int[0]; + int j = 0; + for (int i = 1; i < max; i++) { + if (isPerfect(i)) { + //鍔犲叆鍒版暟缁勪腑 + array = grow(array, 1); + array[j] = i; + j++; + } + } + return array; + } + + //鍒ゆ柇涓涓暟鏄惁鏄滃畬鏁扳 + private static boolean isPerfect(int max) { + int i = 1; + int n = 0; + while (i < max) { + if (max % i == 0) { + n += i; + } + i++; + } + if (n == max) + return true; + return false; + } + + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + if (array == null) + return null; + String s = ""; + for (int i = 0; i < array.length; i++) { + s += array[i]; + if (i != array.length - 1) + s += seperator; + } + return s; + } + +} + + diff --git a/group15/1519_137845093/src_1st_homework_1519_137845093/ArrayList.java b/group15/1519_137845093/src_1st_homework_1519_137845093/ArrayList.java new file mode 100644 index 0000000000..e0ef246f9b --- /dev/null +++ b/group15/1519_137845093/src_1st_homework_1519_137845093/ArrayList.java @@ -0,0 +1,117 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class ArrayList implements List { + //元素个数 + private int size = 0; + //初始长度设为10 + private Object[] elementData = new Object[10]; + + public void add(Object o) { + int len = size + 1; + // 判断list的长度是否大于数组长度 + if (len > elementData.length) { + // 创建新容器 + Object[] newElemData = new Object[elementData.length + 1]; + // 复制旧容器所有元素到新容器 + System.arraycopy(elementData, 0, newElemData, 0, elementData.length); + elementData = newElemData; + } + elementData[size] = o; + size++; + } + + public void add(int index, Object o) { + // 检查下标是否越界 + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index:" + index + "越界;" ); + } + // 插入元素到数组的末尾直接调用add方法 + if (index == size) { + add(o); + } else { + // 创建新容器 + Object[] newElemData = new Object[elementData.length + 1]; + // 复制index以前的所有元素到新容器 + System.arraycopy(elementData, 0, newElemData, 0, index); + newElemData[index] = o; + // 复制index 及以后的元素到新容器 + System.arraycopy(elementData, index, newElemData, index + 1, size - index); + + elementData = newElemData; + size++; + } + } + + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("index:" + index + "越界;"); + } + return elementData[index]; + } + + public Object remove(int index) { + //下标大于数组长度的,抛出异常 + if (index >= size) { + throw new IndexOutOfBoundsException("index:" + index + "越界;"); + } + //index不是最后一个元素的索引值才需要删除操作 + if(index != (size-1)){ + // 创建新容器 + Object[] newElemData = new Object[elementData.length]; + // 复制index以前的所有元素到新容器 + System.arraycopy(elementData, 0, newElemData, 0, index); + // 复制index 以后的元素到新容器 + System.arraycopy(elementData, index+1, newElemData, index, size - index -1); + } + Object removeElement = elementData[index]; + //减小数组的长度 + size--; + return removeElement; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return new MyItr(this); + } + + private class MyItr implements Iterator { + private int l = -1; + private ArrayList array = null; + + private MyItr(ArrayList array) { + this.array = array; + } + + @Override + public boolean hasNext() { + return (l + 1) < array.size; + } + + @Override + public Object next() { + l++; + if (l >= array.size) { + l = array.size - 1 ; + throw new IndexOutOfBoundsException(); + } + + return array.get(l); + } + + @Override + public Object remove() { + if (l < 0) { + throw new NoSuchElementException(); + } + Object val = array.remove(l); + l--; + return val; + } + + } +} \ No newline at end of file diff --git a/group15/1519_137845093/src_1st_homework_1519_137845093/BinaryTreeNode.java b/group15/1519_137845093/src_1st_homework_1519_137845093/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group15/1519_137845093/src_1st_homework_1519_137845093/BinaryTreeNode.java @@ -0,0 +1,32 @@ +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/group15/1519_137845093/src_1st_homework_1519_137845093/Iterator.java b/group15/1519_137845093/src_1st_homework_1519_137845093/Iterator.java new file mode 100644 index 0000000000..2d4150183f --- /dev/null +++ b/group15/1519_137845093/src_1st_homework_1519_137845093/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + public Object remove(); + +} diff --git a/group15/1519_137845093/src_1st_homework_1519_137845093/LinkedList.java b/group15/1519_137845093/src_1st_homework_1519_137845093/LinkedList.java new file mode 100644 index 0000000000..7f06138ec1 --- /dev/null +++ b/group15/1519_137845093/src_1st_homework_1519_137845093/LinkedList.java @@ -0,0 +1,180 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o){ + //判断有没有头结点 + if(head == null) + head = new Node(o,null); + else { + Node newNode = head; + while(newNode.next != null){ + newNode = newNode.next; + } + newNode.next = new Node(o,null); + + } + + } + public void add(int index , Object o){ + //检查下标是否越界 + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("index:" + index + "越界;"); + } + Node node = head; + //插入的是第一个头结点 + if(index == 0){ + Node newNode = new Node(o,head); + head = newNode; + size ++; + } + else{ + for(int i = 1; i < index; i++){ + node = node.next; + } + //在index处插入o,并且将o的next节点设为node.next + Node newNode = new Node(o, node.next); + node.next = newNode; + size++; + } +} + + public Object get(int index){ + //检查下标是否越界 + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("index:" + index + "越界;"); + } + Node node = head; + for (int i = 1; i <= index; i++) { + node = node.next; + } + return node.data; + } + + public Object remove(int index){ + //检查下标是否越界 + if(index < 0 || index > size){ + throw new IndexOutOfBoundsException("index:" + index + "越界;"); + } + Node node = head; + Node removeNode; + if (index == 0) { + //第一个节点直接将头节点指向下一个节点 + removeNode = head; + head = head.next; + } + else { + //找到索引值的前一个节点 + for (int i = 1; i < index; i++) { + node = node.next; + } + removeNode = node.next; + //前一个节点指针,指向被删除节点所指向的节点 + node.next = removeNode.next; + } + size--; + return removeNode.data; + } + + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node newNode = new Node(o, head.next); + head.next = newNode; + size++; + } + + public void addLast(Object o){ + add(o); + } + + public Object removeFirst(){ + if(size <= 0){ + throw new IndexOutOfBoundsException("没有元素;"); + } + Node node = head; + head = head.next; + size--; + return node.data; + } + + public Object removeLast(){ + if(size <= 0){ + throw new IndexOutOfBoundsException("没有元素;"); + } + Node node = head; + while(node.next != null){ + node = node.next; + } + Object val = node.data; + node = null; + size--; + return val; + } + private static class Node{ + Object data; + Node next; + + Node(Object data, Node next) { + this.data = data; + this.next = next; + + } + } + + + + + public Iterator iterator(){ + return new Itr(this); + } + + private class Itr implements Iterator{ + private int l = -1; + private LinkedList list; + private Itr(LinkedList linkedList) { + // TODO Auto-generated constructor stub + this.list = list; + + } + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return l < list.size - 1; + } + + @Override + public Object next() { + // TODO Auto-generated method stub + l++; + if (l >= list.size) { + l--; + throw new IndexOutOfBoundsException(); + } + + return list.get(l); + } + + @Override + public Object remove() { + // TODO Auto-generated method stub + if (l < 0) { + throw new NoSuchElementException(); + } + Object val = list.removeLast(); + l--; + return val; + } + + } + + } diff --git a/group06/1454385822/src/com/coding/basic/List.java b/group15/1519_137845093/src_1st_homework_1519_137845093/List.java similarity index 98% rename from group06/1454385822/src/com/coding/basic/List.java rename to group15/1519_137845093/src_1st_homework_1519_137845093/List.java index 1fd3aa61b3..10d13b5832 100644 --- a/group06/1454385822/src/com/coding/basic/List.java +++ b/group15/1519_137845093/src_1st_homework_1519_137845093/List.java @@ -1,11 +1,9 @@ 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/group15/1519_137845093/src_1st_homework_1519_137845093/Queue.java b/group15/1519_137845093/src_1st_homework_1519_137845093/Queue.java new file mode 100644 index 0000000000..3433125a8b --- /dev/null +++ b/group15/1519_137845093/src_1st_homework_1519_137845093/Queue.java @@ -0,0 +1,29 @@ +package com.coding.basic; +import java.util.NoSuchElementException; + +public class Queue { + private int size; + private LinkedList list = new LinkedList(); + + public void enQueue(Object o){ + list.addLast(o); + size++; + } + + public Object deQueue(){ + if(size<=0){ + throw new NoSuchElementException(); + } + Object deQueue = list.removeLast(); + size--; + return deQueue; + } + + public boolean isEmpty(){ + return (size>=0); + } + + public int size(){ + return size; + } +} diff --git a/group15/1519_137845093/src_1st_homework_1519_137845093/Stack.java b/group15/1519_137845093/src_1st_homework_1519_137845093/Stack.java new file mode 100644 index 0000000000..036baafc73 --- /dev/null +++ b/group15/1519_137845093/src_1st_homework_1519_137845093/Stack.java @@ -0,0 +1,39 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size; + private Object removeElement; + public void push(Object o){ + elementData.add(o); + size++; + } + + public Object pop(){ + if(size<=0){ + throw new NoSuchElementException(); + } + int l = size - 1; + removeElement = elementData.remove(l); + size--; + return removeElement; + } + + public Object peek(){ + if(size<=0){ + throw new NoSuchElementException(); + } + int len = size-1; + return elementData.get(len); + } + + public boolean isEmpty(){ + return (size>=0); + } + + public int size(){ + return size; + } +} diff --git a/group15/1521_653895972/.gitignore b/group15/1521_653895972/.gitignore new file mode 100644 index 0000000000..6c9d8afec3 --- /dev/null +++ b/group15/1521_653895972/.gitignore @@ -0,0 +1,3 @@ +/out +.idea +*.iml \ No newline at end of file diff --git a/group15/1521_653895972/src/com/coding/coderising/array/ArrayUtilTest.java b/group15/1521_653895972/src/com/coding/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..92c581254b --- /dev/null +++ b/group15/1521_653895972/src/com/coding/coderising/array/ArrayUtilTest.java @@ -0,0 +1,73 @@ +package com.coding.coderising.array; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by wanc on 2017/2/28. + */ +public class ArrayUtilTest { + + @Test + public void testReverseArray() throws Exception { + int[] arr = {7, 9, 30, 3}; + SimpleArrayUtil.reverseArray(arr); + System.out.println(Arrays.toString(arr)); + System.out.println("----------------------缃崲 end-----------------------------"); + } + + + @Test + public void testRemoveZero() throws Exception { + int oldArr[] = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + int newArr[] = SimpleArrayUtil.removeZero(oldArr); + System.out.println(Arrays.toString(newArr)); + System.out.println("----------------------鍘婚浂 end-----------------------------"); + } + + @Test + public void testMerge() throws Exception { + int arr1[] = {3, 5, 7, 8}; + int arr2[] = {4, 5, 6, 7}; + int arr3[] = SimpleArrayUtil.merge(arr1, arr2); + System.out.println(Arrays.toString(arr3)); + System.out.println("----------------------merge end-----------------------------"); + } + + @Test + public void testGrow() throws Exception { + int arr1[] = {3, 5, 7, 8}; + int[] newArr = SimpleArrayUtil.grow(arr1, 3); + System.out.println(Arrays.toString(newArr)); + System.out.println("----------------------鎵╁睍 end-----------------------------"); + } + + @Test + public void testFibonacci() throws Exception { + int[] arr = SimpleArrayUtil.fibonacci(15); + System.out.println(Arrays.toString(arr)); + System.out.println("----------------------鏂愭尝閭e end-----------------------------"); + } + + @Test + public void testGetPrimes() throws Exception { + int[] arr = SimpleArrayUtil.getPrimes(23); + System.out.println(Arrays.toString(arr)); + System.out.println("----------------------绱犳暟 end-----------------------------"); + } + + @Test + public void testGetPerfectNumbers() throws Exception { + int[] newArr = SimpleArrayUtil.getPerfectNumbers(50); + System.out.println(Arrays.toString(newArr)); + System.out.println("----------------------瀹屾暟 end-----------------------------"); + } + + @Test + public void testJoin() throws Exception { + int arr1[] = {3, 5, 7, 8}; + System.out.println(SimpleArrayUtil.join(arr1, "-")); + System.out.println("----------------------Join end-----------------------------"); + } +} \ No newline at end of file diff --git a/group15/1521_653895972/src/com/coding/coderising/array/SimpleArrayUtil.java b/group15/1521_653895972/src/com/coding/coderising/array/SimpleArrayUtil.java new file mode 100644 index 0000000000..0aa491fb16 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/coderising/array/SimpleArrayUtil.java @@ -0,0 +1,250 @@ +package com.coding.coderising.array; + +public class SimpleArrayUtil { + + private static void checkNull(int[] array) { + if (array == null) + throw new NullPointerException("array is null"); + } + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + * 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public static void reverseArray(int[] origin) { + checkNull(origin); + int i = 0; + for (int j = origin.length - 1; j > i; ++i) { + int tmp = origin[i]; + origin[i] = origin[j]; + origin[j] = tmp; + --j; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 static int[] removeZero(int[] oldArray) { + checkNull(oldArray); + if (0 == oldArray.length) + return oldArray; + int[] temp = new int[oldArray.length]; + int index = 0; + for (int i = 0; i < oldArray.length; i++) { + if (0 != oldArray[i]) { + temp[index++] = oldArray[i]; + } + } + int[] tem2 = new int[index]; + + System.arraycopy(temp, 0, tem2, 0, Math.min(oldArray.length, index)); + return tem2; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * + * @param array1 + * @param array2 + * @return + */ + public static int[] merge(int[] array1, int[] array2) { + if (array1 == null && array2 == null) + return null; + if (array1.length == 0) + return array2.clone(); + if (array2.length == 0) + return array1.clone(); + //鍒ゆ柇鎺掑簭鏂瑰悜 + boolean sort = array1[0] < array1[array1.length - 1]; + //鍘婚噸 + for (int i = 0; i < array2.length; i++) { + boolean flag = true; + for (int j = 0; j < array1.length; j++) { + if (array2[i] == array1[j]) { + flag = false; + break; + } + } + if (flag) { + //鎵╁ + array1 = grow(array1, 1); + array1[array1.length - 1] = array2[i]; + } + } + //鎺掑簭 + if (sort) {//灏忓埌澶 + int tmp; + //鍐掓场鎺掑簭 + for (int i = 0; i < array1.length; i++) { + for (int j = i + 1; j < array1.length; j++) { + if (array1[i] > array1[j]) { + tmp = array1[i]; + array1[i] = array1[j]; + array1[j] = tmp; + } + } + } + } else {//澶у埌灏 + int tmp; + //鍐掓场鎺掑簭 + for (int i = 0; i < array1.length; i++) { + for (int j = i + 1; j < array1.length; j++) { + if (array1[i] < array1[j]) { + tmp = array1[i]; + array1[i] = array1[j]; + array1[j] = tmp; + } + } + } + } + return array1; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + int[] newArr = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArr, 0, oldArray.length); + return newArr; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + int[] arr = new int[]{}; + int i = 1; + while (true) { + int a = fb(i); + if (a > 15) + break; + //鎵╁ + arr = grow(arr, 1); + arr[i - 1] = a; + i++; + } + return arr; + } + + /** + * 鑾峰彇鏂愭尝閭e鏁 + * F(0)=1锛孎(1)=1, F(n)=F(n-1)+F(n-2)锛坣>=2锛宯鈭圢*锛 + */ + public static int fb(int i) { + i = i < 1 ? 1 : i; + if (i == 1 || i == 2) { + return 1; + } + return fb(i - 1) + fb(i - 2); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + int[] arr = new int[]{2}; + if (max < 1) throw new IllegalArgumentException("涓嶆槸澶т簬1鐨勮嚜鐒舵暟"); + int j = 1; + for (int n = 3; n < max; n++) { + int k = 2; + while (n > k) { + if (n % k == 0) + break; + k++; + } + //鎵╁ + arr = grow(arr, 1); + arr[j++] = n; + } + return arr; + } + + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + int[] arr = new int[0]; + int a = 0; + for (int i = 1; i < max; i++) { + if (isPerfect(i)) { + //鎵╁ + arr = grow(arr, 1); + arr[a++] = i; + } + } + return arr; + } + + //鍒ゆ柇 鈥滃畬鏁扳 + public static boolean isPerfect(int max) { + int i = 1; + int n = 0; + while (i < max) { + if (max % i == 0) { + n += i; + } + i++; + } + if (n == max) + return true; + return false; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public static String join(int[] array, String seperator) { + if (array == null) + return null; + String s = ""; + for (int i = 0; i < array.length; i++) { + s += array[i]; + if (i != array.length - 1) + s += seperator; + } + return s; + } + + +} diff --git a/group15/1521_653895972/src/com/coding/coderising/litestruts/LoginAction.java b/group15/1521_653895972/src/com/coding/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..273741bbfb --- /dev/null +++ b/group15/1521_653895972/src/com/coding/coderising/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +package com.coding.coderising.litestruts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * + * @author wanc + */ +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/group15/1521_653895972/src/com/coding/coderising/litestruts/Struts.java b/group15/1521_653895972/src/com/coding/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..ed20c588fe --- /dev/null +++ b/group15/1521_653895972/src/com/coding/coderising/litestruts/Struts.java @@ -0,0 +1,145 @@ +package com.coding.coderising.litestruts; + +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; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + //瑙f瀽xml + Map xmlInfo = parsersXml(actionName); + //鑾峰彇绫诲悕 + String allName = (String) xmlInfo.get("className"); + try { + //鍔犺浇绫 + Class cls = Class.forName(allName); + //瀹炰緥鍖 + Object object = cls.newInstance(); + for (String key : parameters.keySet()) { + //鎷兼帴set鏂规硶鍚 + String setName = "set" + key.substring(0, 1).toUpperCase() + key.substring(1); + Method setMethod = cls.getDeclaredMethod(setName, String.class); + //鏀惧皠鎵цset鏂规硶 + setMethod.invoke(object, parameters.get(key)); + } + //鎵цexecute鏂规硶 + Method exectue = cls.getDeclaredMethod("execute", null); + String reslut = (String) exectue.invoke(object, null); + //鎵цgetMessage鏂规硶 + Method getMessage = cls.getDeclaredMethod("getMessage", null); + String message = (String) getMessage.invoke(object, null); + //鑾峰彇xml閰嶇疆鎯宠繑鍥炵粨鏋 + String jsp = (String) xmlInfo.get(reslut); + //缁勮view + Map map = new HashMap(); + map.put("message", message); + View view = new View(); + view.setJsp(jsp); + view.setParameters(map); + return view; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return null; + } + + + private static Map parsersXml(String actionName) { + File file = new File(Struts.class.getResource("").getPath() + "/struts.xml"); + //1.鑾峰彇DOM瑙f瀽鍣ㄥ伐鍘 + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + HashMap map = new HashMap<>(); + try { + //2.鑾峰彇瑙f瀽鍣 + DocumentBuilder builder = factory.newDocumentBuilder(); + //3.鍔犺浇xml鏂囨。 + Document document = builder.parse(file); + //4.鑾峰彇鎸囧畾action鐨刟ction闆嗗悎 + NodeList actionlist = document.getElementsByTagName("action"); + //5.濡傛灉actionName閲嶅 鍙幓绗竴涓 + for (int j = 0; j < actionlist.getLength(); j++) { + Element actionElement = (Element) actionlist.item(j); + if (actionElement.getAttribute("name") != null && actionElement.getAttribute("name").equalsIgnoreCase(actionName)) { + //6.鑾峰彇 绫诲叏闄愬畾鍚 + String className = actionElement.getAttribute("class"); + map.put("className", className); + //7.鑾峰彇 action瀛愯妭鐐 + NodeList childList = actionElement.getChildNodes(); + int lenght = childList.getLength(); + for (int i = 0; i < lenght; i++) { + Node node = childList.item(i); + //鍒ゆ柇涓篹lement鑺傜偣 鎺掗櫎绌烘牸 鎹㈣ + if (node.getNodeType() == Node.ELEMENT_NODE) { + Element child = (Element) node; + switch (child.getAttribute("name")) { + case "success": + map.put("success", child.getTextContent()); + break; + case "fail": + map.put("fail", child.getTextContent()); + break; + case "error": + map.put("error", child.getTextContent()); + break; + } + } + } + break; + } + + } + + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } catch (SAXException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return map; + } +} diff --git a/group15/1521_653895972/src/com/coding/coderising/litestruts/StrutsTest.java b/group15/1521_653895972/src/com/coding/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..8699cc3eca --- /dev/null +++ b/group15/1521_653895972/src/com/coding/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coding.coderising.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + + + + +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/group15/1521_653895972/src/com/coding/coderising/litestruts/View.java b/group15/1521_653895972/src/com/coding/coderising/litestruts/View.java new file mode 100644 index 0000000000..2c909058cb --- /dev/null +++ b/group15/1521_653895972/src/com/coding/coderising/litestruts/View.java @@ -0,0 +1,27 @@ +package com.coding.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/1521_653895972/src/com/coding/coderising/litestruts/struts.xml b/group15/1521_653895972/src/com/coding/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..bec462fdf5 --- /dev/null +++ b/group15/1521_653895972/src/com/coding/coderising/litestruts/struts.xml @@ -0,0 +1,12 @@ + + + + /jsp/homepage.jsp + /jsp/showLogin.jsp + + + + /jsp/welcome.jsp + /jsp/error.jsp + + \ No newline at end of file diff --git a/group16/1154151360/.classpath b/group16/1154151360/.classpath index fb5011632c..9746a6c933 100644 --- a/group16/1154151360/.classpath +++ b/group16/1154151360/.classpath @@ -2,5 +2,8 @@ + + + diff --git a/group16/1154151360/src/com/array/ArrayUtil.java b/group16/1154151360/src/com/array/ArrayUtil.java new file mode 100644 index 0000000000..39f587c847 --- /dev/null +++ b/group16/1154151360/src/com/array/ArrayUtil.java @@ -0,0 +1,266 @@ +package com.array; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import com.sun.org.apache.xalan.internal.xsltc.compiler.sym; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public int[] reverseArray(int[] origin){ + + int [] temp = new int [origin.length]; + + for (int i = 0; i < temp.length; i++){ + temp [i] = origin [temp.length - 1 - i]; + } + + origin = temp; + + return origin; + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 newLength = 0; + + for (int i = 0; i < oldArray.length ; i++){ + if (oldArray[i] == 0) + newLength ++; + } + + int [] newArray = new int [oldArray.length - newLength]; + int index = 0; + for (int j = 0; j < oldArray.length; j++){ + if (oldArray[j] != 0) + newArray[index++] = oldArray[j]; + } + + newArray = sort(newArray, 0, newArray.length - 1); + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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 newLength = array1.length + array2.length; + int [] newArray = new int [newLength]; + System.arraycopy(array1, 0, newArray, 0, array1.length); + System.arraycopy(array2, 0, newArray, array1.length, array2.length); + Set arraySet = new HashSet(); + for (int i = 0; i < newArray.length; i++){ + arraySet.add(newArray[i]); + } + int [] tempArray = new int[arraySet.size()]; + Iterator iterator = arraySet.iterator(); + int index = 0; + while (iterator.hasNext()){ + tempArray[index++] = iterator.next(); + } + newArray = sort(tempArray, 0, tempArray.length - 1); + return newArray; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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, size); + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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){ + List array = new ArrayList(); + array.add(1); + array.add(1); + for(int i = 2;;i++){ + array.add(array.get(i - 1) + array.get(i - 2)); + if (array.get(i) > max){ + array.remove(i); + break; + } + } + int [] result = returnArray(array); + return result; + }else{ + int [] b = new int[]{}; + return b; + } + + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + ArrayList list1 = new ArrayList(); + ArrayList list2 = new ArrayList(); + for (int i = 2; i < max; i++){ + list1.add(i); + } + + for (int m = 0; m < list1.size(); m++){ + boolean flag = true; + for (int n = 2; n < list1.get(m); n++){ + if (list1.get(m) % n == 0){ + flag = false; + break; + } + + } + if (flag) + list2.add(list1.get(m)); + } + + int [] result = returnArray(list2); + + return result; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + ArrayList array = new ArrayList(); + Map> arrayMap = new HashMap>(); + ArrayList array2 = new ArrayList(); + for (int i = 2; i < max; i++){ + array.add(i); + } + for(int m = 0; m < array.size(); m++){ + ArrayList tempArray = new ArrayList(); + for (int n = 2; n < array.get(m);n++){ + if (array.get(m) % n == 0) + tempArray.add(n); + } + arrayMap.put(array.get(m), tempArray); + } + for(Map.Entry> entry: arrayMap.entrySet()){ + Integer key = entry.getKey(); + ArrayList tempArray = entry.getValue(); + Integer tempInt = 0; + for (Integer i:tempArray){ + tempInt += i; + } + if (key == tempInt){ + array2.add(key); + } + } + int [] result = returnArray(array2); + + return result; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + + StringBuilder builder = new StringBuilder(); + + for (int i = 0; i < array.length; i++){ + builder.append(array[i]); + builder.append(seperator); + } + builder.deleteCharAt(builder.length() - 1); + return builder.toString(); + } +//***************************宸ュ叿绫***************************************** + public int [] returnArray( List list){ + int [] result = new int[list.size()]; + int index = 0; + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + result[index++] = iterator.next(); + } + return result; + } + + //蹇熸帓搴忕畻娉 + public int [] sort (int [] array, int low, int height ){ + + int start = low; + int end = height; + int key = array[low]; + + while (end > start){ + //浠庡悗寰鍓嶉亶鍘 + while (end > start && array[end] >= key) + end--; + + if (array[end] <= key){ + int temp = array[end]; + array[end] = array[start]; + array[start] = temp; + } + + //浠庡墠寰鍚庨亶鍘 + while(end > start && array[start] <= key) + start++; + + if (array[start] >= key){ + int temp = array[start]; + array[start] = array[end]; + array[end] = temp; + } + } + + if(start > low)sort(array, low, start - 1); + if (end < height)sort(array, end + 1, height); + + return array; + } +//******************************************************************8 +} diff --git a/group16/1154151360/src/com/array/ArrayUtilTest.java b/group16/1154151360/src/com/array/ArrayUtilTest.java new file mode 100644 index 0000000000..159d497b6d --- /dev/null +++ b/group16/1154151360/src/com/array/ArrayUtilTest.java @@ -0,0 +1,102 @@ +package com.array; + +import static org.junit.Assert.*; +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; +@SuppressWarnings("deprecation") +public class ArrayUtilTest { + + ArrayUtil util; + + @Before + public void init(){ + util = new ArrayUtil(); + } + + @Test + public void test_reverseArray() { + int [] a = {7, 9, 30, 3, 4}; + a = util.reverseArray(a); + Assert.assertEquals("[4,3,30,9,7]", toString(a)); + } + + @Test + public void test_removeZero(){ + int [] oldArr={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + + oldArr = util.removeZero(oldArr); + + Assert.assertEquals("[1,3,4,5,6,6,5,4,7,6,7,5]", toString(oldArr)); + } + + + @Test + public void test_merge(){ + int [] a1 = {3, 5, 7,8}; + int [] a2 = {4, 5, 6,7}; + int [] a3 = util.merge(a1, a2); + + Assert.assertEquals("[3,4,5,6,7,8]", toString(a3)); + + } + + @Test + public void test_grow(){ + int [] oldArray = {2,3,6}; + int size = 3; + int [] newArray = util.grow(oldArray, size); + + Assert.assertEquals("[2,3,6,0,0,0]", toString(newArray)); + } + + + @Test + public void test_fibonacci(){ + + int [] array = util.fibonacci(15); + + Assert.assertEquals("[1锛1锛2锛3锛5锛8锛13]", toString(array)); + + } + + + @Test + public void test_getPrimes(){ + + int [] array = util.getPrimes(23); + Assert.assertEquals("[2,3,5,7,11,13,17,19]", toString(array)); + + } + + @Test + public void test_getPerfectNumbers(){ + int [] array = util.getPerfectNumbers(10); + + Assert.assertEquals("[6]", toString(array)); + + } + + @Test + public void test_join(){ + + int [] array = {3,8,9}; + String result = util.join(array, "-"); + Assert.assertEquals("3-8-9", result); + } + + + public String toString(int [] array){ + StringBuilder builder = new StringBuilder(); + builder.append("["); + for(int item: array){ + builder.append(item) + .append(","); + } + builder.replace(builder.length() - 1, builder.length(), ""); + builder.append("]"); + return builder.toString(); + + } +} diff --git a/group16/1154151360/src/com/litestruts/LoginAction.java b/group16/1154151360/src/com/litestruts/LoginAction.java new file mode 100644 index 0000000000..24369135f9 --- /dev/null +++ b/group16/1154151360/src/com/litestruts/LoginAction.java @@ -0,0 +1,40 @@ +package com.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/group16/1154151360/src/com/litestruts/Struts.java b/group16/1154151360/src/com/litestruts/Struts.java new file mode 100644 index 0000000000..49254d6a5f --- /dev/null +++ b/group16/1154151360/src/com/litestruts/Struts.java @@ -0,0 +1,139 @@ +package com.litestruts; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + + + //鍒涘缓SAXreader瀵硅薄 + SAXReader reader = new SAXReader(); + //灏嗚鍙栫殑鏂囦欢灏佽鎴恉ocument瀵硅薄 + Document document = reader.read(new File("src/com/litestruts/struts.xml")); + //鑾峰彇鏂囨。鐨勬牴鑺傜偣 + Element root = document.getRootElement(); + + List elist = listNode(root); + + Map > actionMap = getAction(elist, actionName); + + for(Map.Entry> entity: actionMap.entrySet()){ + String className = entity.getKey(); + Class clazz = Class.forName(className); + Method setName = clazz.getMethod("setName", String.class); + Method setPassword = clazz.getMethod("setPassword", String.class); + Method getMessage = clazz.getMethod("getMessage"); + Method execute = clazz.getMethod("execute"); + Object object = clazz.newInstance(); + setName.invoke(object, parameters.get("name")); + setPassword.invoke(object, parameters.get("password")); + String status = (String) execute.invoke(object); + String message = (String) getMessage.invoke(object); + + String jsp = entity.getValue().get(status); + + Map parameter = new HashMap(); + + parameter.put("message", message); + + View view = new View(); + + view.setJsp(jsp); + view.setParameters(parameter); + + return view; + } + + return null; + } + + //鑾峰彇鑺傜偣鐨勬墍鏈夊瓙鑺傜偣 + private static List listNode(Element node){ + + Iterator iterator = node.elementIterator(); + List elist = new ArrayList(); + while (iterator.hasNext()){ + Element e = iterator.next(); + elist.add(e); + } + if (elist.isEmpty()) + return null; + else + return elist; + } + + //鑾峰彇瀵瑰簲鐨凙ction淇℃伅 + private static Map > getAction(List elist,String actionName){ + + for (Element node:elist){ + List attributes = node.attributes(); + if (attributes.isEmpty()) + getAction(listNode(node),actionName); + else{ + Attribute attribute = node.attribute("name"); + if (attribute.getValue().equals(actionName)){ + String className = node.attribute("class").getValue(); + List childElements = listNode(node); + Map resMap =install(childElements); + Map > actionMap = new HashMap>(); + actionMap.put(className, resMap); + return actionMap; + } + } + } + return null; + } + + //缁勮action鐨剅esult + private static Map install(List elements){ + Map resultMap = new HashMap(); + for (Element node: elements){ + Attribute attribute = node.attribute("name"); + String value; + if (node.getTextTrim().isEmpty()) + value = ""; + else + value = node.getTextTrim(); + + resultMap.put(attribute.getValue(), value); + + } + return resultMap; + } + +} diff --git a/group16/1154151360/src/com/litestruts/StrutsTest.java b/group16/1154151360/src/com/litestruts/StrutsTest.java new file mode 100644 index 0000000000..9317cc5e26 --- /dev/null +++ b/group16/1154151360/src/com/litestruts/StrutsTest.java @@ -0,0 +1,51 @@ +package com.litestruts; + +import static org.junit.Assert.*; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import junit.framework.Assert; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.Test; + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + + 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, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + 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/group16/1154151360/src/com/litestruts/View.java b/group16/1154151360/src/com/litestruts/View.java new file mode 100644 index 0000000000..af63dce301 --- /dev/null +++ b/group16/1154151360/src/com/litestruts/View.java @@ -0,0 +1,23 @@ +package com.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/group16/1154151360/src/com/litestruts/struts.xml b/group16/1154151360/src/com/litestruts/struts.xml new file mode 100644 index 0000000000..ddbcbc38da --- /dev/null +++ b/group16/1154151360/src/com/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/group16/1287642108/0226/src/com/coding/basic/ArrayList.java b/group16/1287642108/0226/src/com/coding/basic/ArrayList.java index e287419dc0..f82d064e2a 100644 --- a/group16/1287642108/0226/src/com/coding/basic/ArrayList.java +++ b/group16/1287642108/0226/src/com/coding/basic/ArrayList.java @@ -1,63 +1,63 @@ -package com.coding.basic; - -public class ArrayList implements List { - - private int size = 0; - - private Object[] elementData = new Object[100]; - - public void add(Object o) { - IncrementsCapacity(size + 1); - elementData[size++] = o; - - } - - public void add(int index, Object o) { - checkIndex(index); - IncrementsCapacity(size + 1); - System.arraycopy(elementData, index, elementData, index + 1, size - index); - elementData[index] = o; - size++; - } - - public Object get(int index) { - checkIndex(index); - return elementData[index]; - } - - public Object remove(int index) { - checkIndex(index); - Object o = elementData[index]; - System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); - elementData[--size] = null; - return o; - } - - public int size() { - return size; - } - - public int length() { - return elementData.length; - } - - // 扩容 - private void IncrementsCapacity(int num) { - if (num > elementData.length) { - int oldCapacity = elementData.length; // 当前数组长度 - int newCapacity = ((num + oldCapacity) * 3) >> 2; // 当前数组长度的1.5倍 - if (newCapacity - num < 0) { - newCapacity = num; // 若新容量还是不够,直接扩为需求值 - } - Object[] oldelements = elementData; - elementData = new Object[newCapacity]; - System.arraycopy(oldelements, 0, elementData, 0, size); - } - } - - // 下标越界判断 - private void checkIndex(int index) { - if (index >= size || index < 0) - throw new IndexOutOfBoundsException("数组下标越界"); - } -} +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o) { + IncrementsCapacity(size + 1); + elementData[size++] = o; + + } + + public void add(int index, Object o) { + checkIndex(index); + IncrementsCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkIndex(index); + return elementData[index]; + } + + public Object remove(int index) { + checkIndex(index); + Object o = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[--size] = null; + return o; + } + + public int size() { + return size; + } + + public int length() { + return elementData.length; + } + + // 扩容 + private void IncrementsCapacity(int num) { + if (num > elementData.length) { + int oldCapacity = elementData.length; // 当前数组长度 + int newCapacity = ((num + oldCapacity) * 3) >> 2; // 当前数组长度的1.5倍 + if (newCapacity - num < 0) { + newCapacity = num; // 若新容量还是不够,直接扩为需求值 + } + Object[] oldelements = elementData; + elementData = new Object[newCapacity]; + System.arraycopy(oldelements, 0, elementData, 0, size); + } + } + + // 下标越界判断 + private void checkIndex(int index) { + if (index >= size || index < 0) + throw new IndexOutOfBoundsException("数组下标越界"); + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Iterator.java b/group16/1287642108/0226/src/com/coding/basic/Iterator.java index e7cbd474ec..ff93e30377 100644 --- a/group16/1287642108/0226/src/com/coding/basic/Iterator.java +++ b/group16/1287642108/0226/src/com/coding/basic/Iterator.java @@ -1,6 +1,6 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); -} +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group16/1287642108/0226/src/com/coding/basic/LinkedList.java b/group16/1287642108/0226/src/com/coding/basic/LinkedList.java index 99c92fb9f1..fc206b4cec 100644 --- a/group16/1287642108/0226/src/com/coding/basic/LinkedList.java +++ b/group16/1287642108/0226/src/com/coding/basic/LinkedList.java @@ -1,101 +1,101 @@ -package com.coding.basic; - -public class LinkedList implements List { - - private Node head; - private Node tail; - private int size = 0 ; - - public void add(Object o){ - addLast(o); - } - - public void add(int index, Object o) { - if (index == 0) { - addFirst(o); - } else if (index >= size) { - addLast(o); - } else { - Node node = new Node(); - node.data = o; - node.next = getNode(index); - getNode(index - 1).next = node; - size++; - } - } - - public Object get(int index) { - Node node = getNode(index); - return node.data; - } - - public Object remove(int index){ - Node currentNode = getNode(index); - Node prevNode = getNode(index - 1); - Node lastNode = getNode(index + 1); - prevNode.next = lastNode; - size--; - return currentNode.data; - } - - public int size(){ - return size; - } - - public void addFirst(Object o){ - Node node=new Node(); - node.data = o; - node.next = head; - head = node; - size++; - } - public void addLast(Object o){ - Node node=new Node(); - node.data = o; - node.next = null; - Node lastNode = getNode(size-1); - lastNode.next = node; - size++; - } - public Object removeFirst(){ - Object obj = getNode(0).data; - Node node = getNode(1); - node.next = head; - size--; - return obj; - } - public Object removeLast(){ - Object obj = getNode(size - 1).data; - Node node = getNode(size - 2); - node.next = null; - size--; - return obj; - } - - //获取节点 - public Node getNode(int index){ - checkIndex(index); - if(index == 0 ){ - return head; - }else if(index == size -1 ){ - return tail; - }else{ - Node node = head; - for(int i=0;i= size || index < 0) - throw new IndexOutOfBoundsException("数组下标越界"); - } - - private static class Node { - Object data; - Node next; - } -} +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private Node tail; + private int size = 0 ; + + public void add(Object o){ + addLast(o); + } + + public void add(int index, Object o) { + if (index == 0) { + addFirst(o); + } else if (index >= size) { + addLast(o); + } else { + Node node = new Node(); + node.data = o; + node.next = getNode(index); + getNode(index - 1).next = node; + size++; + } + } + + public Object get(int index) { + Node node = getNode(index); + return node.data; + } + + public Object remove(int index){ + Node currentNode = getNode(index); + Node prevNode = getNode(index - 1); + Node lastNode = getNode(index + 1); + prevNode.next = lastNode; + size--; + return currentNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node=new Node(); + node.data = o; + node.next = head; + head = node; + size++; + } + public void addLast(Object o){ + Node node=new Node(); + node.data = o; + node.next = null; + Node lastNode = getNode(size-1); + lastNode.next = node; + size++; + } + public Object removeFirst(){ + Object obj = getNode(0).data; + Node node = getNode(1); + node.next = head; + size--; + return obj; + } + public Object removeLast(){ + Object obj = getNode(size - 1).data; + Node node = getNode(size - 2); + node.next = null; + size--; + return obj; + } + + //获取节点 + public Node getNode(int index){ + checkIndex(index); + if(index == 0 ){ + return head; + }else if(index == size -1 ){ + return tail; + }else{ + Node node = head; + for(int i=0;i= size || index < 0) + throw new IndexOutOfBoundsException("数组下标越界"); + } + + private static class Node { + Object data; + Node next; + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/List.java b/group16/1287642108/0226/src/com/coding/basic/List.java index 10d13b5832..396b1f6416 100644 --- a/group16/1287642108/0226/src/com/coding/basic/List.java +++ b/group16/1287642108/0226/src/com/coding/basic/List.java @@ -1,9 +1,9 @@ -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(); -} +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/group16/1287642108/0226/src/com/coding/basic/Queue.java b/group16/1287642108/0226/src/com/coding/basic/Queue.java index 95dee3d81b..418e42826e 100644 --- a/group16/1287642108/0226/src/com/coding/basic/Queue.java +++ b/group16/1287642108/0226/src/com/coding/basic/Queue.java @@ -1,28 +1,28 @@ -package com.coding.basic; - -public class Queue { - private LinkedList elementData = new LinkedList(); - - public void enQueue(Object o){ - elementData.addLast(o); - } - - public Object deQueue(){ - if (isEmpty()) { - return null; - }else{ - return elementData.removeFirst(); - } - } - - public boolean isEmpty(){ - if (elementData.size() == 0) { - return true; - } - return false; - } - - public int size(){ - return elementData.size(); - } -} +package com.coding.basic; + +public class Queue { + private LinkedList elementData = new LinkedList(); + + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + if (isEmpty()) { + return null; + }else{ + return elementData.removeFirst(); + } + } + + public boolean isEmpty(){ + if (elementData.size() == 0) { + return true; + } + return false; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group16/1287642108/0226/src/com/coding/basic/Stack.java b/group16/1287642108/0226/src/com/coding/basic/Stack.java index c0b7da89f8..6138bc6973 100644 --- a/group16/1287642108/0226/src/com/coding/basic/Stack.java +++ b/group16/1287642108/0226/src/com/coding/basic/Stack.java @@ -1,38 +1,38 @@ -package com.coding.basic; - -public class Stack { - private ArrayList elementData = new ArrayList(); - - public void push(Object o) { - if (isEmpty()) { - elementData.add(elementData.length() - 1, o); - } - elementData.add(elementData.length() - elementData.size() - 1, o); - } - - public Object pop() { - if (isEmpty()) { - return null; - } - return elementData.remove(elementData.length() - elementData.size() - 1); - } - - public Object peek() { - if (isEmpty()) { - return null; - } - return elementData.get(elementData.length() - elementData.size() - 1); - } - - public boolean isEmpty() { - if (elementData.size() == 0) { - return true; - } - return false; - } - - public int size() { - return elementData.size(); - } - -} +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + if (isEmpty()) { + elementData.add(elementData.length() - 1, o); + } + elementData.add(elementData.length() - elementData.size() - 1, o); + } + + public Object pop() { + if (isEmpty()) { + return null; + } + return elementData.remove(elementData.length() - elementData.size() - 1); + } + + public Object peek() { + if (isEmpty()) { + return null; + } + return elementData.get(elementData.length() - elementData.size() - 1); + } + + public boolean isEmpty() { + if (elementData.size() == 0) { + return true; + } + return false; + } + + public int size() { + return elementData.size(); + } + +} diff --git a/group16/1287642108/0305/src/com/coderising/array/ArrayUtil.java b/group16/1287642108/0305/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..8e0d8af87b --- /dev/null +++ b/group16/1287642108/0305/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,205 @@ +package com.coderising.array; + +import java.util.ArrayList; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 渚嬪锛 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 = new int[origin.length]; + int index = 0; + for (int i = origin.length - 1; i >= 0; i--) { + temp[index++] = origin[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 static int[] removeZero(int[] oldArray) { + int countZero = 0; + for (int i = 0; i < oldArray.length; i++) { + if(oldArray[i] == 0){ + countZero++; + } + } + + int[] temp = new int[oldArray.length - countZero]; + int index = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + temp[index++] = oldArray[i]; + } + } + return temp; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 渚嬪 a1 = + * {3, 5, 7,8} a2 = {4, 5, 6,7} 鍒 a3 涓簕3,4,5,6,7,8} , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * + * @param array1 + * @param array2 + * @return + */ + + public Integer[] merge(int[] array1, int[] array2) { + Integer[] temp = new Integer[array1.length + array2.length]; + int i = 0,j = 0; + int index = 0; + + while(i < array1.length && j < array2.length){ + if(array1[i] <= array2[j]){ + temp[index++] = array1[i++]; + }else{ + temp[index++] = array2[j++]; + } + } + + while(i < array1.length){ + temp[index++] = array1[i++]; + } + while(j < array2.length){ + temp[index++] = array2[j++]; + } + return removeRepetition(temp); + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 渚嬪 oldArray = {2,3,6} , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * {2,3,6,0,0,0} + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + int[] temp = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, temp, 0, oldArray.length); + return temp; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 渚嬪锛 max = 15 , + * 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max){ + int[] temp = new int[max]; + if(max == 1){ + return null; + } + int index = 0; + for(int i = 0;i temp = new ArrayList<>(); + for(int i=2; i < max; i++){ + isPrime = true; + for (int j = 2; j < i; j++){ + if ((i % j) == 0) { + isPrime = false; + break; + } + } + if(isPrime){ + temp.add(i); + } + } + int[] array = new int[temp.size()]; + int index = 0; + for(Integer te : temp){ + array[index++] = te; + } + return array; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public static Integer[] getPerfectNumbers(int max) { + Integer[] temp = new Integer[max]; + int index = 0; + for (int i = 2; i < max; i++) { + int sum = 0; + for (int j = 1; j < i; j++) { + if (i % j == 0) { + sum += j; + } + if (sum == i){ + temp[index++] = i; + } + } + } + return removeRepetition(temp); + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 渚嬪array= {3,8,9}, seperator = "-" 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + String temp = ""; + for(int i = 0;i < array.length -1; i++){ + temp += array[i] + seperator; + } + temp += array[array.length-1]; + return temp; + } + + public static Integer[] removeRepetition(Integer[] oldArray){ + ArrayList temp = new ArrayList<>(); + for(int i=0;i parameters) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + //鍒涘缓SAXReader璇诲彇鍣紝涓撻棬鐢ㄤ簬璇诲彇xml + SAXReader saxReader = new SAXReader(); + Document document = null; + try { + document = saxReader.read(new File("D:/DemoSpace/coding2017/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml")); + Element root = document.getRootElement(); + //鏍规嵁鑺傜偣鍚嶇О鎵捐妭鐐 + Node node = root.selectSingleNode("action[@name='"+actionName+"']"); + String classPath = ((Element) node).attributeValue("class"); + //鏍规嵁绫诲悕鍙嶅皠瀹炰緥鍖 + Class onwClass = Class.forName(classPath); + Object o = onwClass.newInstance(); + Method setName = onwClass.getMethod("setName",String.class); + Method setPassword = onwClass.getMethod("setPassword",String.class); + Method execute = onwClass.getMethod("execute"); + Method getName = onwClass.getMethod("getName"); + Method getPassword = onwClass.getMethod("getPassword"); + Method getMessage = onwClass.getMethod("getMessage"); + setName.invoke(o,parameters.get("name")); + setPassword.invoke(o,parameters.get("password")); + String result = (String) execute.invoke(o); + //缁勮params鍙傛暟 + HashMap map = new HashMap<>(); + String name = (String) getName.invoke(o); + String password = (String) getPassword.invoke(o); + String message = (String) getMessage.invoke(o); + map.put("name", name); + map.put("password", password); + map.put("message", message); + //缁勮view鏁版嵁 + View view = new View(); + view.setParameters(map); + //鏍规嵁execute鐨勮繑鍥炲硷紝鎵惧搴旂殑jsp椤甸潰璺緞 + String jspPath = node.valueOf("//result[@name='"+result+"']"); + view.setJsp(jspPath); + return view; + } catch (DocumentException e) { + e.printStackTrace(); + } + return null; + } +} diff --git a/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java b/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..2a8ed76022 --- /dev/null +++ b/group16/1287642108/0305/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,40 @@ +package com.coderising.litestruts; + +import java.lang.reflect.InvocationTargetException; +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, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + + 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, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + 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/group16/1287642108/0305/src/com/coderising/litestruts/View.java b/group16/1287642108/0305/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group16/1287642108/0305/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/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml b/group16/1287642108/0305/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e22003fc12 --- /dev/null +++ b/group16/1287642108/0305/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 diff --git a/group16/1325756593/src/com/coderising/array/ArrayUtil.java b/group16/1325756593/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..0f67e6b54f --- /dev/null +++ b/group16/1325756593/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,273 @@ +package com.coderising.array; + +import static org.hamcrest.CoreMatchers.nullValue; + +import java.util.Arrays; + +import com.dong.week1.ArrayList; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + if(origin==null||origin.length==0){ + return; + } + int len = origin.length; + for(int i=0;iarray2[index2]){ + arrayList.add(array2[index2]); + index2++; + }else if(array1[index1]==array2[index2]){ + arrayList.add(array2[index2]); + index1++; + index2++; + } + else{ + arrayList.add(array1[index1]); + index1++; + } + } + if(index1==len1){ + for(int i=index2;i=max){ + break; + } + arrayList.add(third); + first = second; + second=third; + + + } + return ListToArray(arrayList); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + ArrayList arrayList = new ArrayList(); + for(int i=2;i resultAndViewMap; + public String getActioName() { + return actioName; + } + public void setActioName(String actioName) { + this.actioName = actioName; + } + @Override + public String toString() { + return "Action [actioName=" + actioName + ", className=" + className + ", resultAndViewMap=" + resultAndViewMap + + "]"; + } + public String getClassName() { + return className; + } + public void setClassName(String className) { + this.className = className; + } + public Map getResultAndViewMap() { + return resultAndViewMap; + } + public void setResultAndViewMap(Map resultAndViewMap) { + this.resultAndViewMap = resultAndViewMap; + } + + + + +} diff --git a/group16/1325756593/src/com/coderising/litestruts/LoginAction.java b/group16/1325756593/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group16/1325756593/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; + } +} diff --git a/group16/1325756593/src/com/coderising/litestruts/Struts.java b/group16/1325756593/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..ae6289d5f0 --- /dev/null +++ b/group16/1325756593/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,246 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +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.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + + + +public class Struts { + + public static String strutsPath; + + + public static String getStrutsPath() { + return strutsPath; + } + + public static void setStrutsPath(String strutsPath) { + Struts.strutsPath = strutsPath; + } + + + + + public static View runAction(String actionName, Map parameters) throws ParserConfigurationException, SAXException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + if(strutsPath==null||strutsPath.equals("")){ + strutsPath = initStrutsPath(); + } + List actions = parseXml(strutsPath); + Action action = getAction(actions,actionName); + NotBeNull(action); + + + String actionClassName = action.getClassName(); + Class actionClass = Class.forName(actionClassName); + Object actionClassObject = actionClass.newInstance(); + + doSetterMethod(parameters, actionClass, actionClassObject); + + Method excuteMethod = actionClass.getMethod("execute", null); + String result = excuteMethod.invoke(actionClassObject, null).toString(); + String viewPath = action.getResultAndViewMap().get(result); + View view = new View(); + view.setJsp(viewPath); + Map parametersMap = doGetterMethod(actionClass, actionClassObject); + view.setParameters(parametersMap); + + return view; + } + + + /** + * 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + * @param actionClass + * @param actionClassObject + * @return + * @throws IllegalAccessException + * @throws InvocationTargetException + */ + private static Map doGetterMethod(Class actionClass, Object actionClassObject) + throws IllegalAccessException, InvocationTargetException { + Map parametersMap = new HashMap<>(); + for(Method method :actionClass.getMethods()){ + if(method.getName().startsWith("get")){ + String getValue = getMethodValue(method.getName()); + parametersMap.put(getValue, method.invoke(actionClassObject, null)); + } + } + return parametersMap; + } + + /** + * 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + * @param parameters + * @param actionClass + * @param actionClassObject + * @throws NoSuchMethodException + * @throws IllegalAccessException + * @throws InvocationTargetException + */ + private static void doSetterMethod(Map parameters, Class actionClass, Object actionClassObject) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + for( Map.Entry entry: parameters.entrySet()){ + String param = entry.getKey(); + Method methodKeySet = actionClass.getMethod(getMethodSet(param), String.class); + methodKeySet.invoke(actionClassObject, entry.getValue()); + } + } + + /** + * 鍒濆鍖杝trutsPath + * @return + */ + private static String initStrutsPath() { + String path = Struts.class.getClassLoader().getResource("").getPath(); + String strutsXmlPath = path+"/com/coderising/litestruts/struts.xml"; + return strutsXmlPath; + } + + + /** + * 閫氳繃name鏋勯爏etName + * @param name + * @return + */ + public static String getMethodSet(String name){ + String firstChar = name.substring(0, 1).toUpperCase(); + return "set"+firstChar+name.substring(1); + } + + /** + * 鑾峰彇getMessage瀵瑰簲鐨刴essage + * @param name + * @return + */ + public static String getMethodValue(String name){ + if(name.length()>=3){ + name = name.substring(3); + } + String firstChar = name.substring(0, 1).toLowerCase(); + return firstChar+name.substring(1); + } + + + + /** + * 灏唜ml瑙f瀽鎴愪负Action鐨凩ist缁撴瀯 + * @param xmlPath + * @return + * @throws ParserConfigurationException + * @throws SAXException + * @throws IOException + */ + public static List parseXml(String xmlPath) throws ParserConfigurationException, SAXException, IOException{ + List retActionList = new ArrayList<>(); + File xmlFile = new File(xmlPath); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(xmlFile); + NodeList actionLists = doc.getElementsByTagName("action"); + for(int i=0;i resultAndViewMap=getResultAndViewNodeList(currentNode); + action.setResultAndViewMap(resultAndViewMap); + retActionList.add(action); + } + return retActionList; + } + + /** + * 鑾峰彇璇ode瀵瑰簲鐨剅esult鍜屽搴旂殑jsppath + * @param currentNode + * @param resultAndViewMap + */ + private static HashMap getResultAndViewNodeList(Node currentNode) { + Map resultAndViewMap = new HashMap<>(); + NodeList resultAndViewNodeList = currentNode.getChildNodes(); + + for(int j=0;j) resultAndViewMap; + } + + + private static void NotBeNull(Object result) { + if(result==null){ + throw new IllegalArgumentException(); + } + } + + + + public static String getAttribute(Node node ,String name){ + NamedNodeMap nameNodeMap = node.getAttributes(); + NotBeNull(nameNodeMap); + Node nameNode = nameNodeMap.getNamedItem(name); + NotBeNull(nameNode); + String actionName =nameNode.getNodeValue(); + return actionName; + } + + public static Action getAction(List actions ,String actionName){ + for(Action action:actions){ + if(action.getActioName().equals(actionName)){ + return action; + } + } + return null; + + } + + + +} diff --git a/group16/1325756593/src/com/coderising/litestruts/StrutsTest.java b/group16/1325756593/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..487f05542c --- /dev/null +++ b/group16/1325756593/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,48 @@ +package com.coderising.litestruts; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.parsers.ParserConfigurationException; + +import org.junit.Assert; +import org.junit.Test; +import org.xml.sax.SAXException; + + + + + +public class StrutsTest { + + @Test + public void testLoginActionSuccess() throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, ParserConfigurationException, SAXException, IOException { + + 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 InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, ParserConfigurationException, SAXException, IOException { + 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/group16/1325756593/src/com/coderising/litestruts/View.java b/group16/1325756593/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group16/1325756593/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/group16/1325756593/src/com/coderising/litestruts/struts.xml b/group16/1325756593/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..07f80b6476 --- /dev/null +++ b/group16/1325756593/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 diff --git a/group16/214074094/src/com/coding/basic/BinaryTreeNode.java b/group16/214074094/src/com/coding/basic/BinaryTreeNode.java deleted file mode 100644 index b40066ebe1..0000000000 --- a/group16/214074094/src/com/coding/basic/BinaryTreeNode.java +++ /dev/null @@ -1,39 +0,0 @@ -package 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/group16/214074094/src/com/coding/basic/List.java b/group16/214074094/src/com/coding/basic/List.java deleted file mode 100644 index 5da9b0d4c6..0000000000 --- a/group16/214074094/src/com/coding/basic/List.java +++ /dev/null @@ -1,15 +0,0 @@ -package coding.basic; - -public interface List { - - void add(Object o); - - void add(int index, Object o); - - Object get(int index); - - Object remove(int index); - - int size(); - -} diff --git a/group16/214074094/src/com/reading/blog_test.txt b/group16/214074094/src/com/reading/blog_test.txt deleted file mode 100644 index b7e5cbfe14..0000000000 --- a/group16/214074094/src/com/reading/blog_test.txt +++ /dev/null @@ -1 +0,0 @@ -just test new fork \ No newline at end of file diff --git a/group16/214074094/src/main/java/study/coderising/array/ArrayUtil.java b/group16/214074094/src/main/java/study/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..c2b0c62a79 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/array/ArrayUtil.java @@ -0,0 +1,213 @@ +package study.coderising.array; + + +import study.coding.basic.ArrayList; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + * 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * + * @param origin + * @return + */ + public static void reverseArray(int[] origin) { + int tmp; + for (int i = 0; i < origin.length / 2; i++) { + tmp = origin[i]; + origin[i] = origin[origin.length - i - 1]; + origin[origin.length - i - 1] = tmp; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 static int[] removeZero(int[] oldArray) { + int[] newArray = new int[oldArray.length]; + int k = 0; + for (int i = 0; i < oldArray.length; i++) { + if (0 != oldArray[i]) { + newArray[k++] = oldArray[i]; + } + } + return Arrays.copyOf(newArray, k); + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛宎1鍜宎2锛屽垱寤轰竴涓柊鐨勬暟缁刟3锛屼娇寰梐3鍖呭惈a1鍜宎2鐨勬墍鏈夊厓绱狅紝骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = {3, 5, 7, 8}锛宎2 = {4, 5, 6, 7}锛屽垯 a3 涓篬3,4,5,6,7,8]锛屾敞鎰忥細宸茬粡娑堥櫎浜嗛噸澶 + * + * @param a1 + * @param a2 + * @return + */ + + public static int[] merge(int[] a1, int[] a2) { + Set set = new HashSet(); + + for (int i = 0; i < a1.length + a2.length; i++) { + if (i < a1.length) { + set.add(a1[i]); + } else { + set.add(a2[i - a1.length]); + } + } + + int[] a3 = new int[set.size()]; + + Iterator it = set.iterator(); + int i = 0; + while (it.hasNext()) { + a3[i++] = (Integer) it.next(); + } + return a3; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int[] oldArray, int size) { + return Arrays.copyOf(oldArray, oldArray.length + size); + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max == 1) { + return new int[0]; + } + ArrayList list = new ArrayList(); + list.add(1); + list.add(1); + for (int i = 0; ; i++) { + int tmp = list.get(i) + list.get(i + 1); + if (tmp >= max) { + break; + } + list.add(i + 2, tmp); + } + + return convertIntegerArray2Int(list); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + if (max < 2) { + return null; + } + ArrayList list = new ArrayList<>(); + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + list.add(i); + } + } + return convertIntegerArray2Int(list); + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + if (max < 2) { + return null; + } + ArrayList list = new ArrayList<>(); + + int count = 0; + //濡傛灉p鏄川鏁帮紝涓2^p-1涔熸槸璐ㄦ暟锛岄偅涔堬紙2^p-1锛塜2^锛坧-1锛変究鏄竴涓畬鍏ㄦ暟 + for (int i = 2; i <= max / 2; i++) { + count++; + if (isPrime(i) && isPrime((int) Math.pow(2, i) - 1)) { + System.out.println("count " + i + ":" + (int) (Math.pow(2, i) - 1) + " * " + (int) Math.pow(2, (i - 1))); + int perfectNum = (int) ((Math.pow(2, i) - 1) * Math.pow(2, (i - 1))); + if (perfectNum > max) { + break; + } + if (perfectNum < max) { + list.add(perfectNum); + } + } + } + System.out.println("total count : " + count); + return convertIntegerArray2Int(list); + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public static String join(int[] array, String seperator) { + if (null == array || array.length < 1) { + return null; + } + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < array.length; i++) { + sb.append(array[i]).append(seperator); + } + return sb.substring(0, sb.length() - 1); + } + + private static boolean isPrime(int n) { + if (n == 2) { + return true; + } + for (int j = 2; j <= Math.sqrt(n); j++) { + if (n % j == 0) { + return false; + } + } + return true; + } + + private static int[] convertIntegerArray2Int(ArrayList list) { + int[] arr = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + arr[i] = list.get(i).intValue(); + } + return arr; + } + + +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/LoginAction.java b/group16/214074094/src/main/java/study/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..043df200d8 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/LoginAction.java @@ -0,0 +1,42 @@ +package study.coderising.litestruts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * + * @author liuxin + */ +public class LoginAction { + + private String name; + private String password; + private String message; + + 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 String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return this.message; + } +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/LogoutAction.java b/group16/214074094/src/main/java/study/coderising/litestruts/LogoutAction.java new file mode 100644 index 0000000000..e3a10a6726 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/LogoutAction.java @@ -0,0 +1,39 @@ +package study.coderising.litestruts; + +/** + * @Author shane + * @Time 2017/3/4 12:13 + * @Email stevenchenguang@gmail.com + * @Desc ... + */ +public class LogoutAction { + + private String name; + + private String message; + + public String execute() { + if ("test".equalsIgnoreCase(name)) { + message = name + " logout success"; + return "success"; + } + message = name + " logout fail"; + return "error"; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/Struts.java b/group16/214074094/src/main/java/study/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..9a70e7a525 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/Struts.java @@ -0,0 +1,147 @@ +package study.coderising.litestruts; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import study.coderising.litestruts.bean.Action; +import study.coderising.litestruts.bean.Result; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; + + +public class Struts { + + private static final String EXECUTE = "execute"; + + public static View runAction(String actionName, Map parameters) + throws DocumentException, ClassNotFoundException, NoSuchMethodException, + InvocationTargetException, IllegalAccessException, InstantiationException { + + //0. 璇诲彇閰嶇疆鏂囦欢struts.xml + SAXReader reader = new SAXReader(); + Document doc = reader.read("./src/main/resources/struts.xml"); + Element root = doc.getRootElement(); + Action action = getAction(actionName, root); + + //1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class + Class clz = Class.forName(action.getClassPath()); + //鑾峰彇涓涓疄渚,鏂逛究鎺ヤ笅鏉ュ鍚屼竴涓璞¤祴鍊 + Object obj = clz.newInstance(); + + //1.1 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶 + for (Map.Entry entry : parameters.entrySet()) { + String setFunctionName = getFunctionName("set", entry.getKey()); + Method set = clz.getDeclaredMethod(setFunctionName, String.class); + set.invoke(obj, entry.getValue()); + } + + //2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue鏂规硶锛 骞惰幏寰楄繑鍥炲 + Method execute = clz.getDeclaredMethod(EXECUTE); + String response = execute.invoke(obj).toString(); + + //3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶骞惰皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap + Method[] methods = clz.getDeclaredMethods(); + Map map = new HashMap<>(); + for (Method m : methods) { + if (m.getName().startsWith("get")) { + String paramName = m.getName().replaceFirst("get", ""); + map.put(paramName.toLowerCase(), m.invoke(obj)); + } + } + + //3.1 鏀惧埌View瀵硅薄鐨刾arameters + View view = new View(); + view.setParameters(map); + + //4. 鏍规嵁struts.xml涓殑閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝纭畾jsp锛屾斁鍒癡iew瀵硅薄鐨刯sp瀛楁涓 + for (Result result : action.getResults()) { + if (response.equalsIgnoreCase(result.getResult())) { + view.setJsp(result.getJumpPath()); + break; + } + } + + return view; + } + + /** + * @Author: shane + * @Time: 2017/3/4 23:53 + * @Email: stevenchenguang@gmail.com + * @param: begin, key + * @Return: String + * @Throw: + * @Desc: 鏍规嵁寮濮嬪悕绉板拰key鑾峰彇鏂规硶鍚 + * e.g.: begin: get, key: name, return getName + */ + private static String getFunctionName(String begin, String key) { + if (key == null || "".equals(key)) { + return null; + } + StringBuffer sb = new StringBuffer(begin); + if (key.length() < 2) { + sb.append(key.toUpperCase()); + } else { + String first = String.valueOf(key.charAt(0)); + sb.append(first.toUpperCase()); + sb.append(key.substring(1, key.length())); + } + return sb.toString(); + } + + /** + * @Author: shane + * @Time: 2017/3/4 23:55 + * @Email: stevenchenguang@gmail.com + * @param: actionName, node + * @Return: Action + * @Throw: + * @Desc: 鏍规嵁actionName鍜寈ml鑺傜偣鑾峰彇Action + */ + private static Action getAction(String actionName, Element node) { + Action action = new Action(); + + Iterator iterator = node.elementIterator(); + boolean flag = false; + while (iterator.hasNext()) { + Element e = iterator.next(); + List list = e.attributes(); + + //閬嶅巻灞炴ц妭鐐 + for (Attribute attr : list) { + if ("name".equalsIgnoreCase(attr.getName())) { + if (!actionName.equalsIgnoreCase(attr.getValue())) { + continue; + } else { + flag = true; + } + action.setName(attr.getValue()); + } + if ("class".equalsIgnoreCase(attr.getName())) { + action.setClassPath(attr.getValue()); + } + + List results = new ArrayList<>(); + + Iterator it = e.elementIterator(); + while (it.hasNext()) { + Result result = new Result(); + Element el = it.next(); + result.setResult(el.attribute(0).getValue()); + result.setJumpPath(el.getText()); + results.add(result); + } + action.setResults(results); + } + if (flag) { + break; + } + } + return action; + } + +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/StrutsTest.java b/group16/214074094/src/main/java/study/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..db1b37de1b --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/StrutsTest.java @@ -0,0 +1,109 @@ +package study.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() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name", "test"); + params.put("password", "1234"); + + View view = null; + try { + view = Struts.runAction(actionName, params); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + 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 = null; + try { + view = Struts.runAction(actionName, params); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } + + @Test + public void testLogoutSuccess() { + String actionName = "logout"; + + Map params = new HashMap(); + params.put("name", "test"); + + View view = null; + try { + view = Struts.runAction(actionName, params); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/welcome.jsp", view.getJsp()); + Assert.assertEquals("test logout success", view.getParameters().get("message")); + } + + @Test + public void testLogoutFail() { + String actionName = "logout"; + + Map params = new HashMap(); + params.put("name", "unknownUser"); + + View view = null; + try { + view = Struts.runAction(actionName, params); + } catch (DocumentException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/error.jsp", view.getJsp()); + Assert.assertEquals("unknownUser logout fail", view.getParameters().get("message")); + } +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/View.java b/group16/214074094/src/main/java/study/coderising/litestruts/View.java new file mode 100644 index 0000000000..af3df5b9e9 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/View.java @@ -0,0 +1,27 @@ +package study.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/group16/214074094/src/main/java/study/coderising/litestruts/bean/Action.java b/group16/214074094/src/main/java/study/coderising/litestruts/bean/Action.java new file mode 100644 index 0000000000..12aa461cfc --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/bean/Action.java @@ -0,0 +1,43 @@ +package study.coderising.litestruts.bean; + + +import java.util.List; + +/** + * @Author shane + * @Time 2017/3/4 21:49 + * @Email shanbaohua@lxfintech.com + * @Desc ... + */ +public class Action { + + private String name; + + private String classPath; + + private List results; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClassPath() { + return classPath; + } + + public void setClassPath(String classPath) { + this.classPath = classPath; + } + + public List getResults() { + return results; + } + + public void setResults(List results) { + this.results = results; + } +} diff --git a/group16/214074094/src/main/java/study/coderising/litestruts/bean/Result.java b/group16/214074094/src/main/java/study/coderising/litestruts/bean/Result.java new file mode 100644 index 0000000000..649844ff31 --- /dev/null +++ b/group16/214074094/src/main/java/study/coderising/litestruts/bean/Result.java @@ -0,0 +1,30 @@ +package study.coderising.litestruts.bean; + +/** + * @Author shane + * @Time 2017/3/4 21:50 + * @Email shanbaohua@lxfintech.com + * @Desc ... + */ +public class Result { + + private String result; + + private String jumpPath; + + public String getResult() { + return result; + } + + public void setResult(String result) { + this.result = result; + } + + public String getJumpPath() { + return jumpPath; + } + + public void setJumpPath(String jumpPath) { + this.jumpPath = jumpPath; + } +} diff --git a/group16/214074094/src/com/coding/basic/ArrayList.java b/group16/214074094/src/main/java/study/coding/basic/ArrayList.java similarity index 84% rename from group16/214074094/src/com/coding/basic/ArrayList.java rename to group16/214074094/src/main/java/study/coding/basic/ArrayList.java index 158c866d45..ebe04dd177 100644 --- a/group16/214074094/src/com/coding/basic/ArrayList.java +++ b/group16/214074094/src/main/java/study/coding/basic/ArrayList.java @@ -1,4 +1,4 @@ -package coding.basic; +package study.coding.basic; import java.util.Arrays; @@ -10,7 +10,7 @@ * @Email stevenchenguang@gmail.com * @Desc OwnArrayList */ -public class ArrayList implements List { +public class ArrayList implements List { private int size = 0; @@ -28,22 +28,22 @@ public ArrayList() { } @Override - public void add(Object o) { + public void add(E e) { if (elementData == EMPTY_ELEMENTDATA) { elementData = Arrays.copyOf(elementData, DEFAULT_CAPACITY); - elementData[0] = o; + elementData[0] = e; } else if (size < elementData.length) { - elementData[size] = o; + elementData[size] = e; } else { _grow(); - elementData[size] = o; + elementData[size] = e; } size++; _analyze(); } @Override - public void add(int index, Object o) { + public void add(int index, E e) { if (index < 0) { throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); } @@ -52,40 +52,40 @@ public void add(int index, Object o) { throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); } else { elementData = new Object[DEFAULT_CAPACITY]; - elementData[0] = o; + elementData[0] = e; } } else if (index > size) { throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); } else if (index == size) { _grow(); - elementData[size] = o; + elementData[size] = e; size++; } else { if (elementData.length == size) { _grow(); } System.arraycopy(elementData, index, elementData, index + 1, size - index); - elementData[index] = o; + elementData[index] = e; size++; } _analyze(); } @Override - public Object get(int index) { - if (index < 0 || index > size) { + public E get(int index) { + if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); } - return elementData[index]; + return (E) elementData[index]; } @Override - public Object remove(int index) { + public E remove(int index) { - if (index < 0 || index > size) { + if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index:" + index + ", Size:" + size); } - Object oldValue = elementData[index]; + E oldValue = (E) elementData[index]; //闇瑕佸鍒剁殑闀垮害 int needMoveLength = size - index - 1; //濡傛灉璇ラ暱搴﹀皬浜0,銆璇存槑鍙湁涓涓厓绱,銆鐩存帴缃┖鍗冲彲 @@ -102,6 +102,11 @@ public int size() { return size; } + @Override + public E[] toArray() { + return (E[]) elementData; + } + public Iterator iterator() { return new ArrayListItrator(); } diff --git a/group16/214074094/src/main/java/study/coding/basic/BinaryTreeNode.java b/group16/214074094/src/main/java/study/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..4eae81c97a --- /dev/null +++ b/group16/214074094/src/main/java/study/coding/basic/BinaryTreeNode.java @@ -0,0 +1,95 @@ +package study.coding.basic; + +/** + * @Author shane + * @Time 2017/2/26 19:30 + * @Email stevenchenguang@gmail.com + * @Desc Own BinaryTreeNode + */ +public class BinaryTreeNode { + + private Object data; + + private BinaryTreeNode left; + + private BinaryTreeNode right; + + public BinaryTreeNode insert(Object o) { + if (null == data) { + data = o; + return this; + } + if (bigger(data, o)) { + if (null == left) { + left = new BinaryTreeNode(); + left.data = o; + } else { + left.insert(o); + } + } else if (smaller(data, o)) { + if (null == right) { + right = new BinaryTreeNode(); + right.data = o; + } else { + right.insert(o); + } + } else { + throw new RuntimeException("The value has exists"); + } + return this; + } + + 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; + } + + private boolean bigger(Object data1, Object data2) { + return data1.toString().compareTo(data2.toString()) > 0; + } + + private boolean smaller(Object data1, Object data2) { + return data1.toString().compareTo(data2.toString()) < 0; + } + + private ArrayList list = new ArrayList(); + + /** + * 瀵逛簩鍙夋爲杩涜閬嶅巻 缁撴灉瀛樺偍鍒發ist涓 + */ + private void sort(BinaryTreeNode node) { + + list.add(node.data); + if(null != node.left){ + sort(node.left); + } + if(null != node.right){ + sort(node.right); + } + } + + @Override + public String toString() { + sort(this); + return list.toString(); + } +} diff --git a/group16/214074094/src/com/coding/basic/Iterator.java b/group16/214074094/src/main/java/study/coding/basic/Iterator.java similarity index 73% rename from group16/214074094/src/com/coding/basic/Iterator.java rename to group16/214074094/src/main/java/study/coding/basic/Iterator.java index 1acc5349a4..0befe6a209 100644 --- a/group16/214074094/src/com/coding/basic/Iterator.java +++ b/group16/214074094/src/main/java/study/coding/basic/Iterator.java @@ -1,4 +1,4 @@ -package coding.basic; +package study.coding.basic; public interface Iterator { diff --git a/group16/214074094/src/com/coding/basic/LinkedList.java b/group16/214074094/src/main/java/study/coding/basic/LinkedList.java similarity index 97% rename from group16/214074094/src/com/coding/basic/LinkedList.java rename to group16/214074094/src/main/java/study/coding/basic/LinkedList.java index 9108c2b6fe..b6fede6824 100644 --- a/group16/214074094/src/com/coding/basic/LinkedList.java +++ b/group16/214074094/src/main/java/study/coding/basic/LinkedList.java @@ -1,4 +1,4 @@ -package coding.basic; +package study.coding.basic; /** * @Author shane @@ -50,6 +50,7 @@ public Object remove(int index) { } else if (index == size - 1) { return removeLast(); } + Node curr = _node(index); Object data = curr.data; final Node prev = curr.prev; @@ -120,6 +121,11 @@ public int size() { return size; } + @Override + public Object[] toArray() { + return new Object[0]; + } + public Iterator iterator() { return null; } diff --git a/group16/214074094/src/main/java/study/coding/basic/List.java b/group16/214074094/src/main/java/study/coding/basic/List.java new file mode 100644 index 0000000000..e62dd86d52 --- /dev/null +++ b/group16/214074094/src/main/java/study/coding/basic/List.java @@ -0,0 +1,16 @@ +package study.coding.basic; + +public interface List { + + void add(E e); + + void add(int index, E e); + + E get(int index); + + E remove(int index); + + int size(); + + E[] toArray(); +} diff --git a/group16/214074094/src/com/coding/basic/Queue.java b/group16/214074094/src/main/java/study/coding/basic/Queue.java similarity index 95% rename from group16/214074094/src/com/coding/basic/Queue.java rename to group16/214074094/src/main/java/study/coding/basic/Queue.java index 869d0f7333..b15950ac36 100644 --- a/group16/214074094/src/com/coding/basic/Queue.java +++ b/group16/214074094/src/main/java/study/coding/basic/Queue.java @@ -1,4 +1,4 @@ -package coding.basic; +package study.coding.basic; /** * @Author shane diff --git a/group16/214074094/src/com/coding/basic/Stack.java b/group16/214074094/src/main/java/study/coding/basic/Stack.java similarity index 95% rename from group16/214074094/src/com/coding/basic/Stack.java rename to group16/214074094/src/main/java/study/coding/basic/Stack.java index 7ef1c9ad06..188de74ee9 100644 --- a/group16/214074094/src/com/coding/basic/Stack.java +++ b/group16/214074094/src/main/java/study/coding/basic/Stack.java @@ -1,4 +1,4 @@ -package coding.basic; +package study.coding.basic; /** * @Author shane diff --git a/group16/214074094/src/main/java/study/reading/blog.txt b/group16/214074094/src/main/java/study/reading/blog.txt new file mode 100644 index 0000000000..f112f8ea6a --- /dev/null +++ b/group16/214074094/src/main/java/study/reading/blog.txt @@ -0,0 +1 @@ +瀛︿範-CPU銆佸唴瀛樸佺‖鐩樸佹寚浠ゅ強瀹冧滑涔嬮棿鐨勫叧绯:銆https://stevenshane.github.io/study/2017/02/27/coding2017-reading.html \ No newline at end of file diff --git a/group16/214074094/src/main/resources/struts.xml b/group16/214074094/src/main/resources/struts.xml new file mode 100644 index 0000000000..f93213e241 --- /dev/null +++ b/group16/214074094/src/main/resources/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/group16/214074094/src/test/coding/basic/AbstractTest.java b/group16/214074094/src/test/java/study/AbstractTest.java similarity index 71% rename from group16/214074094/src/test/coding/basic/AbstractTest.java rename to group16/214074094/src/test/java/study/AbstractTest.java index 80eaaa6fe5..42737e270f 100644 --- a/group16/214074094/src/test/coding/basic/AbstractTest.java +++ b/group16/214074094/src/test/java/study/AbstractTest.java @@ -1,4 +1,6 @@ -package coding.basic; +package study; + +import com.alibaba.fastjson.JSON; /** * @Author shane @@ -16,4 +18,7 @@ protected void printHyphen() { System.out.println("--------------------------------------------"); } + protected void printJson(Object obj) { + System.out.println(JSON.toJSONString(obj)); + } } diff --git a/group16/214074094/src/test/java/study/coderising/ArrayUtilTest.java b/group16/214074094/src/test/java/study/coderising/ArrayUtilTest.java new file mode 100644 index 0000000000..24cdeadb64 --- /dev/null +++ b/group16/214074094/src/test/java/study/coderising/ArrayUtilTest.java @@ -0,0 +1,82 @@ +package study.coderising; + +import org.junit.Assert; +import org.junit.Test; +import study.AbstractTest; +import study.coderising.array.ArrayUtil; + +/** + * @Author shane + * @Time 2017/3/1 20:29 + * @Email shanbaohua@lxfintech.com + * @Desc ... + */ +public class ArrayUtilTest extends AbstractTest { + + @Test + public void testReverseArray(){ + int[] a = {7, 9 , 30, 3}; + printJson(a); + ArrayUtil.reverseArray(a); + printJson(a); + } + + @Test + public void testremoveZero(){ + int oldArr[] = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int newArr[] = ArrayUtil.removeZero(oldArr); + printJson(oldArr); + printJson(newArr); + } + + @Test + public void testMerge(){ + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + + int[] a3 = ArrayUtil.merge(a1, a2); + printJson(a1); + printJson(a2); + printJson(a3); + } + + @Test + public void testGrow(){ + int[] oldArray = {2,3,6}; + printJson(oldArray); + int[] newArray = ArrayUtil.grow(oldArray, 3); + printJson(newArray); + } + + @Test + public void testFibonacci(){ + int[] onlyOne = {}; + int[] before15 = {1, 1, 2, 3, 5, 8, 13}; + int[] before21 = {1, 1, 2, 3, 5, 8, 13}; + Assert.assertArrayEquals(onlyOne, ArrayUtil.fibonacci(1)); + Assert.assertArrayEquals(before15, ArrayUtil.fibonacci(15)); + Assert.assertArrayEquals(before21, ArrayUtil.fibonacci(21)); + } + + @Test + public void testGetPrimes(){ + int[] _19 = {2,3,5,7,11,13,17,19}; + Assert.assertArrayEquals(_19, ArrayUtil.getPrimes(23)); + int[] _32 = {2,3,5,7,11,13,17,19,23,29,31}; + Assert.assertArrayEquals(_32, ArrayUtil.getPrimes(32)); + } + + @Test + public void testGetPerfectNumbers(){ + int[] arr = {6,28,496,8128,33550336}; + Assert.assertArrayEquals(arr, ArrayUtil.getPerfectNumbers(33550337)); + } + + @Test + public void testJoin(){ + int[] arr = {3,8,9}; + String seperator = "-"; + Assert.assertEquals("3-8-9", ArrayUtil.join(arr, seperator)); + } + +} diff --git a/group16/214074094/src/test/coding/basic/ArrayListTest.java b/group16/214074094/src/test/java/study/coding/basic/ArrayListTest.java similarity index 96% rename from group16/214074094/src/test/coding/basic/ArrayListTest.java rename to group16/214074094/src/test/java/study/coding/basic/ArrayListTest.java index 2f03342d61..6ffe2e5715 100644 --- a/group16/214074094/src/test/coding/basic/ArrayListTest.java +++ b/group16/214074094/src/test/java/study/coding/basic/ArrayListTest.java @@ -1,9 +1,10 @@ -package coding.basic; +package study.coding.basic; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import study.AbstractTest; /** * @Author shane diff --git a/group16/214074094/src/test/java/study/coding/basic/BinaryTreeNodeTest.java b/group16/214074094/src/test/java/study/coding/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..370b339434 --- /dev/null +++ b/group16/214074094/src/test/java/study/coding/basic/BinaryTreeNodeTest.java @@ -0,0 +1,33 @@ +package study.coding.basic; + +import org.junit.Test; +import study.AbstractTest; + +/** + * @Author shane + * @Time 2017/2/26 19:57 + * @Email shanbaohua@lxfintech.com + * @Desc ... + */ +public class BinaryTreeNodeTest extends AbstractTest { + + @Test + public void test(){ + BinaryTreeNode node = new BinaryTreeNode(); + node.insert(8); + node.insert(5); + node.insert(9); + node.insert(1); + node.insert(6); + node.insert(11); + node.insert(10); + node.insert(15); + node.insert(13); + node.insert(19); + + printStar(); + System.out.println(node.getData()); + System.out.println(node); + printStar(); + } +} diff --git a/group16/214074094/src/test/coding/basic/LinkedListTest.java b/group16/214074094/src/test/java/study/coding/basic/LinkedListTest.java similarity index 96% rename from group16/214074094/src/test/coding/basic/LinkedListTest.java rename to group16/214074094/src/test/java/study/coding/basic/LinkedListTest.java index bc78728a25..f22d1cc611 100644 --- a/group16/214074094/src/test/coding/basic/LinkedListTest.java +++ b/group16/214074094/src/test/java/study/coding/basic/LinkedListTest.java @@ -1,9 +1,10 @@ -package coding.basic; +package study.coding.basic; import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; +import study.AbstractTest; /** * @Author shane diff --git a/group16/214074094/src/test/coding/basic/QueueTest.java b/group16/214074094/src/test/java/study/coding/basic/QueueTest.java similarity index 95% rename from group16/214074094/src/test/coding/basic/QueueTest.java rename to group16/214074094/src/test/java/study/coding/basic/QueueTest.java index 12302783b3..cdfe5c95ab 100644 --- a/group16/214074094/src/test/coding/basic/QueueTest.java +++ b/group16/214074094/src/test/java/study/coding/basic/QueueTest.java @@ -1,9 +1,10 @@ -package coding.basic; +package study.coding.basic; import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; +import study.AbstractTest; /** * @Author shane diff --git a/group16/214074094/src/test/coding/basic/StackTest.java b/group16/214074094/src/test/java/study/coding/basic/StackTest.java similarity index 95% rename from group16/214074094/src/test/coding/basic/StackTest.java rename to group16/214074094/src/test/java/study/coding/basic/StackTest.java index f289744a67..8269967dff 100644 --- a/group16/214074094/src/test/coding/basic/StackTest.java +++ b/group16/214074094/src/test/java/study/coding/basic/StackTest.java @@ -1,9 +1,10 @@ -package coding.basic; +package study.coding.basic; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import study.AbstractTest; /** * @Author shane diff --git a/group16/2816977791/.gitignore b/group16/2816977791/.gitignore new file mode 100644 index 0000000000..4ede1404d2 --- /dev/null +++ b/group16/2816977791/.gitignore @@ -0,0 +1,181 @@ +# Created by https://www.gitignore.io/api/java,intellij,eclipse,emacs,svn,maven + +### Java ### +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/workspace.xml +.idea/tasks.xml +.idea/dictionaries +.idea/vcs.xml +.idea/jsLibraryMappings.xml +*.idea + +# Sensitive or high-churn files: +.idea/dataSources.ids +.idea/dataSources.xml +.idea/dataSources.local.xml +.idea/sqlDataSources.xml +.idea/dynamic.xml +.idea/uiDesigner.xml + +# Gradle: +.idea/gradle.xml +.idea/libraries + +# Mongo Explorer plugin: +.idea/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +### Intellij Patch ### +*.iml + + +### Eclipse ### + +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# Eclipse Core +.project + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + + +### Emacs ### +# -*- mode: gitignore; -*- +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + +# cask packages +.cask/ + +# Flycheck +flycheck_*.el + +# server auth directory +/server/ + +# projectiles files +.projectile + +### SVN ### +.svn/ + + +### Maven ### +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties + +Servers/ diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/ArrayList.java b/group16/2816977791/firstExercise/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..7ac196b700 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/ArrayList.java @@ -0,0 +1,94 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void add(Object o) { + ensureCapacity(size + 1); + elementData[size++] = o; + } + + public void add(int index, Object o) { + checkForLength(index); + + ensureCapacity(size + 1); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index) { + checkForLength(index); + return elementData[index]; + } + + public Object remove(int index) { + checkForLength(index); + Object oldValue = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return oldValue; + } + + public int size() { + return size; + } + + public Iterator iterator() { + return null; + } + + private void checkForLength(int index) { + if (index < 0 || index >= size) { + throw new RuntimeException("out of memory"); + } + } + + private void ensureCapacity(int minCapacity) { + if (minCapacity - elementData.length > 0) { + grow(minCapacity); + } + } + + private void grow(int minCapacity) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1);//澧炲ぇ瀹归噺 + if (newCapacity - minCapacity < 0) { + newCapacity = minCapacity; + } + elementData = copyOf(elementData, newCapacity); + } + + private Object[] copyOf(Object[] src, int newCapacity) { + Object[] target = new Object[newCapacity]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + String num = "num"; + for (int i = 0; i < 100; i++) { + list.add(num + String.valueOf(i)); + System.out.println(String.valueOf(i) + ":size:" + list.size()); + System.out.println(String.valueOf(i) + ":length:" + list.elementData.length); + } + System.out.println(list.size()); + + for (int i = 0; i < 100; i++) { + list.add(i, num + String.valueOf(i)); + System.out.println(String.valueOf(i) + ":size:" + list.size()); + System.out.println(String.valueOf(i) + ":length:" + list.elementData.length); + } + System.out.println(list.size()); + + for (int i = 0; i < 200; i++) { + System.out.println(list.remove(0)); + } + + System.out.println(list.size()); + } +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/BinaryTreeNode.java b/group16/2816977791/firstExercise/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..e34a68b3c2 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,67 @@ +package com.coding.basic; + +public class BinaryTreeNode { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode root; + + public BinaryTreeNode(Object data, BinaryTreeNode left, BinaryTreeNode right) { + this.data = data; + this.left = left; + this.right = 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) { + BinaryTreeNode binaryTreeNode = new BinaryTreeNode(o, null, null); + if (root == null) { + root = binaryTreeNode; + } else { + add(o, root); + } + return binaryTreeNode; + } + + private BinaryTreeNode add(Object o, BinaryTreeNode target) { + if (target == null) { + target = new BinaryTreeNode(o, null, null); + } else { + if (compare(o, target.data) > 0) { + target.right = add(o, target.right); + } else if (compare(o, target.data) < 0) { + target.left = add(o, target.left); + } + } + return target; + } + + private int compare(Object src, Object target) { + return ((String) src).compareTo((String) target); + } + +} diff --git a/group20/423184723/src/com/coding/basic/Iterator.java b/group16/2816977791/firstExercise/src/com/coding/basic/Iterator.java similarity index 100% rename from group20/423184723/src/com/coding/basic/Iterator.java rename to group16/2816977791/firstExercise/src/com/coding/basic/Iterator.java diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/LinkedList.java b/group16/2816977791/firstExercise/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..be5b236700 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/LinkedList.java @@ -0,0 +1,140 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private Node last; + + private int size = 0; + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + checkPosition(index); + if (index == size) { + addLast(o); + } else { + addIndex(index, o); + } + } + + public Object get(int index) { + return node(index).data; + } + + public Object remove(int index) { + checkPosition(index); + Object old = get(index); + if (index == 0) { + removeFirst(); + } else if (index == size - 1) { + removeLast(); + } else { + node(index - 1).next = node(index + 1); + size--; + } + return old; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node h = head; + Node newNode = new Node(o, h); + head = newNode; + if (h == null) { + last = newNode; + } + size++; + } + + public void addLast(Object o) { + Node l = last; + Node newNode = new Node(o, null); + last = newNode; + if (l == null) { + head = newNode; + } else { + l.next = newNode; + } + size++; + } + + public Object removeFirst() { + Node old = head; + head = old.next; + size--; + return old.data; + } + + public Object removeLast() { + Node old = last; + Node prev = node(size - 2); + last = prev; + size--; + return old.data; + } + + public Iterator iterator() { + return null; + } + + private void checkPosition(int index) { + if (index < 0 || index >= size) { + throw new RuntimeException("out of memory"); + } + } + + private void addIndex(int index, Object o) { + checkPosition(index); + Node newNode = new Node(o, node(index)); + if (index != 0) { + node(index - 1).next = newNode; + } else { + head = newNode; + } + size++; + } + + private Node node(int index) { + Node x = head; + for (int i = 0; i < index; i++) { + x = x.next; + } + return x; + } + + private static class Node { + Object data; + Node next; + + public Node(Object data, Node next) { + this.data = data; + this.next = next; + } + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.addLast("last"); + System.out.println(list.size()); + list.addFirst("head"); + System.out.println(list.size()); + list.add(0, "0Object"); + System.out.println(list.size()); + list.add(2, "2Object"); + System.out.println(list.size()); + list.removeLast(); + System.out.println(list.size()); + list.removeFirst(); + System.out.println(list.size()); + list.removeLast(); + System.out.println(list.size()); + list.get(0); + System.out.println(list.size()); + } +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/List.java b/group16/2816977791/firstExercise/src/com/coding/basic/List.java new file mode 100644 index 0000000000..01398944e6 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/List.java @@ -0,0 +1,13 @@ +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/group16/2816977791/firstExercise/src/com/coding/basic/Queue.java b/group16/2816977791/firstExercise/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..f2475fcfd6 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/Queue.java @@ -0,0 +1,21 @@ +package com.coding.basic; + +public class Queue { + private ArrayList elementData = new ArrayList(); + + public void enQueue(Object o) { + elementData.add(o); + } + + public Object deQueue() { + return elementData.get(0); + } + + public boolean isEmpty() { + return elementData.size() == 0 ? true : false; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group16/2816977791/firstExercise/src/com/coding/basic/Stack.java b/group16/2816977791/firstExercise/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..12a89eb613 --- /dev/null +++ b/group16/2816977791/firstExercise/src/com/coding/basic/Stack.java @@ -0,0 +1,25 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o) { + elementData.add(0, o); + } + + public Object pop() { + return elementData.remove(0); + } + + public Object peek() { + return elementData.get(0); + } + + public boolean isEmpty() { + return elementData.size() == 0 ? true : false; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/array/ArrayUtil.java b/group16/2816977791/secondExercise/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..3572229739 --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,258 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 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 i = 0; + while (i < origin.length - 1 - i) { + int temp = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = origin[i]; + origin[i] = temp; + 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 point = 0; + for (int i : oldArray) { + if (i != 0) { + oldArray[point++] = i; + } + } + int[] newArray = new int[point]; + System.arraycopy(oldArray, 0, newArray, 0, point); + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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 pos1 = 0; + int pos2 = 0; + int point = 0; + int[] newArray = new int[array1.length + array2.length]; + while (pos1 < array1.length && pos2 < array2.length) { + if (array1[pos1] > array2[pos2]) { + newArray[point++] = array2[pos2++]; + } else if (array1[pos1] < array2[pos2]) { + newArray[point++] = array1[pos1++]; + } else { + newArray[point++] = array1[pos1++]; + pos2++; + } + } + while (pos1 < array1.length) { + newArray[point++] = array1[pos1++]; + } + while (pos1 < array2.length) { + newArray[point++] = array1[pos2++]; + } + int[] array = new int[point]; + System.arraycopy(newArray, 0, array, 0, point); + return array; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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 a = 0; + int b = 1; + int[] array = new int[max]; + int i = 0; + if (max >= 2) { + array[i++] = 1; + } + while (a + b < max) { + int temp = b; + b = a + b; + a = temp; + array[i++] = b; + } + int[] newArray = new int[i]; + System.arraycopy(array, 0, newArray, 0, i); + return newArray; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int[] array = new int[max / 2 + 1]; + int pos = 0; + for (int i = 1; i < max; i++) { + if (prime(i)) { + array[pos++] = i; + } + } + int[] newArray = new int[pos]; + System.arraycopy(array, 0, newArray, 0, pos); + return newArray; + } + + private boolean prime(int value) { + if (value < 2) { + return false; + } else if (value == 2) { + return true; + } else { + for (int i = 2; i < value / 2 + 1; i++) { + if (value % 2 == 0) { + return false; + } + } + return true; + } + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int pos = 0; + int[] array = new int[max]; + for (int i = 1; i < max; i++) { + if (perfectNumber(i)) { + array[pos++] = i; + } + } + int[] newArray = new int[pos]; + System.arraycopy(array, 0, newArray, 0, pos); + return newArray; + } + + private boolean perfectNumber(int value) { + if (value == 1 || prime(value)) { + return false; + } else { + int sum = 0; + for (int i = 1; i <= value / 2; i++) { + if (value % i == 0) { + sum += i; + } + } + if (sum == value) { + return true; + } + return false; + } + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + String out = ""; + for (int i = 0; i < array.length; i++) { + if (i == 0) { + out += String.valueOf(i); + } else { + out += seperator + String.valueOf(i); + } + } + return out; + } + + + public static void main(String[] args) { + ArrayUtil arrayUtil = new ArrayUtil(); + //reverse + int[] a = {7, 9, 30, 3}; + arrayUtil.reverseArray(a); + System.out.println(a); + + //remove zero + int[] zero = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5}; + int[] afterZero = arrayUtil.removeZero(zero); + System.out.println(afterZero); + + //merge + int[] a1 = {3, 5, 7, 8}; + int[] a2 = {4, 5, 6, 7}; + int[] merge = arrayUtil.merge(a1, a2); + System.out.println(merge); + + //grow + int[] oldArray = {2, 3, 6}; + int[] grow = arrayUtil.grow(oldArray, 3); + System.out.println(grow); + + //fibonacci + int[] fibonacci = arrayUtil.fibonacci(2); + System.out.println(fibonacci); + + //primes + int[] primes = arrayUtil.getPrimes(15); + System.out.println(primes); + + //perfect + int[] perfect = arrayUtil.getPerfectNumbers(500); + System.out.println(perfect); + + //join + int[] joinArray = {2}; + String join = arrayUtil.join(joinArray, "-"); + System.out.println(join); + + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/litestruts/ActionXml.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/ActionXml.java new file mode 100644 index 0000000000..55988befbe --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/litestruts/ActionXml.java @@ -0,0 +1,37 @@ +package com.coderising.litestruts; + +import java.util.Map; + +/** + * @author nvarchar + * date 2017/3/1 + */ +public class ActionXml { + private String name; + private String className; + private Map map; + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/litestruts/LoginAction.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group16/2816977791/secondExercise/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; + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/litestruts/Struts.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..f169cb1cbb --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,150 @@ +package com.coderising.litestruts; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + private static Map actionXmlMap; + + static { + //0. 璇诲彇閰嶇疆鏂囦欢struts.xml + actionXmlMap = parserXml("src/com/coderising/litestruts/struts.xml"); + System.out.println("parse end"); + } + + public static View runAction(String actionName, Map parameters) { + + try { + ActionXml actionXml = actionXmlMap.get(actionName); + if (actionName == null) { + return null; + } else { + String className = actionXml.getClassName(); + Class cls = Class.forName(className); + Object obj = cls.newInstance(); + /** + 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + */ + Method[] methods = cls.getMethods(); + for (String field : parameters.keySet()) { + for (Method method : methods) { + if (method.getName().startsWith("set") && method.getName().toLowerCase().endsWith(field.toLowerCase())) { + method.invoke(obj, parameters.get(field)); + break; + } + } + } + Method action = cls.getMethod("execute"); + //閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + String result = (String) action.invoke(obj); + /** + 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + */ + Field[] fields = cls.getDeclaredFields(); + Map params = new HashMap<>(); + for (Field field : fields) { + for (Method method : methods) { + if (method.getName().startsWith("get") && method.getName().toLowerCase().endsWith(field.getName().toLowerCase())) { + String viewResult = (String) method.invoke(obj); + params.put(field.getName(), viewResult); + break; + } + } + } + /** + 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + View view = new View(); + view.setParameters(params); + view.setJsp(actionXmlMap.get(actionName).getMap().get(result)); + return view; + } + } catch (ClassNotFoundException e) { + e.printStackTrace(); + return null; + } catch (InstantiationException e) { + e.printStackTrace(); + return null; + } catch (IllegalAccessException e) { + e.printStackTrace(); + return null; + } catch (NoSuchMethodException e) { + e.printStackTrace(); + return null; + } catch (InvocationTargetException e) { + e.printStackTrace(); + return null; + } + } + + //瑙f瀽xml鏂囦欢 + private static Map parserXml(String fileName) { + try { + Map map = new HashMap<>(); + //create documentBuilder + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + //create document + Document document = db.parse(fileName); + //extract root element + Element root = document.getDocumentElement(); + System.out.println("Root element :" + document.getDocumentElement().getNodeName()); + NodeList nodeList = document.getElementsByTagName("action"); + System.out.println("----------------------------"); + for (int temp = 0; temp < nodeList.getLength(); temp++) { + Node nNode = nodeList.item(temp); + System.out.println("\nCurrent Element :" + + nNode.getNodeName()); + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + ActionXml actionXml = new ActionXml(); + Element eElement = (Element) nNode; + System.out.println("action name : " + + eElement.getAttribute("name")); + System.out.println("class name : " + + eElement.getAttribute("class")); + actionXml.setName(eElement.getAttribute("name")); + actionXml.setClassName(eElement.getAttribute("class")); + NodeList result = eElement.getElementsByTagName("result"); + Map results = new HashMap<>(); + for (int i = 0; i < result.getLength(); i++) { + Node resultNode = result.item(i); + if (resultNode.getNodeType() == Node.ELEMENT_NODE) { + Element resultElement = (Element) resultNode; + System.out.println("result name:" + resultElement.getAttribute("name")); + System.out.println("result context:" + resultElement.getTextContent()); + results.put(resultElement.getAttribute("name"), resultElement.getTextContent()); + } + actionXml.setMap(results); + } + map.put(actionXml.getName(), actionXml); + } + } + return map; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + public static void main(String[] args) { +// parserXml("src/com/coderising/litestruts/struts.xml"); + } +} diff --git a/group16/2816977791/secondExercise/src/com/coderising/litestruts/StrutsTest.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group16/2816977791/secondExercise/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group16/2816977791/secondExercise/src/com/coderising/litestruts/View.java b/group16/2816977791/secondExercise/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group16/2816977791/secondExercise/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/group16/2816977791/secondExercise/src/com/coderising/litestruts/struts.xml b/group16/2816977791/secondExercise/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..246b3595ad --- /dev/null +++ b/group16/2816977791/secondExercise/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 diff --git a/group16/313001956/src/com/coding/basic/ArrayList.java b/group16/313001956/src/com/coding/basic/ArrayList.java index eaaa690fa6..3bec144013 100644 Binary files a/group16/313001956/src/com/coding/basic/ArrayList.java and b/group16/313001956/src/com/coding/basic/ArrayList.java differ diff --git a/group16/313001956/src/com/coding/basic/LinkedList.java b/group16/313001956/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..de886c9084 --- /dev/null +++ b/group16/313001956/src/com/coding/basic/LinkedList.java @@ -0,0 +1,140 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList() { + + } + + public void add(Object o) { + addLast(o); + } + + public void add(int index, Object o) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("越界!"); + } + if (head == null) { + head = new Node(); + head.data = o; + } else { + + Node n = getNodebyIndex(index); + + Node newnode = new Node(); + newnode.data = o; + newnode.next = n.next; + n.next = newnode; + } + size++; + } + + public Object get(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("越界!"); + } + if (head == null) + return null; + Node n = getNodebyIndex(index); + return n.data; + } + + public Object remove(int index) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("越界!"); + } + Node n = getNodebyIndex(index - 1); + Object o = n.next.data; + n.next = n.next.next; + size--; + return o; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + if (head == null) { + head = new Node(); + head.data = o; + } else { + Node newnode = new Node(); + newnode.data = o; + newnode.next = head; + head = newnode; + } + size++; + } + + public void addLast(Object o) { + if (head == null) { + head = new Node(); + head.data = o; + } else { + Node n = getNodebyIndex(size); + Node newnode = new Node(); + newnode.data = o; + n.next = newnode; + } + size++; + } + + public Object removeFirst() { + if (head == null) + return null; + + head = head.next; + size--; + return head.data; + } + + public Object removeLast() { + if (head == null) + return null; + Node n = getNodebyIndex(size - 1); + Object o = n.next.data; + n.next = null; + size--; + return o; + } + + public Iterator iterator() { + return null; + } + + private Node getNodebyIndex(int index) { + int i = 0; + Node n = head; + while (i < index) { + n = n.next; + i++; + } + return n; + } + + private static class Node { + Object data; + Node next; + + Object Getdata() { + return data; + } + + void Setdata(Object o) { + data = o; + } + + Node Getnext() { + return next; + } + + void Setnext(Node n) { + next = n; + } + } +} + diff --git a/group16/313001956/src/com/coding/basic/Queue.java b/group16/313001956/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..4b9f311f99 --- /dev/null +++ b/group16/313001956/src/com/coding/basic/Queue.java @@ -0,0 +1,24 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList elementData = new LinkedList(); + public void enQueue(Object o){ + elementData.addLast(o); + } + + public Object deQueue(){ + Object o= elementData.get(0); + elementData.removeFirst(); + return o; + } + + + public boolean isEmpty(){ + return elementData.size()==0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group16/313001956/src/com/coding/basic/Stack.java b/group16/313001956/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..6ec157201e --- /dev/null +++ b/group16/313001956/src/com/coding/basic/Stack.java @@ -0,0 +1,37 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public Stack() { + // TODO Auto-generated constructor stub + } + + public void push(Object o) { + elementData.add(o); + } + + public Object pop() { + int size = elementData.size(); + Object o = elementData.get(size - 1); + elementData.remove(size - 1); + return o; + } + + public Object peek() { + int size = elementData.size(); + Object o = elementData.get(size - 1); + + return o; + } + + public boolean isEmpty() { + if (elementData.size() == 0) + return true; + return false; + } + + public int size() { + return elementData.size(); + } +} diff --git a/group16/420355244/Homework1/.classpath b/group16/420355244/Homework1/.classpath new file mode 100644 index 0000000000..373dce4005 --- /dev/null +++ b/group16/420355244/Homework1/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group16/420355244/Homework1/.gitignore b/group16/420355244/Homework1/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group16/420355244/Homework1/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group16/420355244/Homework1/.project b/group16/420355244/Homework1/.project new file mode 100644 index 0000000000..ec1134f33a --- /dev/null +++ b/group16/420355244/Homework1/.project @@ -0,0 +1,17 @@ + + + Homework1 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/420355244/Homework1/.settings/org.eclipse.jdt.core.prefs b/group16/420355244/Homework1/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group16/420355244/Homework1/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +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/group16/420355244/Homework1/src/com/coding/basic/ArrayList.java b/group16/420355244/Homework1/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..6534c3c029 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/ArrayList.java @@ -0,0 +1,91 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if(size < elementData.length){ + elementData[size] = o; + }else{ + Object[] newElementData = new Object[elementData.length + elementData.length/2]; + System.arraycopy(elementData, 0, newElementData, 0, size); + newElementData[size] = o; + this.elementData = newElementData; + } + size++; + + } + public void add(int index, Object o){ + if(index >= 0 && index <= size){ + //1.涓嶆墿瀹 + if(index == size - 1){ + //1.1 鍔犲湪鏈鍚 + elementData[index] = o; + }else{ + //1.2 鍔犲湪鍓嶉潰 + //index鐨勪綅缃殑鏁板煎彉涓烘敼瀵硅薄锛宨ndex浠ュ悗浣嶇疆鐨勯兘寰鍚庢尓涓浣 + Object[] newElementData = new Object[elementData.length]; + System.arraycopy(elementData, 0, newElementData, 0, index); + newElementData[index] = o ; + System.arraycopy(elementData, index, newElementData, index + 1, size - index); + this.elementData = newElementData; + } + size++; + }else{ + throw new IndexOutOfBoundsException(); + } + } + + public Object get(int index){ + if(index < size){ + return elementData[index]; + }else{ + throw new IndexOutOfBoundsException(); + } + } + + public Object remove(int index){ + if(index < size){ + Object obj = elementData[index]; + Object[] newElementData = new Object[elementData.length]; + if(size != 1){ + //1.鑻ラ泦鍚堥暱搴︿负1 + if(0 == index){ + //1.1.濡傛灉remove鐨勬槸0绱㈠紩鐨 + System.arraycopy(elementData, 1, newElementData, 0, size - 1); + }else if(index == size -1){ + //1.2.濡傛灉remove鐨勬槸鏈鍚庣储寮曠殑 + System.arraycopy(elementData, 0, newElementData, 0, size - 1); + }else{ + //1.3.鍦ㄤ腑闂 + System.arraycopy(elementData, 0, newElementData, 0, index); + System.arraycopy(elementData, index + 1, newElementData, index, size - index - 1); + } + } + this.elementData = newElementData; + size--; + return obj; + }else{ + throw new IndexOutOfBoundsException(); + } + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + @Override + public String toString() { + return "ArrayList [size=" + size + ", elementData=" + Arrays.toString(elementData) + "]"; + } + + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/ArrayListTest.java b/group16/420355244/Homework1/src/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..420c412a7b --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/ArrayListTest.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + + private static ArrayList arrayList = new ArrayList(); + + @Before + public void setUp() throws Exception { + } + + @Test + public void testAddObject() { + for(int i = 0 ;i < 150; i++){ + arrayList.add("aaa"); + } + System.out.println(arrayList); + System.out.println(arrayList.size()); + } + + @Test + public void testAddIntObject() { + arrayList.add("aaa"); + arrayList.add("bbb"); + arrayList.add("ccc"); + arrayList.add("ddd"); + arrayList.add(1,"eee"); + System.out.println(arrayList); + } + + @Test + public void testGet() { + arrayList.add("aaa"); + arrayList.add("bbb"); + arrayList.add("ccc"); + arrayList.add("ddd"); + Object object = arrayList.get(0); + System.out.println(object); + } + + @Test + public void testRemove() { + arrayList.add("aaa"); + arrayList.add("bbb"); + arrayList.add("ccc"); + arrayList.add("ddd"); + arrayList.remove(0); + System.out.println(arrayList); + } + + @Test + public void testSize() { + arrayList.add("aaa"); + arrayList.add("bbb"); + arrayList.add("ccc"); + arrayList.add("ddd"); + System.out.println(arrayList.size()); + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/BinaryTreeNode.java b/group16/420355244/Homework1/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +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/group06/1454385822/src/com/coding/basic/Iterator.java b/group16/420355244/Homework1/src/com/coding/basic/Iterator.java similarity index 98% rename from group06/1454385822/src/com/coding/basic/Iterator.java rename to group16/420355244/Homework1/src/com/coding/basic/Iterator.java index d2e7a2c23c..06ef6311b2 100644 --- a/group06/1454385822/src/com/coding/basic/Iterator.java +++ b/group16/420355244/Homework1/src/com/coding/basic/Iterator.java @@ -1,8 +1,7 @@ package com.coding.basic; public interface Iterator { - public boolean hasNext(); public Object next(); - + } diff --git a/group16/420355244/Homework1/src/com/coding/basic/LinkedList.java b/group16/420355244/Homework1/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..9af35a399d --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/LinkedList.java @@ -0,0 +1,224 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + + private Node first; + + private Node last; + + private int size = 0; + + public void add(Object o){ + if(null == first){ + //褰撻摼琛ㄥ厓绱犱负绌烘椂锛屾柊寤轰竴涓狽ode + Node node = new Node(); + node.data = o; + node.next = null; + first = node; + last = node; + size ++; + }else{ + addLast(o); + } + } + public void add(int index , Object o){ + if(index < 0 || index >= size){ + //鏁扮粍瓒婄晫寮傚父 + throw new IndexOutOfBoundsException(); + }else{ + if(0 == index){ + //1.濡傛灉鍔犲湪澶翠笂 + addFirst(o); + }else{ + //2.鍔犲湪涓棿浣嶇疆 + Node node = first.next; + int nodeIndex = 1; + if(nodeIndex == index){ + //濡傛灉鏄浜屼釜浣嶇疆鐨勮瘽 + Node nodeAdd = new Node(); + nodeAdd.data = o; + first.next = nodeAdd; + nodeAdd.next = node; + last = node; + size ++; + } + //绗笁涓綅缃強浠ュ悗銆佸紑濮嬮亶鍘嗘墍鏈夌殑绱㈠紩 + while(null != node.next){ + //淇濈暀閬嶅巻涓璶ode涔嬪墠鐨勭粨鐐 + Node nodeLast = node; + node = node.next; + nodeIndex++; + if(nodeIndex == index){ + Node nodeAdd = new Node(); + nodeAdd.data = o; + nodeLast.next = nodeAdd; + nodeAdd.next = node; + size ++; + break; + } + } + } + } + + } + public Object get(int index){ + if(index < 0 || index >= size){ + //鏁扮粍瓒婄晫寮傚父 + throw new IndexOutOfBoundsException(); + }else{ + if(0 == index){ + //1.濡傛灉鍔犲湪澶翠笂 + return first.data; + } + Node node = first.next; + int nodeIndex = 1; + if(nodeIndex == index){ + //濡傛灉鏄浜屼釜浣嶇疆鐨勮瘽 + return node.data; + } + //绗笁涓綅缃強浠ュ悗銆佸紑濮嬮亶鍘嗘墍鏈夌殑绱㈠紩 + while(null != node.next){ + //淇濈暀閬嶅巻涓璶ode涔嬪墠鐨勭粨鐐 + node = node.next; + nodeIndex++; + if(nodeIndex == index){ + return node.data; + } + } + } + throw new IndexOutOfBoundsException(); + } + public Object remove(int index){ + if(index < 0 || index >= size){ + //鏁扮粍瓒婄晫寮傚父 + throw new IndexOutOfBoundsException(); + }else{ + if(0 == index){ + //1.濡傛灉绉婚櫎澶 + removeFirst(); + }else if(index == (size - 1)){ + //2.绉婚櫎灏 + removeLast(); + }else{ + //3.绉婚櫎涓棿浣嶇疆 + Node node = first.next; + //浠巉irst鐨勯浂鍙风储寮曞紑濮 + int nodeIndex = 1; + + //寮濮嬮亶鍘嗘墍鏈夌殑绱㈠紩锛岃浣忚绉婚櫎鐨勭储寮曚綅鏁版嵁鐨勫墠鍚庣粨鐐 + Node lastNode = first; + if(index == nodeIndex){ + //绗竴娆′笉鍖归厤鍒欏悗缁殑寰幆鎵ц + Object o = node.data; + lastNode.next = node.next; + size--; + return o; + }else{ + while(null != node.next){ + lastNode = node; + node = node.next; + nodeIndex++; + if(index == nodeIndex){ + Object o = node.data; + lastNode.next = node.next; + size--; + return o; + } + } + } + } + } + throw new IndexOutOfBoundsException(); + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node node = new Node(); + node.data = o ; + node.next = first; + first = node; + size++; + } + public void addLast(Object o){ + Node node = new Node(); + node.data = o ; + node.next = null; + last.next = node; + last = node; + size++; + } + public Object removeFirst(){ + Object o = first.data; + Node node = first.next; + first = node; + size--; + return o; + } + public Object removeLast(){ + if(0 == size){ + throw new NoSuchElementException(); + + }else if(1 == size){ + //鍙湁涓涓厓绱 + removeFirst(); + }else{ + //绗簩涓厓绱 + Node node = first.next; + if(null == node.next){ + Object o = node.data; + last = first; + first.next = null; + return o; + }else{ + while(null != node.next){ + //鑻ヤ笉姝㈠彧鏈2涓 锛岃褰曟渶鍚庝竴涓粨鐐圭殑鍓嶄竴涓 + Node lastNode = node; + node = node.next; + if(null == node.next){ + Object o = node.data; + lastNode.next = null; + last = lastNode; + size--; + return o; + } + } + } + } + throw new NoSuchElementException(); + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + } + + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if(null != first){ + sb.append(first.data.toString() + ","); + Node node = first.next; + sb.append(node.data.toString() + ","); + while(null != node.next){ + node = node.next; + sb.append(node.data.toString() + ","); + } + } + return sb.toString(); + } + + + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/LinkedListTest.java b/group16/420355244/Homework1/src/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..2caea9679b --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/LinkedListTest.java @@ -0,0 +1,112 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + private static LinkedList linkedList = new LinkedList(); + @Before + public void setUp() throws Exception { + } + + @Test + public void testAddObject() { + linkedList.add("aaa"); + linkedList.add("bbb"); + System.out.println(linkedList); + } + + @Test + public void testAddIntObject() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.add(2,"ddd"); + System.out.println(linkedList); + System.out.println(linkedList.size()); + } + + @Test + public void testGet() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.add("eee"); + linkedList.add("fff"); + linkedList.add("ddd"); +// System.out.println(linkedList.size()); + System.out.println(linkedList.get(3)); + } + + @Test + public void testRemove() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.add("eee"); + linkedList.add("fff"); + linkedList.add("ddd"); + linkedList.remove(5); + linkedList.remove(1); + linkedList.remove(2); + System.out.println(linkedList); + System.out.println(linkedList.size()); + } + + @Test + public void testSize() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.add("eee"); + linkedList.add("fff"); + linkedList.add("ddd"); + System.out.println(linkedList.size()); + } + + @Test + public void testAddFirst() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.addFirst("sss"); + System.out.println(linkedList); + } + + @Test + public void testAddLast() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + System.out.println(linkedList); + } + + @Test + public void testRemoveFirst() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.removeFirst(); + linkedList.addFirst("eee"); + linkedList.removeFirst(); + System.out.println(linkedList); + } + + @Test + public void testRemoveLast() { + linkedList.add("aaa"); + linkedList.add("bbb"); + linkedList.add("ccc"); + linkedList.removeLast(); + linkedList.add("eee"); + linkedList.addFirst("xxx"); + System.out.println(linkedList); + } + + @Test + public void testIterator() { + fail("Not yet implemented"); + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/List.java b/group16/420355244/Homework1/src/com/coding/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/List.java @@ -0,0 +1,9 @@ +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/group16/420355244/Homework1/src/com/coding/basic/Queue.java b/group16/420355244/Homework1/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..a2f9577b7b --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/Queue.java @@ -0,0 +1,66 @@ +package com.coding.basic; + +public class Queue { + private Node first; + private Node last; + private int size = 0; + public void enQueue(Object o){ + if(null == first){ + Node node = new Node(); + node.data = o; + node.next = null; + first = node; + last = node; + }else{ + Node node = new Node(); + node.data = o; + node.next = null; + last.next = node; + last = node; + } + size++; + } + + public Object deQueue(){ + Node second = first.next; + Object o = first.data; + if(null != second){ + first = second; + return o; + }else{ + first = null; + size = 0; + return o; + } + } + + public boolean isEmpty(){ + if(size > 0){ + return false; + }else{ + return true; + } + } + + public int size(){ + return size; + } + static class Node{ + Node next; + Object data; + } + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if(null != first){ + sb.append(first.data.toString() + ","); + Node node = first.next; + sb.append(node.data.toString() + ","); + while(null != node.next){ + node = node.next; + sb.append(node.data.toString() + ","); + } + } + return sb.toString(); + } +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/QueueTest.java b/group16/420355244/Homework1/src/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..50d7fc0903 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/QueueTest.java @@ -0,0 +1,56 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class QueueTest { + + public static Queue queue = new Queue(); + @Before + public void setUp() throws Exception { + } + + @Test + public void testEnQueue() { + queue.enQueue("aaa"); + queue.enQueue("bbb"); + queue.enQueue("ccc"); + queue.enQueue("aaa"); + System.out.println(queue); + } + + @Test + public void testDeQueue() { + queue.enQueue("aaa"); + queue.enQueue("bbb"); + queue.enQueue("ccc"); + queue.enQueue("ddd"); + queue.enQueue("eee"); + queue.deQueue(); + System.out.println(queue); + } + + @Test + public void testIsEmpty() { + System.out.println(queue.isEmpty()); + + } + + @Test + public void testSize() { + queue.enQueue("aaa"); + queue.enQueue("bbb"); + queue.enQueue("ccc"); + queue.enQueue("ddd"); + queue.enQueue("eee"); + System.out.println(queue.size()); + } + + @Test + public void testToString() { + fail("Not yet implemented"); + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/Stack.java b/group16/420355244/Homework1/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..5c59bf34fc --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/Stack.java @@ -0,0 +1,39 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + private int size = 0; + public void push(Object o){ + //鍏ユ爤鍦ㄦ爤椤惰繘鍏ユ渶鍚庡帇鍏 + elementData.add(o); + size ++; + } + + public Object pop(){ + Object object = elementData.get(size -1); + elementData.remove(size -1); + size --; + return object; + } + + public Object peek(){ + Object object = elementData.get(size -1); + return object; + } + public boolean isEmpty(){ + if(size <= 0){ + return true; + }else{ + return false; + } + } + public int size(){ + return size; + } + + @Override + public String toString() { + return elementData.toString(); + } + +} diff --git a/group16/420355244/Homework1/src/com/coding/basic/StackTest.java b/group16/420355244/Homework1/src/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..b436785574 --- /dev/null +++ b/group16/420355244/Homework1/src/com/coding/basic/StackTest.java @@ -0,0 +1,62 @@ +package com.coding.basic; + +import org.junit.Before; +import org.junit.Test; + +public class StackTest { + private static Stack stack = new Stack(); + @Before + public void setUp() throws Exception { + } + + @Test + public void testPush() { + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + System.out.println(stack); + } + + @Test + public void testPop() { + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + Object pop = stack.pop(); + System.out.println(pop); + System.out.println(stack); + } + + @Test + public void testPeek() { + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + Object peek = stack.peek(); + System.out.println(peek); + } + + @Test + public void testIsEmpty() { + System.out.println(stack.isEmpty()); + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + System.out.println(stack.isEmpty()); + stack.pop(); + stack.pop(); + stack.pop(); + System.out.println(stack.isEmpty()); + } + + @Test + public void testSize() { + stack.push("aaa"); + stack.push("bbb"); + stack.push("ccc"); + stack.pop(); + stack.pop(); + System.out.println(stack.size()); + } + +} diff --git a/group16/420355244/Homework2/.classpath b/group16/420355244/Homework2/.classpath new file mode 100644 index 0000000000..f5d4a033ec --- /dev/null +++ b/group16/420355244/Homework2/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/group16/420355244/Homework2/.gitignore b/group16/420355244/Homework2/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group16/420355244/Homework2/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group16/420355244/Homework2/.project b/group16/420355244/Homework2/.project new file mode 100644 index 0000000000..1a13fc592d --- /dev/null +++ b/group16/420355244/Homework2/.project @@ -0,0 +1,17 @@ + + + Homework2 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/420355244/Homework2/src/com/coderising/action/LoginAction.java b/group16/420355244/Homework2/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..5496d8084d --- /dev/null +++ b/group16/420355244/Homework2/src/com/coderising/action/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.action; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @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/group16/420355244/Homework2/src/com/coderising/array/ArrayUtil.java b/group16/420355244/Homework2/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..5fd5f1efba --- /dev/null +++ b/group16/420355244/Homework2/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,120 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static void reverseArray(int[] origin){ + for(int i = 0;i < origin.length/2; i++){ + int x = origin[i]; + origin[i] = origin[origin.length - i -1]; + origin[origin.length - i -1] = x; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 static int[] removeZero(int[] oldArray){ + for(int i = 0;i < oldArray.length ;i++){ + if(oldArray[i] == 0){ + int[] a = {}; + System.arraycopy(oldArray, 0, a, 0, i); + System.arraycopy(oldArray, 0, a, i, oldArray.length); + oldArray = a; + removeZero(oldArray); + } + } + return oldArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){ + return null; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + return null; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + return null; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + return null; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + return null; + } + + public static void main(String[] args) { + /*int[] a = {7, 9 , 30, 3}; + reverseArray(a); + for (int i : a) { + System.out.print(i+","); + }*/ + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} ; + removeZero(oldArr); + for (int i : oldArr) { + System.out.print(i+","); + } + } +} diff --git a/group16/420355244/Homework2/src/com/coderising/litestruts/Struts.java b/group16/420355244/Homework2/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..40af955dfa --- /dev/null +++ b/group16/420355244/Homework2/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,133 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + //0. 璇诲彇閰嶇疆鏂囦欢struts.xml + SAXReader reader = new SAXReader(); + try { + //0.2 璇诲彇鏂囦欢 + Document doc = reader.read(new File("./src/com/coderising/litestruts/struts.xml")); + //0.3 寰楀埌鏍规爣绛 + Element rootElement = doc.getRootElement(); + //0.4 寰楀埌鏍规爣绛句笅鐨勬墍鏈塧ction鏍囩 + Iterator elementIterator = rootElement.elementIterator("action"); + while(elementIterator.hasNext()){ + Element element = elementIterator.next(); + String nameValue = element.attributeValue("name"); + try { + if(null != actionName && actionName.trim() != ""){ + if(actionName.equals(nameValue)){ + View view = new View(); + //杩涘叆璇ction鏍囩鍐,缁撴潫鍚庡仠姝㈠惊鐜 + String classValue = element.attributeValue("class"); + //1.1 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛夋嵁parameters涓殑鏁版嵁 + Class clazz =Class.forName(classValue); + Object instance = clazz.newInstance(); + Method[] methods = clazz.getMethods(); + for (Entry entry : parameters.entrySet()) { + String methodName = "set" + entry.getKey().substring(0, 1).toUpperCase() + entry.getKey().substring(1); + //1.2璋冪敤瀵硅薄鐨剆etter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 ("name"="test" , "password"="1234") ,閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + for (Method setterMethod : methods) { + if(methodName.equals(setterMethod.getName())){ + setterMethod.invoke(instance,entry.getValue()); + break; + } + } + } + //2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + Method method = clazz.getMethod("execute", null); + Object exectueResult = method.invoke(instance, null); + Iterator resultElement = element.elementIterator("result"); + while(resultElement.hasNext()){ + Element result = resultElement.next(); + if(exectueResult.equals(result.attributeValue("name"))){ + String jsp = result.getText(); + view.setJsp(jsp); + break; + } + } + /*3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters*/ + HashMap hashMap = new HashMap<>(); + for (Method getterMethod : methods) { + if(getterMethod.getName().contains("get")){ + Object resultValue = getterMethod.invoke(instance,null); + String resultKey = getterMethod.getName().replace("get", "").substring(0,1).toLowerCase() + + getterMethod.getName().replace("get", "").substring(1); + hashMap.put(resultKey, resultValue); + } + } + view.setParameters(hashMap); + return view; + } + } + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } catch (DocumentException e) { + e.printStackTrace(); + } + return null; + } + public static void main(String[] args) { + runAction("login",null); + } + +} diff --git a/group16/420355244/Homework2/src/com/coderising/litestruts/StrutsTest.java b/group16/420355244/Homework2/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group16/420355244/Homework2/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group16/420355244/Homework2/src/com/coderising/litestruts/View.java b/group16/420355244/Homework2/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group16/420355244/Homework2/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/group16/420355244/Homework2/src/com/coderising/litestruts/struts.xml b/group16/420355244/Homework2/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..dd598a3664 --- /dev/null +++ b/group16/420355244/Homework2/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 diff --git a/group16/502059278/.classpath b/group16/502059278/.classpath index fb5011632c..c0abaf014f 100644 --- a/group16/502059278/.classpath +++ b/group16/502059278/.classpath @@ -2,5 +2,7 @@ + + diff --git "a/group16/502059278/homework/\350\256\241\347\256\227\346\234\272\346\274\253\350\260\210_\344\275\234\344\270\232.docx" "b/group16/502059278/homework/\350\256\241\347\256\227\346\234\272\346\274\253\350\260\210_\344\275\234\344\270\232.docx" deleted file mode 100644 index 31dfe4c14b..0000000000 Binary files "a/group16/502059278/homework/\350\256\241\347\256\227\346\234\272\346\274\253\350\260\210_\344\275\234\344\270\232.docx" and /dev/null differ diff --git a/group16/502059278/src/cn/mark/MyArrayList.java b/group16/502059278/src/cn/mark/work0219/MyArrayList.java similarity index 99% rename from group16/502059278/src/cn/mark/MyArrayList.java rename to group16/502059278/src/cn/mark/work0219/MyArrayList.java index 9e0e406274..3d7a064919 100644 --- a/group16/502059278/src/cn/mark/MyArrayList.java +++ b/group16/502059278/src/cn/mark/work0219/MyArrayList.java @@ -1,4 +1,4 @@ -package cn.mark; +package cn.mark.work0219; import java.util.ArrayList; import java.util.Arrays; diff --git a/group16/502059278/src/cn/mark/MyLinkedList.java b/group16/502059278/src/cn/mark/work0219/MyLinkedList.java similarity index 97% rename from group16/502059278/src/cn/mark/MyLinkedList.java rename to group16/502059278/src/cn/mark/work0219/MyLinkedList.java index 7f9c3856a2..896d7bcb79 100644 --- a/group16/502059278/src/cn/mark/MyLinkedList.java +++ b/group16/502059278/src/cn/mark/work0219/MyLinkedList.java @@ -1,4 +1,4 @@ -package cn.mark; +package cn.mark.work0219; /** * 鑷畾涔夊疄鐜癓inkedList鏁版嵁缁撴瀯 * @author hilih diff --git a/group16/502059278/src/cn/mark/MyList.java b/group16/502059278/src/cn/mark/work0219/MyList.java similarity index 96% rename from group16/502059278/src/cn/mark/MyList.java rename to group16/502059278/src/cn/mark/work0219/MyList.java index 19bc3f92fe..91f26ff8d5 100644 --- a/group16/502059278/src/cn/mark/MyList.java +++ b/group16/502059278/src/cn/mark/work0219/MyList.java @@ -1,4 +1,4 @@ -package cn.mark; +package cn.mark.work0219; public interface MyList { /** diff --git a/group16/502059278/src/cn/mark/work0226/ArrayUtil.java b/group16/502059278/src/cn/mark/work0226/ArrayUtil.java new file mode 100644 index 0000000000..94e253e8bb --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/ArrayUtil.java @@ -0,0 +1,166 @@ +package cn.mark.work0226; + +import java.util.Arrays; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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];//澹版槑缃崲鍚庢暟缁 + int temp = target.length - 1;//璁板綍缃崲鍚庝笅鏍囦綅缃 + for( int i = 0; i < origin.length; i++ ){ + target[temp] = origin[i]; + temp--; + } + System.out.println("缃崲鍓嶏細"+Arrays.toString(origin)); + System.out.println("缃崲鍚庯細"+Arrays.toString(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[] target = new int[1]; + boolean flag = true; + for( int i = 0; i < oldArray.length; i++ ){ + if ( oldArray[i] == 0 ){ // 璺宠繃鍊间负0鐨勫厓绱 + continue; + } + + if ( flag ){ + //棣栦綅璧嬪兼棤闇鎵╁ + target[target.length-1] = oldArray[i]; + flag = false; + }else{ + //纭畾鍊间笉鏄0鎵嶈兘杩涘叆鎵╁姝ラ + target = Arrays.copyOf(target, target.length+1); + target[target.length-1] = oldArray[i]; + } + + + + } + System.out.println("鍘0鍓嶏細"+Arrays.toString(oldArray)); + System.out.println("鍘0鍚庯細"+Arrays.toString(target)); + return target; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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){ + //1.鍘婚噸 + int[] array3 = Arrays.copyOf(array1, array1.length); + for( int i = 0; i < array1.length; i++ ){ + for( int j = 0; j < array2.length ; j++ ){ + if ( array1[i] == array2[j] ){ + + } + } + + + + + } + + System.out.println(Arrays.toString(array1)); + System.out.println(Arrays.toString(array2)); + //2.鍚堝苟 + + //3.鎺掑簭 + + + return null; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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 = Arrays.copyOf(oldArray, oldArray.length+size); + System.out.println("鎵╁鍚庯細"+Arrays.toString(newArray)); + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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){ + return null; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for( int i = 0; i < array.length; i++ ){ + if ( i == 0 ){ + sb.append(array[i]); + }else{ + sb.append(seperator+array[i]); + } + } + return sb.toString(); + } + + + public static void main(String[] args) { + int[] a1 =new int[]{3,8}, a2 = new int[]{4, 5, 6,7}; + System.out.println(new ArrayUtil().join(a1, "*")); + + } +} diff --git a/group16/502059278/src/cn/mark/work0226/TestArrayUtil.java b/group16/502059278/src/cn/mark/work0226/TestArrayUtil.java new file mode 100644 index 0000000000..da1abd3b38 --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/TestArrayUtil.java @@ -0,0 +1,35 @@ +package cn.mark.work0226; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class TestArrayUtil { + ArrayUtil arrayUtil = null; + + @Before + public void setUp() throws Exception { + arrayUtil = new ArrayUtil(); + + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void reverseArray() { + int[] origin = new int[]{1,2,8,31}; + arrayUtil.reverseArray(origin); + } + + @Test + public void removeZero() { + int[] origin = new int[]{0,1,2,0,3,0,4,7,0}; + Assert.assertArrayEquals(new int[]{1, 2, 3, 4, 7}, arrayUtil.removeZero(origin)); + } + +} diff --git a/group16/502059278/src/cn/mark/work0226/litestruts/LoginAction.java b/group16/502059278/src/cn/mark/work0226/litestruts/LoginAction.java new file mode 100644 index 0000000000..4966d7d170 --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package cn.mark.work0226.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/group16/502059278/src/cn/mark/work0226/litestruts/Struts.java b/group16/502059278/src/cn/mark/work0226/litestruts/Struts.java new file mode 100644 index 0000000000..93a449adaf --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/litestruts/Struts.java @@ -0,0 +1,84 @@ +package cn.mark.work0226.litestruts; + +import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + String classPath = null; + String className = null; + + Document dom = XMLUtils.getDocument("bin"+File.separator+"struts.xml"); + Element ele = XMLUtils.getElement(dom, actionName); + Attribute classAttr = ele.attribute("class"); + classPath = classAttr.getValue(); + className = classPath.substring(classPath.lastIndexOf(".")+1); + System.out.println(className); + + + + try { + Class clz = Class.forName(classPath); + System.out.println(clz.getName()); + + + + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + return null; + } + + + public static void main(String[] args) { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + Struts.runAction(actionName,params); + } +} diff --git a/group16/502059278/src/cn/mark/work0226/litestruts/StrutsTest.java b/group16/502059278/src/cn/mark/work0226/litestruts/StrutsTest.java new file mode 100644 index 0000000000..1a60626460 --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package cn.mark.work0226.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/group16/502059278/src/cn/mark/work0226/litestruts/View.java b/group16/502059278/src/cn/mark/work0226/litestruts/View.java new file mode 100644 index 0000000000..31e3df3427 --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/litestruts/View.java @@ -0,0 +1,23 @@ +package cn.mark.work0226.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/group16/502059278/src/cn/mark/work0226/litestruts/XMLUtils.java b/group16/502059278/src/cn/mark/work0226/litestruts/XMLUtils.java new file mode 100644 index 0000000000..2eeb6e624e --- /dev/null +++ b/group16/502059278/src/cn/mark/work0226/litestruts/XMLUtils.java @@ -0,0 +1,49 @@ +package cn.mark.work0226.litestruts; + +import java.util.List; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class XMLUtils { + /** + * 鑾峰彇Document + * @param filePath 閰嶇疆鏂囦欢璺緞鍚 + * @return Document瀵硅薄 + */ + public static Document getDocument(String filePath){ + //1.鍒涘缓瑙f瀽鍣 + SAXReader reader = new SAXReader(); + //2.瑙f瀽XML鏂囨。锛岃繑鍥瀌ocument瀵硅薄 + Document dom = null; + try { + dom = reader.read(filePath); + } catch (DocumentException e) { + e.printStackTrace(); + } + return dom; + } + /** + * 鑾峰彇鎸囧畾action鍏冪礌 + * @param doc Document + * @param actionName 瑕佽幏鍙栫殑鍏冪礌灞炴у悕 + * @return 鍖呭惈鎵瑕佸睘鎬х殑鍏冪礌 + */ + public static Element getElement(Document doc , String actionName){ + Element result = null; + Element root = doc.getRootElement(); + List elements = root.elements(); + for(Element e : elements){ + Attribute attr = e.attribute("name"); + if(attr.getValue().equals(actionName)){ + result = e; + return result; + } + } + return result; + } + +} diff --git a/group16/502059278/src/struts.xml b/group16/502059278/src/struts.xml new file mode 100644 index 0000000000..1aaa6ea5a0 --- /dev/null +++ b/group16/502059278/src/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/group16/542087872/out/production/coding2017/com/coderising/litestruts/struts.xml b/group16/542087872/out/production/coding2017/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..99063bcb0c --- /dev/null +++ b/group16/542087872/out/production/coding2017/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 diff --git a/group16/542087872/out/production/coding2017/net/coding/coderising/litestruts/struts.xml b/group16/542087872/out/production/coding2017/net/coding/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..848f04cfba --- /dev/null +++ b/group16/542087872/out/production/coding2017/net/coding/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 diff --git a/group16/542087872/src/com/coding/basic/ArrayList.java b/group16/542087872/src/net/coding/basic/ArrayList.java similarity index 98% rename from group16/542087872/src/com/coding/basic/ArrayList.java rename to group16/542087872/src/net/coding/basic/ArrayList.java index 1b10b441cf..a0286827d6 100644 --- a/group16/542087872/src/com/coding/basic/ArrayList.java +++ b/group16/542087872/src/net/coding/basic/ArrayList.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; import java.util.Arrays; diff --git a/group16/542087872/src/com/coding/basic/BinaryTreeNode.java b/group16/542087872/src/net/coding/basic/BinaryTreeNode.java similarity index 97% rename from group16/542087872/src/com/coding/basic/BinaryTreeNode.java rename to group16/542087872/src/net/coding/basic/BinaryTreeNode.java index df167343a0..4cac873d08 100644 --- a/group16/542087872/src/com/coding/basic/BinaryTreeNode.java +++ b/group16/542087872/src/net/coding/basic/BinaryTreeNode.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; public class BinaryTreeNode { diff --git a/group16/542087872/src/net/coding/basic/Iterator.java b/group16/542087872/src/net/coding/basic/Iterator.java new file mode 100644 index 0000000000..ca3fd054ae --- /dev/null +++ b/group16/542087872/src/net/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package net.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group16/542087872/src/com/coding/basic/LinkedList.java b/group16/542087872/src/net/coding/basic/LinkedList.java similarity index 99% rename from group16/542087872/src/com/coding/basic/LinkedList.java rename to group16/542087872/src/net/coding/basic/LinkedList.java index 144af4ec8d..0f9d326545 100644 --- a/group16/542087872/src/com/coding/basic/LinkedList.java +++ b/group16/542087872/src/net/coding/basic/LinkedList.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; public class LinkedList implements List { diff --git a/group16/542087872/src/net/coding/basic/List.java b/group16/542087872/src/net/coding/basic/List.java new file mode 100644 index 0000000000..189fe091d8 --- /dev/null +++ b/group16/542087872/src/net/coding/basic/List.java @@ -0,0 +1,9 @@ +package net.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/group16/542087872/src/com/coding/basic/Queue.java b/group16/542087872/src/net/coding/basic/Queue.java similarity index 92% rename from group16/542087872/src/com/coding/basic/Queue.java rename to group16/542087872/src/net/coding/basic/Queue.java index 8e4285464b..d233a02617 100644 --- a/group16/542087872/src/com/coding/basic/Queue.java +++ b/group16/542087872/src/net/coding/basic/Queue.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; public class Queue { diff --git a/group16/542087872/src/com/coding/basic/Stack.java b/group16/542087872/src/net/coding/basic/Stack.java similarity index 94% rename from group16/542087872/src/com/coding/basic/Stack.java rename to group16/542087872/src/net/coding/basic/Stack.java index bfe98dd8b7..4a8099bcee 100644 --- a/group16/542087872/src/com/coding/basic/Stack.java +++ b/group16/542087872/src/net/coding/basic/Stack.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; public class Stack { private ArrayList elementData = new ArrayList(); diff --git a/group16/542087872/src/com/coding/basic/Test.java b/group16/542087872/src/net/coding/basic/Test.java similarity index 99% rename from group16/542087872/src/com/coding/basic/Test.java rename to group16/542087872/src/net/coding/basic/Test.java index 2db5b2f9ab..d973793899 100644 --- a/group16/542087872/src/com/coding/basic/Test.java +++ b/group16/542087872/src/net/coding/basic/Test.java @@ -1,4 +1,4 @@ -package com.coding.basic; +package net.coding.basic; /** diff --git a/group16/542087872/src/net/coding/coderising/array/ArrayUtil.java b/group16/542087872/src/net/coding/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..7cf4e1fd3f --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/array/ArrayUtil.java @@ -0,0 +1,203 @@ +package net.coding.coderising.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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 l = 0, r = origin.length - 1; + while (l < r) { + int tmp = origin[l]; + origin[l] = origin[r]; + origin[r] = tmp; + l++; + r--; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 cntZero = 0; + for (int i = 0; i < oldArray.length;i++) { + if (oldArray[i] == 0) { + cntZero ++; + } + } + if (cntZero == 0) { + return oldArray; + } + + int[] newArray = new int[oldArray.length - cntZero]; + int j = 0; + for (int i = 0; i < oldArray.length;i++) { + if (oldArray[i] != 0) { + newArray[j++] = oldArray[i]; + } + } + + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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 l = 0; + int r = 0; + + int cnt = 0; + while (true) { + if (l >= array1.length && r >= array2.length) { + break; + } + if (l >= array1.length) { + result[cnt++] = array2[r]; + r++; + } else if (r >= array1.length) { + result[cnt++] = array1[l]; + l++; + } else { + if (array1[l] < array2[r]) { + result[cnt++] = array1[l]; + l++; + } else { + result[cnt++] = array2[r]; + r++; + } + } + } + + 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[] result = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, result, 0, oldArray.length); + return result; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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[] result = {}; + int a = 0; + int b = 1; + + int cnt = 0; + while (b < max) { + result = grow(result, 1); + result[cnt++] = b; + int tmp = a + b; + a = b; + b = tmp; + } + + return result; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[1,2,3,5,7,11,13,17,19] + * @param max + * @return + */ + private boolean isPrime(int n) { + for (int i = 2; i <= Math.sqrt(n + 1.0); i++) { + if (n % i == 0) { + return false; + } + } + return true; + } + public int[] getPrimes(int max){ + int[] result = {}; + int cnt = 0; + for (int i = 1; i < max; i++) { + if (isPrime(i)) { + result = grow(result, 1); + result[cnt++] = i; + } + } + return result; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + private boolean isPerfect(int n) { + int total = 0; + for (int i = 1; i < n; i++) { + if (n % i == 0) { + total += i; + } + } + + return total == n; + } + public int[] getPerfectNumbers(int max){ + int[] result = {}; + int cnt = 0; + for (int i = 1; i < max; i++) { + if (isPerfect(i)) { + result = grow(result, 1); + result[cnt++] = i; + } + } + return result; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + if (sb.length() > 0) { + sb.append(seperator); + } + sb.append(array[i]); + } + return sb.toString(); + } + + +} diff --git a/group16/542087872/src/net/coding/coderising/array/ArrayUtilTest.java b/group16/542087872/src/net/coding/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..0ae290bbfd --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/array/ArrayUtilTest.java @@ -0,0 +1,76 @@ +package net.coding.coderising.array; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by xiaoyuan on 02/03/2017. + */ +public class ArrayUtilTest { + + @Test + public void testReverseArray() { + int[] nums = {1, 2, 3}; + new ArrayUtil().reverseArray(nums); + Assert.assertArrayEquals(nums, new int[]{3, 2, 1}); + } + // removeZero + @Test + public void testRemoveZero() { + int[] nums = {0, 1, 0, 2, 3}; + int[] ans = new ArrayUtil().removeZero(nums); + Assert.assertArrayEquals(ans, new int[]{1, 2, 3}); + } + + // merge + @Test + public void testMerge() { + int[] nums1 = {1, 3, 9}; + int[] nums2 = {2, 4, 5}; + int[] ans = new ArrayUtil().merge(nums1, nums2); + Assert.assertArrayEquals(ans, new int[]{1, 2, 3, 4, 5, 9}); + } + + // grow + @Test + public void testGrow() { + int[] nums = {1, 3, 9}; + int[] ans = new ArrayUtil().grow(nums, 2); + Assert.assertArrayEquals(ans, new int[]{1, 3, 9, 0, 0}); + } + + // fibonacci + @Test + public void testFibonacci() { + int[] ans = new ArrayUtil().fibonacci(10); + Assert.assertArrayEquals(ans, new int[]{1, 1, 2, 3, 5, 8}); + } + + + // getPrimes + @Test + public void testgetPrimes() { + int[] ans = new ArrayUtil().getPrimes(10); + Assert.assertArrayEquals(ans, new int[]{1, 2, 3, 5, 7}); + } + + + // getPerfectNumbers + @Test + public void testGetPerfectNumbers() { + int[] ans = new ArrayUtil().getPerfectNumbers(10); + Assert.assertArrayEquals(ans, new int[]{6}); + } + + + // join + + @Test + public void testJoin() { + String ans = new ArrayUtil().join(new int[]{1, 3, 4}, "-"); + Assert.assertEquals(ans, "1-3-4"); + } + + + +} diff --git a/group16/542087872/src/net/coding/coderising/litestruts/LoginAction.java b/group16/542087872/src/net/coding/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..5e4f956c2f --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package net.coding.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/group16/542087872/src/net/coding/coderising/litestruts/LogoutAction.java b/group16/542087872/src/net/coding/coderising/litestruts/LogoutAction.java new file mode 100644 index 0000000000..560d8f4fd8 --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/litestruts/LogoutAction.java @@ -0,0 +1,36 @@ +package net.coding.coderising.litestruts; + +/** + * Created by xiaoyuan on 02/03/2017. + */ +public class LogoutAction { + + String ifLogout; + String message; + + public String execute() { + if (ifLogout.equals("yes")) { + this.message = "success"; + return "success"; + } else { + this.message = "error"; + return "error"; + } + } + + public String getIfLogout() { + return ifLogout; + } + + public void setIfLogout(String ifLogout) { + this.ifLogout = ifLogout; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/group16/542087872/src/net/coding/coderising/litestruts/Struts.java b/group16/542087872/src/net/coding/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..1d9337f4f2 --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/litestruts/Struts.java @@ -0,0 +1,126 @@ +package net.coding.coderising.litestruts; + + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + Map name2ClassMap = new HashMap(); + Map result2JSPMap = new HashMap(); + + + try { + File xmlFile = new File("group16/542087872/src/net/coding/coderising/litestruts/struts.xml"); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = factory.newDocumentBuilder(); + Document doc = documentBuilder.parse(xmlFile); + + doc.getDocumentElement().normalize(); + + NodeList actionList = doc.getElementsByTagName("action"); + for (int i = 0; i < actionList.getLength(); i++) { + Node node = actionList.item(i); + System.out.println(node.getNodeName()); + if (node.getNodeType() == Node.ELEMENT_NODE) { + Element element = (Element)node; + String acName = element.getAttribute("name"); + String acClass = element.getAttribute("class"); + name2ClassMap.put(acName, acClass); + + NodeList resultList = element.getElementsByTagName("result"); + for (int j = 0; j < resultList.getLength(); j++) { + Element resultElemet = (Element)(resultList.item(j)); + String acResultName = resultElemet.getAttribute("name"); + String acResultJSP = resultElemet.getTextContent(); + + result2JSPMap.put(acName + "_" + acResultName, acResultJSP); + } + } + } + + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("parse XML ERROR!"); + } + + String classStr = name2ClassMap.get(actionName); + if (classStr == null) { + throw new RuntimeException("ACTION FOUND ERROR!"); + } + + try { + Class actionClass = Class.forName(classStr); + Object actionObj = actionClass.newInstance(); + for (String key : parameters.keySet()) { + String value = parameters.get(key); + Method theMethod = actionClass.getMethod("set" + key.substring(0, 1).toUpperCase() + key.substring(1), String.class); + theMethod.invoke(actionObj, value); + } + + // execut + Method exeMethod = actionClass.getMethod("execute"); + String result = (String)exeMethod.invoke(actionObj); + + // find JSP + String JSPPath = result2JSPMap.get(actionName + "_" + result); + View view = new View(); + view.setJsp(JSPPath); + + // generate map + Map map = new HashMap(); + for(Method method: actionClass.getMethods()) { + if (method.getName().startsWith("get")) { + Object key = method.getName().substring(3).toLowerCase(); + Object value = method.invoke(actionObj); + map.put(key, value); + } + } + view.setParameters(map); + + return view; + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + + public static void main(String[] args) { + runAction(null, null); + } + +} diff --git a/group16/542087872/src/net/coding/coderising/litestruts/StrutsTest.java b/group16/542087872/src/net/coding/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..8c4de18ac4 --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/litestruts/StrutsTest.java @@ -0,0 +1,70 @@ +package net.coding.coderising.litestruts; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + + +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")); + } + + + @Test + public void testLogoutActionSuccess() { + String actionName = "logout"; + + Map params = new HashMap(); + params.put("ifLogout","yes"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/welcome.jsp", view.getJsp()); + Assert.assertEquals("success", view.getParameters().get("message")); + } + + @Test + public void testLogoutActionError() { + String actionName = "logout"; + + Map params = new HashMap(); + params.put("ifLogout","no"); + + + View view = Struts.runAction(actionName,params); + + Assert.assertEquals("/jsp/error.jsp", view.getJsp()); + Assert.assertEquals("error", view.getParameters().get("message")); + } + +} diff --git a/group16/542087872/src/net/coding/coderising/litestruts/View.java b/group16/542087872/src/net/coding/coderising/litestruts/View.java new file mode 100644 index 0000000000..63fb1d025e --- /dev/null +++ b/group16/542087872/src/net/coding/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package net.coding.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/group16/542087872/src/net/coding/coderising/litestruts/struts.xml b/group16/542087872/src/net/coding/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..848f04cfba --- /dev/null +++ b/group16/542087872/src/net/coding/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 diff --git a/group16/63912401/.classpath b/group16/63912401/.classpath new file mode 100644 index 0000000000..74368f11c3 --- /dev/null +++ b/group16/63912401/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/group16/63912401/.gitignore b/group16/63912401/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group16/63912401/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group16/63912401/.project b/group16/63912401/.project new file mode 100644 index 0000000000..ec6117b543 --- /dev/null +++ b/group16/63912401/.project @@ -0,0 +1,17 @@ + + + 63912401 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group16/63912401/src/com/coderising/action/LoginAction.java b/group16/63912401/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..64d4d355b6 --- /dev/null +++ b/group16/63912401/src/com/coderising/action/LoginAction.java @@ -0,0 +1,45 @@ +package com.coderising.action; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author liuxin + * + */ +public class LoginAction{ + private String name ; + private String password; + private String message; + + 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 String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/group16/63912401/src/com/coderising/array/ArrayUtil.java b/group16/63912401/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..c8510b08e1 --- /dev/null +++ b/group16/63912401/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,270 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + + +/** + * ArrayUtil + * @author greenhills + * 2017骞2鏈28鏃 涓嬪崍10:49:41 + */ +public class ArrayUtil { + + public static void main(String[] args) { +// int[] array1={5,8,9,0,-4}; +// int[] array2={4,5,6,7,8,9}; +// int[] array3=ArrayUtil.merge(array1, array2); +// for(Integer t:array3){ +// System.out.print(t +"\t"); +// } + +// int[] array3={4,5,6,7,8,9}; +// array3 = ArrayUtil.grow(array3,5); +// for(int t:array3){ +// System.out.print(t +"\t"); +// } + +// int[] array4=ArrayUtil.fibonacci(100); +// for(Integer t:array4){ +// System.out.print(t +"\t"); +// } + +// int[] array5=ArrayUtil.getPrimes(2); +// for(Integer t:array5){ +// System.out.print(t +"\t"); +// } + + int[] array6=ArrayUtil.getPerfectNumbers(2000); + for(Integer t:array6){ + System.out.print(t +"\t"); + } + + } + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static void reverseArray(int[] origin){ + for (int i = 0; i < origin.length>>1; i++) { + origin[i]^=origin[origin.length - 1 - i]^(origin[origin.length - 1 - i]=origin[i]); + } + + //鏂规硶2 鍙互浣跨敤Collections.reverse(list)鏂规硶锛屼絾鏄痠nt 鍜 Integer鏁扮粍涔嬮棿杞寲娑堣楁ц兘 + //Collections.reverse(list); + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 Integer 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){ + List list=new ArrayList(); + for(Integer data:oldArray){ + if(data != 0){ + list.add(data); + } + } + int[] newArray=new int[list.size()]; + for(Integer i=0;i set=new HashSet(); + for(Integer t:array1){ + set.add(t); + } + for(Integer t:array2){ + set.add(t); + } + List list=new ArrayList(set); + java.util.Collections.sort(list); + int[] result =new int[list.size()]; + for(int i=0;i 1){ + List list=new ArrayList(); + list.add(1); + list.add(1); + int i=0; + int temp=list.get(i)+list.get(i+1); + while(temp < max){ + list.add(temp); + i++; + temp=list.get(i)+list.get(i+1); + } + + result=new int[list.size()]; + + i=0; + for(int d:list){ + result[i]=d; + i++; + } + } + + return result; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + List list=new ArrayList(); + for(int i=2;i list=new ArrayList(); + for(int i=1;i resultMappings=new HashMap(); + + public ActionMapping() {} + + public ActionMapping(String name, String className, String method) { + this.name = name; + this.className = className; + this.method = StringUtils.isBlank(method) ? "execute" : method; //鏈厤缃椂锛岄粯璁ゆ煡鎵緀xecute鏂规硶鎵ц + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getClassName() { + return className; + } + public void setClassName(String className) { + this.className = className; + } + public String getMethod() { + return method; + } + public void setMethod(String method) { + this.method = method; + } + public Map getResultMappings() { + return resultMappings; + } + public void setResultMappings(Map resultMappings) { + this.resultMappings = resultMappings; + } + + //鎵╁睍鐨勬柟娉曪紝鐢ㄤ簬淇濆瓨 + public void setResultMappings(String key, ResultMapping value) { + this.resultMappings.put(key, value); + } +} diff --git a/group16/63912401/src/com/coderising/litestruts/ConfigurationManager.java b/group16/63912401/src/com/coderising/litestruts/ConfigurationManager.java new file mode 100644 index 0000000000..55204d1b38 --- /dev/null +++ b/group16/63912401/src/com/coderising/litestruts/ConfigurationManager.java @@ -0,0 +1,83 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +/** + * 瑙f瀽XML鏂囦欢 + * ConfigurationManager + * @author greenhills + * 2017骞2鏈27鏃 涓嬪崍10:18:10 + */ +public class ConfigurationManager { + /** + * 璇诲彇xml鏂囦欢鐨勬枃妗e璞 + */ + private static Document document; + /** + * 閰嶇疆鏂囦欢璺緞 + */ + static String configureFileName="struts.xml"; + /** + * 褰撳墠绫昏矾寰 + */ + static String currentPath; + static{ + currentPath = ConfigurationManager.class.getResource("").getPath().substring(1); + } + + static{ + try { + SAXReader sax=new SAXReader(); + document = sax.read(new File(currentPath+configureFileName)); + } catch (DocumentException e) { + e.printStackTrace(); + } + } + + /** + * 瑙f瀽閰嶇疆鏂囦欢 + * ConfigurationManager.java + * @param @return + * @author greenhills + * 2017骞2鏈27鏃 涓嬪崍11:28:59 + */ + public static Map loadXml(){ + Map actionMappings=new HashMap(); + + Element root=document.getRootElement(); + List actionList= root.elements("action"); + + for(Element actionElemnt:actionList){ + ActionMapping actionMapping=new ActionMapping( + actionElemnt.attributeValue("name"), + actionElemnt.attributeValue("class"), + actionElemnt.attributeValue("method") + ); + //鑾峰彇action涓嬬殑result鑺傜偣 + List resultList = actionElemnt.elements("result"); + for(Element resultElemnt:resultList){ + ResultMapping resultMapping=new ResultMapping( + resultElemnt.attributeValue("name"), + resultElemnt.attributeValue("type"), + resultElemnt.getTextTrim() + ); + + //淇濆瓨ResultMapping锛堜互result鏍囩鐨刵ame涓簁ey锛 + actionMapping.setResultMappings(resultMapping.getName(),resultMapping); + } + + //淇濆瓨ActionMapping锛堜互action鏍囩鐨刵ame涓簁ey锛 + actionMappings.put(actionMapping.getName(), actionMapping); + } + + return actionMappings; + } +} diff --git a/group16/63912401/src/com/coderising/litestruts/DefaultAction.java b/group16/63912401/src/com/coderising/litestruts/DefaultAction.java new file mode 100644 index 0000000000..c654a11a02 --- /dev/null +++ b/group16/63912401/src/com/coderising/litestruts/DefaultAction.java @@ -0,0 +1,121 @@ +package com.coderising.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * @author greenhills + * @version 鍒涘缓鏃堕棿锛2017骞2鏈27鏃 涓嬪崍11:48:56 + * + */ +public class DefaultAction { + private ActionMapping actionMapping; + private Object targetAction; //鐢盌efaultAction鍙嶅皠璋冪敤鐨勭洰鏍囧璞 + + //鏋勯犳柟娉曪紝瀹炰緥鍖栧璞 + public DefaultAction(ActionMapping actionMapping) { + this.actionMapping = actionMapping; + //瀹炰緥鍖栧璞 + try { + this.targetAction = Class.forName(this.actionMapping.getClassName()).newInstance(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 鍒濆鍖栧弬鏁 + * DefaultAction.java + * @param + * @author greenhills + * 2017骞2鏈27鏃 涓嬪崍11:58:05 + */ + public void initParam(Map parameters){ + Class clazz=this.targetAction.getClass(); + Set keys=parameters.keySet(); + try { + for(String key:keys){ + String _key = getFirstUpper(key); + //璋冪敤set鏂规硶璧嬪 + Method method=clazz.getDeclaredMethod("set"+_key,clazz.getDeclaredField(key).getType()); + method.invoke(this.targetAction, parameters.get(key)); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 璋冪敤瀹炰緥鏂规硶 + * DefaultAction.java + * @param @return + * @author greenhills + * 2017骞2鏈27鏃 涓嬪崍11:53:56 + */ + public String runMethod(){ + String methodName=this.actionMapping.getMethod(); + Class clazz=this.targetAction.getClass(); + String result="success"; + //璋冪敤set鏂规硶璧嬪 + try { + Method method=clazz.getDeclaredMethod(methodName); + result = (String) method.invoke(this.targetAction); + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } + /** + * 鑾峰彇action瀛楁鐨勫 + * DefaultAction.java + * @param @param fields + * @param @return + * @author greenhills + * 2017骞2鏈28鏃 涓婂崍12:39:01 + */ + public Map getFieldValue(String[] fields){ + Map result=new HashMap(); + try { + Class clazz=this.targetAction.getClass(); + for(String field:fields){ + String _field = getFirstUpper(field); + //璋冪敤get鏂规硶鑾峰彇鍊 + Method method=clazz.getDeclaredMethod("get"+_field); + result.put(field,method.invoke(this.targetAction)); + } + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } + + + /** + * 灏嗛瀛楁瘝鏀逛负澶у啓 + * DefaultAction.java + * @param @param val + * @param @return + * @author greenhills + * 2017骞2鏈28鏃 涓婂崍12:24:34 + */ + private String getFirstUpper(String val){ + return val.substring(0, 1).toUpperCase()+val.substring(1); + } + + + public ActionMapping getActionMapping() { + return actionMapping; + } + public void setActionMapping(ActionMapping actionMapping) { + this.actionMapping = actionMapping; + } + public Object getTargetAction() { + return targetAction; + } + public void setTargetAction(Object targetAction) { + this.targetAction = targetAction; + } +} diff --git a/group16/63912401/src/com/coderising/litestruts/ResultMapping.java b/group16/63912401/src/com/coderising/litestruts/ResultMapping.java new file mode 100644 index 0000000000..9a2650db09 --- /dev/null +++ b/group16/63912401/src/com/coderising/litestruts/ResultMapping.java @@ -0,0 +1,48 @@ +package com.coderising.litestruts; + +import org.apache.commons.lang3.StringUtils; + +/** + * @author greenhills + * @version 鍒涘缓鏃堕棿锛2017骞2鏈27鏃 涓嬪崍10:39:20 + * + */ +public class ResultMapping { + /** + * 鏄犲皠缁撴瀯锛/jsp/homepage.jsp + */ + private String name; + private String type; + private String urlPath; + + public ResultMapping() { + super(); + } + + public ResultMapping(String name, String type, String urlPath) { + super(); + this.name = StringUtils.isBlank(name) ? "success" : name; //榛樿鎴愬姛 + this.type = StringUtils.isBlank(type) ? "dispatcher" : type; //榛樿杞彂 + this.urlPath = urlPath; + } + + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getType() { + return type; + } + public void setType(String type) { + this.type = type; + } + public String getUrlPath() { + return urlPath; + } + public void setUrlPath(String urlPath) { + this.urlPath = urlPath; + } +} diff --git a/group16/63912401/src/com/coderising/litestruts/Struts.java b/group16/63912401/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..06a23bb864 --- /dev/null +++ b/group16/63912401/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,37 @@ +package com.coderising.litestruts; + +import java.util.Map; + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { +// 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + Map actionMappings = ConfigurationManager.loadXml(); + +// 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 +// 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 +// ("name"="test" , "password"="1234") , +// 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + ActionMapping actionMapping = actionMappings.get(actionName); + DefaultAction action=new DefaultAction(actionMapping); + action.initParam(parameters); + +// 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + String result= action.runMethod(); + +// 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, +// 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , +// 鏀惧埌View瀵硅薄鐨刾arameters + + Map messageMaps=action.getFieldValue(new String[]{"message"}); + +// 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 +// 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + View view =new View(); + view.setJsp(actionMapping.getResultMappings().get(result).getUrlPath()); + view.setParameters(messageMaps); + return view; + } + +} diff --git a/group16/63912401/src/com/coderising/litestruts/StrutsTest.java b/group16/63912401/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..4061019b2e --- /dev/null +++ b/group16/63912401/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +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() { + + 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/group16/63912401/src/com/coderising/litestruts/View.java b/group16/63912401/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group16/63912401/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/group16/63912401/src/com/coderising/litestruts/struts.xml b/group16/63912401/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..76cc617a8a --- /dev/null +++ b/group16/63912401/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 diff --git a/group16/63912401/src/com/coding/basic/ArrayList.java b/group16/63912401/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..f86a2e7a8a --- /dev/null +++ b/group16/63912401/src/com/coding/basic/ArrayList.java @@ -0,0 +1,206 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * ArrayList + * @author greenhills + * @version 鍒涘缓鏃堕棿锛2017骞2鏈19鏃 涓嬪崍10:54:02 + * @param + * + */ +public class ArrayList implements List { + /** + * 榛樿瀹归噺 + */ + private static final int DEFAULT_CAPACITY = 10; + + /** + * 鏁版嵁瀛樻斁鍖 + */ + private Object[] elementData; + + /** + * 鐪熷疄鐨勬暟鎹暟閲 + */ + private int size = 0; + + /** + * 鏃犲弬鏋勯犲嚱鏁 + */ + public ArrayList(){ + this.elementData=new Object[DEFAULT_CAPACITY]; + } + + /** + * 甯﹀垵濮嬪ぇ灏忕殑鏋勯犲嚱鏁 + * @param beginSize + */ + public ArrayList(int beginSize){ + if(beginSize<0) + this.elementData=new Object[DEFAULT_CAPACITY]; + else + this.elementData=new Object[beginSize]; + } + + /** + * 鍦ㄥ悗闈㈣拷鍔犳暟鎹 + */ + @Override + public void add(Object o){ + autoGrow(size+1); + this.elementData[size++] = o; //鍦ㄥ熬閮ㄨ拷鍔犳暟鎹 + } + + /** + * 鎶婃暟鎹姞鍏ユ寚瀹氱储寮曞 + */ + @Override + public void add(int index, Object o){ + rangeCheck(index); + autoGrow(size+1); + //鎶奿ndex澶勭殑鎵鏈夋暟鎹線鍚庣Щ + //System.arraycopy(elementData, index, elementData, index+1, size-index); + + for(int i=size;i>index;i--){ + elementData[i] = elementData[i-1]; + } + + this.elementData[index] = o; //浣挎暟鎹繛缁姞鍏 + size++; + } + + /** + * 鑾峰彇鎸囧畾绱㈠紩澶勭殑鏁版嵁 + */ + @Override + public Object get(int index){ + rangeCheck(index); + return elementData[index]; + } + + /** + * 鑾峰彇鏈熬鏁版嵁 + */ + public Object getLast(){ + return elementData[this.size-1]; + } + + /** + * 绉婚櫎绱㈠紩澶勬暟鎹 + */ + @Override + public Object remove(int index){ + rangeCheck(index); + + Object removed = elementData[index]; + int num=size - index - 1; //绉诲姩鏁伴噺 + if(num>0) { + System.arraycopy(elementData, index+1, elementData, index,num); + } + elementData[--size] = null; //娓呴櫎鏈鍚庝竴涓暟鎹綅 + return removed; + } + + /** + * 绉婚櫎鏈熬鏁版嵁 + */ + public Object removeLast(){ + return remove(this.size-1); + } + + /** + * 鑾峰彇鏁版嵁閲 + */ + @Override + public int size(){ + return this.size; + } + + /** + * 鑾峰彇瀛樺偍鏁版嵁鐨勫閲忓ぇ灏 + */ + @Override + public int capacity() { + return this.elementData.length; + } + + /** + * 鍒ゆ柇鏄惁涓虹┖ + */ + @Override + public boolean isEmpty() { + return this.size==0; + } + + /** + * 绌洪棿瀹归噺鑷闀 + * @param minCapacity 澧為暱鍚庢渶灏忓閲 + */ + private void autoGrow(int minCapacity){ + int oldCapacity = elementData.length; + if (minCapacity >= oldCapacity) { + int newCapacity = oldCapacity<<1; //绌洪棿缈诲 + if (newCapacity < minCapacity){ + newCapacity = minCapacity; + } + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + /** + * 鍒ゆ柇鏄惁涓烘湁鏁堢储寮 + * @param @param index + * @param @return + */ + private void rangeCheck(int index) { + if (!isEffectiveIndex(index)) + throw new IndexOutOfBoundsException("Index: "+index+" Out Of Bounds, 鏈夋晥鏁版嵁绱㈠紩鑼冨洿:0~"+(this.size-1)); + } + + /** + * 鍒ゆ柇鏄惁涓烘湁鏁堢储寮 + * @param @param index + * @param @return + */ + private boolean isEffectiveIndex(int index){ + return index >-1 && index < this.size; + } + + /** + * 杩斿洖閬嶅巻鏁版嵁瀵硅薄 + * @param @return + * @author greenhills + * 2017骞2鏈25鏃 涓嬪崍9:55:31 + */ + public Iterator iterator(){ + return new Its(); + } + + /** + * 瀹炵幇Iterator鐨勫唴閮ㄥ疄鐜扮被 + * Its + * @author greenhills + * 2017骞2鏈25鏃 涓嬪崍9:54:54 + */ + private class Its implements Iterator { + private int index=0; + + public Its(){ + //this.len = size; //閫嗗悜閬嶅巻 + } + + @Override + public boolean hasNext() { +// return this.len > 0; //閫嗗悜閬嶅巻 + return this.index < size; //姝e悜閬嶅巻 + } + + @Override + public Object next() { +// return get(--this.len); //閫嗗悜閬嶅巻 +// return elementData[--this.len];//閫嗗悜閬嶅巻 + return get(this.index++); //姝e悜閬嶅巻 + } + } +} diff --git a/group16/63912401/src/com/coding/basic/BinaryTreeNode.java b/group16/63912401/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..3ab1e431c0 --- /dev/null +++ b/group16/63912401/src/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,99 @@ +package com.coding.basic; + +/** + * 浜屽弶鏍戞暟鎹粨鏋 + * BinaryTreeNode + * @author greenhills + * 2017骞2鏈25鏃 涓嬪崍9:51:05 + */ +public class BinaryTreeNode implements Comparable{ + + private int height=0; //褰撳墠鏍戦珮搴 + private Object data; //褰撳墠鑺傜偣鏁版嵁 + private BinaryTreeNode left; //灏忎簬褰撳墠鑺傜偣鏁版嵁data鐨勮妭鐐 + private BinaryTreeNode right; //澶т簬褰撳墠鑺傜偣鏁版嵁data鐨勮妭鐐 + + public BinaryTreeNode() { + } + + public BinaryTreeNode(Object data) { + this.data = data; + } + + public BinaryTreeNode insert(Object o){ + BinaryTreeNode newNode=new BinaryTreeNode(o); + BinaryTreeNode that = findNode(o); + int result=that.compareTo(o); + + if(result<0){//鑺傜偣鏁版嵁灏忎簬鎻掑叆鏁版嵁锛岃繘鍙虫爲 + that.setRight(newNode); + }else if(result>0){ //褰撳墠鑺傜偣鏁版嵁澶т簬鎻掑叆鏁版嵁锛岃繘宸︽爲 + that.setLeft(newNode); + }else{ + return null; + } + newNode.height++; //鏍戦珮搴﹀姞1 + return newNode; + } + + private BinaryTreeNode findNode(Object data){ + int result=this.compareTo(data); + BinaryTreeNode that = new BinaryTreeNode(); //绌鸿妭鐐 + if(result<0){ //褰撳墠鑺傜偣鏁版嵁灏忎簬鎻掑叆鏁版嵁锛岃繘鍙虫爲 + if(this.right==null){ + that = this; + }else{ + that = findNode(this.getRight()); + } + }else if(result>0){ //褰撳墠鑺傜偣鏁版嵁澶т簬鎻掑叆鏁版嵁锛岃繘宸︽爲 + if(this.left==null){ + that = this; + }else{ + that = findNode(this.getLeft()); + } + }else{ + System.out.println("鏃犳晥鏁版嵁"); + } + return that; + } + + public int getTreeMaxHeight(){ + int h=0; + //TODO + + return h; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + 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; + } + + @Override + public int compareTo(Object o) { + if(this.data==null || o==null) return 0; + return Double.valueOf(this.data.toString()).compareTo(Double.valueOf(o.toString())); + } +} diff --git a/group16/63912401/src/com/coding/basic/Iterator.java b/group16/63912401/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..fa815258c1 --- /dev/null +++ b/group16/63912401/src/com/coding/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.coding.basic; + + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group16/63912401/src/com/coding/basic/LinkedList.java b/group16/63912401/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..2120a5a4b8 --- /dev/null +++ b/group16/63912401/src/com/coding/basic/LinkedList.java @@ -0,0 +1,320 @@ +package com.coding.basic; + +import java.util.NoSuchElementException; + +/** + * 閾捐〃鏁版嵁缁撴瀯 + * LinkedList + * @author greenhills + * 2017骞2鏈22鏃 涓嬪崍11:52:41 + */ +public class LinkedList implements List { + /** + * 閾捐〃鏁版嵁閲 + */ + private int size=0; + /** + * 閾捐〃澶磋妭鐐 + */ + private Node head; + /** + * 閾捐〃灏捐妭鐐 + */ + private Node tail; + + + /** + * 鍦ㄦ暟鎹摼灏鹃儴娣诲姞鏁版嵁 + */ + @Override + public void add(Object o) { + addLast(o); + } + + /** + * 鍦ㄦ暟鎹摼鎸囧畾浣嶇疆娣诲姞鏁版嵁 + */ + @Override + public void add(int index, Object data) { + checkIndex(index); + Node old=getNode(index); + link2Before(old,data); + } + + /** + * 鑾峰彇鎸囧畾绱㈠紩澶勬暟鎹 + */ + @Override + public Object get(int index) { + checkIndex(index); + return getNode(index).data; + } + + /** + * 绉婚櫎鎸囧畾绱㈠紩浣嶇疆鐨勮妭鐐 + */ + @Override + public Object remove(int index) { + checkIndex(index); + return unlink(getNode(index)); + } + + /** + * 鑾峰彇鏁版嵁閾剧殑鏁版嵁閲 + */ + @Override + public int size() { + return this.size; + } + + /** + * 鑾峰彇鏁版嵁閾剧殑鏁版嵁閲 + */ + @Override + @Deprecated + public int capacity() { + return size(); + } + + /** + * 鍒ゆ柇鏄惁涓虹┖ + */ + @Override + public boolean isEmpty() { + return this.size==0; + } + + /** + * 鍦ㄦ暟鎹摼澶撮儴杩藉姞鏁版嵁 + * @param data + */ + public void addFirst(Object data){ + final Node oldNode=this.head; + final Node newNode=new Node(null,data,oldNode);//褰㈡垚鏂拌妭鐐癸紝骞舵寚鍚戠涓涓妭鐐 + this.head = newNode; //鍙樻洿闆嗗悎淇濆瓨鐨勯鑺傜偣 + + if(oldNode == null){ //娌℃湁鏁版嵁 + this.tail = newNode; //鍙樻洿闆嗗悎淇濆瓨鐨勫熬鑺傜偣 + }else{ + oldNode.prev = newNode; //鍘熼鑺傜偣鎸囧悜鏂扮殑棣栬妭鐐 + } + this.size++;//鏁版嵁閲忓姞1 + } + + /** + * 鍦ㄦ暟鎹摼灏鹃儴杩藉姞鏁版嵁 + * @param data + */ + public void addLast(Object data){ + final Node oldNode=this.tail; + final Node newNode=new Node(oldNode,data,null);//褰㈡垚鏂拌妭鐐癸紝骞舵寚鍚戞渶鍚庝竴涓妭鐐 + this.tail = newNode; //鍙樻洿闆嗗悎淇濆瓨鐨勫熬鑺傜偣 + + if(oldNode == null){ //娌℃湁鏁版嵁 + this.head = newNode; //鍙樻洿闆嗗悎淇濆瓨鐨勯鑺傜偣 + }else{ + oldNode.next = newNode;//鍘熷熬鑺傜偣鎸囧悜鏂扮殑灏捐妭鐐 + } + this.size++;//鏁版嵁閲忓姞1 + } + + /** + * 鎶婃寚瀹氭暟鎹摼鎺ュ埌鎸囧畾鑺傜偣鍓嶉潰 + */ + void link2Before(Node node,Object data){ + //浼犺繘鏉ョ殑node灏辨槸鍚庤妭鐐 + final Node prev=node.prev; //鎸囧畾鑺傜偣鐨勪笂涓涓妭鐐癸紙鍓嶈妭鐐癸級 + //鐢熸垚鏂拌妭鐐癸紝骞舵寚鍚戝墠鍚庣殑鑺傜偣 + final Node newNode=new Node(prev,data,node);//鐢熸垚鏂拌妭鐐 + //鍚庤妭鐐规寚鍚戞柊鑺傜偣 + node.prev = newNode; + //鍓嶈妭鐐规寚鍚戞柊鑺傜偣 + if(prev == null){//娌℃湁鍓嶈妭鐐逛簡锛堝綋鍓嶈妭鐐瑰凡鏄鑺傜偣锛 + this.head = newNode;//鎶婃柊鐨勮妭鐐逛綔涓洪鑺傜偣 + }else{ + prev.next = newNode; + } + this.size++;//鏁版嵁閲忓姞1 + } + + /** + * 鎶婃寚瀹氭暟鎹摼鎺ュ埌鎸囧畾鑺傜偣鍚庨潰 + */ + void link2Last(Node node,Object data){ + //浼犺繘鏉ョ殑node灏辨槸鍓嶈妭鐐 + final Node next=node.next; //鎸囧畾鑺傜偣鐨勪笅涓涓妭鐐(鍚庤妭鐐) + //鐢熸垚鏂拌妭鐐癸紝骞舵寚鍚戝墠鍚庣殑鑺傜偣 + final Node newNode=new Node(node,data,next); + //鍓嶈妭鐐规寚鍚戞柊鑺傜偣 + node.next = newNode; + //鍚庤妭鐐规寚鍚戞柊鑺傜偣 + if(next == null){//娌℃湁鍚庤妭鐐逛簡锛堝綋鍓嶈妭鐐瑰凡鏄熬鑺傜偣锛 + this.tail = newNode;//鎶婃柊鐨勮妭鐐逛綔涓哄熬鑺傜偣 + }else{ + next.prev = newNode; + } + this.size++;//鏁版嵁閲忓姞1 + } + + /** + * 绉婚櫎棣栬妭鐐 + * @return + */ + public Object removeFirst(){ + return unlink(getNode(0)); + } + + /** + * 绉婚櫎灏捐妭鐐 + * @return + */ + public Object removeLast(){ + return unlink(getNode(this.size-1)); + } + + /** + * 绉婚櫎鑺傜偣 + * @return + */ + Object unlink(Node node){ + final Object element = node.data; + final Node next = node.next; //涓嬩竴涓妭鐐 + final Node prev = node.prev;//鍓嶄竴涓妭鐐 + + if (prev == null) {//寰呭垹闄よ妭鐐规槸棣栬妭鐐 + head = next; + } else { + prev.next = next; + node.prev = null;//瑙i櫎寰呭垹闄よ妭鐐圭殑寮曠敤鍏崇郴 + } + + if (next == null) {//寰呭垹闄よ妭鐐规槸灏捐妭鐐 + tail = prev; + } else { + next.prev = prev; + node.next = null;//瑙i櫎寰呭垹闄よ妭鐐圭殑寮曠敤鍏崇郴 + } + + node.data = null;//娓呴櫎鑺傜偣鏁版嵁 + size--; + return element;//杩斿洖娓呴櫎鑺傜偣鏁版嵁 + } + + /** + * 鍦ㄤ腑闂翠綅缃垱寤篒terator閬嶅巻 + * @return + */ + public Iterator iterator() { + return new Its(this.size>>1); + } + + /** + * 鍦ㄦ寚瀹氫綅缃垱寤篒terator閬嶅巻 + * @return + */ + public Iterator iterator(int index) { + checkIndex(index); + return new Its(index); + } + + /** + * 鑾峰彇鎸囧畾绱㈠紩鐨勮妭鐐瑰璞 + * @param @param index + * @param @return + */ + private Node getNode(int index){ + if (index < (this.size >> 1)) { //鍦ㄥ墠鍗婇儴鍒 + Node node = this.head; + for (int i = 0; i < index; i++){ + node = node.next; //鍚戝悗鏌ユ壘 + } + return node; + } else { //鍦ㄥ悗鍗婇儴鍒 + Node node = this.tail; + for (int i = this.size - 1; i > index; i--){ + node = node.prev; //鍚戝墠鏌ユ壘 + } + return node; + } + } + + /** + * 鍒ゆ柇鏄惁涓烘湁鏁堢储寮 + * @param @param index + * @param @return + */ + private boolean isEffectiveIndex(int index){ + return (index>=0 && index < size); + } + + /** + * 妫娴嬬储寮曟湁鏁堟э紝鏃犳晥鏃舵姏鍑哄紓甯 + * @param index + */ + private void checkIndex(int index) { + if (!isEffectiveIndex(index)) + throw new IndexOutOfBoundsException("Index: "+index+" Out Of Bounds, 鏈澶х储寮: "+(size-1)); + } + + + /** + * 瀹炵幇Iterator鐨勫唴閮ㄥ疄鐜扮被 + */ + private class Its implements Iterator { + private Node lastReturned = null; + private Node node;//褰撳墠鑺傜偣 + private int index;//褰撳墠鑺傜偣绱㈠紩 + + public Its(int index){ + node = isEffectiveIndex(index) ? getNode(index) : null; + this.index = index; + } + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public Object next() { + if (!hasNext()) + throw new NoSuchElementException(); + + lastReturned = node; + node = node.next; + index++; + return lastReturned.data; + + } + + public boolean hasPrevious() { + return index >= 0; + } + + public Object previous() { + if (!hasPrevious()) + throw new NoSuchElementException(); + + lastReturned = node; + node = node.prev; + index--; + return lastReturned.data; + } + } + + /** + * 鑺傜偣鏁版嵁 + * Node + */ + private static class Node{ + Object data; + Node prev; + Node next; + + Node(Node prev,Object data,Node next){ + this.prev=prev; + this.data=data; + this.next=next; + } + } +} diff --git a/group16/63912401/src/com/coding/basic/List.java b/group16/63912401/src/com/coding/basic/List.java new file mode 100644 index 0000000000..7fe012ccab --- /dev/null +++ b/group16/63912401/src/com/coding/basic/List.java @@ -0,0 +1,16 @@ +package com.coding.basic; +/** + * 闆嗗悎鎺ュ彛 + * @author greenhills + * @version 鍒涘缓鏃堕棿锛2017骞2鏈19鏃 涓嬪崍10:49:40 + * + */ +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(); + public int capacity(); + boolean isEmpty(); +} diff --git a/group16/63912401/src/com/coding/basic/Queue.java b/group16/63912401/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..c6007c9e99 --- /dev/null +++ b/group16/63912401/src/com/coding/basic/Queue.java @@ -0,0 +1,43 @@ +package com.coding.basic; + +/** + * 闃熷垪鏁版嵁缁撴瀯 + * Queue + * @author greenhills + * 2017骞2鏈25鏃 涓嬪崍9:50:04 + */ +public class Queue { + private LinkedList elementData = new LinkedList(); + + /** + * 鍏ラ槦 + * @param o + */ + public void enQueue(Object o){ + elementData.addLast(o); + } + + /** + * 鍑洪槦 + * @return + */ + public Object deQueue(){ + return elementData.removeFirst(); + } + + /** + * 鍒ゆ柇鏄惁涓虹┖ + * @return + */ + public boolean isEmpty(){ + return elementData.size()==0; + } + + /** + * 鑾峰彇鏍堝唴鏁版嵁閲 + * @return + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group16/63912401/src/com/coding/basic/Stack.java b/group16/63912401/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..b343c6de1d --- /dev/null +++ b/group16/63912401/src/com/coding/basic/Stack.java @@ -0,0 +1,51 @@ +package com.coding.basic; + +/** + * 鏍堟暟鎹粨鏋 + * Stack + * @author greenhills + * 2017骞2鏈25鏃 涓嬪崍9:49:41 + */ +public class Stack { + private ArrayList elementData = new ArrayList(); + + /** + * 鍏ユ爤 + * @param o + */ + public void push(Object o){ + elementData.add(o); + } + + /** + * 鍑烘爤 + * @return + */ + public Object pop(){ + return elementData.removeLast(); + } + + /** + * 鑾峰彇鏍堥《鏁版嵁 + * @return + */ + public Object peek(){ + return elementData.getLast(); + } + + /** + * 鍒ゆ柇鏄惁涓虹┖ + * @return + */ + public boolean isEmpty(){ + return elementData.size()==0; + } + + /** + * 鑾峰彇鏍堝唴鏁版嵁閲 + * @return + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group16/63912401/src/com/coding/basic/Stack2.java b/group16/63912401/src/com/coding/basic/Stack2.java new file mode 100644 index 0000000000..8c10ccdf06 --- /dev/null +++ b/group16/63912401/src/com/coding/basic/Stack2.java @@ -0,0 +1,51 @@ +package com.coding.basic; + +/** + * 鏍堟暟鎹粨鏋 + * Stack + * @author greenhills + * 2017骞2鏈25鏃 涓嬪崍9:49:41 + */ +public class Stack2 { + private LinkedList elementData = new LinkedList(); + + /** + * 鍏ユ爤 + * @param o + */ + public void push(Object o){ + elementData.addFirst(o); + } + + /** + * 鍑烘爤 + * @return + */ + public Object pop(){ + return elementData.removeFirst(); + } + + /** + * 鑾峰彇鏍堥《鏁版嵁 + * @return + */ + public Object peek(){ + return elementData.get(0); + } + + /** + * 鍒ゆ柇鏄惁涓虹┖ + * @return + */ + public boolean isEmpty(){ + return elementData.size()==0; + } + + /** + * 鑾峰彇鏍堝唴鏁版嵁閲 + * @return + */ + public int size(){ + return elementData.size(); + } +} diff --git a/group16/932886072/djj/ArrayList.java b/group16/932886072/djj/ArrayList.java new file mode 100644 index 0000000000..df3a11c386 --- /dev/null +++ b/group16/932886072/djj/ArrayList.java @@ -0,0 +1,64 @@ +package djj; + + +import java.util.Arrays; + +/** + * Created by jerry on 2017/2/26. + */ +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + if(size>elementData.length*0.8){ + Arrays.copyOf(elementData,elementData.length*2); + } + elementData[size]=o; + size++; + } + public void add(int index, Object o){ + if (size>=index){ + Object[] temp=new Object[elementData.length]; + System.arraycopy(elementData,0,temp,0,index); + temp[index]=o; + System.arraycopy(elementData,index,temp,index+1,size-index); + elementData=temp; + }else if(sizesize){ + throw new RuntimeException("瓒婄晫"); + }else{ + return elementData[index]; + } + } + + public Object remove(int index){ + Object tempObj=null; + if(index<=size){ + Object[] temp=new Object[elementData.length]; + System.arraycopy(elementData,0,temp,0,index); + tempObj=elementData[index]; + System.arraycopy(elementData,index+1,temp,index,size-index-1); + elementData=temp; + } + size--; + return tempObj; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group16/932886072/djj/BinaryTreeNode.java b/group16/932886072/djj/BinaryTreeNode.java new file mode 100644 index 0000000000..d697be6ffc --- /dev/null +++ b/group16/932886072/djj/BinaryTreeNode.java @@ -0,0 +1,34 @@ +package djj; + +public class BinaryTreeNode { + + private BinaryTreeNode root; + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode insert(Object o){ + + return null; + } + + + 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; + } +} diff --git a/group16/932886072/djj/Iterator.java b/group16/932886072/djj/Iterator.java new file mode 100644 index 0000000000..4482e3a408 --- /dev/null +++ b/group16/932886072/djj/Iterator.java @@ -0,0 +1,9 @@ +package djj; + +/** + * Created by jerry on 2017/2/26. + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); +} diff --git a/group16/932886072/djj/LinkedList.java b/group16/932886072/djj/LinkedList.java new file mode 100644 index 0000000000..6e40d5cc69 --- /dev/null +++ b/group16/932886072/djj/LinkedList.java @@ -0,0 +1,96 @@ +package djj; + +public class LinkedList implements List { + //澶磋妭鐐 + private Node head; + //灏捐妭鐐 +// private Node tail; + //褰撳墠娓告爣鑺傜偣 + private Node curNode; + public int size=0; + + + public void add(Object o){ + if(head==null){ + head=new Node(o); + head.next=null; + }else{ + curNode=head; + while(curNode.next!=null){ + curNode=curNode.next; + } + curNode.next=new Node(o); + } + size++; + } + public void add(int index , Object o){ + if(index>size||index<=0){ + throw new RuntimeException("瓒婄晫"); + }else{ + curNode=head; + for(int i=0;isize||index<=0){ + throw new RuntimeException("瓒婄晫"); + } + Node temp=head; + for(int i=0;isize||index<=0){ + throw new RuntimeException("瓒婄晫"); + } + Node temp=head; + for(int i=0;i - - - - - - - + + + + + + + + diff --git a/group17/102228177/work2_26/src/com/coderising/array/ArrayUtil.java b/group17/102228177/work2_26/src/com/coderising/array/ArrayUtil.java index 0434776317..60949d043e 100644 --- a/group17/102228177/work2_26/src/com/coderising/array/ArrayUtil.java +++ b/group17/102228177/work2_26/src/com/coderising/array/ArrayUtil.java @@ -1,8 +1,10 @@ package com.coderising.array; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; +import java.util.List; import java.util.Set; public class ArrayUtil { @@ -29,7 +31,10 @@ public static void main(String[] args) { int[] a1 = {3, 5, 7,8}; int[] a2 = {4, 5, 6,7}; - System.out.println(Arrays.toString(util.merge(a1, a2))); +// System.out.println(Arrays.toString(util.merge(a1, a2))); + System.out.println(Arrays.toString(util.fibonacci(15))); + + System.out.println(Arrays.toString(util.getPrimes(23))); } /** @@ -96,7 +101,9 @@ public int[] merge(int[] array1, int[] array2){ * @return */ public int[] grow(int [] oldArray, int size){ - return null; + int[] newArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, newArray, 0, newArray.length); + return newArray; } /** @@ -107,7 +114,21 @@ public int[] grow(int [] oldArray, int size){ * @return */ public int[] fibonacci(int max){ - return null; + List list = new ArrayList(); + int f1 = 1, f2 = 1, f = 0; + list.add(f1); + list.add(f2); + while(f < max){ + f = f1+f2; + f1 = f2; + f2 = f; + list.add(f); + } + int[] arr = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + arr[i] = list.get(i); + } + return arr; } /** @@ -117,7 +138,24 @@ public int[] fibonacci(int max){ * @return */ public int[] getPrimes(int max){ - return null; + List list = new ArrayList(); + for (int i = 2; i < max; i++) { + boolean flag = true; + for (int j = 2; j < i; j++) { + if ( i % j == 0) { + flag = false; + break; + } + } + if(flag){ + list.add(i); + } + } + int[] arr = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + arr[i] = list.get(i); + } + return arr; } /** @@ -127,7 +165,23 @@ public int[] getPrimes(int max){ * @return */ public int[] getPerfectNumbers(int max){ - return null; + List list = new ArrayList(); + for (int i = 1; i <= max; i++){ + int sum=0; + for (int j = 1; j < i; j++){ + if(i%j==0){ + sum+=j; + } + } + if(i==sum){ + list.add(sum); + } + } + int[] arr = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + arr[i] = list.get(i); + } + return arr; } /** @@ -139,8 +193,10 @@ public int[] getPerfectNumbers(int max){ * @return */ public String join(int[] array, String seperator){ - return null; + String str = ""; + for (int i = 0; i < array.length; i++) { + str += seperator+array[i]; + } + return str.substring(1); } - - } diff --git a/group17/102228177/work2_26/src/com/coderising/litestruts/Struts.java b/group17/102228177/work2_26/src/com/coderising/litestruts/Struts.java index efbc56607a..0aeaf71537 100644 --- a/group17/102228177/work2_26/src/com/coderising/litestruts/Struts.java +++ b/group17/102228177/work2_26/src/com/coderising/litestruts/Struts.java @@ -7,8 +7,6 @@ import org.dom4j.Element; - - public class Struts { public static View runAction(String actionName, Map parameters) { @@ -37,7 +35,7 @@ public static View runAction(String actionName, Map parameters) { Map attribute = Dom4jUtil.getAttribute(element); String className = attribute.get(actionName); try { - Class clazz = Class.forName(className); + Class clazz = Class.forName(className); Object o = clazz.newInstance(); for (Map.Entry entry : parameters.entrySet()) { String key = entry.getKey(); @@ -46,14 +44,14 @@ public static View runAction(String actionName, Map parameters) { Method method = clazz.getDeclaredMethod(BeanUtil.setter(field.getName()),String.class); method.invoke(o, value); } - Method method = clazz.getDeclaredMethod("execute",null); + Method method = clazz.getDeclaredMethod("execute"); String str = (String) method.invoke(o); Field[] fields = clazz.getDeclaredFields(); Map map = new HashMap(); for (Field field : fields) { String fieldName = field.getName(); - Method method2 = clazz.getDeclaredMethod(BeanUtil.getter(fieldName),null); - String ret = (String) method2.invoke(o, null); + Method method2 = clazz.getDeclaredMethod(BeanUtil.getter(fieldName)); + String ret = (String) method2.invoke(o); map.put(fieldName, ret); } View view = new View(); diff --git a/group17/1158154002/src/test02/array/ArrayUtil.java b/group17/1158154002/src/test02/array/ArrayUtil.java new file mode 100644 index 0000000000..1757a08958 --- /dev/null +++ b/group17/1158154002/src/test02/array/ArrayUtil.java @@ -0,0 +1,241 @@ +package test02.array; + +import java.util.ArrayList; +import java.util.Arrays; + +import com.sun.javafx.image.impl.IntArgb; + +public class ArrayUtil { + + public static void main(String[] args) { +// int[] arr={7, 9, 30, 3, 4}; +// reverseArray(arr); + +// int[] oldArr={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; +// removeZero(oldArr); + +// int[] a1={3, 5, 7,8}; +// int[] a2={4, 5, 6,7}; +// merge(a1,a2); + +// int[] a1={3, 5, 7,8}; +// grow(a1, 3); + +// fibonacci(15); + +// getPrimes(23); + +// getPerfectNumbers(4000); + + int[] a={1,2,3,4}; + join(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 static void reverseArray(int[] origin){ + int[] newArr=new int[origin.length]; + int j=0; + for (int i = origin.length-1; i >=0; i--) { + newArr[j++]=origin[i]; + } + System.out.println(Arrays.toString(newArr)); + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 static int[] removeZero(int[] oldArray){ + ArrayList list=new ArrayList<>(); + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i]!=0) { + list.add(oldArray[i]); + } + } + + int[] newArr=new int[list.size()]; + int j=0; + for (int i : list) { + newArr[j++]=i; + } + System.out.println(Arrays.toString(newArr)); + return newArr; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){ + ArrayList list=new ArrayList<>(); + for (Integer arr1 : array1) { + list.add(arr1); + } + + for (Integer arr2 : array2) { + if (!list.contains(arr2)) { + list.add(arr2); + } + } + + int[] newArr=new int[list.size()]; + int i=0; + for (int one : list) { + newArr[i++]=one; + } + + for (int j = 0; j < newArr.length-1; j++) { + for (int k = 0; k < newArr.length-1-j; k++) { + + if (newArr[k]>newArr[k+1]) { + int temp=newArr[k]; + newArr[k]=newArr[k+1]; + newArr[k+1]=temp; + } + } + } + System.out.println(Arrays.toString(newArr)); + return newArr; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + int[] newArr=new int[oldArray.length+size]; + System.arraycopy(oldArray, 0, newArr, 0, oldArray.length); + System.out.println(Arrays.toString(newArr)); + return newArr; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if (max>1) { + ArrayList list=new ArrayList<>(); + list.add(1); + list.add(1); + while (list.get(list.size()-1) list = new ArrayList<>(); + + for (int i = 2; i < max; i++) { + int j = 2; + while (j < i) { +// System.out.println("i:"+i+",j:"+j+",i%j:"+(i%j)); + if (i%j==0) { + break; + } + j++; + } + if (j==i) { + list.add(i); + } + } + + int[] newArr = new int[list.size()]; + int i = 0; + for (int item : list) { + newArr[i++] = item; + } + System.out.println(Arrays.toString(newArr)); + + return newArr; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + ArrayList list = new ArrayList<>(); + for (int i = 1; i < max; i++) { + int j = 1; + int sum=0; + while (j < i) { +// System.out.println("i:"+i+",j:"+j+",i%j:"+(i%j)); + if (i%j==0) { + sum=sum+j; + } + j++; + } + + if (sum==i) { + list.add(i); + } + } + + int[] newArr = new int[list.size()]; + int i = 0; + for (int item : list) { + newArr[i++] = item; + } + System.out.println(Arrays.toString(newArr)); + + return newArr; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + String result=Arrays.toString(array).replace(" ", "").replace("[", "").replace("]", "").replace(",", seperator); + System.out.println(result); + return result; + } +} diff --git a/group17/1158154002/src/test02/litestruts/Action.java b/group17/1158154002/src/test02/litestruts/Action.java new file mode 100644 index 0000000000..d7a88fac57 --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/Action.java @@ -0,0 +1,22 @@ +package test02.litestruts; + +import java.util.HashMap; + +public class Action { + String className; + HashMap resultJspMap; + + public String getClassName() { + return className; + } + public void setClassName(String className) { + this.className = className; + } + public HashMap getResultJspMap() { + return resultJspMap; + } + public void setResultJspMap(HashMap resultJspMap) { + this.resultJspMap = resultJspMap; + } + +} diff --git a/group17/1158154002/src/test02/litestruts/LoginAction.java b/group17/1158154002/src/test02/litestruts/LoginAction.java new file mode 100644 index 0000000000..037c6f6d58 --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package test02.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/group17/1158154002/src/test02/litestruts/Struts.java b/group17/1158154002/src/test02/litestruts/Struts.java new file mode 100644 index 0000000000..cb556a5652 --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/Struts.java @@ -0,0 +1,60 @@ +package test02.litestruts; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import test02.litestruts.sax.SAXParserDemo; +import test02.litestruts.util.StringUtil; + +public class Struts { + + public static View runAction(String actionName, Map parameters){ + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + Action action = SAXParserDemo.run(); + View view=new View(); + try { + Class clazz = Class.forName(action.getClassName()); + Object obj = clazz.newInstance(); + for (String element : parameters.keySet()) { + Method method = clazz.getMethod("set" + StringUtil.captureName(element), String.class); + method.invoke(obj, parameters.get(element)); + } + Method exectue = clazz.getMethod("execute", null); + String result = (String) exectue.invoke(obj, null); + view.setJsp(action.getResultJspMap().get(result)); + + Method getMsg = clazz.getMethod("getMessage", null); + String msg=(String) getMsg.invoke(obj, null); + Map map=new HashMap<>(); + map.put("message", msg); + System.out.println(map); + view.setParameters(map); + + } catch (Exception e) { + e.printStackTrace(); + } + return view; + } + +} \ No newline at end of file diff --git a/group17/1158154002/src/test02/litestruts/StrutsTest.java b/group17/1158154002/src/test02/litestruts/StrutsTest.java new file mode 100644 index 0000000000..ad3c0f2b80 --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/StrutsTest.java @@ -0,0 +1,39 @@ +package test02.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")); + } +} \ No newline at end of file diff --git a/group17/1158154002/src/test02/litestruts/View.java b/group17/1158154002/src/test02/litestruts/View.java new file mode 100644 index 0000000000..73fc11eb13 --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/View.java @@ -0,0 +1,23 @@ +package test02.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/group17/1158154002/src/test02/litestruts/sax/SAXParser.java b/group17/1158154002/src/test02/litestruts/sax/SAXParser.java new file mode 100644 index 0000000000..4efcd0b5cb --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/sax/SAXParser.java @@ -0,0 +1,221 @@ +package test02.litestruts.sax; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; + +import test02.litestruts.Action; + +public class SAXParser { + private static SAXParser parserInstance = new SAXParser(); + private static SAXParserHandler parserHandler; + private SAXParser(){} // Singleton Pattern, a private constructor + private static SAXParserState state = SAXParserState.OUT_OF_TAG; // initial state + + public static SAXParser getInstance() { + return parserInstance; + } + + public Action parse(String path){ + try { + BufferedReader br = new BufferedReader(new FileReader(path)); + int currentCharCode; + // callback start document + parserHandler.startDocument(); + try { + while((currentCharCode = br.read()) != -1){ + char currentChar = (char)currentCharCode; + handleParser(currentChar); + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return parserHandler.endDocument(); + } + + public void setHandler(SAXParserHandler handler){ + parserHandler = handler; + } + + private static void handleParser(char c) { + // This SAX Parser will ignore any line wrap. + if(c == '\n'){ + return; + } + switch (state){ + case OUT_OF_TAG:{ + if(c == '<'){ + if(SAXParsedData.innerText.trim().length() != 0) { + parserHandler.innerText(SAXParsedData.innerText); + } + SAXParsedData.innerText = ""; + SAXParsedData.tagName = ""; + state = SAXParserState.BEGIN_START_OR_END_TAG; + } else if (c == '>') { + state = SAXParserState.SYNTAX_ERROR; + } else { + SAXParsedData.innerText += c; + } + break; + } + case BEGIN_START_OR_END_TAG:{ + if(c == '/') { + SAXParsedData.tagName = ""; + state = SAXParserState.IN_END_TAG; + }else if(c == '?' || c == '!'){ + state = SAXParserState.METADATA; + }else{ + SAXParsedData.tagName += c; + state = SAXParserState.IN_START_TAG; + } + break; + } + case IN_START_TAG:{ + if(c == ' '){ + state = SAXParserState.SPACE_IN_START_TAG; + }else if(c == '>'){ + // callback startElement event; + parserHandler.startElement(SAXParsedData.tagName, SAXParsedData.attributes); + SAXParsedData.clear(); + state = SAXParserState.CLOSE_START_TAG; + }else { + SAXParsedData.tagName += c; + } + break; + } + case SPACE_IN_START_TAG:{ + if(SAXParsedData.tagName.length() > 0){ + if(c != ' '){ + SAXParsedData.attribKey += c; + state = SAXParserState.IN_ATTRIB_KEY; + } + } + break; + } + case IN_ATTRIB_KEY:{ + if(c == '='){ + state = SAXParserState.IN_ATTRIB_EQUAL; + }else{ + SAXParsedData.attribKey += c; + } + break; + } + case IN_ATTRIB_EQUAL:{ + if(c == '"'){ + state = SAXParserState.IN_ATTRIB_VALUE; + } + break; + } + case IN_ATTRIB_VALUE:{ + if(c == '"'){ + SAXParsedData.newAttribute(); + state = SAXParserState.IN_START_TAG; + }else{ + SAXParsedData.attribValue += c; + } + break; + } + case CLOSE_START_TAG:{ + if(c == '<') { + state = SAXParserState.BEGIN_START_OR_END_TAG; + }else{ + SAXParsedData.innerText += c; + state = SAXParserState.OUT_OF_TAG; + } + break; + } + case IN_END_TAG:{ + if(c == '>'){ + // callback endElement event + parserHandler.endElement(SAXParsedData.tagName); + state = SAXParserState.CLOSE_END_TAG; + }else{ + SAXParsedData.tagName += c; + } + break; + } + case CLOSE_END_TAG:{ + if(c == ' '){ + state = SAXParserState.OUT_OF_TAG; + }else if(c == '<'){ + SAXParsedData.tagName = ""; + state = SAXParserState.BEGIN_START_OR_END_TAG; + } + break; + } + case METADATA:{ + if(c == '>'){ + state = SAXParserState.CLOSE_END_TAG; + } + break; + } + case SYNTAX_ERROR:{ + try { + throw new Exception(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + } + + private enum SAXParserState { + // The state when parser meets "<". This is a pending state. + // If the next char is "/", the state will be IN_END_TAG, + // Otherwise, the state will be IN_START_TAG + BEGIN_START_OR_END_TAG, + + // The state when parser is reading between start tag(<...>). + // When parser meets ">", callback "startElement" event + IN_START_TAG, + + // The state when parser is reading between end tag(). + // When parser meets "<", callback "endElement" event + IN_END_TAG, + + // The state when parser meets " ", and is in IN_START_TAG state. + // If the length of tag_name is non-zero, finish parsing tag_name. + // Otherwise, finish parsing a key/value attribute. + SPACE_IN_START_TAG, + IN_ATTRIB_KEY,IN_ATTRIB_EQUAL,IN_ATTRIB_VALUE, + CLOSE_START_TAG,CLOSE_END_TAG, + + // The state when parser is reading any char at the outside of , or between two . This is a pending state. + // Contents between will be recorded, but if the contents consist only spaces, the content will be discarded. + // Otherwise, callback "innerText" event. + OUT_OF_TAG, + + METADATA, SYNTAX_ERROR + } + + private static class SAXParsedData{ + private static String tagName = ""; + private static String attribKey = ""; + private static String attribValue = ""; + private static String innerText = ""; + private static HashMap attributes = new HashMap<>(); + + private static void clear(){ + tagName = ""; + attribKey = ""; + attribValue = ""; + innerText = ""; + attributes.clear(); + } + + private static void newAttribute(){ + attributes.put(attribKey, attribValue); + attribKey = ""; + attribValue = ""; + } + } +} \ No newline at end of file diff --git a/group17/1158154002/src/test02/litestruts/sax/SAXParserDemo.java b/group17/1158154002/src/test02/litestruts/sax/SAXParserDemo.java new file mode 100644 index 0000000000..88c3056a07 --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/sax/SAXParserDemo.java @@ -0,0 +1,76 @@ +package test02.litestruts.sax; + +import java.util.HashMap; + +import test02.litestruts.Action; + +public class SAXParserDemo{ + + public static Action run( ){ + //鍒涘缓瀹炰緥 + SAXParser parser = SAXParser.getInstance(); + //涓鸿В鏋愬櫒璁剧疆濂藉悇涓簨浠剁殑鍥炶皟鍑芥暟 + parser.setHandler(new SAXParserHandler() { + //鍒涘缓濂借嚜瀹氫箟鍙橀噺锛岀敤浠ヨ褰昘ML鏂囨。涓渶瑕佺殑鏁版嵁 + String resultName = ""; + String className = ""; + HashMap resultJspMap = new HashMap<>(); + + boolean foundClass = false; + + //褰撹В鏋愬紑濮嬫椂璋冪敤 + @Override + public void startDocument() { + System.out.println("Start parsing"); + } + + //褰撳畬鎴愪竴涓猉ML寮濮嬫爣绛撅紙渚嬪锛夌殑瑙f瀽鏃惰皟鐢 + @Override + public void startElement(String tagName, HashMap attributes) { + if(tagName.equals("action")){ + if(attributes.get("name").equals("login")){ + className = attributes.get("class"); + foundClass = true; + }else{ + foundClass = false; + } + }else if(tagName.equals("result") && foundClass){ + resultName = attributes.get("name"); + } + } + + //褰撳畬鎴愪竴涓猉ML缁撴潫鏍囩锛堜緥濡锛夌殑瑙f瀽鏃惰皟鐢 + @Override + public void endElement(String tagName) { + + } + + //褰撲竴娈礨ML鏍囩涔嬮棿鐨勫唴瀹硅瑙f瀽瀹屾垚鏃惰皟鐢 + @Override + public void innerText(String innerText) { + if(foundClass){ + String jsp = innerText; + resultJspMap.put(resultName,jsp); + } + } + + @Override + //褰撹В鏋愬櫒璇诲埌XML鏂囨。缁撳熬鏃惰皟鐢 + public Action endDocument() { + + System.out.println(className); + System.out.println(resultJspMap); + System.out.println("End parsing"); + + Action action=new Action(); + action.setClassName(className); + action.setResultJspMap(resultJspMap); + return action; + } + }); + + //璋冪敤姝ゆ柟寮忥紝寮濮嬭В鏋 + return parser.parse("src/test02/litestruts/struts.xml"); + } + +} \ No newline at end of file diff --git a/group17/1158154002/src/test02/litestruts/sax/SAXParserHandler.java b/group17/1158154002/src/test02/litestruts/sax/SAXParserHandler.java new file mode 100644 index 0000000000..d93bdb0133 --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/sax/SAXParserHandler.java @@ -0,0 +1,13 @@ +package test02.litestruts.sax; + +import java.util.HashMap; + +import test02.litestruts.Action; + +public interface SAXParserHandler { + void startDocument(); + void startElement(String tagName, HashMap attributes); + void endElement(String tagName); + Action endDocument(); + void innerText(String innerText); +} \ No newline at end of file diff --git a/group17/1158154002/src/test02/litestruts/struts.xml b/group17/1158154002/src/test02/litestruts/struts.xml new file mode 100644 index 0000000000..238ddf58a2 --- /dev/null +++ b/group17/1158154002/src/test02/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/group17/1158154002/src/test02/litestruts/util/StringUtil.java b/group17/1158154002/src/test02/litestruts/util/StringUtil.java new file mode 100644 index 0000000000..63bc8f2f74 --- /dev/null +++ b/group17/1158154002/src/test02/litestruts/util/StringUtil.java @@ -0,0 +1,13 @@ +package test02.litestruts.util; + +public class StringUtil { + + //棣栧瓧姣嶅ぇ鍐 + public static String captureName(String name) { + // name = name.substring(0, 1).toUpperCase() + name.substring(1); + // return name; + char[] cs=name.toCharArray(); + cs[0]-=32; + return String.valueOf(cs); + } +} diff --git a/group17/1204187480/.gitignore b/group17/1204187480/.gitignore new file mode 100644 index 0000000000..2a5296f902 --- /dev/null +++ b/group17/1204187480/.gitignore @@ -0,0 +1,21 @@ +*.class +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +#ide config +.metadata +.recommenders +.idea/ +*.iml +rebel.* +.rebel.* + +target diff --git a/group17/1204187480/code/homework/coderising/pom.xml b/group17/1204187480/code/homework/coderising/pom.xml new file mode 100644 index 0000000000..b457f36dab --- /dev/null +++ b/group17/1204187480/code/homework/coderising/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + coderising + + com.coding + parent + 1.0-SNAPSHOT + ../parent/pom.xml + + + + 2.1 + 1.2.24 + + + + + commons-digester + commons-digester + ${commons-digester.version} + + + com.alibaba + fastjson + ${fastjson.version} + + + + + + \ No newline at end of file diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/action/LoginAction.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..5496d8084d --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/action/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.action; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @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/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/array/ArrayUtil.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..9aa6404d2a --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/array/ArrayUtil.java @@ -0,0 +1,252 @@ +package com.coderising.array; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ArrayUtil { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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 length = origin.length; + int mid = length >> 1; + for (int i = 0; i < mid; i++) { + int hIndex = length - 1 -i; + int l = origin[i]; + int h = origin[hIndex]; + origin[hIndex] = l; + origin[i] = h; + } + + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 removeValue = 0; + return removeValue(oldArray, removeValue); + } + + private int[] removeValue(int[] oldArray, int removeValue) { + int length = oldArray.length; + int[] dest = new int[length]; + int j = 0; + for(int i = 0; i < length; i++) { + int v = oldArray[i]; + if (v != removeValue) { + dest[j++] = v; + } + } + + int[] retArray = new int[j]; + System.arraycopy(dest, 0, retArray, 0, j); + return retArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * todo 鏁扮粍 a1, b1 鑷韩鍘婚噸 + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2){ + int length1 = array1.length; + int length2 = array2.length; + int length = length1 + length2; + int[] newArray = new int[length]; + + return findAndSetLeastWithOutDuplicate(array1, array2, 0, 0, 0, 0, newArray); + } + + /** + * todo 浼樺寲閫掑綊鍑哄彛鍒ゆ柇, 浼樺寲涓変釜鏉′欢鍒ゆ柇涓轰竴涓 + * @param array1 + * @param array2 + * @param i + * @param j + * @param k + * @param duplicate + * @param newArray + * @return + */ + private int[] findAndSetLeastWithOutDuplicate(int[] array1, int[] array2, int i, int j, int k, int duplicate, int[] newArray) { + + if (i == array1.length && j < array2.length) { + System.arraycopy(array2, j, newArray, k, array2.length - j); + return copyLastValues(newArray, duplicate); + } + if (j == array2.length && i < array1.length) { + System.arraycopy(array1, i, newArray, k, array1.length - i); + return copyLastValues(newArray, duplicate); + } + if (j == array2.length && i == array1.length) { + return copyLastValues(newArray, duplicate); + } + + int v1 = array1[i]; + int v2 = array2[j]; + if (v1 < v2) { + newArray [k] = v1; + return findAndSetLeastWithOutDuplicate(array1, array2, ++i, j, ++k, duplicate, newArray); + } else if (v1 > v2){ + newArray [k] = v2; + return findAndSetLeastWithOutDuplicate(array1, array2, i, ++j, ++k, duplicate, newArray); + } else { + newArray [k] = v2; + return findAndSetLeastWithOutDuplicate(array1, array2, ++i, ++j, ++k, ++duplicate, newArray); + } + + } + + private int[] copyLastValues(int[] newArray, int duplicate) { + int[] retArray = new int[newArray.length - duplicate]; + System.arraycopy(newArray, 0, retArray, 0, retArray.length); + return retArray; + } + + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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[]{}; + } + int [] newArray = new int[max]; + newArray[0] = 1; + return fibonacciN(1, 1, 1, max, newArray); + } + + private int[] fibonacciN(int size, int current, int next, int max, int[] newArray) { + if (next >= max) { + int[] retArray = new int[size]; + System.arraycopy(newArray, 0, retArray, 0, size); + return retArray; + } else { + newArray[++size - 1] = next; + return fibonacciN(size, next, current + next, max, newArray); + } + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * todo 浣跨敤宸叉湁鐨勮川鏁板簭鍒 浼樺寲璐ㄦ暟楠岃瘉 + * @param max + * @return + */ + public int[] getPrimes(int max){ + if (max <= 2) { + return new int[]{}; + } + + int[] newArray = new int[max]; + int j = 0; + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + newArray[j++] = i; + } + } + int[] retArray = new int[j]; + System.arraycopy(newArray, 0, retArray, 0, j); + return retArray; + } + + private boolean isPrime(int number) { + int limit = Double.valueOf(Math.sqrt(number)).intValue(); + for(int i = 2; i <= limit; i++) { + if (number % i == 0 ) { + return false; + } + } + return true; + + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] newArray = new int[48]; // 缁忚繃涓嶅皯鏁板瀹剁爺绌讹紝鍒2013骞2鏈6鏃ヤ负姝紝涓鍏辨壘鍒颁簡48涓畬鍏ㄦ暟銆 + int j = 0; + for (int i = 2; i < max; i++) { + if (isPerfectNumber(i)) { + if (j >= newArray.length) { + newArray = this.grow(newArray, 1); + } + newArray[j++] = i; + + } + } + int[] retArray = new int[j]; + System.arraycopy(newArray, 0, retArray, 0, j); + return retArray; + } + + private boolean isPerfectNumber(int number) { + int sum = 0; + for (int i = 1; i < number; i++) { + if (number % i == 0) { + sum += i; + } + } + + return sum == number; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator){ + StringBuilder builder = new StringBuilder(20); + int length = array.length; + for (int i = 0; i< length; i++) { + builder.append(array[i]).append(seperator); + } + builder.deleteCharAt(builder.length() - seperator.length()); + return builder.toString(); + } + + +} diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/Struts.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..4e1cf74391 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,122 @@ +package com.coderising.litestruts; + +import com.coderising.litestruts.parser.ActionConfig; +import com.coderising.litestruts.parser.DefaultStrutsParser; +import com.coderising.litestruts.parser.StrutsConfig; +import com.coderising.litestruts.parser.StrutsParser; +import com.coderising.litestruts.util.BeanUtils; + +import java.util.HashMap; +import java.util.Map; + + +public class Struts { + + private static StrutsParser strutsParser = new DefaultStrutsParser(); + + private static final String STRUTS_CONFIG_PATH = "struts.xml"; + private static final BeanUtils beanUtils = new BeanUtils(); + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + /** + * 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + */ + StrutsConfig strutsConfig = strutsParser.parser(STRUTS_CONFIG_PATH); + ActionConfig actionConfig = strutsConfig.getActions().get(actionName); + /** + * 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + */ + Object action = setPropertiesForAction(actionConfig, actionName, parameters); + + /** + * 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + */ + String resultName = doExecute(action); + /** + * 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + */ + View view = createViewAndSetParameters(action); + /** + * 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + setViewValue(view, resultName, actionConfig); + return view; + } + + private static void setViewValue(View view, String resultName, ActionConfig config) { + view.setJsp(config.getResults().get(resultName).getView()); + } + + private static View createViewAndSetParameters(Object action) { + View view = new View(); + view.setParameters(beanUtils.describe(action)); + return view; + } + + private static String doExecute(Object action) { + return (String) beanUtils.invokeWithNoParamter("execute", action); + } + + private static Object setPropertiesForAction(ActionConfig actionConfig, String actionName, Map parameters) { + Object action = createInstance(findActionClass(actionConfig.getClassName())); + for (Map.Entry entry : parameters.entrySet()) { + setProperty(entry.getKey(), entry.getValue(), action); + } + return action; + } + + /** + * todo 鏍¢獙 key 鏄惁瀛樺湪 + * + * @param key + * @param value + * @param action + */ + private static void setProperty(String key, String value, Object action) { + beanUtils.setPara(value, key, action); + } + + private static Object createInstance(Class classValue) { + try { + return classValue.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { + throw new IllegalStateException(e); + } + } + + private static Class findActionClass(String className) { + try { + return Class.forName(className); + } catch (ClassNotFoundException e) { + throw new IllegalStateException(e); + } + } + +} diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/StrutsTest.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/View.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/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/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/ActionConfig.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/ActionConfig.java new file mode 100644 index 0000000000..4aaba284fb --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/ActionConfig.java @@ -0,0 +1,45 @@ +package com.coderising.litestruts.parser; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class ActionConfig { + private String name; + private String className; + private Map results = new HashMap<>(10); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getClassName() { + return className; + } + + public void setClassName(String className) { + this.className = className; + } + + public Map getResults() { + return results; + } + + public void setResults(Map results) { + this.results = results; + } + + public void addResult(Result result) { + this.results.put(result.getName(), result); + } + + +} diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/DefaultStrutsParser.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/DefaultStrutsParser.java new file mode 100644 index 0000000000..ea58507738 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/DefaultStrutsParser.java @@ -0,0 +1,52 @@ +package com.coderising.litestruts.parser; + +import com.alibaba.fastjson.JSON; +import org.apache.commons.digester.Digester; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xml.sax.SAXException; + +import java.io.File; +import java.io.IOException; + +/** + * 瑙f瀽 struts.xml 鏂囦欢 + * @apiNote 鍊熼壌 http://www.everycoding.com/coding/78.html; http://blog.csdn.net/caihaijiang/article/details/5944955 + * Created by luoziyihao on 3/5/17. + */ +public class DefaultStrutsParser implements StrutsParser { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Override + public StrutsConfig parser(String filePathInClasspath) { + String path = this.getClass().getClassLoader().getResource(filePathInClasspath).getPath(); + File input = new File(path); + Digester digester = new Digester(); + // 鍒涘缓 StrutsConfig 瀵硅薄 + digester.addObjectCreate("struts", StrutsConfig.class); + // 灏 struts 鑺傜偣涓婄殑attribute灞炴ф槧灏勫埌 StrutsConfig 瀵硅薄鐨勫睘鎬т笂 + digester.addSetProperties("struts"); + digester.addObjectCreate("struts/action", ActionConfig.class); + // 灏 struts/action 鑺傜偣涓婄殑attribute灞炴ф槧灏勫埌 Action 瀵硅薄鐨勫睘鎬т笂, 骞惰嚜瀹氫箟灞炴ф槧灏 + digester.addSetProperties("struts/action" + , new String[]{"name", "class"}, new String[]{"name", "className"}); + digester.addObjectCreate("struts/action/result", Result.class); + digester.addSetProperties("struts/action/result" + , new String[]{"name"}, new String[]{"name"}); + // 灏 struts/action/result 鑺傜偣涓婄殑body灞炴ф槧灏勫埌 Result 瀵硅薄鐨勫睘鎬т笂 + digester.addCallMethod("struts/action/result", "setView", 0); + // 瀵瑰簲struts/action/result 鐢熸垚鐨勫璞℃坊鍔犲埌 Action涓 + digester.addSetNext("struts/action/result", "addResult"); + // 瀵瑰簲struts/action 鐢熸垚鐨勫璞℃坊鍔犲埌 Struts涓 + digester.addSetNext("struts/action", "addAction"); + + try { + StrutsConfig strutsConfig = (StrutsConfig) digester.parse(input); + logger.debug("strutsConfig={}", JSON.toJSONString(strutsConfig)); + return strutsConfig; + } catch (IOException | SAXException e) { + throw new IllegalStateException(e); + } + + } +} diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/Result.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/Result.java new file mode 100644 index 0000000000..c402418e6b --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/Result.java @@ -0,0 +1,22 @@ +package com.coderising.litestruts.parser; + +public class Result { + private String name; + private String view; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getView() { + return view; + } + + public void setView(String view) { + this.view = view; + } +} \ No newline at end of file diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsConfig.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsConfig.java new file mode 100644 index 0000000000..88f769157e --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsConfig.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts.parser; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class StrutsConfig { + public Map actions = new HashMap<>(10); + + public Map getActions() { + return actions; + } + + public void setActions(Map actions) { + this.actions = actions; + } + + public void addAction(ActionConfig action) { + this.actions.put(action.getName(), action); + } +} diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsParser.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsParser.java new file mode 100644 index 0000000000..ab7358c777 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/parser/StrutsParser.java @@ -0,0 +1,8 @@ +package com.coderising.litestruts.parser; + +/** + * Created by luoziyihao on 3/5/17. + */ +public interface StrutsParser { + StrutsConfig parser(String filePathInClasspath); +} diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/util/BeanUtils.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/util/BeanUtils.java new file mode 100755 index 0000000000..775b005466 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/util/BeanUtils.java @@ -0,0 +1,144 @@ +package com.coderising.litestruts.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.math.BigDecimal; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by luoziyihao on 5/25/16. + */ +public class BeanUtils { + + + public static final String SET = "set"; + public static final String GET = "get"; + // 鏃ュ織杈撳嚭绫 + private final Logger log = LoggerFactory.getLogger(this.getClass()); + private final StringUtils stringUtils = new StringUtils(); + + public Object setInvoke(Object para, String methodName, Object obj) { + Method method = null; + Object returnObj = null; + try { + + method = obj.getClass().getMethod(methodName, para.getClass()); + returnObj = method.invoke(obj, para); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + return returnObj; + } + + public Object getInvoke(String methodName, Object obj) { + Method method = null; + Object returnObj = null; + try { + method = obj.getClass().getMethod(methodName); + returnObj = method.invoke(obj); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + return returnObj; + } + + public Object invokeWithNoParamter(String methodName, Object obj) { + Method method; + Object returnObj = null; + try { + method = obj.getClass().getMethod(methodName); + returnObj = method.invoke(obj); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + return returnObj; + } + + + + public Object getPara(String paraName, Object object) { + if (stringUtils.isSpaceOrNull(paraName)) { + throw new RuntimeException("paraname is null or space"); + } + String methodName = new StringBuilder().append(GET).append(stringUtils.toUpperCase(paraName, 0)).toString(); + return getInvoke(methodName, object); + } + + + + public Object setPara(Object para, String paraName, Object object) { + if (stringUtils.isSpaceOrNull(paraName)) { + throw new RuntimeException("paraname is null or space"); + } + String methodName = new StringBuilder().append(SET).append(stringUtils.toUpperCase(paraName, 0)).toString(); + return setInvoke(para, methodName, object); + } + + public T get(String paraName, Object object, Class clazz) { + return (T) getPara(paraName, object); + } + + public Integer getInt(String paraName, Object object) { + return (Integer) getPara(paraName, object); + } + + public Long getLong(String paraName, Object object) { + return (Long) getPara(paraName, object); + } + + public Double getDouble(String paraName, Object object) { + return (Double) getPara(paraName, object); + } + + public BigDecimal getBigDecimal(String paraName, Object object) { + return (BigDecimal) getPara(paraName, object); + } + + public String getString(String paraName, Object object) { + return getPara(paraName, object).toString(); + } + + public Date getDate(String paraName, Object object) { + return (Date) getPara(paraName, object); + } + + public Long getLongByString(String paraName, Object object) { + return Long.parseLong(getString(paraName, object)); + } + + public Integer getIntByString(String paraName, Object object) { + return Integer.parseInt(getString(paraName, object)); + } + + public Double getDoubleByString(String paraName, Object object) { + return Double.parseDouble(getString(paraName, object)); + } + + public BigDecimal getBigDecimalByString(String paraName, Object object) { + return new BigDecimal(getString(paraName, object)); + } + + private final static String GETTER_PRE = "get"; + public Map describe(Object model) { + Method[] methods = model.getClass().getDeclaredMethods(); //鑾峰彇瀹炰綋绫荤殑鎵鏈夊睘鎬э紝杩斿洖Field鏁扮粍 + Map properties = new HashMap<>(); + for (Method method : methods) { + String methodName = method.getName(); + if (methodName.startsWith(GETTER_PRE)) { + try { + Object o = method.invoke(model); + String valueName = stringUtils.toLowwerCase(methodName.substring(GETTER_PRE.length()), 0); + properties.put(valueName, o); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new IllegalStateException(e); + } + } + } + return properties; + } +} diff --git a/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/util/StringUtils.java b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/util/StringUtils.java new file mode 100644 index 0000000000..7fe2ed5ac4 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/java/com/coderising/litestruts/util/StringUtils.java @@ -0,0 +1,34 @@ +package com.coderising.litestruts.util; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class StringUtils { + + /** + * 鏀瑰彉鎸囧畾浣嶇疆鐨 char鐨勫ぇ灏忓啓 + */ + public String toUpperCase(String str, int index) { + char[] chars = str.toCharArray(); + if (index + 1 > chars.length) { + throw new RuntimeException("the char at the index don't exist"); + } + chars[index] = Character.toUpperCase(chars[index]); + return new String(chars); + } + + /** + * 鏀瑰彉鎸囧畾浣嶇疆鐨 char鐨勫ぇ灏忓啓 + */ + public String toLowwerCase(String str, int index) { + char[] chars = str.toCharArray(); + if (index + 1 > chars.length ) {throw new RuntimeException("the char at the index don't exist");} + chars[index] = Character.toLowerCase(chars[index]); + return new String(chars); + } + + public boolean isSpaceOrNull(String paraName) { + return (paraName == null || paraName.trim().isEmpty()); + } + +} diff --git a/group17/1204187480/code/homework/coderising/src/main/resources/struts.xml b/group17/1204187480/code/homework/coderising/src/main/resources/struts.xml new file mode 100644 index 0000000000..dd598a3664 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/main/resources/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/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/api/ComputeTest.java b/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/api/ComputeTest.java new file mode 100644 index 0000000000..0ea94e4183 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/api/ComputeTest.java @@ -0,0 +1,19 @@ +package com.coderising.api; + +import org.junit.Test; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class ComputeTest { + + @Test + public void testDivisionExactly(){ + System.out.println( 7 >> 1); + } + + @Test + public void testSqrt() { + System.out.println(Math.sqrt(10)); + } +} diff --git a/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/api/CycleTest.java b/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/api/CycleTest.java new file mode 100644 index 0000000000..6abb5d925d --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/api/CycleTest.java @@ -0,0 +1,25 @@ +package com.coderising.api; + +import org.junit.Test; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class CycleTest { + + /** + * checkIndex will be excuted in each cycle + */ + @Test + public void testForSize() { + int[] arr = new int[]{1, 2, 3, 4, 54}; + for (int i = 0; checkIndex(i, arr); i++ ) { + + } + } + + private boolean checkIndex(int i, int[] arr) { + System.out.println(i); + return i < arr.length; + } +} diff --git a/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/array/ArrayUtilTest.java b/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..be29b01d42 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,76 @@ +package com.coderising.array; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.*; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class ArrayUtilTest { + + private ArrayUtil creatArrayUtil(){ + return new ArrayUtil(); + } + + @Test + public void reverseArray() throws Exception { + int[] origin = new int[]{1, 2, 3}; + int[] destArray = new int[]{3, 2, 1}; + creatArrayUtil().reverseArray(origin); + Assert.assertArrayEquals(destArray, origin); + } + + @Test + public void removeZero() throws Exception { + int[] origin = new int[]{1, 2, 3, 0, 10}; + int[] destArray = new int[]{1, 2, 3, 10}; + int[] retArray = creatArrayUtil().removeZero(origin); + Assert.assertArrayEquals(destArray, retArray); + } + + @Test + public void merge() throws Exception { + int[] a = new int[]{1, 2, 3}; + int[] b = new int[]{2, 3}; + + int[] newArray = creatArrayUtil().merge(a, b); + info(newArray); + assertArrayEquals(new int[]{1, 2, 3}, newArray); + } + + @Test + public void grow() throws Exception { + assertArrayEquals(new int[]{1, 2, 0, 0}, creatArrayUtil().grow(new int[]{1, 2}, 2)); + } + + @Test + public void fibonacci() throws Exception { + assertArrayEquals(new int[]{1, 1, 2, 3, 5, 8}, creatArrayUtil().fibonacci(10)); + } + + @Test + public void getPrimes() throws Exception { + int max = Double.valueOf(Math.pow(2, 4)).intValue(); + assertArrayEquals(new int[]{2, 3, 5, 7, 11, 13}, creatArrayUtil().getPrimes(max)); + } + + @Test + public void getPerfectNumbers() throws Exception { + int max = Double.valueOf(Math.pow(2, 16)).intValue(); + assertArrayEquals(new int[]{6, 28, 496, 8128}, creatArrayUtil().getPerfectNumbers(max)); + + } + + @Test + public void join() throws Exception { + assertEquals("1_2_3_10", creatArrayUtil().join(new int[]{1, 2, 3, 10}, "_")); + } + + private void info(int[] array) { + System.out.println(Arrays.toString(array)); + } +} \ No newline at end of file diff --git a/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/litestruts/parser/StructsParserTest.java b/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/litestruts/parser/StructsParserTest.java new file mode 100644 index 0000000000..72e841a230 --- /dev/null +++ b/group17/1204187480/code/homework/coderising/src/test/java/com/coderising/litestruts/parser/StructsParserTest.java @@ -0,0 +1,14 @@ +package com.coderising.litestruts.parser; + +import org.junit.Test; + +/** + * Created by luoziyihao on 3/5/17. + */ +public class StructsParserTest { + @Test + public void parser() throws Exception { + new DefaultStrutsParser().parser("struts.xml"); + } + +} \ No newline at end of file diff --git a/group17/1204187480/code/homework/pom.xml b/group17/1204187480/code/homework/pom.xml index df0626e528..c8b94c2bc2 100644 --- a/group17/1204187480/code/homework/pom.xml +++ b/group17/1204187480/code/homework/pom.xml @@ -9,6 +9,7 @@ parent basic + coderising diff --git a/group17/1264835468/src/assignment/List.java b/group17/1264835468/src/assignment/List.java index ba31dc333b..5ab03b0fdf 100644 --- a/group17/1264835468/src/assignment/List.java +++ b/group17/1264835468/src/assignment/List.java @@ -1,14 +1,14 @@ -package assignment; - -// -public interface List { - public void add(E o); - - public void add(int index, E o); - - public E get(int index); - - public E remove(int index); - - public int size(); -} +package assignment; + +// +public interface List { + public void add(E o); + + public void add(int index, E o); + + public E get(int index); + + public E remove(int index); + + public int size(); +} diff --git a/group17/1264835468/src/assignment2_26/ArrayUtil.java b/group17/1264835468/src/assignment2_26/ArrayUtil.java index 826b76a677..89d2a0db29 100644 --- a/group17/1264835468/src/assignment2_26/ArrayUtil.java +++ b/group17/1264835468/src/assignment2_26/ArrayUtil.java @@ -14,7 +14,7 @@ public class ArrayUtil { * @param origin * @return */ - public void reverseArray(int[] origin) { + public static void reverseArray(int[] origin) { int mid = origin.length / 2; for (int i = 0; i < mid; i++) { int temp = origin[i]; @@ -32,7 +32,7 @@ public void reverseArray(int[] origin) { * @return */ - public int[] removeZero(int[] oldArray) { + public static int[] removeZero(int[] oldArray) { int count = 0; for (int i : oldArray) { if (i != 0) @@ -56,7 +56,7 @@ public int[] removeZero(int[] oldArray) { * @return */ - public int[] merge(int[] array1, int[] array2) { + public static int[] merge(int[] array1, int[] array2) { TreeSet set = new TreeSet<>(); for (Integer integer : array1) { set.add(integer); @@ -80,7 +80,7 @@ public int[] merge(int[] array1, int[] array2) { * @param size * @return */ - public int[] grow(int[] oldArray, int size) { + public static int[] grow(int[] oldArray, int size) { return Arrays.copyOf(oldArray, size); } @@ -91,7 +91,7 @@ public int[] grow(int[] oldArray, int size) { * @param max * @return */ - public int[] fibonacci(int max) { + public static int[] fibonacci(int max) { if (max <= 1) return new int[0]; List fList = new ArrayList<>(); @@ -115,7 +115,7 @@ public int[] fibonacci(int max) { * @param max * @return */ - public int[] getPrimes(int max) { + public static int[] getPrimes(int max) { boolean[] isPrime = new boolean[max]; List primes = new ArrayList<>(); for (int i = 0; i < isPrime.length; i++) { @@ -142,7 +142,7 @@ public int[] getPrimes(int max) { * @param max * @return */ - public int[] getPerfectNumbers(int max) { + public static int[] getPerfectNumbers(int max) { int sum = 0; ArrayList perfectNumbers = new ArrayList<>(); for (int i = 1; i < max; i++) { @@ -170,7 +170,7 @@ public int[] getPerfectNumbers(int max) { * @param s * @return */ - public String join(int[] array, String seperator) { + public static String join(int[] array, String seperator) { StringBuilder stringBuilder = new StringBuilder(); for (int i : array) { stringBuilder.append(i + seperator); diff --git a/group17/1264835468/src/assignment2_26/ArrayUtilTest.java b/group17/1264835468/src/assignment2_26/ArrayUtilTest.java index c5ff251592..753c026796 100644 --- a/group17/1264835468/src/assignment2_26/ArrayUtilTest.java +++ b/group17/1264835468/src/assignment2_26/ArrayUtilTest.java @@ -6,95 +6,95 @@ import org.junit.Test; public class ArrayUtilTest { - ArrayUtil arrayUtil = new ArrayUtil(); @Test public void testReverseArray() { int[] array = new int[] {}; - arrayUtil.reverseArray(array); + ArrayUtil.reverseArray(array); assertArrayEquals(new int[] {}, array); array = new int[] { 1 }; - arrayUtil.reverseArray(array); + ArrayUtil.reverseArray(array); assertArrayEquals(new int[] { 1 }, array); array = new int[] { 1, 2, 3 }; - arrayUtil.reverseArray(array); + ArrayUtil.reverseArray(array); assertArrayEquals(new int[] { 3, 2, 1 }, array); } @Test public void testRemoveZero() { int[] array = new int[] {}; - assertArrayEquals(new int[] {}, arrayUtil.removeZero(array)); + assertArrayEquals(new int[] {}, ArrayUtil.removeZero(array)); array = new int[] { 0 }; - assertArrayEquals(new int[] {}, arrayUtil.removeZero(array)); + assertArrayEquals(new int[] {}, ArrayUtil.removeZero(array)); array = new int[] { 1 }; - assertArrayEquals(new int[] { 1 }, arrayUtil.removeZero(array)); + assertArrayEquals(new int[] { 1 }, ArrayUtil.removeZero(array)); array = new int[] { 1, 2, 0, 0, 3 }; - assertArrayEquals(new int[] { 1, 2, 3 }, arrayUtil.removeZero(array)); + assertArrayEquals(new int[] { 1, 2, 3 }, ArrayUtil.removeZero(array)); array = new int[] { 1, 2, 3 }; - assertArrayEquals(new int[] { 1, 2, 3 }, arrayUtil.removeZero(array)); + assertArrayEquals(new int[] { 1, 2, 3 }, ArrayUtil.removeZero(array)); } @Test public void testMerge() { int[] array1 = { 3, 5, 7, 8 }; int[] array2 = { 4, 5, 6, 7 }; - assertArrayEquals(new int[] { 3, 4, 5, 6, 7, 8 }, arrayUtil.merge(array1, array2)); + assertArrayEquals(new int[] { 3, 4, 5, 6, 7, 8 }, ArrayUtil.merge(array1, array2)); } @Test public void testGrow() { int[] array = { 3, 5, 7 }; - assertArrayEquals(new int[] { 3, 5, 7, 0, 0 }, arrayUtil.grow(array, 5)); - assertArrayEquals(new int[] { 3, 5, 7 }, arrayUtil.grow(array, 3)); + assertArrayEquals(new int[] { 3, 5, 7, 0, 0 }, ArrayUtil.grow(array, 5)); + assertArrayEquals(new int[] { 3, 5, 7 }, ArrayUtil.grow(array, 3)); } @Test public void testFibonacci() { - assertArrayEquals(new int[] {}, arrayUtil.fibonacci(1)); + assertArrayEquals(new int[] {}, ArrayUtil.fibonacci(1)); - assertArrayEquals(new int[] { 1, 1 }, arrayUtil.fibonacci(2)); + assertArrayEquals(new int[] { 1, 1 }, ArrayUtil.fibonacci(2)); - assertArrayEquals(new int[] { 1, 1, 2, 3, 5, 8, 13 }, arrayUtil.fibonacci(15)); + assertArrayEquals(new int[] { 1, 1, 2, 3, 5, 8, 13 }, ArrayUtil.fibonacci(15)); } @Test public void testGetPrimes() { - assertArrayEquals(new int[] {}, arrayUtil.getPrimes(1)); + assertArrayEquals(new int[] {}, ArrayUtil.getPrimes(1)); - assertArrayEquals(new int[] {}, arrayUtil.getPrimes(2)); + assertArrayEquals(new int[] {}, ArrayUtil.getPrimes(2)); - assertArrayEquals(new int[] { 2 }, arrayUtil.getPrimes(3)); + assertArrayEquals(new int[] { 2 }, ArrayUtil.getPrimes(3)); - assertArrayEquals(new int[] { 2, 3, 5, 7, 11, 13, 17, 19 }, arrayUtil.getPrimes(20)); + assertArrayEquals(new int[] { 2, 3, 5, 7, 11, 13, 17, 19 }, ArrayUtil.getPrimes(20)); } @Test public void testGetPerfectNumbers() { - assertArrayEquals(new int[] { 6 }, arrayUtil.getPerfectNumbers(10)); + assertArrayEquals(new int[] { 6 }, ArrayUtil.getPerfectNumbers(10)); - assertArrayEquals(new int[] { 6, 28 }, arrayUtil.getPerfectNumbers(100)); + assertArrayEquals(new int[] { 6, 28 }, ArrayUtil.getPerfectNumbers(100)); - assertArrayEquals(new int[] { 6, 28, 496 }, arrayUtil.getPerfectNumbers(1000)); + assertArrayEquals(new int[] { 6, 28, 496 }, ArrayUtil.getPerfectNumbers(1000)); + + assertArrayEquals(new int[] { 6, 28, 496, 8128 }, ArrayUtil.getPerfectNumbers(10000)); - assertArrayEquals(new int[] { 6, 28, 496, 8128 }, arrayUtil.getPerfectNumbers(10000)); } @Test public void testJoin() { - assertEquals("3-4-5", arrayUtil.join(new int[] { 3, 4, 5 }, "-")); + assertEquals("3-4-5", ArrayUtil.join(new int[] { 3, 4, 5 }, "-")); - assertEquals("345", arrayUtil.join(new int[] { 3, 4, 5 }, "")); + assertEquals("345", ArrayUtil.join(new int[] { 3, 4, 5 }, "")); - assertEquals("3", arrayUtil.join(new int[] { 3 }, "")); + assertEquals("3", ArrayUtil.join(new int[] { 3 }, "")); - assertEquals("3--4--5", arrayUtil.join(new int[] { 3, 4, 5 }, "--")); + assertEquals("3--4--5", ArrayUtil.join(new int[] { 3, 4, 5 }, "--")); } } diff --git a/group17/1264835468/src/assignment2_26/Struts.java b/group17/1264835468/src/assignment2_26/Struts.java index a6b56ce26b..ad704e0433 100644 --- a/group17/1264835468/src/assignment2_26/Struts.java +++ b/group17/1264835468/src/assignment2_26/Struts.java @@ -33,7 +33,7 @@ public static View runAction(String actionName, Map parameters) */ // 0 - XmlPhraser phraser = new XmlPhraser(configFile); + XmlParser phraser = new XmlParser(configFile); String className = phraser.getClassNameByActionName(actionName); View view = new View(); try { diff --git a/group17/1264835468/src/assignment2_26/XmlPhraser.java b/group17/1264835468/src/assignment2_26/XmlParser.java similarity index 94% rename from group17/1264835468/src/assignment2_26/XmlPhraser.java rename to group17/1264835468/src/assignment2_26/XmlParser.java index 198a9b573e..aa34335cb6 100644 --- a/group17/1264835468/src/assignment2_26/XmlPhraser.java +++ b/group17/1264835468/src/assignment2_26/XmlParser.java @@ -15,11 +15,11 @@ import org.w3c.dom.NodeList; import org.xml.sax.SAXException; -public class XmlPhraser { +public class XmlParser { private File file; List actionElements; - public XmlPhraser(File file) { + public XmlParser(File file) { this.file = file; actionElements = new ArrayList<>(); getActionsFromFile(); diff --git a/group17/1282579502/.settings/org.eclipse.jdt.core.prefs b/group17/1282579502/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..a698e59674 --- /dev/null +++ b/group17/1282579502/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +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/group17/1282579502/src/com/coderising/array/ArrayUtil.java b/group17/1282579502/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..af9bde1136 --- /dev/null +++ b/group17/1282579502/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,229 @@ +package com.coderising.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + if(origin == null) return; + int tmp; + for(int i = 0; i< origin.length; i++){ + int end = origin.length - 1 -i; + if(end>i){ + tmp = origin[i]; + origin[i] = origin[end]; + origin[end] = tmp; + } + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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){ + LinkedList newArray = new LinkedList<>(); + + for(int i = 0; i< oldArray.length; i++){ + if(oldArray[i] != 0){ + newArray.add(oldArray[i]); + } + } + int[] ret = new int[newArray.size()]; + for(int i = 0; i= array2[j]){ + merged[index] = array2[j]; + j++;} +// }else{ +// merged[index] = array1[i]; +// i++; +// j++; +// } + + index++; + } + //o(n) + while(i store = new LinkedList<>(); + for(int candidate = 2; candidate< max; candidate++ ){ + int sum = 0; + for(int i = 1; i parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + String path = "src/com/coderising/litestruts/struts.xml"; + File rootFile = new File(""); + File rootDir = new File(rootFile.getAbsolutePath()); + File configFile = findFile(rootDir, "struts.xml"); + if(configFile != null){ + //System.out.println("find: " + configFile.getAbsolutePath()); + path = configFile.getAbsolutePath(); + } + View retView = new View(); + XmlParser parser = new XmlParser(path); + //parser.dump(); + List branchNodesList = parser.getActionNodeList(); + try { + for(eNode node : branchNodesList){ + //System.out.println(node); + String classPath = node.attributeMap.get("class"); + Class actionObjectClass = getClassObj(classPath); + if(actionObjectClass != null){ + Constructor actionConstructorObject = createClass(actionObjectClass); + Object newInstance = null; + newInstance = actionConstructorObject.newInstance(); + + Method[] methods = actionObjectClass.getDeclaredMethods(); +// for(Method m : methods){ +// System.out.println("method: " + m.getName()); +// } + Iterator> itr = parameters.entrySet().iterator(); + while(itr.hasNext()){ + Entry call = itr.next(); + //System.out.println(call.getKey() + " " + call.getValue()); + String key = call.getKey(); + String val = call.getValue(); + String setMethodName = "set" + Character.toUpperCase(key.charAt(0)) + key.substring(1); + Method setMethod = getMethod(actionObjectClass, setMethodName, String.class); + if(newInstance != null){ + setMethod.invoke(newInstance, val); + } + } + Method exeMethod = actionObjectClass.getDeclaredMethod("execute", null); + Class returnType = exeMethod.getReturnType(); + Object returnValue = exeMethod.invoke(newInstance, null); + HashMap viewHashParam = new HashMap<>(); + for(Method m:methods){ + if(m.getName().startsWith("get")){ + Object v = m.invoke(newInstance, null); + String getMethodName = m.getName().substring(3); + getMethodName = Character.toLowerCase(getMethodName.charAt(0)) + getMethodName.substring(1); + //System.out.println("get method: " + getMethodName); + viewHashParam.put(getMethodName, v); + } + } + String viewStr = null; + for(eNode resultNode : node.subNodes){ + String nameAttr = resultNode.attributeMap.get("name") ; + if(nameAttr.equals(returnValue)){ + //System.out.println("node: " + resultNode.element_name + " " + resultNode.attributeMap.get("name") + " matches"); + for(eNode jspNode : resultNode.subNodes){ + if(jspNode.isLeaf){ + viewStr = jspNode.element_raw_content; + //System.out.println("jsp str: " + viewStr); + break; + } + } + } + } + + retView.setJsp(viewStr); + retView.setParameters(viewHashParam); + + } + } + } catch (IllegalAccessException | IllegalArgumentException + | InvocationTargetException | InstantiationException | NoSuchMethodException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return retView; + } + + public static Class getClassObj(String classPath){ + Class classObject = null; + try { + classObject = Class.forName(classPath); + } catch (ClassNotFoundException e) { + //System.err.println(e.getMessage()); + } + return classObject; + } + + public static Constructor createClass(Class classObject){ + Constructor constructor = null; + try { + constructor = classObject.getConstructor(); + } catch (NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } + return constructor; + } + + public static Method getMethod(Class clazz, String methodName, Class paramClass){ + Method ret = null; + try { + ret = clazz.getMethod(methodName, paramClass); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } + return ret; + } + + public static void main(String[] args){ + + + //System.out.println("absolute path: " + absPath); + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + String actionName = "login"; + + View view = Struts.runAction(actionName,params); + } + + public static File findFile(File targetDir, String targetFileName){ + //System.out.println("targetDir: " + targetDir.getAbsolutePath() + " " + targetDir.isDirectory()); + File configFile = null; + File[] files = targetDir.listFiles(); + + if(files != null){ + for(File f: files){ + //System.out.println("test: " + f.getName()); + if(f.isDirectory()){ + //System.out.println("Dir: " + f.getName()); + configFile = findFile(f, targetFileName); + if(configFile != null){ + break; + } + } + else{ + //System.out.println("File: " + f.getName()); + if(f.getAbsolutePath().endsWith(targetFileName)){ + configFile = f; + break; + } + } + } + } + return configFile; + } + + public static FilenameFilter createFilter(String targetName){ + return new FilenameFilter(){ + + @Override + public boolean accept(File dir, String name) { + // TODO Auto-generated method stub + System.out.println("testing: " + dir + " name: " + name); + return name.endsWith(targetName); + } + + }; + } + + public static List getDirectories(File targetFile){ + List childDirs = new LinkedList<>(); + if(targetFile.isDirectory()){ + File[] files = targetFile.listFiles(); + for(File f: files){ + if(f.isDirectory()) childDirs.add(f); + } + } + + return childDirs; + } + +} diff --git a/group17/1282579502/src/com/coderising/litestruts/StrutsTest.java b/group17/1282579502/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group17/1282579502/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group17/1282579502/src/com/coderising/litestruts/View.java b/group17/1282579502/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group17/1282579502/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/group17/1282579502/src/com/coderising/litestruts/struts.xml b/group17/1282579502/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..246b3595ad --- /dev/null +++ b/group17/1282579502/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 diff --git a/group17/1282579502/src/com/coderising/parser/TestDriver.java b/group17/1282579502/src/com/coderising/parser/TestDriver.java new file mode 100644 index 0000000000..a4decf8986 --- /dev/null +++ b/group17/1282579502/src/com/coderising/parser/TestDriver.java @@ -0,0 +1,22 @@ +package com.coderising.parser; + +import com.coderising.parser.XmlParser.eNode; + +public class TestDriver { + + public static void main(String[] args) { + XmlParser testParser = new XmlParser("src/xml/input.xml"); + String input = ""; + System.out.println(); + //eNode e = testParser.parseRawData(input); + //XmlParser.dumpNodePreOrder(e); +// try{ +// testParser.loadFile(testParser.getFile()); +// }catch(Exception e){ +// e.printStackTrace(); +// } + } + + + +} diff --git a/group17/1282579502/src/com/coderising/parser/XmlParser.java b/group17/1282579502/src/com/coderising/parser/XmlParser.java new file mode 100644 index 0000000000..f78be14555 --- /dev/null +++ b/group17/1282579502/src/com/coderising/parser/XmlParser.java @@ -0,0 +1,277 @@ +package com.coderising.parser; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class XmlParser { + + private File targetFile = null; + private eNode root = null; + + public XmlParser(String path){ + setTargetFile(path); + try { + String content = loadFile(targetFile); + parseString(content); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void setTargetFile(String path){ + targetFile = new File(path); + if(! targetFile.isFile() || ! targetFile.exists()){ + throw new IllegalArgumentException("Path: " + path + " is NOT valid."); + } + } + + public File getFile(){ + return targetFile; + } + + protected String loadFile(File f) throws FileNotFoundException{ + StringBuilder sb = new StringBuilder(); + FileReader fr = new FileReader(f); + BufferedReader br = new BufferedReader(fr); + String line = null; + try{ + while((line = br.readLine()) != null){ + line = line.trim(); + if(!line.startsWith("#") && !line.startsWith(" cache = new LinkedList<>(); + int bracketStart=0; + int contentStart=0; + for(int i=0; i< str.length(); i++){ + if(str.charAt(i) == '<'){ + bracketStart = i; + if(i>contentStart){ + String tagVal = str.substring(contentStart, i).trim(); + if(tagVal.length() > 0){ + //System.out.println("content: " + tagVal); + cache.add(tagVal); + } + } + } + else if(str.charAt(i) == '>'){ + String elementTag = str.substring(bracketStart, i+1); + //System.out.println("pushing: " + elementTag); + cache.add(elementTag); + contentStart=i+1; + + } + } + + //debug information + //dumpStack(cache); + + root = processElementStack(cache); + //System.out.println(); + //System.out.println("Dump node tree"); + //dumpNodePreOrder(root); + return root; + } + + private eNode processElementStack(Queue stk){ + + if(!stk.isEmpty()){ + String token = stk.remove(); + if(token.startsWith("'){ + String elementName = str.substring(i+1, j); + branchNode.element_name = elementName.trim(); + //System.out.println("element name: " + elementName); + i = j; + break; + } + } + } + else if(Character.isAlphabetic(c) || c == '"'){ + + if(!foundAttributeName){ + for(int j=i; j'){ + String attr_val = str.substring(i, k).trim(); + if(attr_name != null && attr_val != null){ + //System.out.println("Element: " + branchNode.element_name + " put in: " + attr_name + " " + attr_val); + branchNode.attributeMap.put(attr_name, attr_val.replaceAll("\"", "")); + } + foundAttributeName = false; + i = k; + break; + } + } + } + + } + } + //System.out.println(); + return branchNode; + } + + public List getActionNodeList(){ + return root.subNodes; + } + + private void dumpNodeBFS(eNode root){ + Stack stk = new Stack(); + stk.push(root); + while(!stk.isEmpty()){ + eNode cur = stk.pop(); + dump(cur, stk); + } + + } + + public static void dumpNodePreOrder(eNode root){ + if(root.isLeaf){ + System.out.println("Leaf: "+root.element_raw_content); + } + else{ + System.out.println("Branch: " + root.element_name); + Iterator itr = root.attributeMap.keySet().iterator(); + while(itr.hasNext()){ + String key = itr.next(); + System.out.println("["+ key + "]=[" + root.attributeMap.get(key)+"]"); + } + } + for(eNode i : root.subNodes){ + dumpNodePreOrder(i); + } + } + + private void dump(eNode node, Stack stk){ + System.out.println(node.element_raw_content); + for(int i = 0 ; i< node.subNodes.size(); i++){ + stk.push(node.subNodes.get(i)); + } + } + + private void dumpStack(Collection cache){ + Iterator itr = cache.iterator(); + while(itr.hasNext()){ + String item = itr.next(); + System.out.println(""+item+""); + } + } + + public void dump(){ + dumpNodePreOrder(root); + } + +// private String parseOpenElement(String str){ +// +// } +// +// private String parseCloseElment(String str){ +// +// } + + + + public class eNode{ + public boolean isLeaf = false; + public String element_raw_content = null; + public HashMap attributeMap = null; + public String element_name = null; + public List subNodes = null; + + public eNode(){ + _init(); + } + + private void _init(){ + attributeMap = new HashMap<>(); + subNodes = new LinkedList<>(); + } + + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append("element_name: ").append(element_name).append("\n"); + sb.append("isLeaf: ").append(isLeaf).append("\n"); + sb.append("name attr: ").append(attributeMap.get("name")).append("\n"); + if(attributeMap.get("class") != null) + sb.append("class attr: ").append(attributeMap.get("class")).append("\n"); + return sb.toString(); + } + } + +// protected class eGraph{ +// public eNode root = null; +// +// public eGraph(eNode root){ +// this.root = root; +// } +// } + +} diff --git a/group17/240094626/warm-up/src/com/array/ArrayUtil.java b/group17/240094626/warm-up/src/com/array/ArrayUtil.java new file mode 100644 index 0000000000..214ecae3bb --- /dev/null +++ b/group17/240094626/warm-up/src/com/array/ArrayUtil.java @@ -0,0 +1,243 @@ +package com.coderising.array; + +import com.coding.basic.impl.ArrayList; +import com.coding.basic.impl.LinkedList; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + for(int i=0; i < origin.length; i++){ + if(i >= origin.length-1-i){ + break; + } + int tmp = origin[i]; + origin[i] = origin[origin.length-i-1]; + origin[origin.length-i-1] = tmp; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 size = 0, // 璁板綍闈0鏁伴噺 + tmp = 0; // 闇瑕佹瘮杈冪殑涓嬩竴涓綅缃 + for(int i=0; i < oldArray.length;){ + if(oldArray[i] == 0){ + int j=tmp==0?(i+1):tmp; + for(;j < oldArray.length; j++){ + if(oldArray[j] == 0){ + continue; + }else{ + oldArray[i] = oldArray[j]; + oldArray[j] = 0; + size++; + tmp = j+1; + i=j; + break; + } + } + }else{ + i++; + size++; + continue; + } + } + int a[] = new int[size]; + System.arraycopy(oldArray, 0, a, 0, size); + return a; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){ + int[] a3 = new int[array1.length+array2.length]; + int i = array1.length-1, + j = array2.length-1, + k = a3.length-1; + while(i >= 0 && j >= 0){ + if(array1[i] > array2[j]){ + a3[k--] = array1[i--]; + }else{ + a3[k--] = array2[j--]; + } + } + while(j >= 0){ + a3[k--] = array2[j--]; + } + while(i >= 0){ + a3[k--] = array1[i--]; + } + + return a3; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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 a[] = new int[oldArray.length+size]; + System.arraycopy(oldArray, 0, a, 0, oldArray.length); + return a; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if(max == 1){ + int[] a = {0}; + return a; + } + if(max == 2){ + int[] a = {0,1}; + return a; + } + if(max == 3){ + int[] a = {0,1,1,2}; + return a; + } + ArrayList list = new ArrayList(); + list.add(0); + list.add(1); + list.add(1); + list.add(2); + int size = 4; + for(int i = 3; i < max ; i++){ + if(i == ((int)list.get(size-1)+(int)list.get(size-2))){ + list.add(i); + size++; + } + } + int[] a = new int[size]; + for(int i = 0; i < size; i++){ + a[i] = (int) list.get(i); + } + return a; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max < 2){ + int[] a ={}; + return a; + } + if(max == 2){ + int[] a ={2}; + return a; + } + LinkedList list = new LinkedList(); + list.add(2); + for(int n = 3; n < max; n=n+2){ + int i=3; + boolean flag = true; + for(;i*i <= n;i=i+2){ + // 鍏堟帓闄ゅ伓鏁 + if(n%i == 0){ + flag = false; + break; + } + } + if(flag){ + list.add(n); + } + } + int[] a = new int[list.size()]; + for(int i = 0; i < list.size(); i++){ + a[i] = (int) list.get(i); + } + return a; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if(max <= 6){ + int a[] = {}; + return a; + } + LinkedList list = new LinkedList(); + int p = 6,sum; + for(; p < max; p++){ + sum = 0; + for(int i=1; i < p % 2; i++){ + if(p % i == 0){ + sum += i; + } + } + if(sum == p){ + list.add(p); + } + } + int a[] = new int[list.size()]; + for(int i = 0; i < list.size(); i++){ + a[i] = (int) list.get(i); + } + return a; + + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + for(int i=0;i 0){ + s.append("-"); + } + s.append(a); + } + seperator = s.toString();*/ + return seperator; + } + +} diff --git a/group17/240094626/warm-up/src/com/coderising/litestruts/Struts.java b/group17/240094626/warm-up/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..d3dd810e13 --- /dev/null +++ b/group17/240094626/warm-up/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,165 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.util.Iterator; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.Node; +import org.dom4j.io.SAXReader; + +import com.coderising.litestruts.exception.StrutsException; +import com.coderising.litestruts.util.FileUtils; +import com.coding.basic.impl.ArrayList; +import com.sun.org.apache.xml.internal.security.encryption.XMLEncryptionException; + + + +public class Struts { + + public static final String DETAULT_XML = "struts.xml"; + public static final String ACTION_TAGNAME = "action"; + public static final String RESULT_TAGNAME = "result"; + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + return null; + } + + + /** + * 鑾峰彇struts涓厤缃殑鎵鏈塧ction + * 閫氳繃dom4j瀹炵幇璇粁ml鏂囦欢 + * Struts action鍏冪礌鐨勬槸2灞傜粨鏋勶紝鏁咃紝杩欓噷鍙В鏋2灞 + * @param path + * @return + * @return:ArrayList + */ + private static ArrayList getActionBeans(String dir,String fileName){ + if(!FileUtils.createDir(dir)){ + throw new IllegalArgumentException("鏂囦欢璺緞涓嶅瓨鍦細"+dir); + } + File f = new File(dir); + if(null == fileName || "".endsWith(fileName)){ + fileName = DETAULT_XML; + } + SAXReader saxReader = new SAXReader(); + try { + f = new File(f.getPath()+"\\"+fileName); + Document document = saxReader.read(f); + Element element = document.getRootElement(); + ArrayList actions = new ArrayList(); + Iterator iterator = element.elementIterator(ACTION_TAGNAME); + // 閬嶅巻姣忎竴涓猘ction + while(iterator.hasNext()){ + Element actionEle = (Element) iterator.next(); + ActionBean bean = new ActionBean(); + bean.setName(actionEle.attributeValue("name")); + bean.setClazz(actionEle.attributeValue("class")); + Iterator it = actionEle.elementIterator(RESULT_TAGNAME); + ArrayList results = new ArrayList(); + // 閬嶅巻姣忎竴涓猺esult + while(it.hasNext()){ + Element resultEle = (Element) it.next(); + Result result = new Result(); + result.setName(resultEle.attributeValue("name")); + result.setValue(resultEle.getText()); + results.add(result); + System.out.println(result); + } + bean.setResults(results); + System.out.println(bean); + actions.add(bean); + } + System.out.println(); + } catch (DocumentException e) { + e.printStackTrace(); + throw new StrutsException("xml鏂囦欢瑙f瀽澶辫触:,dir="+dir+",fileName="+fileName,e); + } + + return null; + } + + + + private static class ActionBean{ + String name; + String clazz; + ArrayList results; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getClazz() { + return clazz; + } + public void setClazz(String clazz) { + this.clazz = clazz; + } + public ArrayList getResults() { + return results; + } + public void setResults(ArrayList results) { + this.results = results; + } + @Override + public String toString() { + return "ActionBean [name=" + name + ", clazz=" + clazz + + ", results=" + results + "]"; + } + + + } + + private static class Result{ + String name; + String value; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getValue() { + return value; + } + public void setValue(String value) { + this.value = value; + } + @Override + public String toString() { + return "Result [name=" + name + ", value=" + value + "]"; + } + + + } + + public static void main(String[] args) { + Struts.getActionBeans("src/com/coderising/litestruts/",""); + } +} diff --git a/group17/240094626/warm-up/src/com/coderising/litestruts/exception/StrutsException.java b/group17/240094626/warm-up/src/com/coderising/litestruts/exception/StrutsException.java new file mode 100644 index 0000000000..bd1fe8011c --- /dev/null +++ b/group17/240094626/warm-up/src/com/coderising/litestruts/exception/StrutsException.java @@ -0,0 +1,61 @@ +package com.coderising.litestruts.exception; + +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; + +public class StrutsException extends RuntimeException { + + private static final long serialVersionUID = 5481955299021871754L; + + private String message; + private String stackTrace; + private Throwable t; + + public Throwable getCause() { + return this.t; + } + + public String toString() { + return getMessage(); + } + + public String getMessage() { + return this.message; + } + + public void printStackTrace() { + System.err.print(this.stackTrace); + } + + public void printStackTrace(PrintStream paramPrintStream) { + printStackTrace(new PrintWriter(paramPrintStream)); + } + + public void printStackTrace(PrintWriter paramPrintWriter) { + paramPrintWriter.print(this.stackTrace); + } + + public StrutsException(String paramString) { + super(paramString); + this.message = paramString; + this.stackTrace = paramString; + } + + public StrutsException(Throwable paramThrowable) { + super(paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } + + public StrutsException(String paramString, Throwable paramThrowable) { + super(paramString + "; nested exception is " + + paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } +} diff --git a/group17/240094626/warm-up/src/com/coderising/litestruts/struts.xml b/group17/240094626/warm-up/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..fadf460c49 --- /dev/null +++ b/group17/240094626/warm-up/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 diff --git a/group17/240094626/warm-up/src/com/coderising/litestruts/util/FileUtils.java b/group17/240094626/warm-up/src/com/coderising/litestruts/util/FileUtils.java new file mode 100644 index 0000000000..f369fc6cdb --- /dev/null +++ b/group17/240094626/warm-up/src/com/coderising/litestruts/util/FileUtils.java @@ -0,0 +1,25 @@ +package com.coderising.litestruts.util; + +import java.io.File; + +public class FileUtils { + + public static boolean createDir(String dir){ + File f = null; + f = new File(dir); + boolean ok = true; + if(!f.exists()){ + ok = f.mkdirs(); + } + return ok; + } + + public static String getProjectPath(){ + return System.getProperty("user.dir"); + } + + public static void main(String[] args) { + System.out.println(createDir("src/com/coderising/litestruts/")); + System.out.println(getProjectPath()); + } +} diff --git a/group17/240094626/warm-up/src/com/litestruts/LoginAction.java b/group17/240094626/warm-up/src/com/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group17/240094626/warm-up/src/com/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; + } +} diff --git a/group17/240094626/warm-up/src/com/litestruts/Struts.java b/group17/240094626/warm-up/src/com/litestruts/Struts.java new file mode 100644 index 0000000000..44e6398b80 --- /dev/null +++ b/group17/240094626/warm-up/src/com/litestruts/Struts.java @@ -0,0 +1,194 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import com.coderising.litestruts.exception.StrutsRunActionException; +import com.coderising.litestruts.exception.StrutsXMLLoaderException; + + + +/** + * 妯℃嫙Struts璇诲彇xml鏂囦欢锛岃В鏋恆tion锛屽苟鎵цexectue鏂规硶 + * @author 240094626 + * + */ +public class Struts { + /**鍗曚緥瀵硅薄*/ + private static Struts instance = null; + /**榛樿閰嶇疆鏂囦欢鐩綍*/ + private static final String STRUTS_CONFIG_DIR = "src/com/coderising/litestruts/"; + /**榛樿閰嶇疆鏂囦欢鍚*/ + private static final String STRUTS_CONFIG_XML = "struts.xml"; + /**榛樿缂栫爜瀛楃闆*/ + private static final String ENCODE = "UTF-8"; + /**action鏂囨。鑺傜偣鍚嶇О*/ + private static final String DOC_ACTION = "action"; + /**result鏂囨。鑺傜偣鍚嶇О*/ + private static final String DOC_ACTION_RESULT = "result"; + + /** + * 鍗曚緥瀹炵幇锛屽弻check + * @return + */ + public static Struts getInstance(){ + if(instance == null){ + synchronized (Struts.class) { + if(instance == null){ + instance = new Struts(); + } + } + } + return instance; + } + + public View runAction(String actionName, Map parameters) throws StrutsRunActionException { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + View view = new View(); + try { + // 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + Map actions = getActionByDom4J(); + + // 1. 鎵惧埌LoginAction 鍙嶅皠瀹炰緥鍖栵紝setName 鍜宻etPassword + Action action = actions.get(actionName); + Class clz = Class.forName(action.clazz); + Object actionObj = clz.newInstance(); + Method m1 = clz.getDeclaredMethod("setName", String.class); + m1.setAccessible(true); + m1.invoke(actionObj, parameters.get("name")); + Method m2 = clz.getDeclaredMethod("setPassword", String.class); + m2.setAccessible(true); + m2.invoke(actionObj, parameters.get("password")); + + // 2.閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶 + Method mExectue = clz.getDeclaredMethod("execute", null); + String result = (String) mExectue.invoke(actionObj, null); + + // 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶,閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap + Field[] fs = clz.getDeclaredFields(); + Map pMap = new HashMap(10); + for(Field f : fs){ + String methodName = "get"+toUpperCase(f.getName()); + Method m = clz.getDeclaredMethod(methodName, null); + m.setAccessible(true); + pMap.put(f.getName(), m.invoke(actionObj, null)); + } + view.setParameters(pMap); + // 4.鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp, + view.setJsp(action.results.get(result)); + System.out.println(view); + } catch (StrutsXMLLoaderException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new StrutsRunActionException("Action鎵ц澶辫触",e); + } + + + return view; + } + + + /** + * dom4j璇粁ml + * @return + * @throws StrutsXMLLoaderException + */ + private Map getActionByDom4J() throws StrutsXMLLoaderException{ + return getActionByDom4J(STRUTS_CONFIG_DIR,STRUTS_CONFIG_XML,ENCODE); + } + /** + * dom4j璇粁ml + * @param dir + * @param fileName + * @param encode + * @return + * @throws StrutsXMLLoaderException + */ + private Map getActionByDom4J(String dir,String fileName,String encode) throws StrutsXMLLoaderException { + File f = new File(dir); + if(!f.exists()){ + throw new StrutsXMLLoaderException("璺緞涓嶅瓨鍦細"+dir); + } + f = new File(f.getPath()+"\\"+fileName); + if(!f.exists()){ + throw new StrutsXMLLoaderException("璺緞锛"+dir+"涓枃浠朵笉瀛樺湪锛"+fileName); + } + SAXReader saxReader = new SAXReader(); + saxReader.setEncoding(encode); + Map actions = new HashMap(10); + try { + Document document = saxReader.read(f); + Element root =document.getRootElement(); + Iterator it = root.elementIterator(DOC_ACTION); + while(it.hasNext()){ + Action action = new Action(); + Element element = it.next(); + action.name = element.attributeValue("name"); + action.clazz = element.attributeValue("class"); + Map results = new HashMap(10); + Iterator rIt = element.elementIterator(DOC_ACTION_RESULT); + while(rIt.hasNext()){ + Element rElement = rIt.next(); + results.put(rElement.attributeValue("name"), rElement.getText()); + } + action.results = results; + actions.put(action.name, action); + } + } catch (Exception e) { + throw new StrutsXMLLoaderException("xml鏂囦欢瑙f瀽澶辫触",e); + } + + return actions; + } + + /** + * 棣栦釜瀛楃澶у啓 + * @param fieldName + * @return + */ + private String toUpperCase(String fieldName){ + return fieldName.substring(0,1).toUpperCase()+fieldName.substring(1); + } + + + + /** + * Action绫 + */ + private static class Action{ + String name; + String clazz; + Map results; + } + + + +} diff --git a/group17/240094626/warm-up/src/com/litestruts/StrutsTest.java b/group17/240094626/warm-up/src/com/litestruts/StrutsTest.java new file mode 100644 index 0000000000..09b00c8c3b --- /dev/null +++ b/group17/240094626/warm-up/src/com/litestruts/StrutsTest.java @@ -0,0 +1,57 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +import com.coderising.litestruts.exception.StrutsRunActionException; + + + + + +public class StrutsTest { + Struts struts = Struts.getInstance(); + + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = null; + try { + view = struts.runAction(actionName,params); + } catch (StrutsRunActionException e) { + e.printStackTrace(); + } + + 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 = null; + try { + view = struts.runAction(actionName,params); + } catch (StrutsRunActionException e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group17/240094626/warm-up/src/com/litestruts/View.java b/group17/240094626/warm-up/src/com/litestruts/View.java new file mode 100644 index 0000000000..880a7be260 --- /dev/null +++ b/group17/240094626/warm-up/src/com/litestruts/View.java @@ -0,0 +1,28 @@ +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; + } + @Override + public String toString() { + return "View [jsp=" + jsp + ", parameters=" + parameters + "]"; + } + +} diff --git a/group17/240094626/warm-up/src/com/litestruts/exception/StrutsRunActionException.java b/group17/240094626/warm-up/src/com/litestruts/exception/StrutsRunActionException.java new file mode 100644 index 0000000000..5c5a7b9006 --- /dev/null +++ b/group17/240094626/warm-up/src/com/litestruts/exception/StrutsRunActionException.java @@ -0,0 +1,59 @@ +package com.coderising.litestruts.exception; + +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; + +public class StrutsRunActionException extends Exception { + + private static final long serialVersionUID = -242506476923032524L; + private String message; + private String stackTrace; + private Throwable t; + public Throwable getCause() { + return this.t; + } + + public String toString() { + return getMessage(); + } + + public String getMessage() { + return this.message; + } + + public void printStackTrace() { + System.err.print(this.stackTrace); + } + + public void printStackTrace(PrintStream paramPrintStream) { + printStackTrace(new PrintWriter(paramPrintStream)); + } + + public void printStackTrace(PrintWriter paramPrintWriter) { + paramPrintWriter.print(this.stackTrace); + } + + public StrutsRunActionException(String paramString) { + super(paramString); + this.message = paramString; + this.stackTrace = paramString; + } + + public StrutsRunActionException(Throwable paramThrowable) { + super(paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } + + public StrutsRunActionException(String paramString, Throwable paramThrowable) { + super(paramString + "; nested exception is " + + paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } +} diff --git a/group17/240094626/warm-up/src/com/litestruts/exception/StrutsXMLLoaderException.java b/group17/240094626/warm-up/src/com/litestruts/exception/StrutsXMLLoaderException.java new file mode 100644 index 0000000000..4efb8b9152 --- /dev/null +++ b/group17/240094626/warm-up/src/com/litestruts/exception/StrutsXMLLoaderException.java @@ -0,0 +1,59 @@ +package com.coderising.litestruts.exception; + +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; + +public class StrutsXMLLoaderException extends Exception { + + private static final long serialVersionUID = 5481955299021871754L; + private String message; + private String stackTrace; + private Throwable t; + public Throwable getCause() { + return this.t; + } + + public String toString() { + return getMessage(); + } + + public String getMessage() { + return this.message; + } + + public void printStackTrace() { + System.err.print(this.stackTrace); + } + + public void printStackTrace(PrintStream paramPrintStream) { + printStackTrace(new PrintWriter(paramPrintStream)); + } + + public void printStackTrace(PrintWriter paramPrintWriter) { + paramPrintWriter.print(this.stackTrace); + } + + public StrutsXMLLoaderException(String paramString) { + super(paramString); + this.message = paramString; + this.stackTrace = paramString; + } + + public StrutsXMLLoaderException(Throwable paramThrowable) { + super(paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } + + public StrutsXMLLoaderException(String paramString, Throwable paramThrowable) { + super(paramString + "; nested exception is " + + paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } +} diff --git a/group17/240094626/warm-up/src/com/litestruts/struts.xml b/group17/240094626/warm-up/src/com/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group17/240094626/warm-up/src/com/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/group17/240094626/work_0226/src/com/coderising/array/ArrayUtil.java b/group17/240094626/work_0226/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..214ecae3bb --- /dev/null +++ b/group17/240094626/work_0226/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,243 @@ +package com.coderising.array; + +import com.coding.basic.impl.ArrayList; +import com.coding.basic.impl.LinkedList; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + for(int i=0; i < origin.length; i++){ + if(i >= origin.length-1-i){ + break; + } + int tmp = origin[i]; + origin[i] = origin[origin.length-i-1]; + origin[origin.length-i-1] = tmp; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 size = 0, // 璁板綍闈0鏁伴噺 + tmp = 0; // 闇瑕佹瘮杈冪殑涓嬩竴涓綅缃 + for(int i=0; i < oldArray.length;){ + if(oldArray[i] == 0){ + int j=tmp==0?(i+1):tmp; + for(;j < oldArray.length; j++){ + if(oldArray[j] == 0){ + continue; + }else{ + oldArray[i] = oldArray[j]; + oldArray[j] = 0; + size++; + tmp = j+1; + i=j; + break; + } + } + }else{ + i++; + size++; + continue; + } + } + int a[] = new int[size]; + System.arraycopy(oldArray, 0, a, 0, size); + return a; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){ + int[] a3 = new int[array1.length+array2.length]; + int i = array1.length-1, + j = array2.length-1, + k = a3.length-1; + while(i >= 0 && j >= 0){ + if(array1[i] > array2[j]){ + a3[k--] = array1[i--]; + }else{ + a3[k--] = array2[j--]; + } + } + while(j >= 0){ + a3[k--] = array2[j--]; + } + while(i >= 0){ + a3[k--] = array1[i--]; + } + + return a3; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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 a[] = new int[oldArray.length+size]; + System.arraycopy(oldArray, 0, a, 0, oldArray.length); + return a; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if(max == 1){ + int[] a = {0}; + return a; + } + if(max == 2){ + int[] a = {0,1}; + return a; + } + if(max == 3){ + int[] a = {0,1,1,2}; + return a; + } + ArrayList list = new ArrayList(); + list.add(0); + list.add(1); + list.add(1); + list.add(2); + int size = 4; + for(int i = 3; i < max ; i++){ + if(i == ((int)list.get(size-1)+(int)list.get(size-2))){ + list.add(i); + size++; + } + } + int[] a = new int[size]; + for(int i = 0; i < size; i++){ + a[i] = (int) list.get(i); + } + return a; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + if(max < 2){ + int[] a ={}; + return a; + } + if(max == 2){ + int[] a ={2}; + return a; + } + LinkedList list = new LinkedList(); + list.add(2); + for(int n = 3; n < max; n=n+2){ + int i=3; + boolean flag = true; + for(;i*i <= n;i=i+2){ + // 鍏堟帓闄ゅ伓鏁 + if(n%i == 0){ + flag = false; + break; + } + } + if(flag){ + list.add(n); + } + } + int[] a = new int[list.size()]; + for(int i = 0; i < list.size(); i++){ + a[i] = (int) list.get(i); + } + return a; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + if(max <= 6){ + int a[] = {}; + return a; + } + LinkedList list = new LinkedList(); + int p = 6,sum; + for(; p < max; p++){ + sum = 0; + for(int i=1; i < p % 2; i++){ + if(p % i == 0){ + sum += i; + } + } + if(sum == p){ + list.add(p); + } + } + int a[] = new int[list.size()]; + for(int i = 0; i < list.size(); i++){ + a[i] = (int) list.get(i); + } + return a; + + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + for(int i=0;i 0){ + s.append("-"); + } + s.append(a); + } + seperator = s.toString();*/ + return seperator; + } + +} diff --git a/group17/240094626/work_0226/src/com/coderising/litestruts/LoginAction.java b/group17/240094626/work_0226/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..dcdbe226ed --- /dev/null +++ b/group17/240094626/work_0226/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; + } +} diff --git a/group17/240094626/work_0226/src/com/coderising/litestruts/Struts.java b/group17/240094626/work_0226/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..44e6398b80 --- /dev/null +++ b/group17/240094626/work_0226/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,194 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import com.coderising.litestruts.exception.StrutsRunActionException; +import com.coderising.litestruts.exception.StrutsXMLLoaderException; + + + +/** + * 妯℃嫙Struts璇诲彇xml鏂囦欢锛岃В鏋恆tion锛屽苟鎵цexectue鏂规硶 + * @author 240094626 + * + */ +public class Struts { + /**鍗曚緥瀵硅薄*/ + private static Struts instance = null; + /**榛樿閰嶇疆鏂囦欢鐩綍*/ + private static final String STRUTS_CONFIG_DIR = "src/com/coderising/litestruts/"; + /**榛樿閰嶇疆鏂囦欢鍚*/ + private static final String STRUTS_CONFIG_XML = "struts.xml"; + /**榛樿缂栫爜瀛楃闆*/ + private static final String ENCODE = "UTF-8"; + /**action鏂囨。鑺傜偣鍚嶇О*/ + private static final String DOC_ACTION = "action"; + /**result鏂囨。鑺傜偣鍚嶇О*/ + private static final String DOC_ACTION_RESULT = "result"; + + /** + * 鍗曚緥瀹炵幇锛屽弻check + * @return + */ + public static Struts getInstance(){ + if(instance == null){ + synchronized (Struts.class) { + if(instance == null){ + instance = new Struts(); + } + } + } + return instance; + } + + public View runAction(String actionName, Map parameters) throws StrutsRunActionException { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + View view = new View(); + try { + // 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + Map actions = getActionByDom4J(); + + // 1. 鎵惧埌LoginAction 鍙嶅皠瀹炰緥鍖栵紝setName 鍜宻etPassword + Action action = actions.get(actionName); + Class clz = Class.forName(action.clazz); + Object actionObj = clz.newInstance(); + Method m1 = clz.getDeclaredMethod("setName", String.class); + m1.setAccessible(true); + m1.invoke(actionObj, parameters.get("name")); + Method m2 = clz.getDeclaredMethod("setPassword", String.class); + m2.setAccessible(true); + m2.invoke(actionObj, parameters.get("password")); + + // 2.閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶 + Method mExectue = clz.getDeclaredMethod("execute", null); + String result = (String) mExectue.invoke(actionObj, null); + + // 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶,閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap + Field[] fs = clz.getDeclaredFields(); + Map pMap = new HashMap(10); + for(Field f : fs){ + String methodName = "get"+toUpperCase(f.getName()); + Method m = clz.getDeclaredMethod(methodName, null); + m.setAccessible(true); + pMap.put(f.getName(), m.invoke(actionObj, null)); + } + view.setParameters(pMap); + // 4.鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp, + view.setJsp(action.results.get(result)); + System.out.println(view); + } catch (StrutsXMLLoaderException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new StrutsRunActionException("Action鎵ц澶辫触",e); + } + + + return view; + } + + + /** + * dom4j璇粁ml + * @return + * @throws StrutsXMLLoaderException + */ + private Map getActionByDom4J() throws StrutsXMLLoaderException{ + return getActionByDom4J(STRUTS_CONFIG_DIR,STRUTS_CONFIG_XML,ENCODE); + } + /** + * dom4j璇粁ml + * @param dir + * @param fileName + * @param encode + * @return + * @throws StrutsXMLLoaderException + */ + private Map getActionByDom4J(String dir,String fileName,String encode) throws StrutsXMLLoaderException { + File f = new File(dir); + if(!f.exists()){ + throw new StrutsXMLLoaderException("璺緞涓嶅瓨鍦細"+dir); + } + f = new File(f.getPath()+"\\"+fileName); + if(!f.exists()){ + throw new StrutsXMLLoaderException("璺緞锛"+dir+"涓枃浠朵笉瀛樺湪锛"+fileName); + } + SAXReader saxReader = new SAXReader(); + saxReader.setEncoding(encode); + Map actions = new HashMap(10); + try { + Document document = saxReader.read(f); + Element root =document.getRootElement(); + Iterator it = root.elementIterator(DOC_ACTION); + while(it.hasNext()){ + Action action = new Action(); + Element element = it.next(); + action.name = element.attributeValue("name"); + action.clazz = element.attributeValue("class"); + Map results = new HashMap(10); + Iterator rIt = element.elementIterator(DOC_ACTION_RESULT); + while(rIt.hasNext()){ + Element rElement = rIt.next(); + results.put(rElement.attributeValue("name"), rElement.getText()); + } + action.results = results; + actions.put(action.name, action); + } + } catch (Exception e) { + throw new StrutsXMLLoaderException("xml鏂囦欢瑙f瀽澶辫触",e); + } + + return actions; + } + + /** + * 棣栦釜瀛楃澶у啓 + * @param fieldName + * @return + */ + private String toUpperCase(String fieldName){ + return fieldName.substring(0,1).toUpperCase()+fieldName.substring(1); + } + + + + /** + * Action绫 + */ + private static class Action{ + String name; + String clazz; + Map results; + } + + + +} diff --git a/group17/240094626/work_0226/src/com/coderising/litestruts/StrutsTest.java b/group17/240094626/work_0226/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..09b00c8c3b --- /dev/null +++ b/group17/240094626/work_0226/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,57 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +import com.coderising.litestruts.exception.StrutsRunActionException; + + + + + +public class StrutsTest { + Struts struts = Struts.getInstance(); + + + @Test + public void testLoginActionSuccess() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = null; + try { + view = struts.runAction(actionName,params); + } catch (StrutsRunActionException e) { + e.printStackTrace(); + } + + 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 = null; + try { + view = struts.runAction(actionName,params); + } catch (StrutsRunActionException e) { + e.printStackTrace(); + } + + Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp()); + Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message")); + } +} diff --git a/group17/240094626/work_0226/src/com/coderising/litestruts/View.java b/group17/240094626/work_0226/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..880a7be260 --- /dev/null +++ b/group17/240094626/work_0226/src/com/coderising/litestruts/View.java @@ -0,0 +1,28 @@ +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; + } + @Override + public String toString() { + return "View [jsp=" + jsp + ", parameters=" + parameters + "]"; + } + +} diff --git a/group17/240094626/work_0226/src/com/coderising/litestruts/exception/StrutsRunActionException.java b/group17/240094626/work_0226/src/com/coderising/litestruts/exception/StrutsRunActionException.java new file mode 100644 index 0000000000..5c5a7b9006 --- /dev/null +++ b/group17/240094626/work_0226/src/com/coderising/litestruts/exception/StrutsRunActionException.java @@ -0,0 +1,59 @@ +package com.coderising.litestruts.exception; + +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; + +public class StrutsRunActionException extends Exception { + + private static final long serialVersionUID = -242506476923032524L; + private String message; + private String stackTrace; + private Throwable t; + public Throwable getCause() { + return this.t; + } + + public String toString() { + return getMessage(); + } + + public String getMessage() { + return this.message; + } + + public void printStackTrace() { + System.err.print(this.stackTrace); + } + + public void printStackTrace(PrintStream paramPrintStream) { + printStackTrace(new PrintWriter(paramPrintStream)); + } + + public void printStackTrace(PrintWriter paramPrintWriter) { + paramPrintWriter.print(this.stackTrace); + } + + public StrutsRunActionException(String paramString) { + super(paramString); + this.message = paramString; + this.stackTrace = paramString; + } + + public StrutsRunActionException(Throwable paramThrowable) { + super(paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } + + public StrutsRunActionException(String paramString, Throwable paramThrowable) { + super(paramString + "; nested exception is " + + paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } +} diff --git a/group17/240094626/work_0226/src/com/coderising/litestruts/exception/StrutsXMLLoaderException.java b/group17/240094626/work_0226/src/com/coderising/litestruts/exception/StrutsXMLLoaderException.java new file mode 100644 index 0000000000..4efb8b9152 --- /dev/null +++ b/group17/240094626/work_0226/src/com/coderising/litestruts/exception/StrutsXMLLoaderException.java @@ -0,0 +1,59 @@ +package com.coderising.litestruts.exception; + +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; + +public class StrutsXMLLoaderException extends Exception { + + private static final long serialVersionUID = 5481955299021871754L; + private String message; + private String stackTrace; + private Throwable t; + public Throwable getCause() { + return this.t; + } + + public String toString() { + return getMessage(); + } + + public String getMessage() { + return this.message; + } + + public void printStackTrace() { + System.err.print(this.stackTrace); + } + + public void printStackTrace(PrintStream paramPrintStream) { + printStackTrace(new PrintWriter(paramPrintStream)); + } + + public void printStackTrace(PrintWriter paramPrintWriter) { + paramPrintWriter.print(this.stackTrace); + } + + public StrutsXMLLoaderException(String paramString) { + super(paramString); + this.message = paramString; + this.stackTrace = paramString; + } + + public StrutsXMLLoaderException(Throwable paramThrowable) { + super(paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } + + public StrutsXMLLoaderException(String paramString, Throwable paramThrowable) { + super(paramString + "; nested exception is " + + paramThrowable.getMessage()); + this.t = paramThrowable; + StringWriter localStringWriter = new StringWriter(); + paramThrowable.printStackTrace(new PrintWriter(localStringWriter)); + this.stackTrace = localStringWriter.toString(); + } +} diff --git a/group17/240094626/work_0226/src/com/coderising/litestruts/struts.xml b/group17/240094626/work_0226/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group17/240094626/work_0226/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 diff --git a/group17/516886559/ArrayList.java b/group17/516886559/ArrayList.java new file mode 100644 index 0000000000..75222aecce --- /dev/null +++ b/group17/516886559/ArrayList.java @@ -0,0 +1,103 @@ +package com.rd.p2p.common.util.liuxin; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void add(E o){ + if(size >= elementData.length){ + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + elementData[size++] = o; + size++; + } + + public void add(int index, E o){ + if(index > size){ + throw new ArrayIndexOutOfBoundsException("鎻掑叆绱㈠紩鏁颁笉鑳藉ぇ浜庢暟鍒楁婚暱搴 " + index + ">" + size); + } + if(size >= elementData.length){ + elementData = Arrays.copyOf(elementData, elementData.length * 2); + } + Object[] tempData = Arrays.copyOfRange(elementData, index, size); + elementData[index] = o; + for(int i = 1; i <= tempData.length; i++){ + elementData[i+index] = tempData[i-1]; + } + size++; + + } + + public Object get(int index){ + if(index >= size){ + throw new ArrayIndexOutOfBoundsException("鍙栧嚭鏁扮粍绱㈠紩涓嶈兘澶т簬绛変簬鏁扮粍鎬婚暱搴 " + index + ">=" + size); + } + return elementData[index]; + } + + public Object remove(int index){ + if(index >= size){ + throw new ArrayIndexOutOfBoundsException("绉婚櫎绱㈠紩鏁颁笉鑳藉ぇ浜庣瓑浜庢暟鍒楁婚暱搴 " + index + ">=" + size); + } + Object data = get(index); + Object[] tempData = Arrays.copyOfRange(elementData, index+1, size); + for(int i = 0; i < tempData.length; i++){ + elementData[index+i] = tempData[i]; + } + elementData[size - 1] = null; + size--; + return data; + } + + public int size(){ + return size; + } + + @Override + public String toString() { + for (Object object : elementData) { + System.out.println(object); + } + return null; + } + + //杩唬鍣 + public Iterator iterator(){ + return new Iterator() { + + private int index = 0; + + @Override + public Object next() { + if(index >= size){ + throw new ArrayIndexOutOfBoundsException("鍙栧嚭鏁扮粍绱㈠紩涓嶈兘澶т簬绛変簬鏁扮粍鎬婚暱搴 " + index + ">=" + size); + } + return get(index++); + } + + @Override + public boolean hasNext() { + if(size > index){ + return true; + }else{ + return false; + } + } + }; + } + + public static void main(String[] args) { + ArrayList list = new ArrayList(); + list.add(1); + list.add(2); + list.add(3,3); + Iterator in = list.iterator(); + while(in.hasNext()){ + System.out.println(in.next()); + } + } +} \ No newline at end of file diff --git a/group17/516886559/BinaryTreeNode.java b/group17/516886559/BinaryTreeNode.java new file mode 100644 index 0000000000..75c17d1198 --- /dev/null +++ b/group17/516886559/BinaryTreeNode.java @@ -0,0 +1,68 @@ +package com.rd.p2p.common.util.liuxin; + +/** + * 鐢↖nteger鏄撲簬姣旇緝鍜屾彃鍏 + * @author jhn + * time:2017骞2鏈24鏃 + */ +public class BinaryTreeNode { + + private Integer data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(){ + + } + + public BinaryTreeNode(Integer integer){ + this.data = integer; + } + + public Integer getData() { + return data; + } + public void setData(Integer 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(Integer o){ + if(data == null){ + data = o; + return this; + } + BinaryTreeNode node = new BinaryTreeNode(o); + BinaryTreeNode tempBinaryTreeNode = this; + boolean begin = true; + while(begin){ + if(o < data){ + tempBinaryTreeNode = tempBinaryTreeNode.getLeft(); + if(tempBinaryTreeNode.getLeft() == null){ + tempBinaryTreeNode.setLeft(node); + begin = false;; + } + }else{ + tempBinaryTreeNode = tempBinaryTreeNode.getRight(); + if(tempBinaryTreeNode.getRight() == null){ + tempBinaryTreeNode.setRight(node); + begin = false;; + } + } + + } + return node; + } + +} diff --git a/group17/516886559/Iterator.java b/group17/516886559/Iterator.java new file mode 100644 index 0000000000..d29475ed43 --- /dev/null +++ b/group17/516886559/Iterator.java @@ -0,0 +1,7 @@ +package com.rd.p2p.common.util.liuxin; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group17/516886559/LinkedList.java b/group17/516886559/LinkedList.java new file mode 100644 index 0000000000..1b0345de7d --- /dev/null +++ b/group17/516886559/LinkedList.java @@ -0,0 +1,150 @@ +package com.rd.p2p.common.util.liuxin; + +public class LinkedList implements List { + + private Node head; + + private int size; + + public void add(E o){ + if(size == 0){ + head = new Node(); + head.data = o; + }else{ + Node node = new Node(); + node.data = o; + getNode(size-1).next = node; + } + size ++; + } + + public void add(int index , E o){ + Node node = new Node(); + node.data = o; + if(index == 0){ + node.next = head; + head = node; + }else{ + Node indexNode = getNode(index - 1); + indexNode.next = node; + if(index < size){ + node.next = getNode(index); + } + } + size ++; + } + + public Object get(int index){ + return getNode(index).data; + } + + public Object remove(int index){ + if(index > size - 1){ + throw new ArrayIndexOutOfBoundsException("绉婚櫎绱㈠紩瓒呭嚭鏁扮粍绱㈠紩杈圭晫 " + index + ">" + (size - 1)); + } + if(index < 0){ + throw new ArrayIndexOutOfBoundsException("绱㈠紩涓嶈兘涓鸿礋鏁"); + } + Node returnNode = null; + if(index == 0){ + returnNode = head; + if(head.next != null){ + head = head.next; + }else{ + head = null; + } + }else{ + returnNode = getNode(index); + if(returnNode.next != null){ + Node preNode = getNode(index-1); + Node nextNode = getNode(index+1); + preNode.next = nextNode; + } + } + size--; + return returnNode.data; + } + + public int size(){ + return size; + } + + public void addFirst(E o){ + add(0,o); + } + + public void addLast(E o){ + add(size-1,o); + } + + public Object removeFirst(){ + return remove(0); + } + + public Object removeLast(){ + return remove(size - 1); + } + + private Node getNode(int index){ + if(index > size - 1){ + throw new ArrayIndexOutOfBoundsException("鏌ヨ绱㈠紩瓒呭嚭鏁扮粍绱㈠紩杈圭晫 " + index + ">" + (size - 1)); + } + if(index < 0){ + throw new ArrayIndexOutOfBoundsException("绱㈠紩涓嶈兘涓鸿礋鏁"); + } + Node tempNode = head; + if(index == 0){ + tempNode = head; + }else{ + for(int i = 0; i < index; i++){ + tempNode = tempNode.next; + } + } + return tempNode; + } + + private static class Node{ + Object data; + Node next; + } + + @Override + public String toString() { + for (int i = 0; i < size; i++) { + System.out.println(get(i)); + } + return null; + } + + //杩唬鍣 + public Iterator iterator(){ + return new Iterator() { + private int index = 0; + + @Override + public Object next() { + if(index >= size){ + throw new ArrayIndexOutOfBoundsException("鍙栧嚭鏁扮粍绱㈠紩涓嶈兘澶т簬绛変簬鏁扮粍鎬婚暱搴 " + index + ">=" + size); + } + return get(index++); + } + + @Override + public boolean hasNext() { + if(size > index){ + return true; + }else{ + return false; + } + } + }; + } + + public static void main(String[] args) { + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(3); + list.toString(); + } +} diff --git a/group17/516886559/List.java b/group17/516886559/List.java new file mode 100644 index 0000000000..fd6e7883ba --- /dev/null +++ b/group17/516886559/List.java @@ -0,0 +1,9 @@ +package com.rd.p2p.common.util.liuxin; + +public interface List { + public void add(E o); + public void add(int index, E o); + public Object get(int index); + public Object remove(int index); + public int size(); +} diff --git a/group17/516886559/Queue.java b/group17/516886559/Queue.java new file mode 100644 index 0000000000..d077a71a32 --- /dev/null +++ b/group17/516886559/Queue.java @@ -0,0 +1,30 @@ +package com.rd.p2p.common.util.liuxin; + +public class Queue { + + ArrayList arrayList = new ArrayList(); + + public void enQueue(T o){ + arrayList.add(o); + } + + public T deQueue(){ + if(arrayList.size() > 0){ + @SuppressWarnings("unchecked") + T t = (T) arrayList.get(0); + arrayList.remove(0); + return t; + }else{ + return null; + } + } + + public boolean isEmpty(){ + return arrayList.size() == 0 ? true : false; + } + + public int size(){ + return arrayList.size(); + } +} + diff --git a/group17/516886559/Stack.java b/group17/516886559/Stack.java new file mode 100644 index 0000000000..53ffc21e09 --- /dev/null +++ b/group17/516886559/Stack.java @@ -0,0 +1,52 @@ +package com.rd.p2p.common.util.liuxin; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(T o){ + elementData.add(o); + } + + public T pop(){ + if(elementData.size() > 0){ + @SuppressWarnings("unchecked") + T obj = (T)elementData.get(elementData.size()-1); + elementData.remove(elementData.size()-1); + return obj; + }else{ + return null; + } + } + + @SuppressWarnings("unchecked") + public T peek(){ + if(elementData.size() > 0){ + return (T)elementData.get(elementData.size()-1); + }else{ + return null; + } + } + public boolean isEmpty(){ + return elementData.size() == 0 ? true : false; + } + public int size(){ + return elementData.size(); + } + + public static void main(String[] args) { + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + + System.out.println(stack.pop()); + System.out.println(stack.peek()); + System.out.println(stack.peek()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.pop()); + System.out.println(stack.peek()); + } +} + diff --git a/group17/785396327/binarytree/BinaryTree.java b/group17/785396327/2.26/binarytree/BinaryTree.java similarity index 96% rename from group17/785396327/binarytree/BinaryTree.java rename to group17/785396327/2.26/binarytree/BinaryTree.java index d55c65f8e4..bf5a6b639c 100644 --- a/group17/785396327/binarytree/BinaryTree.java +++ b/group17/785396327/2.26/binarytree/BinaryTree.java @@ -1,55 +1,55 @@ -package binarytree; - -/** - * Created by william on 2017/2/16. - */ -public class BinaryTree { - private Node root; - - class Node { - private Node left; - private Node right; - private Comparable data; - - public Node(Node left, Node right, Comparable data) { - this.left = left; - this.right = right; - this.data = data; - } - - private void add(Comparable data) { - if (this.data.compareTo(data) >= 0) - if (this.left == null) - this.left = new Node(null, null, data); - else - left.add(data); - else if (this.data.compareTo(data) < 0) - if (this.right == null) - this.right = new Node(null, null, data); - else - this.right.add(data); - } - - public Comparable getData() { - return this.data; - } - - public Node getLeft() { - return this.left; - } - - public Node getRight() { - return this.right; - } - } - - public void add(Comparable data) { - if (this.root == null) - root = new Node(null, null, data); - else this.root.add(data); - } - - public void printByType(SearchType type) { - type.printByType(this.root); - } -} +package binarytree; + +/** + * Created by william on 2017/2/16. + */ +public class BinaryTree { + private Node root; + + class Node { + private Node left; + private Node right; + private Comparable data; + + public Node(Node left, Node right, Comparable data) { + this.left = left; + this.right = right; + this.data = data; + } + + private void add(Comparable data) { + if (this.data.compareTo(data) >= 0) + if (this.left == null) + this.left = new Node(null, null, data); + else + left.add(data); + else if (this.data.compareTo(data) < 0) + if (this.right == null) + this.right = new Node(null, null, data); + else + this.right.add(data); + } + + public Comparable getData() { + return this.data; + } + + public Node getLeft() { + return this.left; + } + + public Node getRight() { + return this.right; + } + } + + public void add(Comparable data) { + if (this.root == null) + root = new Node(null, null, data); + else this.root.add(data); + } + + public void printByType(SearchType type) { + type.printByType(this.root); + } +} diff --git a/group17/785396327/binarytree/DLRSearchType.java b/group17/785396327/2.26/binarytree/DLRSearchType.java similarity index 95% rename from group17/785396327/binarytree/DLRSearchType.java rename to group17/785396327/2.26/binarytree/DLRSearchType.java index 7f9ba1c38d..3d1adbbdc7 100644 --- a/group17/785396327/binarytree/DLRSearchType.java +++ b/group17/785396327/2.26/binarytree/DLRSearchType.java @@ -1,16 +1,16 @@ -package binarytree; - -/** - * Created by william on 2017/2/18. - */ -public class DLRSearchType implements SearchType { - - @Override - public void printByType(BinaryTree.Node root) { - if (root != null) { - System.out.print(root.getData()+" "); - printByType(root.getLeft()); - printByType(root.getRight()); - } - } -} +package binarytree; + +/** + * Created by william on 2017/2/18. + */ +public class DLRSearchType implements SearchType { + + @Override + public void printByType(BinaryTree.Node root) { + if (root != null) { + System.out.print(root.getData()+" "); + printByType(root.getLeft()); + printByType(root.getRight()); + } + } +} diff --git a/group17/785396327/binarytree/LDRSearchType.java b/group17/785396327/2.26/binarytree/LDRSearchType.java similarity index 96% rename from group17/785396327/binarytree/LDRSearchType.java rename to group17/785396327/2.26/binarytree/LDRSearchType.java index f67b6dcb81..da18dbc1b9 100644 --- a/group17/785396327/binarytree/LDRSearchType.java +++ b/group17/785396327/2.26/binarytree/LDRSearchType.java @@ -1,15 +1,15 @@ -package binarytree; - -/** - * Created by william on 2017/2/18. - */ -public class LDRSearchType implements SearchType { - @Override - public void printByType(BinaryTree.Node root) { - if (root != null) { - printByType(root.getLeft()); - System.out.print(root.getData() + " "); - printByType(root.getRight()); - } - } -} +package binarytree; + +/** + * Created by william on 2017/2/18. + */ +public class LDRSearchType implements SearchType { + @Override + public void printByType(BinaryTree.Node root) { + if (root != null) { + printByType(root.getLeft()); + System.out.print(root.getData() + " "); + printByType(root.getRight()); + } + } +} diff --git a/group17/785396327/binarytree/LFSearchType.java b/group17/785396327/2.26/binarytree/LFSearchType.java similarity index 87% rename from group17/785396327/binarytree/LFSearchType.java rename to group17/785396327/2.26/binarytree/LFSearchType.java index e88e76b210..5f908664c9 100644 --- a/group17/785396327/binarytree/LFSearchType.java +++ b/group17/785396327/2.26/binarytree/LFSearchType.java @@ -1,27 +1,27 @@ -package binarytree; - -import java.util.LinkedList; - -/** - * Created by william on 2017/2/18. - */ -public class LFSearchType implements SearchType { - private LinkedList queue = new LinkedList<>(); - - @Override - public void printByType(BinaryTree.Node root) { - if (root == null) - return; - queue.offer(root); - while (!queue.isEmpty()) { - BinaryTree.Node curNode = queue.poll(); - System.out.print(curNode.getData() + " "); - if (curNode.getLeft() != null) - queue.offer(curNode.getLeft()); - if (curNode.getRight() != null) - queue.offer(curNode.getRight()); - } - - } - -} +package binarytree; + +import java.util.LinkedList; + +/** + * Created by william on 2017/2/18. + */ +public class LFSearchType implements SearchType { + private LinkedList queue = new LinkedList(); + + @Override + public void printByType(BinaryTree.Node root) { + if (root == null) + return; + queue.offer(root); + while (!queue.isEmpty()) { + BinaryTree.Node curNode = queue.poll(); + System.out.print(curNode.getData() + " "); + if (curNode.getLeft() != null) + queue.offer(curNode.getLeft()); + if (curNode.getRight() != null) + queue.offer(curNode.getRight()); + } + + } + +} diff --git a/group17/785396327/binarytree/LRDSearchType.java b/group17/785396327/2.26/binarytree/LRDSearchType.java similarity index 96% rename from group17/785396327/binarytree/LRDSearchType.java rename to group17/785396327/2.26/binarytree/LRDSearchType.java index dbd5f1e820..250645e6df 100644 --- a/group17/785396327/binarytree/LRDSearchType.java +++ b/group17/785396327/2.26/binarytree/LRDSearchType.java @@ -1,15 +1,15 @@ -package binarytree; - -/** - * Created by william on 2017/2/18. - */ -public class LRDSearchType implements SearchType { - @Override - public void printByType(BinaryTree.Node root) { - if (root != null) { - printByType(root.getLeft()); - printByType(root.getRight()); - System.out.print(root.getData() + " "); - } - } -} +package binarytree; + +/** + * Created by william on 2017/2/18. + */ +public class LRDSearchType implements SearchType { + @Override + public void printByType(BinaryTree.Node root) { + if (root != null) { + printByType(root.getLeft()); + printByType(root.getRight()); + System.out.print(root.getData() + " "); + } + } +} diff --git a/group17/785396327/binarytree/SearchType.java b/group17/785396327/2.26/binarytree/SearchType.java similarity index 93% rename from group17/785396327/binarytree/SearchType.java rename to group17/785396327/2.26/binarytree/SearchType.java index 78f43d0c2d..606124a781 100644 --- a/group17/785396327/binarytree/SearchType.java +++ b/group17/785396327/2.26/binarytree/SearchType.java @@ -1,9 +1,9 @@ -package binarytree; - -/** - * Created by william on 2017/2/18. - */ -public interface SearchType { - - void printByType(T root); -} +package binarytree; + +/** + * Created by william on 2017/2/18. + */ +public interface SearchType { + + void printByType(T root); +} diff --git a/group17/785396327/list/ArrayList.java b/group17/785396327/2.26/list/ArrayList.java similarity index 71% rename from group17/785396327/list/ArrayList.java rename to group17/785396327/2.26/list/ArrayList.java index f4e5f26f40..119079f7ce 100644 --- a/group17/785396327/list/ArrayList.java +++ b/group17/785396327/2.26/list/ArrayList.java @@ -1,135 +1,153 @@ -package list; - -import java.util.Arrays; -import java.util.NoSuchElementException; - -/** - * Created by william on 2017/2/25. - */ -public class ArrayList implements List { - private static final int DEFAULT_CAPACITY = 10; - private int size; - private Object[] elementData; - - public ArrayList() { - elementData = new Object[DEFAULT_CAPACITY]; - } - - public ArrayList(int initialCapacity) { - if (initialCapacity < 0) - throw new RuntimeException("闈炴硶鍒濆鍖栧ぇ灏忓弬鏁!"); - elementData = new Object[initialCapacity]; - } - - public boolean add(T ele) { - grow(); - elementData[size] = ele; - size++; - return true; - } - - public T get(int index) { - checkBounds(index); - return (T) elementData[index]; - } - - public T remove(int index) { - checkBounds(index); - T removeEle = (T) elementData[index]; - System.arraycopy(elementData, index + 1, elementData, index, size - index); - size--; - return removeEle; - } - - public boolean add(int index, T ele) { - checkBounds(index); - size++;//鏈夋晥鍏冪礌鍐呭鍏堝姞锛屼繚璇侀暱搴︽瀬闄愭儏鍐礸row鍦ㄦ坊鍔犲墠鐢熸晥 - grow(); - //灏嗗師鏈暟缁勪粠寰呮彃鍏ョ殑index鎴彇锛屽皢鍘熸湰index鍚庣殑鏈夋晥鍊硷紝澶嶅埗鍒板師鏈暟缁刬ndex+1涔嬪悗 - System.arraycopy(elementData, index, elementData, index + 1, size - index); - elementData[index] = ele; - return true; - } - - @Override - public boolean remove(T ele) { - int index; - if ((index = indexOf(ele)) == -1) - return false; - remove(index); - return true; - } - - private void checkBounds(int index) { - if (index < 0 || index >= size) - throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); - } - - public int size() { - return size; - } - - private void grow() { - if (size >= elementData.length) { - int curLen = elementData.length; - int newLen = curLen + (curLen >> 1) > Integer.MAX_VALUE ? Integer.MAX_VALUE : curLen + (curLen >> 1); - elementData = Arrays.copyOf(elementData, newLen); - } - } - - public boolean isEmpty() { - return size == 0; - } - - @Override - public boolean contains(T ele) { - return indexOf(ele) != -1; - } - - public int indexOf(T ele) { - for (int i = 0; i < size; i++) { - if (ele == null) - if (null == elementData[i]) - return i; - else if (ele.equals(elementData[i])) - return i; - } - return -1; - } - - public Iterator iterator() { - return new Itr(); - } - - private class Itr implements Iterator { - int cursor;//寰呴亶鍘嗗厓绱犵殑涓嬫爣 - - @Override - public boolean hasNext() { - return cursor != size; - } - - @Override - public T next() { - if (cursor >= size) - throw new NoSuchElementException(); - return (T) elementData[cursor++]; - } - - @Override - public void remove() { - if (cursor >= size) - throw new NoSuchElementException(); - ArrayList.this.remove(cursor--); - } - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder("[ "); - for (Object ele : elementData) { - sb.append(ele).append(" "); - } - return sb.append("]").toString(); - } -} +package list; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + * Created by william on 2017/2/25. + */ +public class ArrayList implements List { + private static final int DEFAULT_CAPACITY = 10; + private int size; + private Object[] elementData; + + public ArrayList() { + elementData = new Object[DEFAULT_CAPACITY]; + } + + public ArrayList(int initialCapacity) { + if (initialCapacity < 0) + throw new RuntimeException("闈炴硶鍒濆鍖栧ぇ灏忓弬鏁!"); + elementData = new Object[initialCapacity]; + } + + public ArrayList(T[] array) { + if (array == null) + throw new NullPointerException(); + elementData = array; + } + + public boolean add(T ele) { + grow(size); + elementData[size++] = ele; + return true; + } + + public T get(int index) { + checkBounds(index); + return (T) elementData[index]; + } + + public T remove(int index) { + checkBounds(index); + T removeEle = (T) elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index); + size--; + return removeEle; + } + + public boolean add(int index, T ele) { + checkBounds(index); + grow(size++); + //灏嗗師鏈暟缁勪粠寰呮彃鍏ョ殑index鎴彇锛屽皢鍘熸湰index鍚庣殑鏈夋晥鍊硷紝澶嶅埗鍒板師鏈暟缁刬ndex+1涔嬪悗 + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = ele; + return true; + } + + + @Override + public boolean remove(T ele) { + int index; + if ((index = indexOf(ele)) == -1) + return false; + remove(index); + return true; + } + + private void checkBounds(int index) { + if (index < 0 || index >= size) + throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); + } + + public int size() { + return size; + } + + private void grow(int miniCapacity) { + if (miniCapacity >= elementData.length) { + int oldCapacity = elementData.length; + int newCapacity = oldCapacity + (oldCapacity >> 1) > Integer.MAX_VALUE ? Integer.MAX_VALUE : oldCapacity + (oldCapacity >> 1); + if (newCapacity - miniCapacity < 0) + newCapacity = miniCapacity > Integer.MAX_VALUE ? Integer.MAX_VALUE : miniCapacity; + elementData = Arrays.copyOf(elementData, newCapacity); + } + } + + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean contains(T ele) { + return indexOf(ele) != -1; + } + + public int indexOf(T ele) { + for (int i = 0; i < size; i++) { + if ((ele == null && elementData[i] == null) || (ele.equals(elementData[i]))) + return i; + } + return -1; + } + + @Override + public boolean addAll(List l) { + Object[] eles = l.toArray(); + grow(eles.length + size); + System.arraycopy(eles, 0, elementData, size, eles.length); + return true; + } + + @Override + public Object[] toArray() { + return Arrays.copyOf(elementData, size); + } + + + public Iterator iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + int cursor;//寰呴亶鍘嗗厓绱犵殑涓嬫爣 + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public T next() { + if (cursor >= size) + throw new NoSuchElementException(); + return (T) elementData[cursor++]; + } + + @Override + public void remove() { + if (cursor >= size) + throw new NoSuchElementException(); + ArrayList.this.remove(cursor--); + } + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("[ "); + for (Object ele : elementData) { + sb.append(ele).append(" "); + } + return sb.append("]").toString(); + } +} diff --git a/group17/785396327/list/Iterator.java b/group17/785396327/2.26/list/Iterator.java similarity index 91% rename from group17/785396327/list/Iterator.java rename to group17/785396327/2.26/list/Iterator.java index 382dbf0c84..0df87c6cf1 100644 --- a/group17/785396327/list/Iterator.java +++ b/group17/785396327/2.26/list/Iterator.java @@ -1,13 +1,13 @@ -package list; - -/** - * Created by IBM on 2017/2/25. - */ -public interface Iterator { - - boolean hasNext(); - - T next(); - - void remove(); -} +package list; + +/** + * Created by IBM on 2017/2/25. + */ +public interface Iterator { + + boolean hasNext(); + + T next(); + + void remove(); +} diff --git a/group17/785396327/list/LinkedList.java b/group17/785396327/2.26/list/LinkedList.java similarity index 96% rename from group17/785396327/list/LinkedList.java rename to group17/785396327/2.26/list/LinkedList.java index c0cbaf7670..a55f723ecb 100644 --- a/group17/785396327/list/LinkedList.java +++ b/group17/785396327/2.26/list/LinkedList.java @@ -1,163 +1,173 @@ -package list; - -/** - * Created by william on 2017/2/25. - */ -public class LinkedList implements List { - private int size; - private Node first; - private Node last; - - private static class Node { - Node next; - Node prev; - T data; - - Node(Node prev, Node next, T data) { - this.prev = prev; - this.next = next; - this.data = data; - } - } - - @Override - public int size() { - return this.size; - } - - @Override - public boolean isEmpty() { - return size == 0; - } - - @Override - public boolean contains(T ele) { - Node head = first; - while (head != null) { - if ((ele == null && head.data == null) || (ele.equals(head.data))) - return true; - head = head.next; - } - return false; - } - - @Override - public boolean add(T ele) { - if (first == null) - first = last = new Node(null, null, ele); - else { - //鏂版坊鍔犺妭鐐圭殑涓婁竴涓妭鐐规槸鍘熸潵閾捐〃鐨勬渶鍚庝竴涓妭鐐 - Node addNode = new Node(last, null, ele); - //鍘熸潵閾捐〃鐨勬渶鍚庝竴涓妭鐐圭殑涓嬩竴涓妭鐐归渶瑕佹寚鍚戞柊娣诲姞鐨勮妭鐐 - last.next = addNode; - //鏇存柊鏈鍚庝竴涓妭鐐逛负鏂版坊鍔犵殑鑺傜偣 - last = addNode; - } - size++; - return true; - } - - @Override - public boolean add(int index, T ele) { - checkBounds(index, true); - if (index == size) add(ele); - else { - Node head = first; - for (int i = 0; i < size; i++) { - if (i == index - 1)//寰楀埌瑕佹彃鍏ヤ綅缃殑鍓嶄竴涓妭鐐 - head.next = new Node(head, head.next, ele); - else - head = head.next; - } - } - size++; - return true; - } - - @Override - public boolean remove(T ele) { - if (!contains(ele)) - return false; - Node head = first; - Node prev = head.prev; - while (head != null) { - if ((ele == null && ele == head.data) || ele.equals(head.data)) { - prev.next = head.next; - size--; - return true; - } - prev = head; - head = head.next; - } - return false; - } - - @Override - public T remove(int index) { - checkBounds(index, false); - T removeEle = get(index); - remove(removeEle); - return removeEle; - } - - @Override - public T get(int index) { - checkBounds(index, false); - if (index > (size >> 1)) { - //绱㈠紩浣嶇疆澶т簬1/2size锛屼粠鍚庡線鍓 - Node tail = last; - for (int i = size - 1; i >= 0; i--) { - if (i == index) - return (T) tail.data; - else - tail = tail.prev; - } - } else { - //浠庡墠寰鍚 - Node head = first; - for (int i = 0; i < size; i++) { - if (i == index) - return (T) head.data; - else - head = head.next; - } - } - return null; - } - - @Override - public int indexOf(T ele) { - if (first == null) return -1; - Node head = first; - for (int i = 0; i < size; i++) { - if ((ele == null && ele == head.data) || ele.equals(head.data)) - return i; - head = head.next; - } - return -1; - } - - /** - * 鎸囧畾浣嶇疆鏌ユ壘鍏冪礌鍜屾彃鍏ュ厓绱犲埌鎸囧畾浣嶇疆IndexOutofBounds鐨勫垽鏂爣鍑嗕笉涓鏍 - * - * @param index - * @param isInsert - */ - private void checkBounds(int index, boolean isInsert) { - if (isInsert && (index < 0 || index > size))//鍏佽鎻掑叆鍒版渶鍚庝竴涓厓绱犱箣鍚庯紝涓嶈兘鎺掗櫎= - throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); - if (index < 0 || index >= size)//鏌ヨ浠0 --- size-1 - throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder("[ "); - Node head = first; - while (head != null) { - sb.append(head.data + " "); - head = head.next; - } - return sb.append("]").toString(); - } -} +package list; + +/** + * Created by william on 2017/2/25. + */ +public class LinkedList implements List { + private int size; + private Node first; + private Node last; + + private static class Node { + Node next; + Node prev; + T data; + + Node(Node prev, Node next, T data) { + this.prev = prev; + this.next = next; + this.data = data; + } + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean contains(T ele) { + Node head = first; + while (head != null) { + if ((ele == null && head.data == null) || (ele.equals(head.data))) + return true; + head = head.next; + } + return false; + } + + @Override + public boolean add(T ele) { + if (first == null) + first = last = new Node(null, null, ele); + else { + //鏂版坊鍔犺妭鐐圭殑涓婁竴涓妭鐐规槸鍘熸潵閾捐〃鐨勬渶鍚庝竴涓妭鐐 + Node addNode = new Node(last, null, ele); + //鍘熸潵閾捐〃鐨勬渶鍚庝竴涓妭鐐圭殑涓嬩竴涓妭鐐归渶瑕佹寚鍚戞柊娣诲姞鐨勮妭鐐 + last.next = addNode; + //鏇存柊鏈鍚庝竴涓妭鐐逛负鏂版坊鍔犵殑鑺傜偣 + last = addNode; + } + size++; + return true; + } + + @Override + public boolean add(int index, T ele) { + checkBounds(index, true); + if (index == size) add(ele); + else { + Node head = first; + for (int i = 0; i < size; i++) { + if (i == index - 1)//寰楀埌瑕佹彃鍏ヤ綅缃殑鍓嶄竴涓妭鐐 + head.next = new Node(head, head.next, ele); + else + head = head.next; + } + } + size++; + return true; + } + + @Override + public boolean remove(T ele) { + if (!contains(ele)) + return false; + Node head = first; + Node prev = head.prev; + while (head != null) { + if ((ele == null && ele == head.data) || ele.equals(head.data)) { + prev.next = head.next; + size--; + return true; + } + prev = head; + head = head.next; + } + return false; + } + + @Override + public T remove(int index) { + checkBounds(index, false); + T removeEle = get(index); + remove(removeEle); + return removeEle; + } + + @Override + public T get(int index) { + checkBounds(index, false); + if (index > (size >> 1)) { + //绱㈠紩浣嶇疆澶т簬1/2size锛屼粠鍚庡線鍓 + Node tail = last; + for (int i = size - 1; i >= 0; i--) { + if (i == index) + return (T) tail.data; + else + tail = tail.prev; + } + } else { + //浠庡墠寰鍚 + Node head = first; + for (int i = 0; i < size; i++) { + if (i == index) + return (T) head.data; + else + head = head.next; + } + } + return null; + } + + @Override + public int indexOf(T ele) { + if (first == null) return -1; + Node head = first; + for (int i = 0; i < size; i++) { + if ((ele == null && ele == head.data) || ele.equals(head.data)) + return i; + head = head.next; + } + return -1; + } + + @Override + public boolean addAll(List l) { + return false; + } + + @Override + public Object[] toArray() { + return new Object[0]; + } + + /** + * 鎸囧畾浣嶇疆鏌ユ壘鍏冪礌鍜屾彃鍏ュ厓绱犲埌鎸囧畾浣嶇疆IndexOutofBounds鐨勫垽鏂爣鍑嗕笉涓鏍 + * + * @param index + * @param isInsert + */ + private void checkBounds(int index, boolean isInsert) { + if (isInsert && (index < 0 || index > size))//鍏佽鎻掑叆鍒版渶鍚庝竴涓厓绱犱箣鍚庯紝涓嶈兘鎺掗櫎= + throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); + if (index < 0 || index >= size)//鏌ヨ浠0 --- size-1 + throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("[ "); + Node head = first; + while (head != null) { + sb.append(head.data + " "); + head = head.next; + } + return sb.append("]").toString(); + } +} diff --git a/group17/785396327/list/List.java b/group17/785396327/2.26/list/List.java similarity index 84% rename from group17/785396327/list/List.java rename to group17/785396327/2.26/list/List.java index 54fb72108d..e03b4cfa7c 100644 --- a/group17/785396327/list/List.java +++ b/group17/785396327/2.26/list/List.java @@ -1,25 +1,30 @@ -package list; - -/** - * Created by william on 2017/2/25. - */ -public interface List { - - int size(); - - boolean isEmpty(); - - boolean contains(T ele); - - boolean add(T ele); - - boolean add(int index, T ele); - - boolean remove(T ele); - - T remove(int index); - - T get(int index); - - int indexOf(T ele); -} +package list; + +/** + * Created by william on 2017/2/25. + */ +public interface List { + + int size(); + + boolean isEmpty(); + + boolean contains(T ele); + + boolean add(T ele); + + boolean add(int index, T ele); + + boolean remove(T ele); + + T remove(int index); + + T get(int index); + + int indexOf(T ele); + + boolean addAll(List l); + + Object[] toArray(); + +} diff --git a/group17/785396327/queue/Queue.java b/group17/785396327/2.26/queue/Queue.java similarity index 95% rename from group17/785396327/queue/Queue.java rename to group17/785396327/2.26/queue/Queue.java index 7e399961ed..62404a951e 100644 --- a/group17/785396327/queue/Queue.java +++ b/group17/785396327/2.26/queue/Queue.java @@ -1,41 +1,43 @@ -package queue; - -import java.util.NoSuchElementException; - -/** - * Created by william on 2017/2/25. - */ -public class Queue extends LinkedList { - - public boolean add(T ele) { - return add(ele); - } - - public T element() { - if (size() == 0) - throw new NoSuchElementException("闃熷垪涓病鏈夊厓绱狅紒"); - return get(0); - } - - public boolean offer(T ele) { - return add(ele); - } - - public T peek() { - if (size() == 0) - return null; - return get(0); - } - - public T poll() { - if (size() == 0) - return null; - return remove(0); - } - - public T remove() { - if (size() == 0) - throw new NoSuchElementException("闃熷垪涓病鏈夊厓绱狅紒"); - return remove(0); - } -} +package queue; + +import list.LinkedList; + +import java.util.NoSuchElementException; + +/** + * Created by william on 2017/2/25. + */ +public class Queue extends LinkedList { + + public boolean add(T ele) { + return add(ele); + } + + public T element() { + if (size() == 0) + throw new NoSuchElementException("闃熷垪涓病鏈夊厓绱狅紒"); + return get(0); + } + + public boolean offer(T ele) { + return add(ele); + } + + public T peek() { + if (size() == 0) + return null; + return get(0); + } + + public T poll() { + if (size() == 0) + return null; + return remove(0); + } + + public T remove() { + if (size() == 0) + throw new NoSuchElementException("闃熷垪涓病鏈夊厓绱狅紒"); + return remove(0); + } +} diff --git a/group17/785396327/stack/Stack.java b/group17/785396327/2.26/stack/Stack.java similarity index 94% rename from group17/785396327/stack/Stack.java rename to group17/785396327/2.26/stack/Stack.java index 980ca2c0e6..58efd9c0c3 100644 --- a/group17/785396327/stack/Stack.java +++ b/group17/785396327/2.26/stack/Stack.java @@ -1,29 +1,31 @@ -package stack; - -import java.util.EmptyStackException; - -/** - * Created by william on 2017/2/25. - */ -public class Stack extends ArrayList { - - public boolean empty() { - return isEmpty(); - } - - public T peek() { - if (size() == 0) - throw new EmptyStackException(); - return (T) get(0); - } - - public T pop() { - if (size() == 0) - throw new EmptyStackException(); - return (T) remove(0); - } - - public void push(T ele) { - add(0, ele); - } -} +package stack; + +import list.ArrayList; + +import java.util.EmptyStackException; + +/** + * Created by william on 2017/2/25. + */ +public class Stack extends ArrayList { + + public boolean empty() { + return isEmpty(); + } + + public T peek() { + if (size() == 0) + throw new EmptyStackException(); + return (T) get(0); + } + + public T pop() { + if (size() == 0) + throw new EmptyStackException(); + return (T) remove(0); + } + + public void push(T ele) { + add(0, ele); + } +} diff --git a/group17/785396327/3.5/array/ArrayUtils.java b/group17/785396327/3.5/array/ArrayUtils.java new file mode 100644 index 0000000000..c98aec55dd --- /dev/null +++ b/group17/785396327/3.5/array/ArrayUtils.java @@ -0,0 +1,112 @@ +package array; + +import list.ArrayList; +import list.List; + +import java.util.Arrays; +import java.util.InputMismatchException; +import java.util.NoSuchElementException; + + +/** + * Created by william on 2017/2/27. + */ +public class ArrayUtils { + + public static void reserveArray(int[] src) { + int begin = 0; + int end = src.length - 1; + while (begin < end) { + swap(src, begin++, end--); + } + } + + public static int[] removeZero(int[] oldArray) { + List newResult = new ArrayList(); + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) + newResult.add(oldArray[i]); + } + + return toIntArray(newResult); + } + + public static int[] merge(int[] array1, int[] array2) { + int[] temp = new int[array1.length + array2.length]; + System.arraycopy(array1, 0, temp, 0, array1.length); + System.arraycopy(array2, 0, temp, array1.length, array2.length); + Arrays.sort(temp); + List result = new ArrayList(); + for (int ele : temp) { + if (!result.contains(ele)) + result.add(ele); + } + return toIntArray(result); + } + + public static int[] grow(int[] oldArray, int size) { + if (size <= 0) + throw new NoSuchElementException(); + int[] result = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, result, 0, oldArray.length); + return result; + } + + public static int[] fibonacci(int max) { + if (max <= 1) + return new int[0]; + List fList = new ArrayList(); + fList.add(1); + fList.add(1); + int last = fList.size() - 1; + while (fList.get(last) < max) { + fList.add(fList.get(last) + fList.get(last - 1)); + last++; + } + return toIntArray(fList); + } + + public static int[] getPrimes(int max) { + List result = new ArrayList(); + for (int i = 0; i < max; i++) { + if (i % 2 == 1) + result.add(i); + } + return toIntArray(result); + } + + public static 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 + 1; j++) { + if (i % j == 0) + sum += j; + } + if (i == sum) + result.add(i); + } + return toIntArray(result); + } + + private static int[] toIntArray(List src) { + int[] result = new int[src.size()]; + for (int i = 0; i < src.size(); i++) { + result[i] = src.get(i); + } + return result; + } + + public static String join(int[] array, String seperator) { + String value = Arrays.toString(array).replaceAll(", ", seperator == null ? "-" : seperator); + return value.substring(1, value.length() - 1); + } + + + private static void swap(int[] array, int i, int j) { + array[i] ^= array[j]; + array[j] ^= array[i]; + array[i] ^= array[j]; + } + +} diff --git a/group17/785396327/3.5/array/ArrayUtilsTest.java b/group17/785396327/3.5/array/ArrayUtilsTest.java new file mode 100644 index 0000000000..276e840b44 --- /dev/null +++ b/group17/785396327/3.5/array/ArrayUtilsTest.java @@ -0,0 +1,86 @@ +package array; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Random; + +/** + * Created by william on 2017/2/27. + */ +public class ArrayUtilsTest { + int[] array; + private static final int RANGE = 9; + private static final int BOUNDS = 10; + private static Random random = new Random(); + + + @Before + public void setUp() { + array = randomArray(RANGE); + } + + private int[] randomArray(int range) { + int[] array = new int[range]; + for (int i = 0; i < range; i++) { + array[i] = random.nextInt(BOUNDS); + } + return array; + } + + @Test + public void reverseArrayTest() { + System.out.println(Arrays.toString(array)); + ArrayUtils.reserveArray(array); + System.out.println(Arrays.toString(array)); + } + + @Test + public void removeZeroTest() { + System.out.println(Arrays.toString(array)); + int[] newArray = ArrayUtils.removeZero(array); + System.out.println(Arrays.toString(newArray)); + } + + @Test + public void mergeTest() { + System.out.println(Arrays.toString(array)); + int[] array2 = {2, 5, 1, 6, 8}; + int[] merge = ArrayUtils.merge(array, array2); + System.out.println(Arrays.toString(merge)); + } + + @Test + public void growTest() { + System.out.println(Arrays.toString(array)); + int[] result = ArrayUtils.grow(array, 4); + System.out.println(Arrays.toString(result)); + } + + @Test + public void getPrimesTest() { + int[] primes = ArrayUtils.getPrimes(54); + System.out.println(Arrays.toString(primes)); + } + + @Test + public void getPerfectNumbersTest() { + int[] perfectNumbers = ArrayUtils.getPerfectNumbers(100000); + System.out.println(Arrays.toString(perfectNumbers)); + } + + @Test + public void joinTest() { + String value = ArrayUtils.join(array, "-"); + System.out.println(value); + } + + @Test + public void fibonacciTest() { + System.out.println(Arrays.toString(ArrayUtils.fibonacci(34))); + } + +} diff --git a/group17/785396327/3.5/struts/LoginAction.java b/group17/785396327/3.5/struts/LoginAction.java new file mode 100644 index 0000000000..3fc624ff90 --- /dev/null +++ b/group17/785396327/3.5/struts/LoginAction.java @@ -0,0 +1,39 @@ +package struts; + +/** + * Created by IBM on 2017/3/4. + */ +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/group17/785396327/3.5/struts/LoginActionTest.java b/group17/785396327/3.5/struts/LoginActionTest.java new file mode 100644 index 0000000000..a2ce2379a5 --- /dev/null +++ b/group17/785396327/3.5/struts/LoginActionTest.java @@ -0,0 +1,36 @@ +package struts; + +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.Assert; +import org.junit.Test; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by IBM on 2017/3/4. + */ +public class LoginActionTest { + + @Test + public void fun1() throws DocumentException { + InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("struts/struts.xml"); + SAXReader reader = new SAXReader(); + Element rootElement = reader.read(resourceAsStream).getRootElement(); + Element selectedElement = (Element) rootElement.selectSingleNode("//action[@name='login']"); + System.out.println(selectedElement.attributeValue("class")); + + } + + @Test + public void fun2() { + Map parameters = new HashMap(); + parameters.put("name", "test"); + parameters.put("password", "1234"); + View view = Struts.runAction("login", parameters); + Assert.assertEquals("/jsp/homepage.jsp", view.getJsp()); + } +} diff --git a/group17/785396327/3.5/struts/Struts.java b/group17/785396327/3.5/struts/Struts.java new file mode 100644 index 0000000000..609c493852 --- /dev/null +++ b/group17/785396327/3.5/struts/Struts.java @@ -0,0 +1,88 @@ +package struts; + +import org.dom4j.Element; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by william on 2017/3/4. + */ +public class Struts { + + + public static View runAction(String actionName, Map parameters) { + Element root = StrutsUtils.getRoot("struts/struts.xml"); + View view = new View(); + if (root != null) { + Element selectedEle = (Element) root.selectSingleNode("//action[@name='" + actionName + "']"); + if (selectedEle != null) { + + Class clazz = genClass(selectedEle.attributeValue("class")); + Object target = setValue(parameters, clazz); + + String result; + try { + result = (String) clazz.getMethod("execute").invoke(target); + } catch (Exception e) { + throw new RuntimeException("invoke execute have some error", e); + } + + Map response = getValue(clazz, target); + view.setParameters(response); + Element selectedResult = (Element) root.selectSingleNode("//action[@name='" + actionName + "']//result[@name='" + result + "']"); + view.setJsp(selectedResult == null ? null : selectedResult.getText()); + } + } + return view; + } + + + private static Class genClass(String className) { + Class clazz = null; + try { + clazz = Class.forName(className); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return clazz; + } + + private static Object setValue(Map parameters, Class clazz) { + try { + Object target = clazz.newInstance(); + if (!StrutsUtils.isEmpty(parameters)) { + for (Map.Entry entry : parameters.entrySet()) { + String key = entry.getKey(); + if (!StrutsUtils.isEmpty(key)) { + String setterName = new StringBuilder("set").append(key.substring(0, 1).toUpperCase()).append(key.substring(1)).toString(); + clazz.getMethod(setterName, String.class).invoke(target, entry.getValue()); + } + } + } + return target; + } catch (Exception e) { + throw new RuntimeException("create class instance have some error ", e); + } + } + + private static Map getValue(Class clazz, Object target) { + Map resultsMap = new HashMap(); + Method[] methods = clazz.getMethods(); + for (Method method : methods) { + String fieldName = method.getName(); + if (fieldName.startsWith("get") && !fieldName.equals("getClass")) { + try { + Object value = method.invoke(target); + resultsMap.put(new StringBuilder(fieldName.substring(3, 4)).append(fieldName.substring(4)).toString(), value); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + return resultsMap; + } + + +} diff --git a/group17/785396327/3.5/struts/StrutsUtils.java b/group17/785396327/3.5/struts/StrutsUtils.java new file mode 100644 index 0000000000..dfe5fe614f --- /dev/null +++ b/group17/785396327/3.5/struts/StrutsUtils.java @@ -0,0 +1,30 @@ +package struts; + +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +import java.io.InputStream; + +/** + * Created by IBM on 2017/3/4. + */ +public class StrutsUtils { + + public static Element getRoot(String filePath) { + try { + InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath); + SAXReader saxReader = new SAXReader(); + return saxReader.read(resourceAsStream).getRootElement(); + } catch (DocumentException e) { + throw new RuntimeException("鍒濆鍖栨枃浠跺紓甯", e); + } + } + + public static boolean isEmpty(Object obj) { + if (obj instanceof String) + return obj == null || obj.equals(""); + else + return obj == null; + } +} diff --git a/group17/785396327/3.5/struts/View.java b/group17/785396327/3.5/struts/View.java new file mode 100644 index 0000000000..a02a5d1216 --- /dev/null +++ b/group17/785396327/3.5/struts/View.java @@ -0,0 +1,26 @@ +package struts; + +import java.util.Map; + +/** + * Created by IBM on 2017/3/4. + */ +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/group17/785396327/3.5/struts/struts.xml b/group17/785396327/3.5/struts/struts.xml new file mode 100644 index 0000000000..0c69b730f3 --- /dev/null +++ b/group17/785396327/3.5/struts/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/group17/82427129/.gitignore b/group17/82427129/.gitignore index ccfa635638..2d0da18be3 100644 --- a/group17/82427129/.gitignore +++ b/group17/82427129/.gitignore @@ -1,7 +1,16 @@ + /.metadata/ -/RemoteSystemsTempFiles/ + +/RemoteSystemsTempFiles/ + + + +/JavaUtil/.settings/ + +/JavaUtil/target/ + -/JavaUtil/.settings/ .classpath + .project diff --git a/group17/82427129/JavaUtil/.gitignore b/group17/82427129/JavaUtil/.gitignore deleted file mode 100644 index 24d64373c4..0000000000 --- a/group17/82427129/JavaUtil/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/group17/82427129/JavaUtil/pom.xml b/group17/82427129/JavaUtil/pom.xml index 6c95dbb077..fb857bcd00 100644 --- a/group17/82427129/JavaUtil/pom.xml +++ b/group17/82427129/JavaUtil/pom.xml @@ -23,5 +23,10 @@ 4.7 test + + dom4j + dom4j + 1.6.1 + \ No newline at end of file diff --git a/group17/82427129/JavaUtil/src/main/java/com/coderising/array/ArrayUtil.java b/group17/82427129/JavaUtil/src/main/java/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..00ec1757ba --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/java/com/coderising/array/ArrayUtil.java @@ -0,0 +1,343 @@ +package com.coderising.array; + +import java.util.Arrays; + +import com.coding.basic.ArrayList; +import com.coding.basic.List; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static int[] reverseArray(int[] origin){ + int length = origin.length; + if(length <= 1) + return origin; + + int[] newArray = new int[length]; + for (int i = 0; i < length; i++) { + newArray[i] = origin[length-i-1]; + } + return newArray; + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 static int[] removeZero(int[] oldArray){ + int length = oldArray.length; + if(length == 0) + return oldArray; + + int[] newArray = new int[length]; + int oi = 0; + int ni = 0; + while(oi < length){ + if(oldArray[oi] == 0){ + oi++; + }else{ + newArray[ni++] = oldArray[oi++]; + } + } + if(ni == 0) + return null;//杈撳叆鐨勬暟缁勫叏閮ㄩ兘鏄0鏃 + if(ni == length) + return newArray;//杈撳叆鐨勬暟缁勪笉鍚0鏃讹紝涓嶉渶瑕佹埅鍙 + + return Arrays.copyOf(newArray, ni); + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2){//鏃堕棿澶嶆潅搴(n) + int length1 = array1.length; + int length2 = array2.length; + if(length1 == 0 ||(length1 == 0 && length2 ==0)){ + return array2; + } + if(length2 == 0){ + return array1; + } + + int i1 = 0;//濮嬬粓鎸囧悜array1涓墿浣欑殑鏈灏忓厓绱 + int i2 = 0;//濮嬬粓鎸囧悜array2涓墿浣欑殑鏈灏忓厓绱 + ArrayList stack = new ArrayList();//鈥滆繃鍘荤殑鈥濇渶灏忓兼斁鍏ユ爤涓 + + while (i1 < length1 || i2 < length2){//鍒ゅ畾锛屽彧瑕佽繕鏈変竴涓暟缁勪腑鏈夋湭鍏ユ爤鍏冪礌 + if(i1 == length1){//鍒ゅ畾锛宎rray1鍏冪礌閮藉凡缁忓叆鏍 + stack.add(array2[i2++]); + continue; + } + if(i2 == length2){//鍒ゅ畾锛宎rray2鍏冪礌閮藉凡缁忓叆鏍 + stack.add(array1[i1++]); + continue; + } + + int comRes = compare(array1[i1], array2[i2]); + if(comRes > 0){ + stack.add(array2[i2++]); + }else if(comRes <0){ + stack.add(array1[i1++]); + }else{ + stack.add(array1[i1]); + i1++; + i2++; + } + } + + int[] result = new int[stack.size()]; + for (int i = 0; i < stack.size(); i++) { + result[i] = (int) stack.get(i); + } + return result; + } + /** + * when i < j returns -1; + * when i = j returns 0; + * when i > j returns 1; + * @param i + * @param j + * @return + */ + private static int compare(int i, int j){ + if(i < j){ + return -1; + }else if(i == j){ + return 0; + }else{ + return 1; + } + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + int[] newArray = new int[oldArray.length+size]; + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if(max <= 1) + return new int[0]; + + ArrayList l = new ArrayList(); + for (int i = 0; max>fibonacciNum(i); i++) { + l.add(fibonacciNum(i)); + } + + int[] result = new int[l.size()]; + for (int i = 0; i < l.size(); i++) { + result[i] = (int) l.get(i); + } + return result; + } + /** + * 绉佹湁鏂规硶 + * 鏍规嵁 鏂愭尝閭e鏁板垪 + * 閫氳繃涓嬫爣鑾峰彇鏁板垪鍏冪礌 + * + * 渚嬪 [1锛1锛2锛3锛5锛8锛13锛...] fibonacciNum(1) = 1,fibonacciNum(4) = 3 + * @param index 涓嬫爣 + * @return 鏂愭尝閭e鏁板垪鍏冪礌 + */ + private static int fibonacciNum(int index){ + if(index < 0 ) + throw new IndexOutOfBoundsException("涓嬫爣瓒婄晫锛宨ndex>=0"); + + if(index == 0||index == 1) + return 1; + return fibonacciNum(index-1)+fibonacciNum(index-2); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){ + if(max<2) + return new int[0]; + + ArrayList list = new ArrayList(); + for (int i = 0; i < max; i++) { + if(isPrimeNum(i)) + list.add(i); + } + + int[] result = new int[list.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = (int) list.get(i); + } + return result; + } + /** + * 鍒ゆ柇涓涓暣鏁版槸鍚︽槸璐ㄦ暟 + * @param num + * @return + */ + private static boolean isPrimeNum(int num){ + if(num < 2)//璐ㄦ暟閮芥槸姝f暟锛屽苟涓斿ぇ浜庣瓑浜2 + return false; + + for (int i = 2; i <= Math.sqrt(num); i++) { + if(num % i ==0){ + return false; + } + } + return true; + + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + ArrayList list = new ArrayList(); + for (int i = 6; i < max; i++) { + if(isPerfectNumber(i)) + list.add(i); + } + + int[] result = new int[list.size()]; + for (int i = 0; i < result.length; i++) { + result[i] = (int) list.get(i); + } + return result; + } + /** + * 鍒ゆ柇涓涓暣鏁版槸鍚︽槸瀹屾暟 + * + * 鍥犱负鎵鏈夌殑瀹屾暟閮芥槸涓夎褰㈡暟锛屾墍鏈夋垜浠氳繃閬嶅巻涓夎褰㈡暟鍒ゆ柇锛屽鏋滄槸涓夎褰㈡暟锛屽啀鍒ゆ柇鏄惁鎵鏈夊洜瀛愮殑绉负杩欎釜鏁存暟 + * @param num + * @return + */ + private static boolean isPerfectNumber(int num){ + if(num < 6)// the first perfect number is 6 + return false; + if(!isEndWith6or8(num))//perfect number is only end with 6 or 8 + return false; + + /*if(!isTriangularNumber(num)) + return false;*/ + + List list = new ArrayList(); + + double sqr = Math.sqrt(num); + list.add(1); + for (int i = 2; i <= sqr; i++) { + if(num % i == 0){ + list.add(num/i); + list.add(i); + } + if(i == sqr){ + list.add(i); + break; + } + } + int sum = 0; + for (int i = 0; i < list.size(); i++) { + Integer in = (Integer) list.get(i); + sum += in; + } + if(sum == num) + return true; + else + return false; + } + + private static boolean isEndWith6or8(int num){ + if(num == 6) + return true; + + Integer integer = new Integer(num); + String string = integer.toString(); + String lastIndex = string.substring(string.length()-1); + if(lastIndex.equals("6") || lastIndex.equals("8")) + return true; + else + return false; + } + private static boolean isTriangularNumber(int num){ + if(num < 1 ) + return false; + + int i = 0; + while(triangularNumber(i)<=num){ + if(triangularNumber(i)==num) + return true; + i++; + } + + return false; + } + /** + * 鑾峰彇涓夎褰㈡暟锛屼粠鏁板垪涓幏鍙栥 + * [1,3,6,10,...] 涓嬫爣浠0寮濮嬶紝渚嬪triangularNumber(0) = 1,triangularNumber(1) = 3,triangularNumber(2)=6 + * @param index + * @return + */ + private static int triangularNumber(int index){ + if(index < 0) + throw new IndexOutOfBoundsException("涓嬫爣瓒婄晫锛宨ndex>=0"); + if(index == 0) + return 1; + return triangularNumber(index-1)+index+1; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + if(array == null) + return null; + if(array.length == 0) + return ""; + + String result = String.valueOf(array[0]); + for (int i = 1; i < array.length; i++) { + result += seperator + array[i]; + } + return result; + } + + +} diff --git a/group17/82427129/JavaUtil/src/main/java/com/coderising/litestruts/LoginAction.java b/group17/82427129/JavaUtil/src/main/java/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..1005f35a29 --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/java/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; + } +} diff --git a/group17/82427129/JavaUtil/src/main/java/com/coderising/litestruts/Struts.java b/group17/82427129/JavaUtil/src/main/java/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..b63cca0b77 --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/java/com/coderising/litestruts/Struts.java @@ -0,0 +1,149 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class Struts { + + private static SAXReader reader = new SAXReader(); + private static Document document; + private static Element root; + private static List actionList; + static{ + try { + document = reader.read(new File("src/main/resources/liteStruts.xml")); + } catch (DocumentException e) { + e.printStackTrace(); + } + root = document.getRootElement(); + actionList = root.elements("action"); + } + + /* + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + public static View runAction(String actionName, Map parameters) { + + if(actionName == null || actionName.equals("")){ + System.out.println("璇锋眰鍚嶄负绌猴紝閿欒锛"); + return null; + } + + View view = new View(); + + String className = null; + try { + Map actionInfo = getActionInfo(actionName); + + className = actionInfo.get("className"); + Class actionClass = Class.forName(className); + Object obj =actionClass.newInstance(); + for(Map.Entry entry : parameters.entrySet()){ + Method setMethod = actionClass.getDeclaredMethod(("set"+toUpperCaseFirstLetter(entry.getKey())), String.class); + setMethod.invoke(obj, entry.getValue()); + } + Method execute = actionClass.getDeclaredMethod("execute"); + String msg = (String) execute.invoke(obj); + view.setJsp(actionInfo.get(msg)); + + Method[] m = actionClass.getMethods(); + Map map = new HashMap(); + for (Method method : m) { + String methodName = method.getName(); + if(methodName.startsWith("get") + && !methodName.endsWith("Class") + && method.getParameterTypes().length == 0){ + map.put(toSubString_LowerCaseFirstLetter(methodName), method.invoke(obj)); + } + } + view.setParameters(map); + + return view; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } + + return null; + } + private static Map getActionInfo(String actionName){ + + HashMap hashMap = new HashMap(); + + for (Element action : actionList) { + Attribute attr = action.attribute("name"); + if(actionName.equals(attr.getStringValue())){ + Attribute attrClass = action.attribute("class"); + String className = attrClass.getStringValue(); + hashMap.put("className", className); + + @SuppressWarnings("unchecked") + List resultList = action.elements("result"); + for (Element result : resultList) { + Attribute attrResult = result.attribute("name"); + hashMap.put(attrResult.getStringValue(), result.getText()); + } + break; + } + } + return hashMap; + } + + /** + * return a String upcased the first letter + * @param param + * @return + */ + private static String toUpperCaseFirstLetter(String param){ + if(param == null || param.length() == 0) + return ""; + if(param.length() == 1) + return param.substring(0, 1).toUpperCase(); + + return param.substring(0, 1).toUpperCase()+param.substring(1); + } + private static String toSubString_LowerCaseFirstLetter(String param){ + if(!param.startsWith("get")) + return null; + int length = param.length(); + if(length < 4) + return ""; + + param = param.substring(3); + return param.substring(0, 1).toLowerCase()+param.substring(1); + } + +} diff --git a/group17/82427129/JavaUtil/src/main/java/com/coderising/litestruts/View.java b/group17/82427129/JavaUtil/src/main/java/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..41b10d8a68 --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/java/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/group17/82427129/JavaUtil/src/main/java/com/coding/basic/ArrayList.java b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/ArrayList.java index d9f3d217f5..2a1a99bc9d 100644 --- a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/ArrayList.java +++ b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/ArrayList.java @@ -2,7 +2,7 @@ import java.util.Arrays; -public class ArrayList implements List { +public class ArrayList implements List { private int size = 0; @@ -35,34 +35,48 @@ public void add(int index, Object o){ } public Object set(int index, Object o){ - rangeCheck(index); + elementIndexCheck(index); Object oldValue = elementData[index]; elementData[index] = o; return oldValue; } - public Object get(int index){ - rangeCheck(index); - return elementData[index]; + public E get(int index){ + elementIndexCheck(index); + return elementData(index); } - public Object remove(int index){ - rangeCheck(index); - Object oldValue = elementData[index]; + @SuppressWarnings("unchecked") + E elementData(int index){ + return (E) elementData[index]; + } + + public E remove(int index){ + elementIndexCheck(index); + E oldValue = elementData(index); int movedLength = size - index - 1; - if(movedLength > 0)//当要删除最后一个元素时,不需要移动数组,只需要把最后一个元素置null + if(movedLength > 0)//褰撹鍒犻櫎鏈鍚庝竴涓厓绱犳椂锛屼笉闇瑕佺Щ鍔ㄦ暟缁勶紝鍙渶瑕佹妸鏈鍚庝竴涓厓绱犵疆null System.arraycopy(elementData, index+1, elementData, index, size-index-1); elementData[--size] = null; return oldValue; } - + /** + * range check, + * permit the range [0,size] + * @param index + */ private void rangeCheckForAdd(int index){ if( index > size || index<0 ){ throw new IndexOutOfBoundsException(outofIndex(index)); } } - private void rangeCheck(int index){ + /** + * element's index check, + * permit the index [0,size) + * @param index + */ + private void elementIndexCheck(int index){ if( index >= size || index < 0){ throw new IndexOutOfBoundsException(outofIndex(index)); } @@ -78,7 +92,7 @@ public Iterator iterator(){ private void ensureCapacity(int minCapacity){ if(elementData == EMPTY_ELEMENTDATA){ - minCapacity = Math.max(minCapacity, INITIALCAPACITY);//针对addall首次增加的数量就比INITIALCAPACITY多 + minCapacity = Math.max(minCapacity, INITIALCAPACITY);//閽堝addall棣栨澧炲姞鐨勬暟閲忓氨姣擨NITIALCAPACITY澶 } if(minCapacity - elementData.length > 0){ grow(minCapacity); diff --git a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/Iterator.java b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/Iterator.java index 06ef6311b2..dbe8b9afb2 100644 --- a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/Iterator.java +++ b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/Iterator.java @@ -1,7 +1,7 @@ -package com.coding.basic; - -public interface Iterator { - public boolean hasNext(); - public Object next(); - -} +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LinkedList.java b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LinkedList.java index 2a63eee0fc..f9934963e7 100644 --- a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LinkedList.java +++ b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/LinkedList.java @@ -2,17 +2,17 @@ import java.util.NoSuchElementException; -public class LinkedList implements List { +public class LinkedList implements List { private int size = 0; - private Node first; + private Node first; - private Node last; + private Node last; - public void add(Object o){ + public void add(E o){ add(size,o); } - public void add(int index , Object o){ + public void add(int index , E o){ rangeCheck(index); if(index == size){ @@ -21,9 +21,9 @@ public void add(int index , Object o){ linkBefore(o, indexOf(index)); } } - private void linkBefore(Object o ,Node succ){ - final Node prev = succ.prev; - final Node newNode = new Node(prev, o, succ); + private void linkBefore(E o ,Node succ){ + final Node prev = succ.prev; + final Node newNode = new Node(prev, o, succ); succ.prev = newNode; if(prev == null){ first = newNode; @@ -32,9 +32,9 @@ private void linkBefore(Object o ,Node succ){ } size++; } - private void linkLast(Object o){ - final Node succ = last; - final Node newNode = new Node(succ, o, null); + private void linkLast(E o){ + final Node succ = last; + final Node newNode = new Node(succ, o, null); last = newNode; if(succ == null){ first = newNode; @@ -43,29 +43,38 @@ private void linkLast(Object o){ } size++; } + /** + * range check, + * permit the range [0,size] + * @param index + */ private void rangeCheck(int index) { if(index > size|| index < 0 ) throw new IndexOutOfBoundsException("Size"+size+":index"+index); } + /** + * element's index check, + * permit the index [0,size) + * @param index + */ private void elementIndexCheck(int index){ if(index >=size||index < 0) throw new IndexOutOfBoundsException("Size"+size+":index"+index); } /** - * 获取“下标”为index的值, - * index为size时返回null + * * @param index * @return */ - private Node indexOf(int index){ + private Node indexOf(int index){ if(index < (this.size>>1) ){ - Node x = first; + Node x = first; for (int i = 0; i < index; i++) { x = x.next; } return x; }else{ - Node x = last; + Node x = last; for (int i = this.size-1; i > index; i--) { x = x.prev; } @@ -73,12 +82,12 @@ private Node indexOf(int index){ } } - public Object get(int index){ + public E get(int index){ elementIndexCheck(index); - return indexOf(index); + return indexOf(index).data; } - public Object remove(int index){ + public E remove(int index){ elementIndexCheck(index); if(index == 0){ @@ -90,10 +99,10 @@ public Object remove(int index){ } } - private Object unlinkNode(Node node) { - final Node next = node.next; - final Node prev = node.prev; - final Object element = node.data; + private E unlinkNode(Node node) { + final Node next = node.next; + final Node prev = node.prev; + final E element = node.data; if(next == null){ last = node; }else{ @@ -115,20 +124,20 @@ public int size(){ return size; } - public void addFirst(Object o){ + public void addFirst(E o){ linkBefore(o, first); } - public void addLast(Object o){ + public void addLast(E o){ linkLast(o); } - public Object removeFirst(){ + public E removeFirst(){ if(first == null) throw new NoSuchElementException("first is null"); - Object oldData = first.data; - final Node next = first.next; + E oldData = first.data; + final Node next = first.next; first.data = null; first.next = null;//GC first = next; @@ -143,12 +152,12 @@ public Object removeFirst(){ return oldData; } - public Object removeLast(){ + public E removeLast(){ if(last == null) throw new NoSuchElementException("last is null"); - Object oldData = last.data; - final Node prev = last.prev; + E oldData = last.data; + final Node prev = last.prev; last.prev = null; last.data = null;//GC last = prev; @@ -167,11 +176,11 @@ public Iterator iterator(){ return null; } - private static class Node{ - Object data; - Node next; - Node prev; - Node(Node prev,Object data,Node next){ + private static class Node{ + E data; + Node next; + Node prev; + Node(Node prev,E data,Node next){ this.data = data; this.next = next; this.prev = prev; diff --git a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/List.java b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/List.java index 10d13b5832..e79d164654 100644 --- a/group17/82427129/JavaUtil/src/main/java/com/coding/basic/List.java +++ b/group17/82427129/JavaUtil/src/main/java/com/coding/basic/List.java @@ -1,9 +1,25 @@ -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(); -} +package com.coding.basic; + +/** + * 浠g爜杩涜浜嗗娆¢噸鏋勶紝鎸夌収JDK婧愮爜鐨勬濊矾杩涜缂栧啓锛
+ *
+ * 閲嶇偣1锛歳angeCheck鍜宔lementIndexCheck 瀹屽叏绠$悊浜嗘墍鏈夌殑Index鐨勬娴嬪伐浣滐紝杩欎竴鐐瑰氨 鐪佸幓浜嗗叾浠栧湴鏂瑰叧浜庝笅鏍囨槸鍚﹁秺鐣岀殑妫娴
+ *
+ * 閲嶇偣2锛歛rrayList鐨別lementData(int i)浠ュ強LinkedList鐨刵ode(int i)鏂规硶閫氳繃閲嶆瀯缁熶竴浜嗛氳繃涓嬫爣鑾峰彇鍏冪礌鐨勬搷浣
+ * + * @author Walker + * + * @param + */ +public interface List { + + void add(E o); + + void add(int index, E o); + + Object get(int index); + + Object remove(int index); + + int size(); +} diff --git a/group17/82427129/JavaUtil/src/main/resources/liteStruts.xml b/group17/82427129/JavaUtil/src/main/resources/liteStruts.xml new file mode 100644 index 0000000000..a4ca47c733 --- /dev/null +++ b/group17/82427129/JavaUtil/src/main/resources/liteStruts.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/group17/82427129/JavaUtil/src/test/java/com/coderising/array/ArrayUtilTest.java b/group17/82427129/JavaUtil/src/test/java/com/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..4401629b5d --- /dev/null +++ b/group17/82427129/JavaUtil/src/test/java/com/coderising/array/ArrayUtilTest.java @@ -0,0 +1,80 @@ +package com.coderising.array; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ArrayUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testReverseArray() { + int[] a = {7, 9 , 30, 3}; + int[] ra = {3, 30, 9,7}; + int[] reverseArray = ArrayUtil.reverseArray(a); + Assert.assertArrayEquals(ra, reverseArray); + } + + @Test + public void testRemoveZero() { + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int newArr[]={1,3,4,5,6,6,5,4,7,6,7,5}; + Assert.assertArrayEquals(newArr, ArrayUtil.removeZero(oldArr)); + } + + @Test + public void testMerge() { + int[] a1 = {3,5,7,8}; + int[] a2 = {4,5,6,7}; + int[] a3 = {3,4,5,6,7,8}; + Assert.assertArrayEquals(a3, ArrayUtil.merge(a1, a2)); + } + + @Test + public void testGrow() { + int[] oldArray = {2,3,6}; + int[] newArray = {2,3,6,0,0,0,0}; + Assert.assertArrayEquals(newArray, ArrayUtil.grow(oldArray, 4)); + } + + @Test + public void testFibonacci() { + int max = 15; + int[] fibonacci = ArrayUtil.fibonacci(max); + int[] f = {1,1,2,3,5,8,13}; + Assert.assertArrayEquals(f, fibonacci); + } + + @Test + public void testGetPrimes() { + int max = 23; + int[] primes = ArrayUtil.getPrimes(max); + int[] expecteds = {2,3,5,7,11,13,17,19}; + Assert.assertArrayEquals(expecteds, primes); + } + + @Test + public void testGetPerfectNumbers() { + int max = 8129; + int[] perfectNumbers = ArrayUtil.getPerfectNumbers(max); + int[] expected = {6,28,496,8128}; + Assert.assertArrayEquals(expected, perfectNumbers); + } + + @Test + public void testJoin() { + int[] array = {3,8,9}; + String sep = "-"; + String join = ArrayUtil.join(array, sep); + Assert.assertEquals("3-8-9", join); + } + +} diff --git a/group17/82427129/JavaUtil/src/test/java/com/coderising/litestruts/StrutsTest.java b/group17/82427129/JavaUtil/src/test/java/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..a44c1878ac --- /dev/null +++ b/group17/82427129/JavaUtil/src/test/java/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group17/82427129/JavaUtil/src/test/java/com/coding/basic/ArrayListTest.java b/group17/82427129/JavaUtil/src/test/java/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..19ce5fbbd8 --- /dev/null +++ b/group17/82427129/JavaUtil/src/test/java/com/coding/basic/ArrayListTest.java @@ -0,0 +1,64 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testArrayList() { + fail("Not yet implemented"); + } + + @Test + public void testArrayListInt() { + fail("Not yet implemented"); + } + + @Test + public void testAddObject() { + fail("Not yet implemented"); + } + + @Test + public void testAddIntObject() { + fail("Not yet implemented"); + } + + @Test + public void testSet() { + fail("Not yet implemented"); + } + + @Test + public void testGet() { + fail("Not yet implemented"); + } + + @Test + public void testRemove() { + fail("Not yet implemented"); + } + + @Test + public void testSize() { + fail("Not yet implemented"); + } + + @Test + public void testIterator() { + fail("Not yet implemented"); + } + +} diff --git a/group17/82427129/JavaUtil/src/test/java/com/coding/basic/LinkedListTest.java b/group17/82427129/JavaUtil/src/test/java/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..6020e772c9 --- /dev/null +++ b/group17/82427129/JavaUtil/src/test/java/com/coding/basic/LinkedListTest.java @@ -0,0 +1,81 @@ +package com.coding.basic; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class LinkedListTest { + private LinkedList list; + + @Before + public void setUp() throws Exception { + list = new LinkedList(); + } + + @After + public void tearDown() throws Exception { + list = null; + } + + @Test + public void testAddObject() { + list.add(100); + Assert.assertEquals(1, list.size()); + list.add(100); + Assert.assertEquals(2, list.size()); + list.add(100); + Assert.assertEquals(3, list.size()); + } + + @Test + public void testAddIntObject() { + for (int i = 0; i < 5; i++) { + list.add(i, i); + } + Assert.assertEquals(5, list.size()); + } + + @Test + public void testGet() { + + } + + @Test + public void testRemove() { + fail("Not yet implemented"); + } + + @Test + public void testSize() { + fail("Not yet implemented"); + } + + @Test + public void testAddFirst() { + fail("Not yet implemented"); + } + + @Test + public void testAddLast() { + fail("Not yet implemented"); + } + + @Test + public void testRemoveFirst() { + fail("Not yet implemented"); + } + + @Test + public void testRemoveLast() { + fail("Not yet implemented"); + } + + @Test + public void testIterator() { + fail("Not yet implemented"); + } + +} diff --git a/group17/82427129/JavaUtil/src/test/java/com/coding/basic/Test.java b/group17/82427129/JavaUtil/src/test/java/com/coding/basic/Test.java new file mode 100644 index 0000000000..572907fd34 --- /dev/null +++ b/group17/82427129/JavaUtil/src/test/java/com/coding/basic/Test.java @@ -0,0 +1,14 @@ +package com.coding.basic; + +public class Test { + public static void main(String[] args) { + System.out.println(new A().e == new A().e); + } +} +class A{ + public A() { + e = E; + } + Object[] e; + static Object[] E = {}; +} diff --git a/group17/article/20170226-20170305.md b/group17/article/20170226-20170305.md new file mode 100644 index 0000000000..77e9b85142 --- /dev/null +++ b/group17/article/20170226-20170305.md @@ -0,0 +1,56 @@ +# 鑷敱鍐欎綔 + +## 椤荤煡 +--- + +浜や綔涓氭椂璇峰湪QQ 鍙峰悗闈㈠~涓婂悇鑷殑鏂囩珷閾炬帴, 姣斿: + +51075907 http://m.blog.csdn.net/article/details?id=57083764 + +## 鏂囩珷 +--- + +1204187480 + +102228177 + +876385982 + +785396327 + +1059107701 + +240094626 + +82427129 + +296910598 + +1264835468 http://www.jianshu.com/p/634ca8cdd6e3 + +516886559 + +1282579502 https://www.evernote.com/shard/s413/sh/142601dd-edc3-4e37-871e-37a7489d7634/a092bf080e1aefbaeab96d34edac8cf0 + +614982500 + +865797761 + +1540186032 + +176653813 + +116665530 + +51075907 + +1158154002 + +345450234 + +919764878 + +1368331120 + +517970312 + diff --git a/group17/article/template.md b/group17/article/template.md new file mode 100644 index 0000000000..d92ffef426 --- /dev/null +++ b/group17/article/template.md @@ -0,0 +1,56 @@ +# XXXX + +## 椤荤煡 +--- + +浜や綔涓氭椂璇峰湪QQ 鍙峰悗闈㈠~涓婂悇鑷殑鏂囩珷閾炬帴, 姣斿: + +51075907 http://m.blog.csdn.net/article/details?id=57083764 + +## 鏂囩珷 +--- + +1204187480 + +102228177 + +876385982 + +785396327 + +1059107701 + +240094626 + +82427129 + +296910598 + +1264835468 + +516886559 + +1282579502 + +614982500 + +865797761 + +1540186032 + +176653813 + +116665530 + +51075907 + +1158154002 + +345450234 + +919764878 + +1368331120 + +517970312 + diff --git "a/group17/article/\345\206\231\344\270\200\347\257\207\346\226\207\347\253\240\344\273\213\347\273\215cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244\344\273\245\345\217\212\344\273\226\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273_20170226.md" "b/group17/article/\345\206\231\344\270\200\347\257\207\346\226\207\347\253\240\344\273\213\347\273\215cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244\344\273\245\345\217\212\344\273\226\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273_20170226.md" index 4e6e0b6b1b..df34ed9d6d 100644 --- "a/group17/article/\345\206\231\344\270\200\347\257\207\346\226\207\347\253\240\344\273\213\347\273\215cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244\344\273\245\345\217\212\344\273\226\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273_20170226.md" +++ "b/group17/article/\345\206\231\344\270\200\347\257\207\346\226\207\347\253\240\344\273\213\347\273\215cpu, \345\206\205\345\255\230, \347\243\201\347\233\230, \346\214\207\344\273\244\344\273\245\345\217\212\344\273\226\344\273\254\344\271\213\351\227\264\347\232\204\345\205\263\347\263\273_20170226.md" @@ -13,6 +13,7 @@ 1204187480 102228177 http://note.youdao.com/noteshare?id=74a51e7f93461dfb77c69a1cf4755624&sub=004F10FA5D2046ABAA060F19C0D2A18F + http://note.youdao.com/noteshare?id=6d117ad0ead79eafee2a5308c00d6a3a 876385982 http://www.totoro-fly.com/?p=59 @@ -20,9 +21,9 @@ 1059107701 -240094626 +240094626 http://note.youdao.com/noteshare?id=d129c169ed611f6099b96e8e0f3f5c51 -82427129 http://blog.csdn.net/walk_er/article/details/57406278 +82427129 http://blog.csdn.net/walk_er/article/details/57406278 ;http://blog.csdn.net/walk_er/article/details/60475450 296910598 @@ -30,7 +31,7 @@ 516886559 -1282579502 https://www.evernote.com/shard/s413/sh/3af5f6a4-a580-4a49-b63c-90f5b178aca4/7cf052e3789c862e38d6d6b3cce1ceed +1282579502 (鏂囩珷1锛塰ttps://www.evernote.com/shard/s413/sh/3af5f6a4-a580-4a49-b63c-90f5b178aca4/7cf052e3789c862e38d6d6b3cce1ceed 锛堟枃绔2锛塰ttps://www.evernote.com/shard/s413/sh/142601dd-edc3-4e37-871e-37a7489d7634/a092bf080e1aefbaeab96d34edac8cf0 614982500 @@ -53,4 +54,3 @@ 1368331120 517970312 - diff --git a/group17/count/homework.md b/group17/count/homework.md new file mode 100644 index 0000000000..c37e964c5e --- /dev/null +++ b/group17/count/homework.md @@ -0,0 +1,21 @@ +# 浣滀笟缁熻 + +## 椤荤煡 +--- + +鏃ユ湡涓嬮潰鐨勯摼鎺ユ槸姣忔浣滀笟鐨勭粺璁, 澶у浣滀笟瀹屾垚鍚庣偣鍑婚摼鎺ュ嵆鍙繘鍏ユ枃妗i〉闈㈠~鍐. + +## 鏂囨。 +--- + +### 20170219-20170226 + +[鏂囩珷缁熻](https://github.com/luoziyihao/coding2017/blob/master/group17/article/%E5%86%99%E4%B8%80%E7%AF%87%E6%96%87%E7%AB%A0%E4%BB%8B%E7%BB%8Dcpu%2C%20%E5%86%85%E5%AD%98%2C%20%E7%A3%81%E7%9B%98%2C%20%E6%8C%87%E4%BB%A4%E4%BB%A5%E5%8F%8A%E4%BB%96%E4%BB%AC%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB_20170226.md) + +[浣滀笟缁熻](https://shimo.im/sheet/LQaZ3eVFgZYh8c1X/銆岀17缁勪綔涓氬畬鎴愭儏鍐礯2017...0226銆) + +### 20170226-20170205 + +[鏂囩珷缁熻](https://github.com/luoziyihao/coding2017/blob/master/group17/article/20170226-20170305.md) + +[浣滀笟缁熻](https://shimo.im/sheet/8T7YWAlIJpg6apn6/銆岀17缁勪綔涓氬畬鎴愭儏鍐礯2017...0305銆) diff --git a/group19/1294642551/src/arrayTests/ArrayUtil.java b/group19/1294642551/src/arrayTests/ArrayUtil.java new file mode 100644 index 0000000000..a9f23394e9 --- /dev/null +++ b/group19/1294642551/src/arrayTests/ArrayUtil.java @@ -0,0 +1,270 @@ +package arrayTests; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.TreeSet; + +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 int[] reverseArray(int[] origin){ + + int len = origin.length; + int[] arr = new int[len]; + + for(int i = 0; i < len; i++) + { + arr[i] = origin[ len -1 - i]; + } + + return arr; + } + + /** + * 现在有如下的一个数组: 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){ + + ArrayList al = new ArrayList(); + + int len = oldArray.length; + for(int i = 0; i < len; i++) + { + if(oldArray[i] != 0) + { + al.add(oldArray[i]); + } + } + + int arrLen = al.size(); + int[] arr = new int[arrLen]; + for(int i = 0; i < arrLen; i++) + { + arr[i] = al.get(i); + } + + return arr; + + + } + + /** + * 给定两个已经排序好的整形数组, 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){ + TreeSet tr = new TreeSet(); + for(int i = 0; i < array1.length; i++) + { + tr.add(array1[i]); + } + for(int j = 0; j < array2.length; j++) + { + tr.add(array2[j]); + } + + int arrLen = tr.size(); + int[] arr = new int[arrLen]; + int index = 0; + + Iterator it = tr.iterator(); + while(it.hasNext()) + { + arr[index] = (int) it.next(); + index++; + } + + return arr; + } + /** + * 把一个已经存满数据的数组 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 len = oldArray.length; + int arrLen = len + size; + int[] arr = new int[arrLen]; + + for(int i = 0; i < arrLen; i++) + { + if (i < len) + arr[i] = oldArray[i]; + else + arr[i] = 0; + } + + return arr; + + } + + /** + * 斐波那契数列为: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){ + + ArrayList al = new ArrayList(); + int first = 1; + int second = 1; + int value = 0; + if(max >= 2) + { + al.add(first); + al.add(second); + } + do + { + value = first + second; + if(value < max) + { + al.add(value); + first = second; + second = value; + } + }while(value < max); + + int arrLen = al.size(); + int[] arr = new int[arrLen]; + for(int i = 0; i < arrLen; i++) + { + arr[i] = al.get(i); + } + + return arr; + + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + + ArrayList al = new ArrayList(); + if(max > 2) + al.add(2); + + int value = 3; + while(value < max) + { + int flag = 1; + for(int i = 2; i < value; i++) + { + if(value % i == 0) + { + flag = 0; + break; + } + } + + if (flag == 1) + al.add(value); + + value++; + } + + int arrLen = al.size(); + int[] arr = new int[arrLen]; + for(int i = 0; i < arrLen; i++) + { + arr[i] = al.get(i); + } + + return arr; + + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + + ArrayList al = new ArrayList(); + for(int i = 1; i < max; i++) + { + if (isPerfectNumber(i)) + al.add(i); + } + + int arrLen = al.size(); + int[] arr = new int[arrLen]; + for(int i = 0; i < arrLen; i++) + { + arr[i] = al.get(i); + } + + return arr; + } + + public boolean isPerfectNumber(int number) + { + ArrayList al = new ArrayList(); + + for(int i = 1; i < number; i++) + { + if(number % i == 0) + al.add(i); + } + + int value = 0; + for(int j = 0; j < al.size(); j++) + { + value = value + al.get(j); + } + + return value == number; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + + String str = ""; + int len = array.length; + for(int i = 0; i < len-1; i++) + { + str = str + array[i] + seperator; + } + str = str + array[len-1]; + + return str; + } + + + +} diff --git a/group19/1294642551/src/struts_Reflect/Struts.java b/group19/1294642551/src/struts_Reflect/Struts.java new file mode 100644 index 0000000000..1694cf369c --- /dev/null +++ b/group19/1294642551/src/struts_Reflect/Struts.java @@ -0,0 +1,142 @@ +package struts_Reflect; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) throws Exception { + + /* + + 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字段中。 + + * + */ + + //创建变量保存解析到的className的字符串 + String className = null; + + //---------解析XML文件--------------------------------------------------------------------------- + + // 创建saxReader对象 + SAXReader reader = new SAXReader(); + // 通过read方法读取一个文件 转换成Document对象 + Document document = reader.read(new File("src/struts_Reflect/Struts.xml")); + //获取根节点元素对象 + Element root = document.getRootElement(); + //System.out.println("Root: " + root.getName()); + + // 迭代器,获取元素name属性值为“login”的class属性的值,保存到变量className中。 + for (Iterator iter = root.elementIterator(); iter.hasNext();) + { + Element e = (Element) iter.next(); + String name = e.attributeValue("name"); + + if(name.equals("login")) + { + className = e.attributeValue("class"); + } + + } + + //-----------反射获取LoginAction类的实例并调用其方法--------------------------------------------------------- + + //获取实例 + Class clazz = Class.forName("struts_Reflect.LoginAction"); + Object obj = clazz.newInstance(); + + //调用setName 和 setPassword方法 + Method mSetName = clazz.getMethod("setName", String.class); + Method mSetPassWord = clazz.getMethod("setPassword", String.class); + mSetName.invoke(obj, parameters.get("name")); + mSetPassWord.invoke(obj, parameters.get("password")); + + //调用excute方法 + Method mExecute = clazz.getMethod("execute", null); + String result = (String) mExecute.invoke(obj, null); + System.out.println(result); + + //找到所有getter方法,属性和值放到parameterMap中。 + Method[] methods = clazz.getDeclaredMethods(); +// HashMap paraMap = new HashMap(); + + ArrayList al = new ArrayList(); + al.add("name"); + al.add("password"); + al.add("message"); + String key = null; + String value = null; + Field field = null; + + for(int i = 0; i < al.size(); i++) + { + key = al.get(i); + field = clazz.getDeclaredField(key); + field.setAccessible(true); + value = (String) field.get(obj); + parameters.put(key, value); + System.out.println(key+"---"+value); + } + + + + View view = new View(); + view.setParameters(parameters); + + //----------将JSP字段放到View对象中------------------------------------- + + //获取名称为action的元素节点 + Element actionE = root.element("action"); + + // 迭代器,获取元素result中的文字 + String rValue = null; + + for (Iterator iter = actionE.elementIterator(); iter.hasNext();) + { + Element e = (Element) iter.next(); + String name = e.attributeValue("name"); + + if(name.equals(result)) + { + rValue = e.getText(); + view.setJsp(rValue); + System.out.println(rValue); + } + + } + + return view; + + } + +} \ No newline at end of file diff --git a/group19/1294642551/test/arrayTests/ArrayUtilTest.java b/group19/1294642551/test/arrayTests/ArrayUtilTest.java new file mode 100644 index 0000000000..d20c9a6c46 --- /dev/null +++ b/group19/1294642551/test/arrayTests/ArrayUtilTest.java @@ -0,0 +1,101 @@ +package arrayTests; + +import static org.junit.Assert.*; + +import org.junit.Assert; +import org.junit.Test; + +public class ArrayUtilTest { + + @Test + public void testReverseArray() { + ArrayUtil au = new ArrayUtil(); + int[] origin = {7, 9, 30, 3, 4}; + int[] expecteds ={4, 3, 30, 9, 7}; + int[] actuals = au.reverseArray(origin); + + Assert.assertArrayEquals(expecteds, actuals); + } + + @Test + public void testRemoveZero() { + ArrayUtil au = new ArrayUtil(); + int[] oldArray = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] expecteds = {1,3,4,5,6,6,5,4,7,6,7,5}; + int[] actuals = au.removeZero(oldArray); + + Assert.assertArrayEquals(expecteds, actuals); + } + + @Test + public void testMerge() { + + ArrayUtil au = new ArrayUtil(); + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + int[] expecteds = {3,4,5,6,7,8}; + int[] actuals = au.merge(a1, a2); + + Assert.assertArrayEquals(expecteds, actuals); + } + + @Test + public void testGrow() { + + ArrayUtil au = new ArrayUtil(); + int[] oldArray = {2,3,6}; + int size = 3; + int[] expecteds = {2,3,6,0,0,0}; + int[] actuals = au.grow(oldArray, size); + + Assert.assertArrayEquals(expecteds, actuals); + } + + @Test + public void testFibonacci() { + + ArrayUtil au = new ArrayUtil(); + int max = 15; + int[] expecteds = {1, 1, 2, 3, 5, 8, 13}; + int[] actuals = au.fibonacci(max); + + Assert.assertArrayEquals(expecteds, actuals); + } + + + + @Test + public void testGetPrimes() { + ArrayUtil au = new ArrayUtil(); + int max = 23; + int[] expecteds = {2,3,5,7,11,13,17,19}; + int[] actuals = au.getPrimes(max); + + Assert.assertArrayEquals(expecteds, actuals); + + } + + @Test + public void testGetPerfectNumbers() { + + ArrayUtil au = new ArrayUtil(); + int max = 30; + int[] expecteds = {6, 28}; + int[] actuals = au.getPerfectNumbers(max); + + Assert.assertArrayEquals(expecteds, actuals); + } + + @Test + public void testJoin() { + + ArrayUtil au = new ArrayUtil(); + int[] array = {3, 8, 9}; + String seperator = "-"; + String expecteds = "3-8-9"; + String actuals = au.join(array, seperator); + + Assert.assertEquals(expecteds, actuals); + } + +} diff --git a/group19/1592562638/src/com/coderising/action/LoginAction.java b/group19/1592562638/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..01af400247 --- /dev/null +++ b/group19/1592562638/src/com/coderising/action/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.action; +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * @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/group19/1592562638/src/com/coderising/action/LogoutAction.java b/group19/1592562638/src/com/coderising/action/LogoutAction.java new file mode 100644 index 0000000000..569a109dad --- /dev/null +++ b/group19/1592562638/src/com/coderising/action/LogoutAction.java @@ -0,0 +1,6 @@ +package com.coderising.action; + +public class LogoutAction { + LogoutAction(){ + } +} diff --git a/group19/1592562638/src/com/coderising/action/TestMain.java b/group19/1592562638/src/com/coderising/action/TestMain.java new file mode 100644 index 0000000000..f6f2e60ef2 --- /dev/null +++ b/group19/1592562638/src/com/coderising/action/TestMain.java @@ -0,0 +1,112 @@ +package com.coderising.action; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.jdom2.Document; +import org.jdom2.Element; +import org.jdom2.JDOMException; +import org.jdom2.filter.Filters; +import org.jdom2.input.SAXBuilder; +import org.jdom2.xpath.XPathExpression; +import org.jdom2.xpath.XPathFactory; + +/* +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字段中。 + +*/ +//通过JDOM来读取xml文件 +public class TestMain { + + private static String xmlSource=System.getProperty("user.dir")+"\\src\\com\\coderising\\action\\struts.xml"; + private static Map container = new HashMap();//获取xml中name字符串跟class类 + private static Map containerStr = new HashMap();//获取xml中name跟class字符串 + private static Map containerBak = new HashMap();//获取getter方法属性 + + private static View view; + private static String resultStr; + private static LoginAction lAction; + + public static void main(String[] args) throws JDOMException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, InvocationTargetException { + // TODO Auto-generated method stub + + //构造器 + SAXBuilder builder=new SAXBuilder(); + //StringReader reader=new StringReader(System.getProperty("user.dir")+"\\src\\com\\second\\xml/struts.xml"); + + Document document=builder.build(xmlSource); + + XPathFactory xFactory=XPathFactory.instance(); + XPathExpression expr=xFactory.compile("//struts//action",Filters.element()); + + List links=expr.evaluate(document); + for(Element linkElement:links){ + String name=linkElement.getAttributeValue("name"); + String clazz=linkElement.getAttributeValue("class"); + //利用反射生成实例 然后装进容器 + Object o=Class.forName(clazz).newInstance(); + container.put(name,o); + containerStr.put(name, clazz); + + System.out.println("name: "+name+", class: "+clazz); + } + + /************** + 3.通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + */ + if(container.containsKey("login")){ + lAction=(LoginAction)container.get("login"); + lAction.setName("test"); + lAction.setPassword("1234"); + + resultStr=lAction.execute();//执行execute方法 + System.out.println("name="+lAction.getName()+" password="+lAction.getPassword()+"->登录结果:"+resultStr); + + Class clazz=Class.forName(containerStr.get("login")); + Method[] methods=clazz.getMethods(); + for(Method method:methods){ + String methodName=method.getName(); + //int indexTmp=methodName.indexOf("get"); + //if(indexTmp!=-1){ + if(methodName.startsWith("get")){ + //包含get的方法名 + //String methodStr=methodName.substring(indexTmp+3); + String methodStr=methodName.substring(3); + System.out.println(methodStr); + + //System.out.println(method.invoke(lAction)); + + containerBak.put(methodName, (String)method.invoke(lAction)); + } + } + view.setParameters(containerBak);//保存到view中 + } + + /************** + 4.根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中 + */ + + } +} diff --git a/group19/1592562638/src/com/coderising/action/View.java b/group19/1592562638/src/com/coderising/action/View.java new file mode 100644 index 0000000000..8a80428d33 --- /dev/null +++ b/group19/1592562638/src/com/coderising/action/View.java @@ -0,0 +1,22 @@ +package com.coderising.action; +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/group19/1592562638/src/com/coderising/action/struts.xml b/group19/1592562638/src/com/coderising/action/struts.xml new file mode 100644 index 0000000000..fb0c2be3de --- /dev/null +++ b/group19/1592562638/src/com/coderising/action/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/group19/1592562638/\350\257\273\344\271\246\347\254\224\350\256\260/2\347\216\260\344\273\243\346\223\215\344\275\234\347\263\273\347\273\237[\350\275\254]/\350\257\273\344\271\246\347\254\224\350\256\260_\347\216\260\344\273\243\346\223\215\344\275\234\347\263\273\347\273\237.pdf" "b/group19/1592562638/\350\257\273\344\271\246\347\254\224\350\256\260/2\347\216\260\344\273\243\346\223\215\344\275\234\347\263\273\347\273\237[\350\275\254]/\350\257\273\344\271\246\347\254\224\350\256\260_\347\216\260\344\273\243\346\223\215\344\275\234\347\263\273\347\273\237.pdf" new file mode 100644 index 0000000000..065c59f54f Binary files /dev/null and "b/group19/1592562638/\350\257\273\344\271\246\347\254\224\350\256\260/2\347\216\260\344\273\243\346\223\215\344\275\234\347\263\273\347\273\237[\350\275\254]/\350\257\273\344\271\246\347\254\224\350\256\260_\347\216\260\344\273\243\346\223\215\344\275\234\347\263\273\347\273\237.pdf" differ diff --git a/group19/2558178127/src/com/coderising/action/LoginAction.java b/group19/2558178127/src/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..72e00894bb --- /dev/null +++ b/group19/2558178127/src/com/coderising/action/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.action; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @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/group19/2558178127/src/com/coderising/array/ArrayUtil.java b/group19/2558178127/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..fb6a84f255 --- /dev/null +++ b/group19/2558178127/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,189 @@ +package com.coderising.array; + + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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) { + if (origin == null || origin.length <= 1) { + return; + } + int tem = 0; + for (int i = 0, len = origin.length; i < len / 2; i++) { + tem = origin[i]; + origin[i] = origin[len - i + 1]; + origin[len - i + 1] = tem; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 static int[] removeZero(int[] oldArray){ + if(oldArray == null){ + return null; + } + int [] ret = new int[oldArray.length]; + int cou = 0; + for(int i=0;itemint[j]){ + int temp = temint[i]; + int p = index[i]; + temint[i] = temint[j]; + index[i] = index[j]; + temint[j] = temp; + index[j] = p; + } + } + } + + return null; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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[] res = new int[oldArray.length+size]; + System.arraycopy(oldArray, 0,res,0,oldArray.length); + return res; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + int[] a = new int[max]; + int[] retf = null; + if (max < 3) { + retf =new int[]{1,1}; + } else if (max >= 3) { + a[0] = a[1] = 1; + for (int i = 2; i < max; i++) { + a[i] = a[i - 1] + a[i - 2]; + if(a[i]>max){ + break; + } + } + retf = removeZero(a); + } + return retf; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max) { + int[] prime = new int[max]; + int sum = 0; + for (int i = 2; i <= max; i++) {// 浠2寮濮嬫槸鍥犱负锛1鏃笉鏄礌鏁颁篃涓嶆槸鍚堟暟 + boolean sign = true; + for (int j = 2; j < i; j++) { + if (i % j == 0) {// 鑳借闄や簡1鍜岃嚜宸辨暣闄ょ殑鏁拌偗瀹氫笉鏄礌鏁帮紝鍥犳鍙鏈変竴涓氨鍙互璺宠繃寰幆 + sign = false; + continue; + } + } + if (sign) { + prime[i] = i; + } + } + int[] retf = null; + retf = removeZero(prime); + return retf; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuffer sbf = new StringBuffer(""); + for(int i=0;i parameters) throws Exception{ + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + new XmlService().readDocument(); //璇诲彇xml銆傛妸xml淇℃伅瀛樺叆map + + String clsName = readxml(actionName,"class");//鑾峰彇action瀵瑰簲class瀵硅薄 + Class c1 = Class.forName(clsName); + Object obj = c1.newInstance(); + //action灞炴ц祴鍊 + Iterator iter = parameters.keySet().iterator(); + while (iter.hasNext()) { + String key = iter.next(); + String namval = parameters.get((String) key); + Method m = c1.getMethod("set" + key.substring(0, 1).toUpperCase() + + key.substring(1), String.class); + m.invoke(obj, namval); + } + + String result = ""; + Method mt=c1.getMethod("execute");//璋冪敤鎵ц鏂规硶 + result = (String) mt.invoke(obj); + View view = new View(); + view.setJsp(readxml(actionName,result)); + return view; + } + /** + * 璇诲彇Map涓瓁ml淇℃伅 + * @param name + * @param attr + * @return + */ + public static String readxml(String name,String attr){ + + Map xmlMap = XmlService.map; + + return (String) xmlMap.get(name+attr); + } + + +} diff --git a/group19/2558178127/src/com/coderising/litestruts/StrutsTest.java b/group19/2558178127/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..3fedbec428 --- /dev/null +++ b/group19/2558178127/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,53 @@ +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 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")); + } + public static void main(String [] args)throws Exception{ + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + System.out.println(view.getJsp()); + } + @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/group19/2558178127/src/com/coderising/litestruts/View.java b/group19/2558178127/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..0194c681f6 --- /dev/null +++ b/group19/2558178127/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/group19/2558178127/src/com/coderising/litestruts/struts.xml b/group19/2558178127/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..fb0c2be3de --- /dev/null +++ b/group19/2558178127/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 diff --git a/group19/2558178127/src/com/coderising/litestruts/xml/read/XmlService.java b/group19/2558178127/src/com/coderising/litestruts/xml/read/XmlService.java new file mode 100644 index 0000000000..fc4ad6c23e --- /dev/null +++ b/group19/2558178127/src/com/coderising/litestruts/xml/read/XmlService.java @@ -0,0 +1,88 @@ +package com.coderising.litestruts.xml.read; + + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class XmlService { + + private File file; + + public static Map map = new HashMap(); + + public XmlService() { + file = new File("E:/2017learn/coding2017/group19/2558178127/src/com/coderising/litestruts/struts.xml"); + + } + + public Document getDocument() { + SAXReader reader = new SAXReader(); + + Document document = null; + + try { + document = reader.read(file); + } catch (Exception e) { + + e.printStackTrace(); + } + return document; + } + + //璇诲彇xml + public void readDocument() { + + List sonElementList = getDocument().getRootElement().elements(); + + Element root = getDocument().getRootElement(); + getElement(sonElementList); + System.out.println(map); + } + + private List list = new ArrayList(); + + private void getElement(List sonElemetList) { + for (Element sonElement : sonElemetList) { + if (sonElement.elements().size() != 0) { + getNodes(sonElement,list); + getElement(sonElement.elements()); + }else{ + getNodes(sonElement,list); + System.out.println(sonElement.getName() + ":"+ sonElement.getText()); + } + + + } + } + + public void getNodes(Element node,List pname){ + //褰撳墠鑺傜偣鐨勫悕绉般佹枃鏈唴瀹瑰拰灞炴 + System.out.println("褰撳墠鑺傜偣鍚嶇О锛"+node.getName());//褰撳墠鑺傜偣鍚嶇О + System.out.println("褰撳墠鑺傜偣鐨勫唴瀹癸細"+node.getTextTrim());//褰撳墠鑺傜偣鍐呭 + List listAttr=node.attributes();//褰撳墠鑺傜偣鐨勬墍鏈夊睘鎬х殑list + String classValue=""; + for(Attribute attr:listAttr){//閬嶅巻褰撳墠鑺傜偣鐨勬墍鏈夊睘鎬 + String name=attr.getName();//灞炴у悕绉 + String value=attr.getValue();//灞炴х殑鍊 + System.out.println("灞炴у悕绉帮細"+name+"灞炴у硷細"+value); + if("login".equals(value) || "logout".equals(value)){ + pname.add(0,value); + }else{ + map.put(pname.get(0)+value, node.getTextTrim()); + } + if("class".equals(name)&&pname!=null){ + classValue=value; + map.put(pname.get(0)+"class", value); + } +// + } + } +} diff --git a/group19/376248270/0-20170215-20170226/README.md b/group19/376248270/0-20170215-20170226/README.md new file mode 100644 index 0000000000..6a92672f18 --- /dev/null +++ b/group19/376248270/0-20170215-20170226/README.md @@ -0,0 +1,11 @@ +### 0x00 (Deadline: 2017-02-26) +#### 1. 瀹炵幇鍩烘湰鐨勬暟鎹粨鏋 +- [x] ArrayList +- [] LinkedList +- [] Queue +- [] Stack +- [] BinaryTree(鍙) +- [] Interor(鍙) + +#### 2. 鏂囩珷 +- [x] [寤鸿鍐欎竴绡囨湁鍏矯PU銆佸唴瀛樸佺‖鐩樸佹寚浠ょ殑鏂囩珷](http://bosschow.github.io/2017/02/24/recursion-and-tail-recursion/) diff --git a/group19/376248270/0-20170215-20170226/arraylist/ArrayList.java b/group19/376248270/0-20170215-20170226/arraylist/ArrayList.java new file mode 100644 index 0000000000..8b4a30c5ff --- /dev/null +++ b/group19/376248270/0-20170215-20170226/arraylist/ArrayList.java @@ -0,0 +1,97 @@ +package arraylist; +import java.util.Arrays; + +import base.List; + +public class ArrayList implements List { + public static void main(String[] args) { + ArrayList arr = new ArrayList(); + System.out.println(arr.size()); + arr.add("ele1"); + arr.add("ele2"); + + for (int i = 0; i < arr.size(); i++) { + System.out.println((String)arr.get(i)); + } + + System.out.println("============"); + arr.remove(0); + + for (int i = 0; i < arr.size(); i++) { + System.out.println((String)arr.get(i)); + } + } + + // 鍒濆瀹归噺 + static final int DEFAULT_INITIAL_CAPACITY = 10; + + // 鏁扮粍鎵╁睍閫熷害 + static final int INCRE_SPEED = 2; + + // 鍏冪礌鏁扮粍 + private Object[] elementData = new Object[DEFAULT_INITIAL_CAPACITY]; + + // 鍏冪礌鏁伴噺 + private int size = 0; + + /** + * 娣诲姞鍏冪礌鍒版寚瀹氫綅缃 + */ + public void add(int index, Object obj) { + add(elementData[size-1]); + for (int i = size - 1; i > index; i--) { + elementData[i] = elementData[i - 1]; + } + elementData[index] = obj; + } + + /** + * 鍒犻櫎鎸囧畾浣嶇疆鐨勫厓绱 + */ + public Object remove(int index) { + Object result = elementData[index]; + + while (index < size) { + elementData[index] = elementData[index + 1]; + index++; + } + size--; + return result; + } + + /** + * 鑾峰彇鎸囧畾浣嶇疆鐨勫厓绱 + */ + public Object get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index: "+index); + } + return elementData[index]; + } + + /** + * 鑾峰彇褰撳墠鍏冪礌鏁伴噺 + */ + public int size() { + return size; + } + + /** + * 娣诲姞鍏冪礌鍒板熬閮 + */ + public void add(Object obj) { + elementData[size++] = obj; + if (size == elementData.length) { + resize(); + } + + } + + /** + * 鍏冪礌鏁扮粍鑷姩鎵╁睍 + */ + private void resize() { + elementData = Arrays.copyOf(elementData, elementData.length * INCRE_SPEED); + } + +} diff --git a/group19/376248270/0-20170215-20170226/arraylist/ArrayListTest.java b/group19/376248270/0-20170215-20170226/arraylist/ArrayListTest.java new file mode 100644 index 0000000000..c48142416e --- /dev/null +++ b/group19/376248270/0-20170215-20170226/arraylist/ArrayListTest.java @@ -0,0 +1,57 @@ +package arraylist; + +import org.junit.Before; +import org.junit.Test; + +public class ArrayListTest { + ArrayList arr = null; + + @Before + public void setup() { + arr = new ArrayList(); + } + + @Test + public void testAdd() { + arr.add("ele-1"); + arr.add("ele-2"); + printArrayList(); + arr.add(0, "ele-0"); + printArrayList(); + } + + @Test + public void testGet() { + arr.add("ele-1"); + arr.add("ele-2"); + printArrayList(); + arr.remove(0); + printArrayList(); + System.out.println((String)arr.get(1)); + } + + @Test + public void testRemove() { + + for (int i = 0; i < 10; i++) { + arr.add("ele-"+i); + } + + printArrayList(); + arr.remove(1); + System.out.println("After Remove"); + printArrayList(); + } + + /** + * 鎵撳嵃ArrayList + * @param arr + */ + private void printArrayList() { + System.out.print("["); + for (int i = 0; i < arr.size(); i++) { + System.out.print((String)arr.get(i) + ", "); + } + System.out.println("]"); + } +} diff --git a/group19/376248270/0-20170215-20170226/base/List.java b/group19/376248270/0-20170215-20170226/base/List.java new file mode 100644 index 0000000000..8ca593dec2 --- /dev/null +++ b/group19/376248270/0-20170215-20170226/base/List.java @@ -0,0 +1,8 @@ +package base; +public interface List { + public void add(Object obj); + public int size(); + public Object get(int index); + public Object remove(int index); + public void add(int index, Object obj); +} diff --git a/group19/376248270/0-20170215-20170226/linkedlist/LinkedList.java b/group19/376248270/0-20170215-20170226/linkedlist/LinkedList.java new file mode 100644 index 0000000000..da357f7164 --- /dev/null +++ b/group19/376248270/0-20170215-20170226/linkedlist/LinkedList.java @@ -0,0 +1,25 @@ +package linkedlist; + +public class LinkedList implements List { + private Node head; + + private int size; + + public void add(Object obj) { + Node current = new + } + + /** + * 鑾峰彇鍏冪礌鏁伴噺 + */ + public int size() { + return size; + } + + private static class Node { + + Object data; + Node next; + + } +} diff --git a/group19/376248270/1-20170226-20170305/README.md b/group19/376248270/1-20170226-20170305/README.md new file mode 100644 index 0000000000..1bbe089b52 --- /dev/null +++ b/group19/376248270/1-20170226-20170305/README.md @@ -0,0 +1,8 @@ +### 0x01 (Deadline: 2017-03-05) + +#### 1. coding +- [x] 瀹炵幇绠鍗曠殑绫讳技涓嶴truts鐨勬搷浣滐紝闇瑕侀氳繃Junit娴嬭瘯銆 +- [x] 瀹炵幇ArrayUtil + +#### 2. 鏂囩珷 +- [x] [鍐欎竴绡囨枃绔狅紝鑷富瀹氶鐩甝(http://bosschow.github.io/2017/03/05/get-prime-number-1/) diff --git a/group19/376248270/1-20170226-20170305/array/ArrayUtil.java b/group19/376248270/1-20170226-20170305/array/ArrayUtil.java new file mode 100644 index 0000000000..a64eb8da05 --- /dev/null +++ b/group19/376248270/1-20170226-20170305/array/ArrayUtil.java @@ -0,0 +1,231 @@ +package array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Stack; + +public class ArrayUtil { + + public static void main(String[] args) { + int[] origin = new int[]{1, 2, 3, 4}; + System.out.println(Arrays.toString(getPrimes(23))); + } + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + 濡傛灉锛 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + * @param origin + * @return + */ + /** + * 1. 閫氳繃鍙屾寚閽堝疄鐜板弽杞 + * @param origin + * @return + */ + public static int[] reverseArray_1(int[] origin){ + int i = 0; + int j = origin.length - 1; + for (;i < j;) { + int swap = origin[i]; + origin[i] = origin[j]; + origin[j] = swap; + i++; + j--; + } + + return origin; + } + + /** + * 2. 閫氳繃鏍堝悗杩涘厛鍑虹殑鐗圭偣瀹炵幇鍙嶈浆 + * @param origin + * @return + */ + public static int[] reverseArray_2(int[] origin) { + Stack stack = new Stack<>(); + + for (int num : origin) { + stack.push(num); + } + for (int i = 0; i < origin.length; i++) { + origin[i] = stack.pop(); + } + return origin; + } + + /** + * 3. 閫氳繃鍙嶅簭閬嶅巻瀹炵幇鍙嶈浆 + * @param origin + * @return + */ + public static int[] reverseArray_3(int[] origin) { + int[] result = new int[origin.length]; + for (int i = origin.length - 1; i >= 0; i--) { + result[origin.length - i - 1] = origin[i]; + } + return result; + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細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 static int[] removeZero_1(int[] oldArray){ + List list = new ArrayList<>(); + for (int num : oldArray) { + if (num != 0) { + list.add(num); + } + } + + int[] result = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + result[i] = list.get(i); + } + return result; + } + + /** + * 閫氳繃鍙屾寚閽堝疄鐜 + * @param oldArray + * @return + */ + public static int[] removeZero_2(int[] oldArray) { + int j = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + oldArray[j++] = oldArray[i]; + } + } + + return Arrays.copyOf(oldArray, j); + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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){ + return null; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + return Arrays.copyOf(oldArray, oldArray.length + size); + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + + List list = new ArrayList<>(); + int current = 0; + for (int i = 0; i < max; i++) { + if (i <= 1) { + current = 1; + } else { + current = list.get(i - 1) + list.get(i - 2); + } + if (current >= max) break; + list.add(current); + } + + int[] result = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + result[i] = list.get(i); + } + return result; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * 鍐欎簡涓绡囨枃绔狅細http://bosschow.github.io/2017/03/05/get-prime-number-1/ + * @param max + * @return + */ + public static int[] getPrimes(int max){ + List list = new ArrayList<>(); + + for (int i = 2; i < max; i++) { + boolean isPrime = true; + for (Integer primeNum : list) { + if (primeNum <= Math.sqrt(i)) { + if (i % primeNum == 0) { + isPrime = false; + break; + } + } else { + break; + } + } + + if (isPrime) { + list.add(i); + } + } + + int[] result = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + result[i] = list.get(i); + } + + return result; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + StringBuilder stringBuilder = new StringBuilder(); + + for (int i = 0; i < array.length; i++) { + if (i == array.length - 1) { + stringBuilder.append(array[i]); + } else { + stringBuilder.append(array[i] + seperator); + } + } + + return stringBuilder.toString(); + } + + +} diff --git a/group19/376248270/1-20170226-20170305/com/coderising/action/LoginAction.java b/group19/376248270/1-20170226-20170305/com/coderising/action/LoginAction.java new file mode 100644 index 0000000000..5496d8084d --- /dev/null +++ b/group19/376248270/1-20170226-20170305/com/coderising/action/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.action; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @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/group19/376248270/1-20170226-20170305/struts/Struts.java b/group19/376248270/1-20170226-20170305/struts/Struts.java new file mode 100644 index 0000000000..c431672037 --- /dev/null +++ b/group19/376248270/1-20170226-20170305/struts/Struts.java @@ -0,0 +1,245 @@ +package struts; + +import java.io.File; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class Struts { + + private static final String CONFIG_FILE = "struts/struts.xml"; + private static final String SETTER_METHOD_PREFIX = "set"; + private static final String GETTER_METHOD_PREFIX = "get"; + + private static Map actionList; + + static { + System.out.println("Loading struts config file: " + CONFIG_FILE); + actionList = parseConfigFile(CONFIG_FILE); + System.out.println("Load struts config success!"); + } + + /** + * 鑷祴 + * @param args + */ + public static void main(String[] args) { + Map map = new HashMap<>(); + map.put("name", "test"); + map.put("password", "1234"); + runAction("login", map); + } + + /** + * 杩愯鎸囧畾action + * @param actionName + */ + public static View runAction(String actionName, Map parameters) { + Action action = getActionByName(actionName); + View view = new View(); + + try { + Class cls = Class.forName(action.getClassName()); + Object actionInstance = cls.newInstance(); + + // 璁剧疆鍙傛暟 + setParameters(cls, actionInstance, parameters); + + // 鎵цexecute鏂规硶 + String executeResult = execute(cls, actionInstance); + + // 鑾峰彇灞炴 + String[] attributes = new String[]{"message"}; + Map results = getAttributes(cls, actionInstance, attributes); + + // 鑾峰彇JSP + String resultJsp = getResultJSP(action, executeResult); + + // 缁勮瑙嗗浘 + view.setJsp(resultJsp); + view.setParameters(results); + } catch (Exception e) { + e.printStackTrace(); + } + + return view; + } + + /** + * 鑾峰彇闇瑕佽繑鍥炵殑JSP鍚嶇О + * @param action + * @param executeResult + * @return + */ + private static String getResultJSP(Action action, String executeResult) { + Map resultMap = action.getResultMap(); + String resultJSP = ""; + + if (resultMap.containsKey(executeResult)) { + resultJSP = resultMap.get(executeResult); + } + return resultJSP; + } + + /** + * 鎵цaction鐨別xecute鏂规硶 + * @param cls + * @param instance + * @return + */ + private static String execute(Class cls, Object instance) { + String result = null; + try { + Method method = cls.getMethod("execute"); + result = (String) method.invoke(instance); + } catch(Exception e) { + e.printStackTrace(); + } + return result; + } + + /** + * 閫氳繃getter鏂规硶鑾峰彇瀹炰緥灞炴 + * @param cls + * @param instance + * @param parameterName + * @return + */ + private static Map getAttributes(Class cls, Object instance, String[] attributes) { + Map result = new HashMap<>(); + + try { + for (String attr : attributes) { + String methodName = GETTER_METHOD_PREFIX + capitalizeFirstLetter(attr); + Method method = cls.getMethod(methodName); + String returnValue = (String)method.invoke(instance); + result.put(attr, returnValue); + } + } catch(Exception e) { + e.printStackTrace(); + } + return result; + } + + /** + * 涓篴ction瀹炰緥璁剧疆灞炴 + * @param cls + * @param instance + * @param parameters + */ + private static void setParameters(Class cls, Object instance, Map parameters) { + for (String name : parameters.keySet()) { + String methodName = SETTER_METHOD_PREFIX + capitalizeFirstLetter(name); + try { + Method method = cls.getMethod(methodName, String.class); + method.invoke(instance, parameters.get(name)); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** + * 灏嗕紶鍏ョ殑瀛楃涓查瀛楁瘝澶у啓 + * @param inputString + * @return + */ + private static String capitalizeFirstLetter(String inputString) { + if (inputString == null || inputString.length() == 0) { + return inputString; + } + + return inputString.substring(0, 1).toUpperCase() + inputString.substring(1); + } + + /** + * 瑙f瀽struts閰嶇疆鏂囦欢 + * @param configFile + * @return + */ + private static Map parseConfigFile(String configFile) { + Map parseResult = new HashMap<>(); + + try { + File inputFile = new File(configFile); + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(inputFile); + NodeList nList = doc.getElementsByTagName("action"); + + for (int i = 0; i < nList.getLength(); i++) { + Action action = new Action(); + Map resultList = new HashMap<>(); + String actionName = ""; + + Node nNode = nList.item(i); + + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + Element eElement = (Element)nNode; + actionName = eElement.getAttribute("name"); + String className = eElement.getAttribute("class"); + + action.setName(actionName); + action.setClassName(className); + + NodeList nodeList = eElement.getElementsByTagName("result"); + for (int j = 0; j < nodeList.getLength(); j++) { + String resultName = ((Element)nodeList.item(j)).getAttribute("name"); + String resultValue = ((Element)nodeList.item(j)).getTextContent(); + resultList.put(resultName, resultValue); + } + action.setResultMap(resultList); + } + + parseResult.put(actionName, action); + } + } catch(Exception e) { + e.printStackTrace(); + } + + return parseResult; + } + + /** + * 杩斿洖鎸囧畾鐨刟ction + * @param actionName + * @return + */ + private static Action getActionByName(String actionName) { + return actionList.get(actionName); + } + + private static class Action { + String name; + String className; + Map resultMap; + + public String getName() { + return name; + } + public String getClassName() { + return className; + } + public Map getResultMap() { + return resultMap; + } + public void setName(String name) { + this.name = name; + } + public void setClassName(String className) { + this.className = className; + } + public void setResultMap(Map resultList) { + this.resultMap = resultList; + } + } + +} diff --git a/group19/376248270/1-20170226-20170305/struts/StrutsTest.java b/group19/376248270/1-20170226-20170305/struts/StrutsTest.java new file mode 100644 index 0000000000..3a4d69ee20 --- /dev/null +++ b/group19/376248270/1-20170226-20170305/struts/StrutsTest.java @@ -0,0 +1,43 @@ +package struts; + +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/group19/376248270/1-20170226-20170305/struts/View.java b/group19/376248270/1-20170226-20170305/struts/View.java new file mode 100644 index 0000000000..4e006ba71e --- /dev/null +++ b/group19/376248270/1-20170226-20170305/struts/View.java @@ -0,0 +1,23 @@ +package struts; + +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/group19/376248270/1-20170226-20170305/struts/struts.xml b/group19/376248270/1-20170226-20170305/struts/struts.xml new file mode 100644 index 0000000000..dd598a3664 --- /dev/null +++ b/group19/376248270/1-20170226-20170305/struts/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/group19/376248270/README.md b/group19/376248270/README.md new file mode 100644 index 0000000000..e7b15ad6f0 --- /dev/null +++ b/group19/376248270/README.md @@ -0,0 +1,3 @@ +### Profile +- nickname: kian +- QQ: 376248270 diff --git a/group19/527220084/xukai_coding/.gitignore b/group19/527220084/xukai_coding/.gitignore index ba13ec60db..a269ef720f 100644 --- a/group19/527220084/xukai_coding/.gitignore +++ b/group19/527220084/xukai_coding/.gitignore @@ -5,4 +5,15 @@ *iml .idea *gen-* -rebel.xml \ No newline at end of file + +*.class + +.metadata +.recommenders + +#macOS +.DS_Store + +rebel.* +.rebel.* + diff --git a/group19/527220084/xukai_coding/coding-common/pom.xml b/group19/527220084/xukai_coding/coding-common/pom.xml index ab70ddd636..023093e4d7 100644 --- a/group19/527220084/xukai_coding/coding-common/pom.xml +++ b/group19/527220084/xukai_coding/coding-common/pom.xml @@ -16,10 +16,6 @@ junit junit - - org.freemarker - freemarker - commons-beanutils commons-beanutils @@ -82,8 +78,14 @@ joda-time joda-time - - + + dom4j + dom4j + + + jaxen + jaxen + diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/array/ArrayUtil.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..3edaebe9ef --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/array/ArrayUtil.java @@ -0,0 +1,306 @@ +package org.xukai.coderising.array; + +import org.junit.Test; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + for (int i = 0; i < origin.length; i++) { + if (i == origin.length/2) { + break; + } + swap(origin,i,origin.length - 1 -i); + } + } + private void swap(int[] array,int a,int b){ + array[a] = array[a] + array[b]; + array[b] = array[a] - array[b]; + array[a] = array[a] - array[b]; + } + + @Test + public void testReverseArray() { + int[] test = new int[]{7, 9, 30,-11, 3, 4}; + reverseArray(test); + for (int i = 0; i < test.length; i++) { + System.out.println(test[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 nullIndex = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + nullIndex ++; + } + } + int[] newArray = new int[nullIndex]; + nullIndex = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArray[nullIndex] = oldArray[i]; + nullIndex++; + } + } + return newArray; + } + @Test + public void removeZero() { + int[] test = new int[]{1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] newArray = removeZero(test); + for (int i = 0; i < newArray.length; i++) { + System.out.println(newArray[i]); + } + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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 index_a = 0; + int index_b = 0; + int[] array = new int[array1.length + array2.length]; + int i = 0 ; + while(index_a < array1.length && index_b < array2.length){ + if (array1[index_a] < array2[index_b]) { + array[i] = array1[index_a]; + index_a++; + i++; + } else if(array1[index_a] > array2[index_b]) { + array[i] = array2[index_b]; + index_b++; + i++; + } else { + array[i] = array1[index_a]; + i++; + index_a++; + index_b++; + } + } + if (index_a == array1.length) { + for (int j = index_b; j < array2.length; j++) { + array[i] = array2[j]; + i++; + } + } else { + for (int j = index_a; j < array1.length; j++) { + array[i] = array1[j]; + i++; + } + } + int[] result = new int[i]; + System.arraycopy(array,0,result,0,i); + return result; + } + + @Test + public void testMerge() { + int[] test1 = new int[]{2,3, 5, 7,8}; + int[] test2 = new int[]{4, 5, 6,7,8,21,33}; + int[] newArray = merge(test1,test2); + for (int i = 0; i < newArray.length; i++) { + System.out.println(newArray[i]); + } + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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; + } + + @Test + public void testGrow() { + int[] test1 = new int[]{2,3, 5, 7,8}; + int[] newArray = grow(test1, 5); + for (int i = 0; i < newArray.length; i++) { + System.out.println(newArray[i]); + } + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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 index_a = 1; + int index_b = 1; + int temp = 0; + int count = 2; + while ((temp = index_a + index_b) < max) { + index_a = index_b; + index_b = temp; + count++; + } + int[] newArray = new int[count]; + for (int i = count-1; i > -1 ; i--) { + newArray[i] = index_b; + temp = index_b - index_a; + index_b = index_a; + index_a = temp; + } + return newArray; + } + + @Test + public void testfibonacci() { + int[] newArray = fibonacci(15); + for (int i = 0; i < newArray.length; i++) { + System.out.println(newArray[i]); + } + } + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + int[] container = new int[5]; + int count = 0; + for (int i = 3; i < max; i++) { + if (isShusu(i)) { + if (count == container.length) { + container = grow(container,container.length << 1); + } + container[count] = i; + count++; + } + + } + int[] array = new int[count]; + System.arraycopy(container,0,array,0,count); + return array; + } + @Test + public void testGetPrimes() { + int[] newArray = getPrimes(4); + for (int i = 0; i < newArray.length; i++) { + System.out.println(newArray[i]); + } + } + private boolean isShusu(int num){ + int sqrt = 1; + while (sqrt * sqrt < num){ + sqrt++; + } + for (int i = 2; i < sqrt; i++) { + if (num % i == 0) { + return false; + } + } + return true; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] container = new int[5]; + int count = 0; + for (int i = 1; i < max; i++) { + if (isWanshu(i)) { + if (count == container.length) { + container = grow(container,container.length << 1); + } + container[count] = i; + count++; + } + + } + int[] array = new int[count]; + System.arraycopy(container,0,array,0,count); + return array; + } + @Test + public void testGetPerfectNumbers() { + int[] newArray = getPerfectNumbers(29); + for (int i = 0; i < newArray.length; i++) { + System.out.println(newArray[i]); + } +// isWanshu(6); + } + private boolean isWanshu(int num){ + int sqrt = 1; + while (sqrt * sqrt < num){ + sqrt++; + } + int sum = 1; + for (int i = 2; i < sqrt; i++) { + if (num % i == 0 ) { + sum = sum + i + (num/i); + } + } + if (sum == num) { + return true; + } + return false; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param + * @return + */ + public String join(int[] array, String seperator){ + String result = ""; + + for (int i = 0; i < array.length; i++) { + if (i != 0) { + result = result + seperator; + } + result = result + array[i]; + } + + return result; + } + @Test + public void testJoin() { + int[] test = {1, 5, 8, 4}; + String seperator = "-"; + String join = join(test, seperator); + System.out.println(join); + } + +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/Action.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/Action.java new file mode 100644 index 0000000000..3a354754a4 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/Action.java @@ -0,0 +1,41 @@ +package org.xukai.coderising.litestruts; + +import java.util.Map; + +/** + * @author xukai + * @desc + * @date 2017-02-27-涓嬪崍 2:59 + */ +public class Action { + + private String name; + + private Class aClass; + + private Map resultMapping; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Class getaClass() { + return aClass; + } + + public void setaClass(Class aClass) { + this.aClass = aClass; + } + + public Map getResultMapping() { + return resultMapping; + } + + public void setResultMapping(Map resultMapping) { + this.resultMapping = resultMapping; + } +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/BusinessException.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/BusinessException.java new file mode 100644 index 0000000000..29a74ec3be --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/BusinessException.java @@ -0,0 +1,29 @@ +package org.xukai.coderising.litestruts; + +/** + * 涓氬姟閫昏緫寮傚父 + */ +public class BusinessException extends Exception { + + private static final long serialVersionUID = 1L; + + public BusinessException() { + + } + + public BusinessException(String message) { + super(message); + + } + + public BusinessException(Throwable cause) { + super(cause); + + } + + public BusinessException(String message, Throwable cause) { + super(message, cause); + + } + +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/LoginAction.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..2681d32a1c --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package org.xukai.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/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/LogoutAction.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/LogoutAction.java new file mode 100644 index 0000000000..a40fe43dc4 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/LogoutAction.java @@ -0,0 +1,39 @@ +package org.xukai.coderising.litestruts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * @author liuxin + * + */ +public class LogoutAction { + 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/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/Struts.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..524b0634e8 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/Struts.java @@ -0,0 +1,133 @@ +package org.xukai.coderising.litestruts; + +import org.apache.commons.lang.StringUtils; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.xukai.coderising.util.ReflectUtil; +import org.xukai.coderising.util.XmlParseHelper; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + + +public class Struts { + + private static final ConcurrentHashMap action= new ConcurrentHashMap(); + + private static String strutsPath = Thread.currentThread().getContextClassLoader().getResource("struts.xml").getPath().substring(1); + + private static List actions; + + static { + try { + actions = initalStruts(); + System.out.println(actions.size()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static View runAction(String actionName, Map parameters) { + if (actionName != null && !actionName.equals("")) { + for(Action action : actions){ + //鏍规嵁璇锋眰鍙傛暟瀹氫綅鍒伴渶瑕佹墽琛岀殑action + if (action.getName().equals(actionName)) { + Class cls = action.getaClass(); + View view = new View(); + try { + //鍒涘缓action瀹炰緥 + Object obj = ReflectUtil.newInstance(cls); + for(Map.Entry entry : parameters.entrySet()){ + ReflectUtil.setValue(obj,entry.getKey(),entry.getValue()); + } + Method method = ReflectUtil.getMethod(cls,"execute"); + if (method != null) { + //鎵цexecute鏂规硶 + Object result = ReflectUtil.invokeMethod(obj, method); + if (result instanceof String) { + Map mapping = action.getResultMapping(); + //鏍规嵁鏄犲皠鍏崇郴灏嗗兼斁鍏 + view.setJsp(mapping.get(result)); + List gets = ReflectUtil.getMethodBeginWith(cls, "get"); + HashMap models = new HashMap<>(); + for(Method getMethod : gets){ + //璋冪敤鎵鏈塯etter鏂规硶 + Object getFieldResult = ReflectUtil.invokeMethod(obj, getMethod); + if (result instanceof String) { + String getFieldName = StringUtils.lowerCase((String) getMethod.getName()).substring(3); + models.put(getFieldName,(String) getFieldResult); + } + } + view.setParameters(models); + } + } + } catch (Exception e) { + e.printStackTrace(); + view.setJsp("500"); + } + return view; + } + } + } + //璇锋眰鐨勫弬鏁颁笉姝g‘鎴栬呮病鏈夊搴斿疄渚嬭繑鍥404 + View view = new View(); + view.setJsp("404"); + return view; + } + + private static List initalStruts() throws ClassNotFoundException, DocumentException { + SAXReader sr = new SAXReader();//鑾峰彇璇诲彇鏂瑰紡 + + Document doc = sr.read(strutsPath); + XmlParseHelper helper = new XmlParseHelper(doc); + List actions = helper.getNodeByPath("//action"); + ArrayList actionsList = new ArrayList<>(); + for (Element action : actions){ + Action obj = new Action(); + String nameAttr = helper.getNodeAttrValue(action, "name"); + String classAttr = helper.getNodeAttrValue(action, "class"); + obj.setName(nameAttr); + obj.setaClass(Class.forName(classAttr)); + List results = helper.getChildNodeByName(action, "result"); + HashMap map = new HashMap<>(); + for (Element result : results){ + String resultNameAttr = helper.getNodeAttrValue(result, "name"); + String resultValue = helper.getNodeValue(result); + map.put(resultNameAttr,resultValue); + } + obj.setResultMapping(map); + actionsList.add(obj); + } + + return actionsList; + } + + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/StrutsTest.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..53bf155c33 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/StrutsTest.java @@ -0,0 +1,81 @@ +package org.xukai.coderising.litestruts; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.junit.Assert; +import org.junit.Test; +import org.xukai.coderising.util.XmlParseHelper; + + +public class StrutsTest { + + private String strutsPath = Thread.currentThread().getContextClassLoader().getResource("struts.xml").getPath() + .substring(1); + + @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")); + } + + @Test + public void testInital() throws ClassNotFoundException { + SAXReader sr = new SAXReader();//鑾峰彇璇诲彇鏂瑰紡 + try { + Document doc = sr.read(strutsPath); + XmlParseHelper helper = new XmlParseHelper(doc); + List actions = helper.getNodeByPath("//action"); + ArrayList actionsList = new ArrayList<>(); + for (Element action : actions){ + Action obj = new Action(); + String nameAttr = helper.getNodeAttrValue(action, "name"); + String classAttr = helper.getNodeAttrValue(action, "class"); + obj.setName(nameAttr); + obj.setaClass(Class.forName(classAttr)); + List results = helper.getChildNodeByName(action, "result"); + for (Element result : results){ + String resultNameAttr = helper.getNodeAttrValue(result, "name"); + String resultValue = helper.getNodeValue(result); + HashMap map = new HashMap<>(); + map.put("name",resultNameAttr); + map.put("viewPath",resultValue); + obj.setResultMapping(map); + } + actionsList.add(obj); + } + + } catch (DocumentException e) { + e.printStackTrace(); + } + } +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/View.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/View.java new file mode 100644 index 0000000000..4ce909e87c --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/litestruts/View.java @@ -0,0 +1,23 @@ +package org.xukai.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/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/util/ReflectUtil.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/util/ReflectUtil.java new file mode 100644 index 0000000000..7bca0d6506 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/util/ReflectUtil.java @@ -0,0 +1,148 @@ +package org.xukai.coderising.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +/** + * @author xukai + * @desc + * @date 2017-02-27-涓嬪崍 4:19 + */ +public class ReflectUtil { + + private static final Logger logger = LoggerFactory.getLogger(ReflectUtil.class); + + public static Object newInstance(Class cls){ + Object instance = null; + try { + instance = cls.newInstance(); + } catch (Exception e) { + logger.error("new instance failure",e); + throw new RuntimeException(e); + } + return instance; + } + + + public static Object invokeMethod(Object obj, Method method,Object... args){ + Object result = null; + try { + method.setAccessible(true); + result = method.invoke(obj, args); + } catch (Exception e) { + logger.error("invoke method failure",e); + throw new RuntimeException(e); + } + return result; + } + + public static Method getMethod(Class cls, String methodName){ + Method result = null; + try { + Method[] methods = cls.getDeclaredMethods(); + for(Method method : methods){ + if (method.getName().equals(methodName)) { + result = method; + } + } + } catch (Exception e) { + logger.error("get method failure",e); + throw new RuntimeException(e); + } + return result; + } + + public static List getMethodBeginWith(Class cls, String methodName){ + ArrayList methodsList = new ArrayList<>(); + try { + Method[] methods = cls.getDeclaredMethods(); + for(Method method : methods){ + if (method.getName().startsWith(methodName)) { + methodsList.add(method); + } + } + } catch (Exception e) { + logger.error("get methods failure",e); + throw new RuntimeException(e); + } + return methodsList; + } + + public static void setField(Object obj, Field field, Object values){ + try { + field.setAccessible(true); + field.set(obj,values); + } catch (Exception e) { + logger.error("set field failure",e); + throw new RuntimeException(e); + } + } + + /** + * 閫氳繃鍙嶅皠鍙栧璞℃寚瀹氬瓧娈(灞炴)鐨勫 + * @param target 鐩爣瀵硅薄 + * @param fieldName 瀛楁鐨勫悕瀛 + * @throws RuntimeException 濡傛灉鍙栦笉鍒板璞℃寚瀹氬瓧娈电殑鍊煎垯鎶涘嚭寮傚父 + * @return 瀛楁鐨勫 + */ + public static Object getValue(Object target, String fieldName) { + Class clazz = target.getClass(); + String[] fs = fieldName.split("\\."); + + try { + for(int i = 0; i < fs.length - 1; i++) { + Field f = clazz.getDeclaredField(fs[i]); + f.setAccessible(true); + target = f.get(target); + clazz = target.getClass(); + } + + Field f = clazz.getDeclaredField(fs[fs.length - 1]); + f.setAccessible(true); + return f.get(target); + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * 閫氳繃鍙嶅皠缁欏璞$殑鎸囧畾瀛楁璧嬪 + * @param target 鐩爣瀵硅薄 + * @param fieldName 瀛楁鐨勫悕绉 + * @param value 鍊 + */ + public static void setValue(Object target, String fieldName, Object value) { + Class clazz = target.getClass(); + String[] fs = fieldName.split("\\."); + try { + for(int i = 0; i < fs.length - 1; i++) { + Field f = clazz.getDeclaredField(fs[i]); + f.setAccessible(true); + Object val = f.get(target); + if(val == null) { + Constructor c = f.getType().getDeclaredConstructor(); + c.setAccessible(true); + val = c.newInstance(); + f.set(target, val); + } + target = val; + clazz = target.getClass(); + } + + Field f = clazz.getDeclaredField(fs[fs.length - 1]); + f.setAccessible(true); + f.set(target, value); + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/util/XmlParseHelper.java b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/util/XmlParseHelper.java new file mode 100644 index 0000000000..71b200aff8 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/java/org/xukai/coderising/util/XmlParseHelper.java @@ -0,0 +1,44 @@ +package org.xukai.coderising.util; + +import org.dom4j.Document; +import org.dom4j.Element; +import org.dom4j.xpath.DefaultXPath; + +import java.util.List; + +/** + * @author xukai + * @desc + * @date 2017-02-27-涓嬪崍 2:28 + */ +public class XmlParseHelper { + + private Document doc; + + private DefaultXPath xPath; + + public XmlParseHelper(Document doc) { + if (doc == null) { + throw new RuntimeException("鏋勯爔ml瑙f瀽鍣ㄥ嚭閿"); + } + this.doc = doc; + } + + public List getNodeByPath(String path){ + xPath = new DefaultXPath(path); + return xPath.selectNodes(doc); + } + + public List getChildNodeByName(Element parentNode,String childNodeName){ + xPath = new DefaultXPath("./" + childNodeName); + return xPath.selectNodes(parentNode); + } + + public String getNodeAttrValue(Element node,String attrName){ + return node.attributeValue(attrName); + } + + public String getNodeValue(Element node){ + return node.getTextTrim(); + } +} diff --git a/group19/527220084/xukai_coding/coding-common/src/main/resources/struts.xml b/group19/527220084/xukai_coding/coding-common/src/main/resources/struts.xml new file mode 100644 index 0000000000..248d8bf7a5 --- /dev/null +++ b/group19/527220084/xukai_coding/coding-common/src/main/resources/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/group19/527220084/xukai_coding/pom.xml b/group19/527220084/xukai_coding/pom.xml index f8e396ae46..fc66b7c29c 100644 --- a/group19/527220084/xukai_coding/pom.xml +++ b/group19/527220084/xukai_coding/pom.xml @@ -50,6 +50,8 @@ 3.1 4.0 2.8.0 + 1.1.1 + 1.6.1 @@ -281,7 +283,7 @@ dom4j dom4j - 1.6.1 + ${dom4j.version} javax.servlet @@ -345,6 +347,11 @@ jedis ${jedis.version}} + + jaxen + jaxen + ${jaxen.version} + diff --git a/group19/709960951/CodeLearning/lib/dom4j-1.6.1.jar b/group19/709960951/CodeLearning/lib/dom4j-1.6.1.jar new file mode 100644 index 0000000000..c8c4dbb92d Binary files /dev/null and b/group19/709960951/CodeLearning/lib/dom4j-1.6.1.jar differ diff --git a/group19/709960951/CodeLearning/src/com/coderising/array/ArrayUtil.java b/group19/709960951/CodeLearning/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..afd98cbfeb --- /dev/null +++ b/group19/709960951/CodeLearning/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,218 @@ +package com.coderising.array; + +import java.util.LinkedList; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + if(origin==null) + { + return ; + } + int from=0; + int to=origin.length-1; + while(from results; + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getActionClass() { + return actionClass; + } + public void setActionClass(String actionClass) { + this.actionClass = actionClass; + } + public Map getResults() { + return results; + } + public void setResults(Map results) { + this.results = results; + } + +} diff --git a/group19/709960951/CodeLearning/src/com/coderising/litestruts/Struts.java b/group19/709960951/CodeLearning/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..536a6be705 --- /dev/null +++ b/group19/709960951/CodeLearning/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,91 @@ +package com.coderising.litestruts; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + StrutsManager sm=StrutsManager.getInstance(); + StructsAction actionConfig=sm.getAction(actionName); + Class actionClz; + try { + actionClz = Class.forName(actionConfig.getActionClass()); + Object actionObj=actionClz.newInstance(); + //璁剧疆鍙傛暟鍊 + for(String key:parameters.keySet()) + { + if(key!=null && key.length()>0) + { + String value=parameters.get(key); + String methodName="set"+key.substring(0, 1).toUpperCase()+key.substring(1); + Method method=actionClz.getMethod(methodName, String.class); + method.invoke(actionObj, value); + } + } + //鎵ц + Method executeMethod=actionClz.getMethod("execute"); + String result=(String)executeMethod.invoke(actionObj); + + //鐢熸垚View瀵硅薄 + View view=new View(); + String jsp=actionConfig.getResults().get(result); + view.setJsp(jsp); + //璁剧疆View鐨刾arameters + Map vParams=new HashMap<>(); + for(Method method:actionClz.getMethods()) + { + String methodName=method.getName(); + if(methodName.startsWith("get")) + { + Object paramValue=method.invoke(actionObj); + String paramKey=methodName.substring(3, 4).toLowerCase()+methodName.substring(4); + vParams.put(paramKey, paramValue); + } + } + view.setParameters(vParams); + return view; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return new View(); + } + +} diff --git a/group19/709960951/CodeLearning/src/com/coderising/litestruts/StrutsManager.java b/group19/709960951/CodeLearning/src/com/coderising/litestruts/StrutsManager.java new file mode 100644 index 0000000000..1af4b38bdc --- /dev/null +++ b/group19/709960951/CodeLearning/src/com/coderising/litestruts/StrutsManager.java @@ -0,0 +1,93 @@ +package com.coderising.litestruts; + +import java.io.File; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + + +public class StrutsManager { + private static StrutsManager instance; + private Map actions; + private StrutsManager(){} + public static StrutsManager getInstance() + { + if(instance==null) + { + synchronized (StrutsManager.class) { + if(instance==null) + { + instance=new StrutsManager(); + instance.readStrutsXml(); + } + + } + } + return instance; + } + + /** + * 璇诲彇struts閰嶇疆鏂囦欢 + */ + private void readStrutsXml() { + actions = new HashMap<>(); + String path = StrutsManager.class.getResource("struts.xml").getFile(); + SAXReader reader = new SAXReader(); + Document doc; + try { + doc = reader.read(new File(path)); + Element root = doc.getRootElement(); + //閬嶅巻action鑺傜偣 + for(Iterator childIter=root.elementIterator("action");childIter.hasNext();) + { + StructsAction action=new StructsAction(); + Element ele=(Element)childIter.next(); + String name=ele.attributeValue("name"); + String className=ele.attributeValue("class"); + //System.out.println(name+":"+className); + action.setName(name); + action.setActionClass(className); + Map results=new HashMap<>(); + //閬嶅巻result鑺傜偣 + for(Iterator resultIter=ele.elementIterator("result");resultIter.hasNext();) + { + Element resultEle=(Element)resultIter.next(); + String resultName=resultEle.attributeValue("name"); + String resultUrl=resultEle.getText(); + results.put(resultName, resultUrl); + //System.out.println(resultName+":"+resultUrl); + } + action.setResults(results); + actions.put(action.getName(), action); + } + } catch (DocumentException e) { + e.printStackTrace(); + } + } + + public StructsAction getAction(String name) + { + return actions.get(name); + } + + public static void main(String[] argv) { + + StrutsManager instance=StrutsManager.getInstance(); + StructsAction actionObj=instance.getAction("login"); + System.out.println(actionObj.getName()); + System.out.println(actionObj.getActionClass()); + System.out.println(actionObj.getResults().get("success")); + System.out.println(actionObj.getResults().get("fail")); + try { + Class.forName("com.coderising.litestruts.LoginAction"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } +} diff --git a/group19/709960951/CodeLearning/src/com/coderising/litestruts/StrutsTest.java b/group19/709960951/CodeLearning/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..b8c81faf3c --- /dev/null +++ b/group19/709960951/CodeLearning/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +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() { + + 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/group19/709960951/CodeLearning/src/com/coderising/litestruts/View.java b/group19/709960951/CodeLearning/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group19/709960951/CodeLearning/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/group19/709960951/CodeLearning/src/com/coderising/litestruts/struts.xml b/group19/709960951/CodeLearning/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group19/709960951/CodeLearning/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 diff --git a/group19/972815123/.classpath b/group19/972815123/.classpath new file mode 100644 index 0000000000..fceb4801b5 --- /dev/null +++ b/group19/972815123/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/group19/972815123/.project b/group19/972815123/.project new file mode 100644 index 0000000000..aec36208a6 --- /dev/null +++ b/group19/972815123/.project @@ -0,0 +1,17 @@ + + + Homework + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group19/972815123/.settings/org.eclipse.jdt.core.prefs b/group19/972815123/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..3a21537071 --- /dev/null +++ b/group19/972815123/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +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/group19/972815123/src/com/coderising/array/ArrayUtil.java b/group19/972815123/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..fca1de56e4 --- /dev/null +++ b/group19/972815123/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,221 @@ +package com.coderising.array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + if(origin == null || origin.length <= 1){ + return; + } + int tem = 0; + for(int i = 0, len = origin.length; i < len/2; i ++){ + tem = origin[i]; + origin[i] = origin[len - i + 1]; + origin[len - i + 1] = tem; + } + + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 withoutZeroSize = 0; + for(int i = 0, len = oldArray.length; i < len; i ++ ){ + if( oldArray[i] == 0){ + withoutZeroSize ++; + } + } + int[] newArray = new int[withoutZeroSize]; + int point = 1; + for(int i = 0 ,len = oldArray.length; i < len; i ++ ){ + if( oldArray[i] != 0){ + newArray[point] = oldArray[i]; + point ++; + } + } + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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 point2 = 0; + int[] result = new int[array1.length + array2.length]; + int point1 = 0, len1 = array1.length; + while(point1 < len1){ + if(array1[point1] < array2[point2]){ + result[point1 + point2] = array1[point1]; + point1 ++; + }else{ + result[point1 + point2] = array2[point2]; + point2 ++; + } + } + + 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]; + for(int i = 0, len = oldArray.length; i < len; i ++){ + newArray[i] = oldArray[i]; + } + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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[] result = {1,1,2}; + int a1 = 1, a2 = 2; + if(max <= 1){ + return null; + }else if(max == 2){ + return result; + }else{ + result = grow(result, 10); + int index = 2; + while(result[index] > max){ + if(result.length < index + 2){ + result = grow(result, 10); + } + result[index + 1] = result[index -1] + result[index]; + index ++; + } + } + return result; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + int[] temArr = null; + if(max < 2){ + return null; + }else if (max == 2){ + int[] re = {2}; + return re; + }else{ + temArr = new int[max/2]; + temArr[0] = 2; + int index = 1; + for(int i = 3; i < max ; i= i+2){ + boolean flag = true; + int isql = (int) Math.sqrt(i); + for(int j = 3; j < isql; j++){ + if(i % j == 0){ + flag = false; + } + } + if(flag){ + temArr[index] = i; + index ++; + } + } + } + temArr = this.removeZero(temArr); + return temArr; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + int[] result = new int[10]; + int index = 0; + if(max < 6){ + return null; + }else{ + } + for (int n = 6; n <= max ; n ++){ + int[] allPrimeFactore = getPrimeFactors(n); + int sum = 0; + for(int i = 0, len = allPrimeFactore.length; i < len; i ++){ + sum += allPrimeFactore[i]; + } + if(sum == n){ + if(result.length < index + 1){ + result = this.grow(result, 1); + } + result[index] = n; + index ++; + } + + } + return removeZero(result); + } + + private int[] getPrimeFactors(int n){ + int[] allPrimes = getPrimes(n); + int[] result = new int[allPrimes.length]; + int index = 0; + for(int i = 0, len = allPrimes.length; i < len; i ++){ + int devide = n; + while(devide % allPrimes[i] == 0){ + devide = devide / allPrimes[i]; + result[index] = allPrimes[i]; + index ++; + } + } + return this.removeZero(result); + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator){ + StringBuffer sb = new StringBuffer(); + for(int i = 0, len = array.length; i < len; i ++){ + if(i != 0){ + sb.append(seperator); + } + sb.append(array[i]); + } + return sb.toString(); + } + + +} diff --git a/group19/972815123/src/com/coderising/litestruts/LoginAction.java b/group19/972815123/src/com/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..22f70d59e8 --- /dev/null +++ b/group19/972815123/src/com/coderising/litestruts/LoginAction.java @@ -0,0 +1,45 @@ +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(){ + System.out.println("execute"); + if("test".equals(name) && "1234".equals(password)){ + this.message = "login successful"; + System.out.println("successful"); + + return "success"; + } + System.out.println("failed"); + this.message = "login failed,please check your user/pwd"; + return "fail"; + } + + public void setName(String name){ + this.name = name; + System.out.println("***name***"); + } + public void setPassword(String password){ + System.out.println("***pswd***"); + this.password = password; + } + public String getMessage(){ + return this.message; + } +} diff --git a/group19/972815123/src/com/coderising/litestruts/Main.java b/group19/972815123/src/com/coderising/litestruts/Main.java new file mode 100644 index 0000000000..66f3e67edd --- /dev/null +++ b/group19/972815123/src/com/coderising/litestruts/Main.java @@ -0,0 +1,9 @@ +package com.coderising.litestruts; + +public class Main { + + public static void main(String[] args) { + Struts.runAction(null, null); + } + +} diff --git a/group19/972815123/src/com/coderising/litestruts/Struts.java b/group19/972815123/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..7d908282b6 --- /dev/null +++ b/group19/972815123/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,155 @@ +package com.coderising.litestruts; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; +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 com.coding.basic.Iterator; + + + +public class Struts { + private static String url = "C:\\Users\\alioc\\workspace\\Homework\\src\\com\\coderising\\litestruts\\struts.xml"; + private static int SET = 1; + private static int GET = -1; + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + public static View runAction(String actionName, Map parameters) { + HashMap> xml = parseStrutsXml(url); + HashMap login = xml.get("login"); + String loginClassName = login.get("class"); + System.out.println(loginClassName); + + ClassLoader loader = ClassLoader.getSystemClassLoader(); + try { + Class clazz = Class.forName(loginClassName); + Object obj = clazz.newInstance(); + java.util.Iterator iter = parameters.keySet().iterator(); + while(iter.hasNext()){ + String key = iter.next(); + String val = parameters.get(key); + Method method = clazz.getDeclaredMethod(getEncapsulateMethodName(SET, key), String.class); + method.invoke(obj, val); + } + Method executeMethod = clazz.getDeclaredMethod("execute"); + String logResult = (String) executeMethod.invoke(obj); + if("success".equals(logResult)){ + View view = new View(); + view.setJsp(login.get("success")); + + Method getMessageMethod = clazz.getDeclaredMethod(getEncapsulateMethodName(GET, "message")); + String message = (String) getMessageMethod.invoke(obj); + +// Field messageField = clazz.getDeclaredField("message"); +// messageField.setAccessible(true); +// String message = (String) messageField.get(clazz); + Map map = new HashMap<>(); + map.put("message", message); + view.setParameters(map); + + return view; + } + } catch (Exception e) { + e.printStackTrace(); + } + + + + + + + return null; + } + + private static HashMap> parseStrutsXml(String url){ + + HashMap> result = new HashMap>(); + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + try { + DocumentBuilder builder = dbf.newDocumentBuilder(); + InputStream is = new FileInputStream(url); + Document doc = builder.parse(is); + Element root = doc.getDocumentElement(); + + NodeList actionNodes = root.getChildNodes(); + for(int i = 0; i < actionNodes.getLength(); i ++){ + Node actionNode = actionNodes.item(i); + if(actionNode.getNodeType() == Node.ELEMENT_NODE){ + if(actionNode.getAttributes() != null){ + HashMap action = new HashMap<>(); + String name = actionNode.getAttributes().getNamedItem("name").getNodeValue(); + String clazz = actionNode.getAttributes().getNamedItem("class").getNodeValue(); + result.put(name, action); + action.put("class", clazz); + + + NodeList resultNodes = actionNode.getChildNodes(); + + for(int j = 0; j < resultNodes.getLength(); j ++){ + Node resultNode = resultNodes.item(j); + if(resultNode.getNodeType() == Node.ELEMENT_NODE){ + String fieldName = resultNode.getAttributes().getNamedItem("name").getNodeValue(); + String fieldValue = resultNode.getTextContent(); + action.put(fieldName, fieldValue); + } + } + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("--------------------"); + System.out.println(result); + return result; + } + + private static String getEncapsulateMethodName(int type, String methodName){ + StringBuffer sb = new StringBuffer(); + if(type < 0){ + sb.append("get"); + }else{ + sb.append("set"); + } + sb.append(methodName.substring(0, 1).toUpperCase()); + sb.append(methodName.substring(1)); + return sb.toString(); + } +} diff --git a/group19/972815123/src/com/coderising/litestruts/StrutsTest.java b/group19/972815123/src/com/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..561885ccaf --- /dev/null +++ b/group19/972815123/src/com/coderising/litestruts/StrutsTest.java @@ -0,0 +1,44 @@ +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() { + + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + + View view = Struts.runAction(actionName,params); + System.out.println(view.getJsp()); + + 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/group19/972815123/src/com/coderising/litestruts/View.java b/group19/972815123/src/com/coderising/litestruts/View.java new file mode 100644 index 0000000000..07df2a5dab --- /dev/null +++ b/group19/972815123/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/group19/972815123/src/com/coderising/litestruts/struts.xml b/group19/972815123/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e5d9aebba8 --- /dev/null +++ b/group19/972815123/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 diff --git a/group20/1040154728/1040154728Learning/src/ArrayList.java b/group20/1040154728/1040154728Learning/src/ArrayList.java new file mode 100644 index 0000000000..77fed2b0f4 --- /dev/null +++ b/group20/1040154728/1040154728Learning/src/ArrayList.java @@ -0,0 +1,46 @@ +public class ArrayList implements List { + //private fields + private int size = 0; + private Object[] elementData = new Object[100]; + //check if list is empty + public boolean isEmpty() { + return size == 0; + } + + public void add(Object o){ + elementData[size++] = o; + } + public void add(int index, T o){ + /* Not familiar with array copy, copied from GitMori */ + System.arraycopy(elementData, index, elementData, + index + 1, size-index); + elementData[index] = o; + size++; + } + + public T get(int index){ + return (T) elementData[index]; + } + + public T remove(int index){ + T t = this.get(index); + int position = size - index - 1; //why ? + if (position > 0){ + System.arraycopy(elementData, index+1, elementData, + index, position); + } + elementData[--size] = null; + return t; + } + + public int size(){ + return size; + } + + //?? + public Iterator iterator(){ + return null; + } + + +} diff --git a/group20/1040154728/1040154728Learning/src/BinaryTreeNode.java b/group20/1040154728/1040154728Learning/src/BinaryTreeNode.java new file mode 100644 index 0000000000..425894819d --- /dev/null +++ b/group20/1040154728/1040154728Learning/src/BinaryTreeNode.java @@ -0,0 +1,28 @@ +public class BinaryTreeNode { + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private int size; + public T getData() { + return data; + } + + public void setData(T 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; + } + + + +} diff --git a/group20/1040154728/1040154728Learning/src/Iterator.java b/group20/1040154728/1040154728Learning/src/Iterator.java new file mode 100644 index 0000000000..0e1a09433b --- /dev/null +++ b/group20/1040154728/1040154728Learning/src/Iterator.java @@ -0,0 +1,5 @@ +public interface Iterator { + public boolean hasNext(); + public T next(); + +} diff --git a/group20/1040154728/1040154728Learning/src/LinkedList.java b/group20/1040154728/1040154728Learning/src/LinkedList.java new file mode 100644 index 0000000000..18f6fddf37 --- /dev/null +++ b/group20/1040154728/1040154728Learning/src/LinkedList.java @@ -0,0 +1,110 @@ +public class LinkedList implements List { + + private Node head; + private Node tail; + private int size; + + public void add(T o){ + Node node = new Node(o); + if(head == null) + { + head = node; + } + else + { + tail.next = node; + } + tail = node; + tail.next = null; + size++; + + } + public void add(int index , T o){ + Node node = new Node(o); + Node temp = head; + Node tempTemp = null; + for(int i = 0; i <= index; i++) + { + temp = temp.next; + } + tempTemp = temp.next; + temp.next = node; + node.next = tempTemp; + size++; + } + public T get(int index){ + Node temp = head; + for(int i = 0; i <= index; i++) + { + temp = temp.next; + } + return (T)temp.data; + } + public T remove(int index){ + if(index == 0){ + T o = (T) removeFirst(); + return o; + } + Node temp = head; + Node tempTemp = null; + for(int i = 0; i <= index; i++) + { + temp = temp.next; + } + T o = (T) temp.next.data; + tempTemp = temp.next.next; + temp.next = null; + temp.next = tempTemp; + size--; + return o; + } + + public int size(){ + return size; + } + + @Override + public boolean isEmpty() { + return false; + } + + public void addFirst(T o){ + Node node = new Node(o); + node.next = head; + head = node; + size++; + } + public void addLast(T o){ + this.add(o); + } + public T removeFirst(){ + T o = (T) head.data; + head = head.next; + size--; + return o; + } + public Object removeLast(){ + Node temp = head; + for(int i = 0; i <= size; i++) + { + temp = temp.next; + } + temp.next = null; + T o = (T) tail.data; + tail = temp; + size--; + return o; + } + public Iterator iterator(){ + return null; + } + + + private class Node{ + T data; + Node next; + + public Node(T o) { + } + } +} diff --git a/group20/1040154728/1040154728Learning/src/List.java b/group20/1040154728/1040154728Learning/src/List.java new file mode 100644 index 0000000000..eb27d39f6b --- /dev/null +++ b/group20/1040154728/1040154728Learning/src/List.java @@ -0,0 +1,8 @@ +public interface List { + public void add(T object); + public void add(int index, T object); + public T get(int index); + public T remove(int index); + public int size(); + boolean isEmpty(); +} diff --git a/group20/1040154728/1040154728Learning/src/Queue.java b/group20/1040154728/1040154728Learning/src/Queue.java new file mode 100644 index 0000000000..b7e03881fe --- /dev/null +++ b/group20/1040154728/1040154728Learning/src/Queue.java @@ -0,0 +1,22 @@ +public class Queue { + + private LinkedList element = new LinkedList(); + + + + public void enQueue(T o){ + element.addLast(o); + } + + public T deQueue(){ + return element.removeFirst(); + } + + public boolean isEmpty(){ + return element.isEmpty(); + } + + public int size(){ + return element.size(); + } +} diff --git a/group20/1040154728/1040154728Learning/src/SinglyLinkedList/LinkedList0.java b/group20/1040154728/1040154728Learning/src/SinglyLinkedList/LinkedList0.java deleted file mode 100644 index 7cdf1e7f26..0000000000 --- a/group20/1040154728/1040154728Learning/src/SinglyLinkedList/LinkedList0.java +++ /dev/null @@ -1,75 +0,0 @@ -package SinglyLinkedList; - -/** - * Created by Honoka on 2/16/2017. - */ -public class LinkedList0 { - //Node class represents a list node - private class Node - { - String value; - Node next; - /** - * Constructor - * @param val The element to store in this node. - * @param n The reference to the next node. - */ - Node (String val, Node n) - { - value = val; - next = n; - } - - /** - * Constructor - * @param val The element to store in this node. - */ - Node(String val) - { - value = val; - next = null; - } - } - //Reference to the first node in the list - private Node first = null; - /** - * Constructor - * Builds a linked list - */ - public LinkedList0() - { - //test - first = new Node("Apple"); - first.next = new Node("Peach"); - first.next.next = new Node("Kiwi"); - first = new Node("Blueberry",first); - - //Using an array to add elements into list - String[] fruits = {"Banana", "Cherry"}; - for (String f : fruits) - { - first = new Node(f, first); - } - } - /** - * print method - * traverses the list and prints all elements - */ - public void print() - { - Node reference = first; - while(reference != null) - { - System.out.println(reference.value + " "); - reference = reference.next; - } - } - - //Main test method - public static void main(String [] args) - { - LinkedList0 list = new LinkedList0(); - System.out.println("The elements inside this list are "); - list.print(); - } -} diff --git a/group20/1040154728/1040154728Learning/src/SinglyLinkedList2/LinkedList1.java b/group20/1040154728/1040154728Learning/src/SinglyLinkedList2/LinkedList1.java deleted file mode 100644 index 8c93bbc640..0000000000 --- a/group20/1040154728/1040154728Learning/src/SinglyLinkedList2/LinkedList1.java +++ /dev/null @@ -1,242 +0,0 @@ -package SinglyLinkedList2; -/** - * Created by Honoka on 2/16/2017. - */ -public class LinkedList1 { - private class Node - { - String value; - Node next; - - Node(String val, Node n) - { - value = val; - next = n; - } - Node(String val) - { - //Call the other(daddy(or sister(whatever))) constructor. - this(val, null); - } - } - - private Node first; // head - private Node last; //the last element in list - - public LinkedList1() - { - first = null; - last = null; - } - - /**This method checks to see - * if the list is empty - * @return true if list is empty - */ - public boolean isEmpty() - { - return first == null; - } - - /** - * size method returns the length of the list - * @return The number of the elements in the list - */ - public int size() - { - int counter = 0; - Node p = first; - while (p != null) - { - counter ++; - p = p.next; - } - return counter; - } - - /** - * add method add an element to the end of the list - * @param element the value to add - */ - public void add(String element) - { - if (isEmpty()) - { - //Obviously, add the element to the first position in the list - first = new Node(element); - last = first; - } - else - { - //add to the end of existing list - last.next = new Node(element); - last = last.next; - } - } - - /** - * add method, or you might call it insert method since it can - * add element to a specific position - * @param index The position at which to add the element - * @param element you should know what is this - */ - public void add (int index, String element) - { - if (index < 0 || index > size()) - { - String message = String.valueOf(index); - throw new IndexOutOfBoundsException(message); - } - - //index is at least 0 - if(index == 0) - { - //new element add to the head - first = new Node(element, first); - if (last == null) - { - last = first; - } - return; - } - //set a reference predecessor to point to the node that - //will be the predecessor of the new node - Node predecessor = first; - for (int k = 1; k <= index - 1; k++) - { - predecessor = predecessor.next; - } - //Splice in a node containing the new element - predecessor.next = new Node(element, predecessor.next); - - //if there is a new last element - if(predecessor.next.next == null) - last = predecessor.next; - } - - /** - * toString method, like print method, hopefully it will display the contents of the list - * @return say something I'm giving up on you( - */ - public String toString() - { - StringBuffer strBuilder = new StringBuffer(); - //Use p to walk down the list - Node p = first; - while (p != null) - { - strBuilder.append(p.value + "\n"); - p = p.next; - } - return strBuilder.toString(); - } - - /** - * remove method removes the element with the position you want - * @param index the position of the element that you want to remove - * @return the removed element - */ - public String remove (int index) - { - /* Index out of bounds */ - if (index < 0 || index >= size()) - { - String message = String.valueOf(index); - throw new IndexOutOfBoundsException(message); - } - String element = null; - if(index == 0) - { - //Removal of first item in the list - element = first.value; - first = first.next; - if (first == null) - { - last = null; - } - } - else - { - /* find the predecessor of the element to be removed */ - Node predecessor = first; - - /* Move predecessor forward index - 1 times */ - for (int k = 1; k <= index - 1; k++) - { - predecessor = predecessor.next; - /* Store the value to return */ - element = predecessor.next.value; - /* Route link around the node to be removed */ - predecessor.next = predecessor.next.next; - /* Check if predecessor is now last */ - if(predecessor.next == null) - { - last = predecessor; - } - } - } - return element; - } - - /** - * The remove method removes an element - * @param element the element to remove - * @return true if the remove succeeded - */ - public boolean remove(String element) - { - if (isEmpty()) - { - return false; - } - - if (element.equals(first.value)) - { - //Removal of first element in the list - first = first.next; - if(first == null) - { - last = null; - } - return true; - } - - /* Find the predecessor of the element to remove */ - Node predecessor = first; - while (predecessor.next != null && - !predecessor.next.value.equals(element)) - { - predecessor = predecessor.next; - } - /* predecessor.next == null OR predecessor.next.value is element */ - if(predecessor.next == null) - { - return false; - } - /* predecessor.next.value is element */ - predecessor.next = predecessor.next.next; - - /* check if predecessor is now last */ - if (predecessor.next == null) - { - last = predecessor; - } - return true; - } - - public static void main (String [] args) - { - LinkedList1 testList = new LinkedList1(); - testList.add("Apple"); - testList.add("Banana"); - testList.add(0,"Blueberry"); - testList.add(2,"Cherry"); - testList.add(4,"Peach"); - System.out.println("The list has : "); - System.out.println(testList); - testList.remove("Cherry"); - testList.remove(2); - System.out.println("The list has : "); - System.out.println(testList); - } -} diff --git a/group20/1040154728/1040154728Learning/src/Stack.java b/group20/1040154728/1040154728Learning/src/Stack.java new file mode 100644 index 0000000000..959b081239 --- /dev/null +++ b/group20/1040154728/1040154728Learning/src/Stack.java @@ -0,0 +1,21 @@ +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + return elementData.remove(elementData.size() -1); + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return elementData.isEmpty(); + } + public int size(){ + return elementData.size(); + } +} diff --git a/group20/1107837739/1107837739Learning/lib/dom4j-1.6.1.jar b/group20/1107837739/1107837739Learning/lib/dom4j-1.6.1.jar new file mode 100644 index 0000000000..c8c4dbb92d Binary files /dev/null and b/group20/1107837739/1107837739Learning/lib/dom4j-1.6.1.jar differ diff --git a/group20/1107837739/1107837739Learning/lib/fastjson-1.2.7.jar b/group20/1107837739/1107837739Learning/lib/fastjson-1.2.7.jar new file mode 100644 index 0000000000..ce431a92c3 Binary files /dev/null and b/group20/1107837739/1107837739Learning/lib/fastjson-1.2.7.jar differ diff --git a/group20/1107837739/1107837739Learning/src/org/korben/coderising/array/ArrayUtil.java b/group20/1107837739/1107837739Learning/src/org/korben/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..87ea998621 --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/array/ArrayUtil.java @@ -0,0 +1,258 @@ +package org.korben.coderising.array; + +import java.util.ArrayList; +import java.util.List; + +/** + * ArrayUtil + * + * Created by Korben on 26/02/2017. + */ +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + * 渚嬪锛 a = [7, 9 , 30, 3] , 缃崲鍚庝负 [3, 30, 9,7] + * 濡傛灉 a = [7, 9, 30, 3, 4] , 缃崲鍚庝负 [4,3, 30 , 9,7] + */ + public static void reverseArray(int[] origin) { + ensureNotNull(origin); + + int length = origin.length; + for (int i = 0; i < length / 2; i++) { + int tmp = origin[i]; + origin[i] = origin[length - i - 1]; + origin[length - i - 1] = tmp; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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} + */ + public static int[] removeZero(int[] oldArray) { + ensureNotNull(oldArray); + + int nonZeroCount = 0; + for (int i : oldArray) { + if (i != 0) { + nonZeroCount++; + } + } + + int newArr[] = new int[nonZeroCount]; + int newArrIndex = 0; + for (int i : oldArray) { + if (i != 0) { + newArr[newArrIndex++] = i; + } + } + + return newArr; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + */ + public static int[] merge(int[] array1, int[] array2) { + ensureNotNull(array1); + ensureNotNull(array2); + + int maxArraySize = array1.length + array2.length; + int[] mergedArray = new int[maxArraySize]; + + int index1 = 0; + int index2 = 0; + int mergedIndex = -1; + for (int i = 0; i < maxArraySize; i++) { + if (index1 == array1.length) { + System.arraycopy(array2, index2, mergedArray, mergedIndex + 1, array2.length - index2); + mergedIndex += array2.length - index2; + break; + } else if (index2 == array2.length) { + System.arraycopy(array1, index1, mergedArray, mergedIndex + 1, array1.length - index1); + mergedIndex += array1.length - index1; + break; + } else { + int compare = Integer.compare(array1[index1], array2[index2]); + if (compare < 0) { + mergedArray[++mergedIndex] = array1[index1++]; + } else if (compare > 0) { + mergedArray[++mergedIndex] = array2[index2++]; + } else { + mergedArray[++mergedIndex] = array1[index1++]; + index2++; + } + } + } + + // 娓呴櫎鏁扮粍澶氫綑閮ㄥ垎 + if (mergedIndex + 1 < maxArraySize) { + int[] resultArray = new int[mergedIndex + 1]; + System.arraycopy(mergedArray, 0, resultArray, 0, mergedIndex + 1); + return resultArray; + } + + return mergedArray; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 + * 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + */ + public static int[] grow(int[] oldArray, int size) { + ensureNotNull(oldArray); + + if (size < 0) { + throw new IllegalArgumentException("size must > 0"); + } + + int[] newArray = new int[oldArray.length + size]; + + System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); + for (int i = oldArray.length; i < newArray.length; i++) { + newArray[i] = 0; + } + + return newArray; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + */ + public static int[] fibonacci(int max) { + if (max == 1) { + int[] array = new int[1]; + array[1] = 1; + return array; + } + + List list = new ArrayList<>(); + + for (int i = 1; ; i++) { + int fibonacciNumber = getFibonacciNumber(i); + if (fibonacciNumber <= max) { + list.add(fibonacciNumber); + } else { + break; + } + } + + return list2Array(list); + } + + private static int getFibonacciNumber(int index) { + if (index == 1) { + return 1; + } + if (index == 2) { + return 1; + } + return getFibonacciNumber(index - 2) + getFibonacciNumber(index - 1); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + */ + public static int[] getPrimes(int max) { + List primeList = new ArrayList<>(); + if (max <= 1) { + return new int[0]; + } + + if (max >= 2) { + primeList.add(2); + } + + // 鎵鏈夊伓鏁伴兘涓嶆槸绱犳暟, 鎵浠ヨ繖閲岄噰鐢 i += 2 + for (int i = 3; i < max; i += 2) { + if (isPrimeNumber(i, primeList)) { + primeList.add(i); + } + } + + return list2Array(primeList); + } + + private static boolean isPrimeNumber(int number, List primeList) { + for (Integer prime : primeList) { + if (number % prime == 0) { + return false; + } + } + return true; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + */ + public static int[] getPerfectNumbers(int max) { + if (max <= 1) { + return new int[0]; + } + + List perfectNumberList = new ArrayList<>(); + for (int i = 2; i < max; i++) { + if (isPerfectNumber(i)) { + perfectNumberList.add(i); + } + } + return list2Array(perfectNumberList); + } + + private static boolean isPerfectNumber(int number) { + int sum = 1; + for (int i = 2; i <= number / 2; i++) { + if (number % i == 0) { + sum += i; + } + } + + return sum == number; + } + + /** + * 鐢╯eparator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], separator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + */ + public static String join(int[] array, String separator) { + ensureNotNull(array); + + if (separator == null) { + throw new NullPointerException(); + } + + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + stringBuilder.append(array[i]); + if (i != array.length - 1) { + stringBuilder.append(separator); + } + } + + return stringBuilder.toString(); + } + + private static int[] list2Array(List list) { + int[] result = new int[list.size()]; + for (int i = 0; i < list.size(); i++) { + result[i] = list.get(i); + } + return result; + } + + private static void ensureNotNull(int[] array) { + if (array == null) { + throw new NullPointerException(); + } + } +} \ No newline at end of file diff --git a/group20/1107837739/1107837739Learning/src/org/korben/coderising/array/ArrayUtilTest.java b/group20/1107837739/1107837739Learning/src/org/korben/coderising/array/ArrayUtilTest.java new file mode 100644 index 0000000000..9f23e2c341 --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/array/ArrayUtilTest.java @@ -0,0 +1,185 @@ +package org.korben.coderising.array; + +import org.junit.Assert; +import org.junit.Test; + +/** + * ArrayUtil test + * + * Created by Korben on 26/02/2017. + */ +public class ArrayUtilTest { + + @Test + public void reverseArray() throws Exception { + // test reverse even number + { + int[] testArray = new int[5]; + for (int i = 0; i < 5; i++) { + testArray[i] = i; + } + ArrayUtil.reverseArray(testArray); + for (int i = 0; i < 5; i++) { + Assert.assertEquals(5 - 1 - i, testArray[i]); + } + } + + // test reverse odd number + { + int[] testArray = new int[4]; + for (int i = 0; i < 4; i++) { + testArray[i] = i; + } + ArrayUtil.reverseArray(testArray); + for (int i = 0; i < 4; i++) { + Assert.assertEquals(4 - 1 - i, testArray[i]); + } + } + } + + @Test + public void removeZero() throws Exception { + // 娴嬭瘯闈炵┖鏁扮粍 + { + int[] testArray = new int[20]; + for (int i = 0; i < 20; i++) { + if (i % 5 == 0) { + testArray[i] = 0; + } else { + testArray[i] = i; + } + } + + int[] newArray = ArrayUtil.removeZero(testArray); + Assert.assertNotNull(newArray); + for (int i = 0; i < 20; i++) { + if (i % 5 == 0) { + continue; + } + + Assert.assertEquals(testArray[i], i); + } + } + + // 娴嬭瘯绌烘暟缁 + { + int[] testArray = new int[5]; + for (int i = 0; i < 5; i++) { + testArray[i] = 0; + } + + int[] newArray = ArrayUtil.removeZero(testArray); + Assert.assertNotNull(newArray); + Assert.assertEquals(newArray.length, 0); + } + } + + @Test + public void merge() throws Exception { + // 鏋勫缓鏁扮粍 + int[] array1 = new int[10]; + int[] array2 = new int[11]; + array2[10] = 100; + for (int i = 0; i < 10; i++) { + if (i % 2 == 0) { + array1[i / 2] = i; // 0, 2, 4, 6, 8 + } else { + array2[i / 2] = i; // 1, 3, 5, 7, 9 + } + } + + for (int i = 10; i < 15; i++) { + array1[i - 5] = i; // 10, 11, 12, 13, 14, 15 + array2[i - 5] = i; // 10, 11, 12, 13, 14, 15 + } + + // 娴嬭瘯merge + { + int[] merge = ArrayUtil.merge(array1, array2); + Assert.assertNotNull(merge); + Assert.assertEquals(merge.length, 16); + for (int i = 0; i < 15; i++) { + Assert.assertEquals(merge[i], i); + } + Assert.assertEquals(merge[15], 100); + } + // 璋冩崲鏁扮粍椤哄簭 + { + int[] merge = ArrayUtil.merge(array2, array1); + Assert.assertNotNull(merge); + Assert.assertEquals(merge.length, 16); + for (int i = 0; i < 15; i++) { + Assert.assertEquals(merge[i], i); + } + Assert.assertEquals(merge[15], 100); + } + // 娴嬭瘯绌烘暟缁 + { + int[] array3 = new int[0]; + int[] merge1 = ArrayUtil.merge(array1, array3); + Assert.assertArrayEquals(merge1, array1); + + int[] merge2 = ArrayUtil.merge(array3, array1); + Assert.assertArrayEquals(merge2, array1); + } + // 娴嬭瘯鐩稿悓鏁扮粍 + { + int[] merge = ArrayUtil.merge(array1, array1); + Assert.assertArrayEquals(merge, array1); + } + } + + @Test + public void grow() throws Exception { + int[] oldArray = new int[5]; + for (int i = 0; i < 5; i++) { + oldArray[i] = i; + } + + int[] newArray = ArrayUtil.grow(oldArray, 5); + for (int i = 0; i < 10; i++) { + if (i < 5) { + Assert.assertEquals(newArray[i], i); + } else { + Assert.assertEquals(newArray[i], 0); + } + } + } + + @Test + public void fibonacci() throws Exception { + int[] fibonacciArray = {1, 1, 2, 3, 5, 8, 13, 21}; + + int[] calculatedFibonacci = ArrayUtil.fibonacci(22); + Assert.assertArrayEquals(fibonacciArray, calculatedFibonacci); + } + + @Test + public void getPrimes() throws Exception { + int[] expected = {2, 3, 5, 7, 11, 13, 17, 19}; + int[] primes = ArrayUtil.getPrimes(23); + Assert.assertArrayEquals(primes, expected); + } + + @Test + public void getPerfectNumbers() throws Exception { + int[] perfectNumbers = {6, 28, 496, 8128}; + int[] calculatedPerfectNumbers = ArrayUtil.getPerfectNumbers(8220); + Assert.assertArrayEquals(perfectNumbers, calculatedPerfectNumbers); + } + + @Test + public void join() throws Exception { + { + int[] array = {1}; + String joinStr = ArrayUtil.join(array, "-"); + Assert.assertEquals("1", joinStr); + } + + { + int[] array = {1, 2, 3}; + String joinStr = ArrayUtil.join(array, "-"); + Assert.assertEquals("1-2-3", joinStr); + } + } +} \ No newline at end of file diff --git a/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/LoginAction.java b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/LoginAction.java new file mode 100644 index 0000000000..20fa9e766b --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/LoginAction.java @@ -0,0 +1,41 @@ +package org.korben.coderising.litestruts; + +/** + * 杩欐槸涓涓敤鏉ュ睍绀虹櫥褰曠殑涓氬姟绫伙紝 鍏朵腑鐨勭敤鎴峰悕鍜屽瘑鐮侀兘鏄‖缂栫爜鐨勩 + * + * @author liuxin + */ +public class LoginAction { + private String name; + private String password; + private String message; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = 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 String getMessage() { + return this.message; + } +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/Struts.java b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..7ac88522bf --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/Struts.java @@ -0,0 +1,111 @@ +package org.korben.coderising.litestruts; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import org.korben.coderising.litestruts.dom.StrutsAction; +import org.korben.coderising.litestruts.util.StrutsParser; + +public class Struts { + + /** + * 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + * + * 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + * 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + * ("name"="test" , "password"="1234") , + * 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + * + * 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xecute 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + * + * 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + * 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + * 鏀惧埌View瀵硅薄鐨刾arameters + * + * 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + * 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + public static View runAction(String actionName, Map parameters) { + Map actionMap = StrutsParser.doParse(); + StrutsAction action = actionMap.get(actionName); + + if (action == null) { + System.out.println("couldn't get action: " + actionName + ", return"); + return null; + } + + try { + // 閫氳繃鍙嶅皠, 鍒涘缓瀹炰緥瀵硅薄 + Class actionClass = Class.forName(action.getActionClassName()); + Object actionObj = actionClass.newInstance(); + + // 璋冪敤 parameters 涓殑 set 鏂规硶 + for (Map.Entry parameterEntry : parameters.entrySet()) { + Method[] methods = actionClass.getMethods(); + for (Method method : methods) { + if (method.getName().equalsIgnoreCase("set" + parameterEntry.getKey())) { + method.invoke(actionObj, parameterEntry.getValue()); + } + } + } + //for (Map.Entry parameterEntry : parameters.entrySet()) { + // for (PropertyDescriptor propertyDescriptor : + // Introspector.getBeanInfo(actionClass).getPropertyDescriptors()) { + // if (propertyDescriptor.getDisplayName().equals(parameterEntry.getKey())) { + // Method writeMethod = propertyDescriptor.getWriteMethod(); + // writeMethod.invoke(actionObj, parameterEntry.getValue()); + // } + // } + //} + + // 璋冪敤 execute 鏂规硶 + Method executeMethod = actionClass.getMethod("execute"); + Object executeResult = executeMethod.invoke(actionObj); + + // 鏍规嵁 execute 鏂规硶鐨勭粨鏋, 鑾峰彇 xml 閰嶇疆鐨 jsp 椤甸潰 + String jsp = action.getAttributes().get(Objects.toString(executeResult)); + + // 璋冪敤 get 鏂规硶 + Map actionFieldMap = new HashMap<>(); + Field[] actionFields = actionClass.getDeclaredFields(); + for (Field actionFiled : actionFields) { + Method[] methods = actionClass.getMethods(); + for (Method method : methods) { + if (method.getName().equalsIgnoreCase("get" + actionFiled.getName())) { + method.invoke(actionObj); + actionFieldMap.put(actionFiled.getName(), Objects.toString(method.invoke(actionObj))); + } + } + } + //for (PropertyDescriptor propertyDescriptor : + // Introspector.getBeanInfo(actionClass).getPropertyDescriptors()) { + // Method readMethod = propertyDescriptor.getReadMethod(); + // Object readMethodResult = readMethod.invoke(actionObj); + // actionFieldMap.put(propertyDescriptor.getDisplayName(), Objects.toString(readMethodResult)); + //} + + // 杩斿洖 View 瀵硅薄 + View view = new View(); + view.setParameters(actionFieldMap); + view.setJsp(jsp); + return view; + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + }/* catch (IntrospectionException e) { + e.printStackTrace(); + }*/ + + return null; + } +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/StrutsTest.java b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/StrutsTest.java new file mode 100644 index 0000000000..024f678100 --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/StrutsTest.java @@ -0,0 +1,37 @@ +package org.korben.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() { + + 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/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/View.java b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/View.java new file mode 100644 index 0000000000..f4af03febc --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/View.java @@ -0,0 +1,26 @@ +package org.korben.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/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/dom/StrutsAction.java b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/dom/StrutsAction.java new file mode 100644 index 0000000000..c16de22c44 --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/dom/StrutsAction.java @@ -0,0 +1,27 @@ +package org.korben.coderising.litestruts.dom; + +import java.util.Map; + +/** + * Created by Korben on 03/03/2017. + */ +public class StrutsAction { + private String actionClassName; + private Map attributes; + + public String getActionClassName() { + return actionClassName; + } + + public void setActionClassName(String actionClassName) { + this.actionClassName = actionClassName; + } + + public Map getAttributes() { + return attributes; + } + + public void setAttributes(Map attributes) { + this.attributes = attributes; + } +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/util/StrutsParser.java b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/util/StrutsParser.java new file mode 100644 index 0000000000..239ac2e4cd --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/util/StrutsParser.java @@ -0,0 +1,82 @@ +package org.korben.coderising.litestruts.util; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; +import org.korben.coderising.litestruts.dom.StrutsAction; + +/** + * 瑙f瀽struts.xml + * + * Created by Korben on 03/03/2017. + */ +public class StrutsParser { + + private static final String STRUTS_XML = "struts.xml"; + + public static void main(String[] args) { + Map strutsActions = doParse(); + System.out.println(strutsActions.size()); + } + + public static Map doParse() { + Map resultMap = new HashMap<>(); + + SAXReader reader = new SAXReader(); + InputStream in = getStrutsInputStream(); + try { + Document document = reader.read(in); + Element rootElement = document.getRootElement(); + + // parse action element + List elementActions = rootElement.elements(); + for (Element elementAction : elementActions) { + StrutsAction action = new StrutsAction(); + + // parse "name" attribute from action element + resultMap.put(elementAction.attribute("name").getValue(), action); + + // parse "class" attribute from action element + action.setActionClassName(elementAction.attribute("class").getValue()); + + // parse sub elements in action element + List elements = elementAction.elements(); + Map map = new HashMap<>(); + for (Element element : elements) { + map.put(element.attribute("name").getValue(), element.getStringValue()); + } + action.setAttributes(map); + } + + return resultMap; + } catch (DocumentException e) { + throw new IllegalStateException("failed to parse " + STRUTS_XML, e); + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + private static InputStream getStrutsInputStream() { + StrutsParser.class.getPackage().getName(); + InputStream in = StrutsParser.class.getClassLoader() + .getResourceAsStream("org/korben/coderising/litestruts/util/" + STRUTS_XML); + if (in == null) { + throw new IllegalStateException(STRUTS_XML + " doesn't exist"); + } + + return in; + } +} diff --git a/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/util/struts.xml b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/util/struts.xml new file mode 100644 index 0000000000..1cb3f4a5df --- /dev/null +++ b/group20/1107837739/1107837739Learning/src/org/korben/coderising/litestruts/util/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/group20/1107837739/1107837739Learning/src/org/korben/list/KArrayList.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KArrayList.java similarity index 99% rename from group20/1107837739/1107837739Learning/src/org/korben/list/KArrayList.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KArrayList.java index 4d6236b537..0f443462ed 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/list/KArrayList.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KArrayList.java @@ -1,4 +1,4 @@ -package org.korben.list; +package org.korben.coding.basic.list; import java.util.Objects; diff --git a/group20/1107837739/1107837739Learning/src/org/korben/list/KIterator.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KIterator.java similarity index 75% rename from group20/1107837739/1107837739Learning/src/org/korben/list/KIterator.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KIterator.java index c29e566178..b5245ecca0 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/list/KIterator.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KIterator.java @@ -1,4 +1,4 @@ -package org.korben.list; +package org.korben.coding.basic.list; /** * Created by Korben on 24/02/2017. diff --git a/group20/1107837739/1107837739Learning/src/org/korben/list/KLinkedList.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KLinkedList.java similarity index 98% rename from group20/1107837739/1107837739Learning/src/org/korben/list/KLinkedList.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KLinkedList.java index e14efad19c..2ca4452981 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/list/KLinkedList.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KLinkedList.java @@ -1,4 +1,4 @@ -package org.korben.list; +package org.korben.coding.basic.list; import java.util.Objects; diff --git a/group20/1107837739/1107837739Learning/src/org/korben/list/KList.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KList.java similarity index 91% rename from group20/1107837739/1107837739Learning/src/org/korben/list/KList.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KList.java index e5d33b984b..e5b166094e 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/list/KList.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KList.java @@ -1,4 +1,4 @@ -package org.korben.list; +package org.korben.coding.basic.list; /** * Korben's List diff --git a/group20/1107837739/1107837739Learning/src/org/korben/list/KListIteratorTest.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KListIteratorTest.java similarity index 94% rename from group20/1107837739/1107837739Learning/src/org/korben/list/KListIteratorTest.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KListIteratorTest.java index 994538732f..7017e0ed2c 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/list/KListIteratorTest.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KListIteratorTest.java @@ -1,4 +1,4 @@ -package org.korben.list; +package org.korben.coding.basic.list; import org.junit.Assert; import org.junit.Before; diff --git a/group20/1107837739/1107837739Learning/src/org/korben/list/KListTest.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KListTest.java similarity index 99% rename from group20/1107837739/1107837739Learning/src/org/korben/list/KListTest.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KListTest.java index ed3055b74e..2c6febecc3 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/list/KListTest.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KListTest.java @@ -1,4 +1,4 @@ -package org.korben.list; +package org.korben.coding.basic.list; import java.util.Objects; import org.junit.Assert; diff --git a/group20/1107837739/1107837739Learning/src/org/korben/queue/KArrayQueue.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KArrayQueue.java similarity index 98% rename from group20/1107837739/1107837739Learning/src/org/korben/queue/KArrayQueue.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KArrayQueue.java index 3e975058f4..eea57cf035 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/queue/KArrayQueue.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KArrayQueue.java @@ -1,4 +1,4 @@ -package org.korben.queue; +package org.korben.coding.basic.queue; import java.util.NoSuchElementException; diff --git a/group20/1107837739/1107837739Learning/src/org/korben/queue/KQueue.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KQueue.java similarity index 84% rename from group20/1107837739/1107837739Learning/src/org/korben/queue/KQueue.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KQueue.java index 14763efd99..9d8146d50d 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/queue/KQueue.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KQueue.java @@ -1,4 +1,4 @@ -package org.korben.queue; +package org.korben.coding.basic.queue; /** * Korben's Queue Interface diff --git a/group20/1107837739/1107837739Learning/src/org/korben/queue/KQueueTest.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KQueueTest.java similarity index 97% rename from group20/1107837739/1107837739Learning/src/org/korben/queue/KQueueTest.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KQueueTest.java index 3d9557748f..17c3703a64 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/queue/KQueueTest.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/queue/KQueueTest.java @@ -1,4 +1,4 @@ -package org.korben.queue; +package org.korben.coding.basic.queue; import java.util.NoSuchElementException; import org.junit.Assert; diff --git a/group20/1107837739/1107837739Learning/src/org/korben/stack/KStack.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/stack/KStack.java similarity index 97% rename from group20/1107837739/1107837739Learning/src/org/korben/stack/KStack.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/stack/KStack.java index 8dc39e4efd..eb83eb47cd 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/stack/KStack.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/stack/KStack.java @@ -1,4 +1,4 @@ -package org.korben.stack; +package org.korben.coding.basic.stack; import java.util.EmptyStackException; import java.util.Objects; diff --git a/group20/1107837739/1107837739Learning/src/org/korben/stack/KStackTest.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/stack/KStackTest.java similarity index 97% rename from group20/1107837739/1107837739Learning/src/org/korben/stack/KStackTest.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/stack/KStackTest.java index e3f173eb3f..20b451a7ef 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/stack/KStackTest.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/stack/KStackTest.java @@ -1,4 +1,4 @@ -package org.korben.stack; +package org.korben.coding.basic.stack; import java.util.EmptyStackException; import org.junit.Assert; diff --git a/group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNode.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/tree/BinaryTreeNode.java similarity index 98% rename from group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNode.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/tree/BinaryTreeNode.java index 30c613edd0..3cfcacc37c 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNode.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/tree/BinaryTreeNode.java @@ -1,4 +1,4 @@ -package org.korben.tree; +package org.korben.coding.basic.tree; /** * Korben's BinaryTreeNode diff --git a/group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNodeTest.java b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/tree/BinaryTreeNodeTest.java similarity index 97% rename from group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNodeTest.java rename to group20/1107837739/1107837739Learning/src/org/korben/coding/basic/tree/BinaryTreeNodeTest.java index a6fb4ed4e9..05873872a7 100644 --- a/group20/1107837739/1107837739Learning/src/org/korben/tree/BinaryTreeNodeTest.java +++ b/group20/1107837739/1107837739Learning/src/org/korben/coding/basic/tree/BinaryTreeNodeTest.java @@ -1,4 +1,4 @@ -package org.korben.tree; +package org.korben.coding.basic.tree; import org.junit.Assert; diff --git a/group20/1107837739/korben.md b/group20/1107837739/korben.md index 983f6f47b6..91ee50808b 100644 --- a/group20/1107837739/korben.md +++ b/group20/1107837739/korben.md @@ -5,3 +5,5 @@ | Blog Title | Date| | ---------- | -----------| | [鍒濈璁$畻鏈虹▼搴忕殑杩愯](http://korben-chy.github.io/2017/02/26/%E5%88%9D%E7%AA%A5%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A8%8B%E5%BA%8F%E7%9A%84%E8%BF%90%E8%A1%8C/) | 2017/02/26 | +| +[绋嬪簭鍦ㄨ绠楁満涓殑杩愯杩囩▼绠鏋怾(http://korben-chy.github.io/2017/03/06/%E7%A8%8B%E5%BA%8F%E5%9C%A8%E8%AE%A1%E7%AE%97%E6%9C%BA%E4%B8%AD%E7%9A%84%E8%BF%90%E8%A1%8C%E8%BF%87%E7%A8%8B%E7%AE%80%E6%9E%90) | 2017/03/05 | diff --git a/group20/1430208241/1430208241leaning/src/GithubWork/ArrayList.java b/group20/1430208241/1430208241leaning/src/GithubWork/ArrayList.java new file mode 100644 index 0000000000..f74501b5ba --- /dev/null +++ b/group20/1430208241/1430208241leaning/src/GithubWork/ArrayList.java @@ -0,0 +1,73 @@ +package GithubWork; + +import java.util.Arrays; + +public class ArrayList implements List { + private int size = 0; + private Object[] elementdata = new Object[100]; + + public void add(Object o) { + if (elementdata.length <= size) { + ensureCapacity(size + 1); + } + elementdata[size++] = o; + } + + private void ensureCapacity(int minCapacity) { + int oldCapacity = elementdata.length; + if (oldCapacity < minCapacity) { + + int newCapacity = (int) (oldCapacity * 1.5); + if (newCapacity < minCapacity) + newCapacity = minCapacity; + elementdata = Arrays.copyOf(elementdata, newCapacity); + } + } + + public void add(int index, Object o) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException(); + + } + + ensureCapacity(size + 1); + System.arraycopy(elementdata, index, elementdata, index, size - index); + elementdata[index] = o; + size++; + } + + public Object get(int index) { + RangeCheck(index); + + return elementdata[index]; + } + + public Object remove(int index) { + RangeCheck(index); + Object oldvalue = elementdata[index]; + int numMoved = size - index - 1; + if (numMoved > 0) + System.arraycopy(elementdata, index + 1, elementdata, index, numMoved); + elementdata[--size] = null; + return oldvalue; + } + + private void RangeCheck(int index) { + if (index >= size) + throw new IndexOutOfBoundsException(); + } + + public int size() { + int i; + for (i = 0; i < elementdata.length; i++) { + size++; + if (null == elementdata[i]) { + break; + } + + } + return size; + + } + +} diff --git a/group20/1430208241/1430208241leaning/src/GithubWork/JunitTest.java b/group20/1430208241/1430208241leaning/src/GithubWork/JunitTest.java new file mode 100644 index 0000000000..b011de10f0 --- /dev/null +++ b/group20/1430208241/1430208241leaning/src/GithubWork/JunitTest.java @@ -0,0 +1,47 @@ +package GithubWork; + +import org.junit.Test; + +public class JunitTest { + @Test + public void ArrayList(){ + ArrayList a=new ArrayList(); + a.add(1); + a.add(2); + a.add(3); + a.add(4); + a.get(2); + + System.out.println(a); + + + } + @Test + public void Queue(){ + Queue q=new Queue(4); + q.enQueue(1); + q.enQueue(2); + q.enQueue(3); + q.enQueue(4); + + while(!q.isEmpty()){ + int i=(int) q.deQueue(); + System.out.println(i); + } + System.out.println(q.size()); + } + @Test + public void LinkedList(){ + LinkedList ls=new LinkedList(); + ls.add(1); + ls.add(7); + ls.add(3); + ls.add(4, 5); + ls.get(2); + ls.addFirst(0); + + ls.remove(3); + System.out.println(ls); + + } +} diff --git a/group20/1430208241/1430208241leaning/src/GithubWork/LinkedList.java b/group20/1430208241/1430208241leaning/src/GithubWork/LinkedList.java new file mode 100644 index 0000000000..657ca117dc --- /dev/null +++ b/group20/1430208241/1430208241leaning/src/GithubWork/LinkedList.java @@ -0,0 +1,136 @@ +package GithubWork; + +import java.util.Iterator; + +public class LinkedList implements List { + + private Node head = null;// 头节点 + private int size = 0; + private Node last = null; + + /* + * 向链表中插入数据 (non-Javadoc) + * + * @see GithubWork.List#add(java.lang.Object) + */ + public void add(Object o) { + Node newNode = new Node(0);// 实例化一个节点 + if (head == null) { + head = newNode; + return; + } + Node tmp = head; + while (tmp.next != null) { + tmp = tmp.next; + } + tmp.next = newNode; + size++; + } + + public void add(int index, Object o) { + Node newNode = new Node(0); + Node indexNode = head; + int i = 0; + while (i == index) { + indexNode = indexNode.next; + i++; + } + Node indexNextNode = indexNode.next; + indexNode.next = newNode; + newNode.pre = indexNode; + newNode.next = indexNextNode; + indexNextNode.pre = newNode; + size++; + } + + public Object get(int index) { + Node indexNode = head; + int i = 0; + while (i == index) { + indexNode = indexNode.next; + i++; + } + return indexNode; + } + + public Object remove(int index) { + if (index < 1 || index > size()) { + throw new IndexOutOfBoundsException(); + } + if (index == 1) { + head = head.next; + return head; + } + int i = 1; + Node preNode = head; + Node curNode = preNode.next; + while (curNode != null) { + if (i == index) { + preNode.next = curNode.next; + + } + preNode = curNode; + curNode = curNode.next; + i++; + } + return curNode; + } + + public int size() { + return size; + } + + public void addFirst(Object o) { + Node newNode = new Node(o); + newNode.data = o; + newNode.next = head; + head.pre = newNode; + head = newNode; + size++; + } + + public void addLast(Object o) { + Node newNode = new Node(o); + newNode.data = o; + + newNode.pre = last; + last.next = newNode; + last = newNode; + size++; + + } + + public Object removeFirst() { + Node ref = head; + head = head.next; + head.pre = null; + size--; + return ref; + } + + public Object removeLast() { + Node rel = last; + last = last.pre; + last.next = null; + size--; + return rel; + + } + + public Iterator iterator() { + + return null; + } + + private static class Node { + Object data;// 节点内容 + Node next = null;// 头节点 + Node pre = null; + + public Node(Object data) { + this.data = data; + } + + } + +} diff --git a/group20/1430208241/1430208241leaning/src/GithubWork/List.java b/group20/1430208241/1430208241leaning/src/GithubWork/List.java new file mode 100644 index 0000000000..169fc14fd1 --- /dev/null +++ b/group20/1430208241/1430208241leaning/src/GithubWork/List.java @@ -0,0 +1,10 @@ +package GithubWork; + +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/group20/1430208241/1430208241leaning/src/GithubWork/Queue.java b/group20/1430208241/1430208241leaning/src/GithubWork/Queue.java new file mode 100644 index 0000000000..792ca233f5 --- /dev/null +++ b/group20/1430208241/1430208241leaning/src/GithubWork/Queue.java @@ -0,0 +1,43 @@ +package GithubWork; + +public class Queue { + private int maxSize; + private Object[] array;//存放元素数组 + private int front;//前一个元素索引 + private int rear;//后一个元素索引 + private int items=0;//元素个数 + //构造对象并初始化 + public Queue(int s){ + maxSize=s; + array=new Object[maxSize]; + front=0; + rear=-1; + + } + public void enQueue(Object o){ + if(rear==maxSize-1){ + rear=-1; + } + array[++rear]=o; + items++; + + } + + public Object deQueue(){ + Object temp =array[front++]; + if(front==maxSize){ + front=0; + } + items--; + return temp; + } + + public boolean isEmpty(){ + + return items==0; + } + + public int size(){ + return array.length; + } +} diff --git a/group20/1430208241/1430208241leaning/src/GithubWork/Stack.java b/group20/1430208241/1430208241leaning/src/GithubWork/Stack.java new file mode 100644 index 0000000000..43f138021c --- /dev/null +++ b/group20/1430208241/1430208241leaning/src/GithubWork/Stack.java @@ -0,0 +1,22 @@ +package GithubWork; + +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/group20/1430208241/1430208241leaning/src/\346\226\207\347\253\240\345\234\260\345\235\200" "b/group20/1430208241/1430208241leaning/src/\346\226\207\347\253\240\345\234\260\345\235\200" new file mode 100644 index 0000000000..472bb524e9 --- /dev/null +++ "b/group20/1430208241/1430208241leaning/src/\346\226\207\347\253\240\345\234\260\345\235\200" @@ -0,0 +1 @@ +http://blog.csdn.net/wifi619/article/details/57510982 \ No newline at end of file diff --git a/group20/2421586846/.project b/group20/2421586846/.project new file mode 100644 index 0000000000..b0d9d8a3f0 --- /dev/null +++ b/group20/2421586846/.project @@ -0,0 +1,11 @@ + + + 2421586846 + + + + + + + + diff --git a/group20/2421586846/DS/.project b/group20/2421586846/DS/.project new file mode 100644 index 0000000000..ca3bc54b4a --- /dev/null +++ b/group20/2421586846/DS/.project @@ -0,0 +1,17 @@ + + + DS + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group20/2421586846/DS/src/basic/ArrayList.java b/group20/2421586846/DS/src/basic/ArrayList.java new file mode 100644 index 0000000000..7e9f70169d --- /dev/null +++ b/group20/2421586846/DS/src/basic/ArrayList.java @@ -0,0 +1,77 @@ +package basic; + + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[2]; + + public void EnsureEnoughSize(){ + if (size == elementData.length ) + { + elementData = Arrays.copyOf(elementData, elementData.length +10); + + } + } + + public void add(Object o){ + EnsureEnoughSize(); + + // elementData = Arrays.copyOf(elementData, elementData.length +1); + //Object[] NewelementData = new Object[size+1]; + //System.arraycopy( elementData,0, NewelementData, 0, elementData.length ); + + + elementData[size]=o; + size++; + } + public void add(int index, Object o){ + if (index >= size || index < 0){ + return; + } + + EnsureEnoughSize(); + + for (int i = elementData.length-1; i>index;i --){ + elementData[i]=elementData[i-1]; + } + elementData[index]=o; + size++; + } + + public Object get(int index){ + if (index >= size || index < 0){ + return null; + } + else { + return elementData[index]; + } + + } + + public Object remove(int index){ + if (index >= size || index < 0){ + return null; + } + else { + Object o = elementData[index]; + for (int i =index; i< size-1; i++){ + elementData[i]= elementData[i+1]; + } + size--; + return o; + } + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + +} diff --git a/group20/2421586846/DS/src/basic/BinaryTreeNode.java b/group20/2421586846/DS/src/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..e8e37a9a41 --- /dev/null +++ b/group20/2421586846/DS/src/basic/BinaryTreeNode.java @@ -0,0 +1,33 @@ +package 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/group11/171535320/Iterator.java b/group20/2421586846/DS/src/basic/Iterator.java similarity index 83% rename from group11/171535320/Iterator.java rename to group20/2421586846/DS/src/basic/Iterator.java index 96a43dbe0a..9154df57bf 100644 --- a/group11/171535320/Iterator.java +++ b/group20/2421586846/DS/src/basic/Iterator.java @@ -1,3 +1,5 @@ +package basic; + public interface Iterator { public boolean hasNext(); public Object next(); diff --git a/group20/2421586846/DS/src/basic/LinkedList.java b/group20/2421586846/DS/src/basic/LinkedList.java new file mode 100644 index 0000000000..8bdc7ec661 --- /dev/null +++ b/group20/2421586846/DS/src/basic/LinkedList.java @@ -0,0 +1,177 @@ +package basic; + + +public class LinkedList implements List { + + private Node head; + + private int size; + //鍔犲埌鏈鍚 + public void add(Object o){ + Node newNode= new Node(); + newNode.data = o; + newNode.next = null; + + if (head == null ){ + head= newNode; + size++; + return; + } + + Node currentNode = head; + + while (currentNode.next != null ){ + currentNode = currentNode.next ; + } + /* + for (int i=0;i< size;i++){ + currentNode = currentNode.next ; + } + */ + currentNode.next =newNode; + size++; + } + + public void add(int index , Object o){ + if (index >= size ||index < 0) { + return; + } + + Node newNode= new Node(); + newNode.data = o; + Node PrevNode =null; + + if (index ==0 ){ + + Node tempNode = head ; + head = newNode; + newNode.next = tempNode; + + } + else { + Node currentNode = head; + for (int i=0; i = size ||index < 0) { + return null; + } + Node currentNode = head; + for (int i=0; i = size ||index < 0) { + return null; + } + + Node currentNode = head; + if (index ==0 ){ + head =head.next; + size--; + return currentNode.data; + } + Node PrevNode = null; + for (int i=0; i =maxSize){ + if(index<0||index>size-1) + throw new IndexOutOfBoundsException("鏁扮粍涓嬫爣瓒婄晫寮傚父銆"); + if (size==maxSize){ Object[] targt = new Object[++maxSize]; - System.arraycopy(elementData,0,targt,0,maxSize); - for (int j = targt.length;j>=index;j--){ - targt[j-1] = targt[j-2]; + System.arraycopy(elementData,0,targt,0,elementData.length); + for (int j = targt.length-2;j>=index;j--){ + targt[j+1] = targt[j]; } targt[index] = o; size++; + elementData = targt; }else if(size=index;j--){ - elementData[j-1] = elementData[j-2]; + for (int j = size-1;j>=index;j--){ + elementData[j+1] = elementData[j]; } elementData[index] = o; size++; @@ -50,28 +55,35 @@ public void add(int index, Object o) { */ @Override public void add(Object o) { - if (size>=maxSize){ + if (size==maxSize){ Object[] targt = new Object[++maxSize]; - System.arraycopy(elementData,0,targt,0,maxSize); + System.arraycopy(elementData,0,targt,0,elementData.length); targt[maxSize-1] = o; size++; + elementData = targt; }else if(sizesize-1) + throw new IndexOutOfBoundsException("鏁扮粍涓嬫爣瓒婄晫寮傚父"); + Object o= elementData[index]; + return o; } @Override public Object remove(int index) { + if (index<0||index>size-1) + throw new IndexOutOfBoundsException("鏁扮粍涓嬭〃瓒婄晫寮傚父"); Object temp = elementData[index]; - for (int i = index;i>size-1;i++){ + for (int i = index;i<=size-1;i++){ elementData[i] = elementData[i+1]; } + size--; return temp; } @@ -81,6 +93,10 @@ public int size() { } - - + @Override + public String toString() { + return "ArrayList{" + + "elementData=" + Arrays.toString(elementData) + + '}'; + } } diff --git a/group20/286166752/src/wiki/liven/code/dataStructures/LinkedList.java b/group20/286166752/src/wiki/liven/code/dataStructures/LinkedList.java index b2cc5f8668..52695305fb 100644 --- a/group20/286166752/src/wiki/liven/code/dataStructures/LinkedList.java +++ b/group20/286166752/src/wiki/liven/code/dataStructures/LinkedList.java @@ -5,36 +5,119 @@ */ public class LinkedList implements List{ - private Node head; + private Node head;//閾捐〃鐨勫ご鑺傜偣 + private Node tail;//閾炬帴鐨勫熬鑺傜偣 + private int size;//瀹氫箟浜嗛摼琛ㄤ腑鐨勫厓绱犵殑涓暟 + /** + * 瀹氫箟浜哊ODE鑺傜偣鐨勬暟鎹粨鏋 + */ private static class Node{ Object data; Node next; - } + private Node(){ + } + private Node(Object o,Node node){ + this.data = o; + this.next = node; + } + } + /** + * 鍦ㄦ寚瀹氱殑绱㈠紩鍐欏叆鏂扮殑鑺傜偣 + * 1.鎵惧埌缁欎綅缃笂鐨勮妭鐐筺ode0 + * 2.淇敼node0鐨刵ext鎸囧悜鏂扮殑鑺傜偣node + * 3.鏂拌妭鐐筺ode鎸囧悜鍘熷厛node0鐨勫悗缁ц妭鐐筺ode1 + * 4.闀垮害++ + * 5.瀹岀粨 + * @param index 绱㈠紩 浠0寮濮 + * @param o + */ @Override public void add(int index, Object o) { - + if (index<0||index>size-1) + throw new IndexOutOfBoundsException("鍗曢摼琛ㄨ秺鐣屽紓甯搞"); + Node node = new Node(); + node.data = o; + Node nodeIndex = findNodeByIndex(index); + if(node==null) + throw new NullPointerException("绌烘寚閽堝紓甯搞"); + Node temp = nodeIndex.next; + nodeIndex.next = node; + node.next = temp; + size++; + } + + /** + * 鏍规嵁绱㈠紩鍙锋煡璇㈣妭鐐 + * @param index 绱㈠紩 + * @return + */ + private Node findNodeByIndex(int index) { + if (index<0||index>size-1) + throw new IndexOutOfBoundsException("鍗曢摼琛ㄨ秺鐣屽紓甯搞"); + Node current = head; + if (1<=index&&index<=size-1){//绱㈠紩妫鏌 + for (int i = 0;i>=size-1&¤t!=null;i++,current = current.next) + if (i==index){ + return current; + } + } + return null; } @Override public void add(Object o) { - + Node node = new Node(); + node.data = o; + if(head==null){ + head = node; + node.next = tail; + size++; + }else{ + Node temp = tail; + node.next = temp; + temp.next = node; + size++; + } } @Override public Object get(int index) { - return null; + if(index<0||index>size-1) + throw new IndexOutOfBoundsException("鍗曢摼琛ㄨ秺鐣屻"); + Node node = findNodeByIndex(index); + if(node==null) + throw new NullPointerException("绌烘寚閽堝紓甯搞"); + return node.data; } + /** + * 鍒犻櫎鎸囧畾绱㈠紩鐨勫厓绱 + * @param index + * @return + */ @Override public Object remove(int index) { - return null; + if(index<0||index>size-1) + throw new IndexOutOfBoundsException("鍗曢摼琛ㄨ秺鐣屻"); + Node node = findNodeByIndex(index); + if(node==null) + throw new NullPointerException("绌烘寚閽堝紓甯搞"); + Node prvNode = findNodeByIndex(index-1); + if(prvNode==null) + throw new NullPointerException("绌烘寚閽堝紓甯搞"); + Node nextNode = node.next; + node.next = null; + prvNode.next = nextNode; + size--; + return node.data; + } @Override public int size() { - return 0; + return size; } } diff --git a/group20/286166752/src/wiki/liven/code/dataStructures/List.java b/group20/286166752/src/wiki/liven/code/dataStructures/List.java index 2d1840ef0f..57d5c217ce 100644 --- a/group20/286166752/src/wiki/liven/code/dataStructures/List.java +++ b/group20/286166752/src/wiki/liven/code/dataStructures/List.java @@ -1,7 +1,7 @@ package wiki.liven.code.dataStructures; /** - * Created by leven on 2017/2/21. + * Created by leven on 2017/2/26. */ public interface List { @@ -11,4 +11,5 @@ public interface List { public Object remove(int index); public int size(); + } diff --git a/group20/286166752/src/wiki/liven/code/dataStructures/Queue.java b/group20/286166752/src/wiki/liven/code/dataStructures/Queue.java index b8c8430daa..d52c0de6f5 100644 --- a/group20/286166752/src/wiki/liven/code/dataStructures/Queue.java +++ b/group20/286166752/src/wiki/liven/code/dataStructures/Queue.java @@ -2,6 +2,63 @@ /** * Created by leven on 2017/2/21. + * 闃熷垪 + * 鍙厑璁镐竴绔繘琛屽嚭闃熸搷浣,鍙︿竴绔繘琛屽叆闃熸搷浣溿 + * 鐗规ф槸:鍏堣繘鍏堝嚭 + * 鏈川涓婃槸涓绉嶆搷浣滃彈闄愬埗鐨勭嚎鎬ц〃 + * 鏈疄鐜扮嚎鎬ц〃閲囩敤鑷繁瀹炵幇鐨凙rrayList, + * ArrayList鐞嗚涓婁細鍙楀埌JVM鍒嗛厤鐨勫唴瀛樺ぇ灏,浠庤屽叾绌洪棿浼氭湁涓婇檺銆 + * 浣嗘槸,璇ラ敊璇,灏嗘湁JVM鎶涘嚭銆 + * 鎵浠,鏈疄鐜,闃熷垪蹇界暐鎵ц鍒ゆ柇婊¢槦鎿嶄綔銆 */ public class Queue { + + private ArrayList list; + private int head;//闃熷ご鎸囬拡 + private int foot;//闃熷熬鎸囬拡 + + + public Queue(){ + head = foot = 0; + } + + /** + * 鍒ょ┖ + * @return + */ + public boolean queueEmpty(){ + if (head==0&&foot==0){ + return true; + }else { + return false; + } + } + + /** + * 鍏ラ槦鍒 + * 1.鍏堟妸鍏冪礌鏀剧疆鍒伴槦鍒椾腑 + * 2.灏嗛槦灏炬寚閽堝姞1 + * @param o + */ + public void enQueue(Object o){ + list.add(foot,o); + foot++; + } + + /** + * 鍒犻櫎闃熷ご鍏冪礌 + * 0.鍏堝垽鏂槦鍒楁槸鍚︿负绌 + * 1.鍏堝彇鍑洪槦澶寸殑鍏冪礌 + * 2.鐒跺悗灏嗛槦澶寸殑鎸囬拡鍔1 + * @return + */ + public int deQueue(){ + if (queueEmpty()==true) + throw new IndexOutOfBoundsException("闃熷垪涓虹┖,鏃犳硶鎵ц璇ユ搷浣溿"); + list.remove(head); + return head++; + } + + + } diff --git a/group20/286166752/src/wiki/liven/code/dataStructures/Stack.java b/group20/286166752/src/wiki/liven/code/dataStructures/Stack.java index 59cb18c416..cc33ed2985 100644 --- a/group20/286166752/src/wiki/liven/code/dataStructures/Stack.java +++ b/group20/286166752/src/wiki/liven/code/dataStructures/Stack.java @@ -2,6 +2,61 @@ /** * Created by leven on 2017/2/21. + * 鏍:鍙厑璁稿湪涓绔繘琛屽垹闄ゆ垨鑰呭鍔犳搷浣滅殑绾挎ц〃銆 + * 鏈疄鐜,閲囩敤ArrayList銆 */ public class Stack { + + + private ArrayList list;//绾挎ц〃 + private int top = -1;//鏍堥《鎸囬拡,榛樿鎸囧悜鏍堜綆 + + + /** + * 鍏冪礌杩涙爤 + * @param o + * 1.鎸囬拡鍏堝姞1 + * 2.灏嗗厓绱犳斁鍏ユ爤涓 + * + */ + public void push(Object o){ + top++; + list.add(o); + } + + /** + * 鏍堥《鍏冪礌鍑烘爤,杩斿洖鏍堥《鎸囬拡鐨勫 + * @return + */ + public int pop(){ + if (top==-1) + throw new IndexOutOfBoundsException("鏍堜负绌,鏃犳硶鎵ц鍑烘爤鎿嶄綔銆"); + list.remove(top); + top--; + return top; + } + + /** + * 鑾峰彇鏍堥《鍏冪礌鐨勫 + * @return + */ + public Object getTop() { + if (top==-1) + throw new IndexOutOfBoundsException("鏍堜负绌,鏃犳硶鎵ц鍑烘爤鎿嶄綔銆"); + Object o = list.get(top); + return o; + } + + /** + * 鍒ょ┖ + * @return + */ + public boolean stackEmpty(){ + if (top==-1){ + return true; + }else { + return false; + } + } + } diff --git a/group20/286166752/src/wiki/liven/code/test/SomeDemos.java b/group20/286166752/src/wiki/liven/code/test/SomeDemos.java deleted file mode 100644 index 90a74edc35..0000000000 --- a/group20/286166752/src/wiki/liven/code/test/SomeDemos.java +++ /dev/null @@ -1,15 +0,0 @@ -package wiki.liven.code.test; - -/** - * Created by leven on 2017/2/21. - */ -public class SomeDemos { - - public static void main(String[] args){ - int[] a = new int[10]; - a[0] = 4; - System.out.println(a.length); - - } - -} diff --git a/group20/331798361/assignment2/src/array/ArrayUtil.java b/group20/331798361/assignment2/src/array/ArrayUtil.java new file mode 100644 index 0000000000..3fc1eabfa8 --- /dev/null +++ b/group20/331798361/assignment2/src/array/ArrayUtil.java @@ -0,0 +1,150 @@ +package array; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 + 渚嬪锛 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){ + if (origin.length == 0) { + return; + } + int n = origin.length - 1; + int temp; + for (int i = 0; i < n/2; i++) { + temp = origin[i]; + origin[i] = origin[n - i]; + origin[n - 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){ + if (oldArray.length == 0) { + return null; + } + int n = oldArray.length; + int zeros = 0; + for (int i = 0; i < n - 1; i++) { + if (oldArray[i] == 0) { + zeros++; + } + } + int[] result = new int[n - zeros]; + int j = 0; + for (int i = 0; i < n - zeros - 1; i++) { + while (oldArray[j] == 0) { + j++; + } + result[i] = oldArray[j]; + j++; + } + return result; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 + * 渚嬪 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){ + return null; + } + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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){ + return null; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 + * 渚嬪锛 max = 15 , 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] + * max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if (max <= 1) { + return new int[0]; + } + int n = 1; + int init = helpFibonacci(n); + while (init < max) { + n++; + init = helpFibonacci(n); + } + int[] result = new int[n - 1]; + for (int i = 0; i < n - 1; i++) { + result[i] = helpFibonacci(i + 1); + } + return result; + } + + public static int helpFibonacci(int n) { + if(n == 0) + return 0; + else if(n == 1) + return 1; + else + return helpFibonacci(n -1 ) + helpFibonacci(n - 2); + } + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 + * 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public int[] getPrimes(int max){ + return null; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 + * 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * @param max + * @return + */ + public int[] getPerfectNumbers(int max){ + return null; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 + * 渚嬪array= [3,8,9], seperator = "-" + * 鍒欒繑鍥炲间负"3-8-9" + * @param array + * @param + * @return + */ + public String join(int[] array, String seperator){ + return null; + } + + public static void main(String[] args) { + int[] a = fibonacci(15); + for (int i = 0; i < a.length; i++) { + System.out.println(a[i]); + } + } +} diff --git a/group20/331798361/assignment2/src/litestruts/LoginAction.java b/group20/331798361/assignment2/src/litestruts/LoginAction.java new file mode 100644 index 0000000000..bba4c11c9f --- /dev/null +++ b/group20/331798361/assignment2/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/group20/331798361/assignment2/src/litestruts/Struts.java b/group20/331798361/assignment2/src/litestruts/Struts.java new file mode 100644 index 0000000000..e6b77ef929 --- /dev/null +++ b/group20/331798361/assignment2/src/litestruts/Struts.java @@ -0,0 +1,76 @@ +package litestruts; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.File; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + try { + // parse xml file + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse("src/litestruts/struts.xml"); + doc.getDocumentElement().normalize(); + + // get action items + NodeList list = doc.getElementsByTagName("action"); + for (int i = 0; i < list.getLength(); i++) { + Node node = list.item(i); + Element element = (Element) node; + // look for action-related class + if (element.getAttribute("name").equals(actionName)){ + + Class c = Class.forName(element.getAttribute("class")); + Object o = c.newInstance(); + + //set name + Method setName = c.getDeclaredMethod("setName", String.class); + setName.invoke(o, parameters.get("name")); + + //set password + Method setPassword = c.getDeclaredMethod("setPassword", String.class); + setPassword.invoke(o, parameters.get("password")); + + //execute + Method execute = c.getDeclaredMethod("execute", null); + // login result + String result = execute.invoke(o).toString(); + + //get login messsage + Method getMessage = c.getDeclaredMethod("getMessage", null); + HashMap map = new HashMap<>(); + map.put("message", getMessage.invoke(o).toString()); + + // new view with parameter map + View view = new View(); + view.setParameters(map); + NodeList list1 = element.getElementsByTagName("result"); + for (int j = 0; j < list1.getLength(); j++) { + Node node1 = list1.item(j); + Element element1 = (Element) node1; + if (element1.getAttribute("name").equals(result)) { + view.setJsp(node1.getTextContent()); + } + } + return view; + } + } + } catch (Exception e) { + System.out.println("parse error"); + } + return null; + } +} diff --git a/group20/331798361/assignment2/src/litestruts/StrutsTest.java b/group20/331798361/assignment2/src/litestruts/StrutsTest.java new file mode 100644 index 0000000000..f227801865 --- /dev/null +++ b/group20/331798361/assignment2/src/litestruts/StrutsTest.java @@ -0,0 +1,44 @@ +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/group20/331798361/assignment2/src/litestruts/View.java b/group20/331798361/assignment2/src/litestruts/View.java new file mode 100644 index 0000000000..1eed614744 --- /dev/null +++ b/group20/331798361/assignment2/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/group20/331798361/assignment2/src/litestruts/struts.xml b/group20/331798361/assignment2/src/litestruts/struts.xml new file mode 100644 index 0000000000..d6da22a638 --- /dev/null +++ b/group20/331798361/assignment2/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/group20/404130810/src/com/coderising/array/ArrayUtil.java b/group20/404130810/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..22017ef9d0 --- /dev/null +++ b/group20/404130810/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,155 @@ +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[] reversedArray = new int[origin.length]; + for (int i = origin.length - 1, j = 0; i >= 0 && j < origin.length; i--, j++) { + reversedArray[j] = origin[i]; + } + origin = reversedArray; + } + + /** + * 现在有如下的一个数组: 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 zeroCount = 0; + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) { + zeroCount++; + } + } + int[] removedZeroArray = new int[oldArray.length - zeroCount]; + + for (int i = 0, j = 0; i < oldArray.length; i++, j++) { + if (oldArray[i] == 0) { + j--; + continue; + } + removedZeroArray[j] = oldArray[i]; + } + return removedZeroArray; + } + + /** + * 给定两个已经排序好的整形数组, 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) { + List mergedArrayList = new ArrayList<>(); + return null; + } + + /** + * 把一个已经存满数据的数组 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[] resultArray = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, resultArray, 0, oldArray.length); + return resultArray; + } + + /** + * 斐波那契数列为: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) { + + return null; + } + + private static int fibonacciNum(int n) { + if (n <= 2) { + return 1; + } else { + return fibonacciNum(n - 1) + fibonacciNum(n - 2); + } + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + return null; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + return null; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < array.length; i++) { + sb.append(array[i]); + if (i != array.length - 1) { + sb.append(seperator); + } + } + return sb.toString(); + } + + public static void main(String[] args) { + /* + * int[] origin = { 0, 1, 2, 0, 12 }; new + * ArrayUtil().removeZero(origin); + */ + /* + * int[] array1 = { 3, 5, 7, 8 }; int[] array2 = { 4, 5, 6, 7 }; new + * ArrayUtil().merge(array1, array2); + */ + /* + * int[] array = { 3, 8, 9, 10, 12 }; new ArrayUtil().grow(array, 9); + */ + /* + * int[] array = { 3, 8, 9, 10, 12 }; String seperator = "-"; + * System.out.println(new ArrayUtil().join(array, seperator)); + */ + } + +} \ No newline at end of file diff --git a/group20/404130810/src/com/coderising/litestruts/Struts.java b/group20/404130810/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..6e1e1bab9f --- /dev/null +++ b/group20/404130810/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,87 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.coderising.litestruts.utils.StrutsUtil; +import com.coderising.litestruts.view.View; + +public class Struts { + + public static View runAction(String actionName, Map params) { + + /* + * + * 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字段中。 + * + */ + View view = new View(); + String className = readActionInConfig(actionName); + List rtnList = invokeMethod(className, params); + String result = rtnList.get(0); + String msg = rtnList.get(1); + view.setParameters(buildViewParams(msg)); + view.setJsp(buildViewJsp(result,actionName)); + return view; + } + + + + private static String readActionInConfig(String actionName) { + StrutsUtil util = new StrutsUtil(); + return util.invokedAction(actionName); + } + + private static List invokeMethod(String className, Map params) { + + List rtnList = new ArrayList(); + try { + String name = params.get("name"); + String password = params.get("password"); + // Invoke set method + Class actionClass = Class.forName(className); + Method setNameMethod = actionClass.getMethod("setName", String.class); + Method setPasswordMethod = actionClass.getMethod("setPassword", String.class); + Object action = actionClass.newInstance(); + setNameMethod.invoke(action, name); + setPasswordMethod.invoke(action, password); + // Invoke execute method and add to the return List as first element + Method executeMethod = actionClass.getMethod("execute"); + rtnList.add(executeMethod.invoke(action).toString()); + // Invoke getMessage method and add to the return List as second element + Method getMessageMethod = actionClass.getMethod("getMessage"); + rtnList.add(getMessageMethod.invoke(action).toString()); + + } catch (Exception e) { + e.printStackTrace(); + } + return rtnList; + } + + private static Map buildViewParams(String msg) { + Map viewParams = new HashMap(); + viewParams.put("message", msg); + return viewParams; + } + + private static String buildViewJsp(String result, String actionName) { + StrutsUtil util = new StrutsUtil(); + return util.invokeResult(actionName,result); + } + +} \ No newline at end of file diff --git a/group20/404130810/src/com/coderising/litestruts/action/LoginAction.java b/group20/404130810/src/com/coderising/litestruts/action/LoginAction.java new file mode 100644 index 0000000000..3e252d26ee --- /dev/null +++ b/group20/404130810/src/com/coderising/litestruts/action/LoginAction.java @@ -0,0 +1,42 @@ +package com.coderising.litestruts.action; + +/** + * 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 + * + * @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/group20/404130810/src/com/coderising/litestruts/struts.xml b/group20/404130810/src/com/coderising/litestruts/struts.xml new file mode 100644 index 0000000000..e05b09a433 --- /dev/null +++ b/group20/404130810/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 diff --git a/group20/404130810/src/com/coderising/litestruts/test/StrutsTest.java b/group20/404130810/src/com/coderising/litestruts/test/StrutsTest.java new file mode 100644 index 0000000000..300b6efe6a --- /dev/null +++ b/group20/404130810/src/com/coderising/litestruts/test/StrutsTest.java @@ -0,0 +1,43 @@ +package com.coderising.litestruts.test; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +import com.coderising.litestruts.Struts; +import com.coderising.litestruts.view.View; + + +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")); + } +} \ No newline at end of file diff --git a/group20/404130810/src/com/coderising/litestruts/utils/StrutsUtil.java b/group20/404130810/src/com/coderising/litestruts/utils/StrutsUtil.java new file mode 100644 index 0000000000..3d26041ef4 --- /dev/null +++ b/group20/404130810/src/com/coderising/litestruts/utils/StrutsUtil.java @@ -0,0 +1,76 @@ +package com.coderising.litestruts.utils; + +import java.io.File; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class StrutsUtil { + + public final static String CONFIG_PATH = "../struts.xml"; + + public final static String CONFIG_NODE_ACTION = "action"; + + public final static String CONFIG_ATTR_NAME = "name"; + + public final static String CONFIG_ATTR_CLASS = "class"; + + public String invokedAction(String actionName){ + if(null != actionName && !"".equals(actionName)){ + Document doc = generateDoc(); + NodeList nodeList = doc.getElementsByTagName(CONFIG_NODE_ACTION); + for (int i = 0; i < nodeList.getLength(); i++) { + String actionNameConfiged = nodeList.item(i).getAttributes().getNamedItem(CONFIG_ATTR_NAME).getNodeValue(); + if(actionName.equals(actionNameConfiged)){ + return nodeList.item(i).getAttributes().getNamedItem(CONFIG_ATTR_CLASS).getNodeValue(); + } + } + } + throw new RuntimeException("actionName can't be found"); + } + + private Document generateDoc(){ + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + Document doc = null; + try { + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); + doc = docBuilder.parse (lookupConfigFile()); + } catch (Exception e) { + e.printStackTrace(); + } + return doc; + } + + private File lookupConfigFile(){ + URL url = getClass().getResource(CONFIG_PATH); + return new File(url.getPath()); + } + + + public String invokeResult(String actionName, String result) { + Document doc = generateDoc(); + NodeList nodeList = doc.getElementsByTagName("result"); + List subNodeList = new ArrayList(); + for (int i = 0; i < nodeList.getLength(); i++) { + Node parentNode = nodeList.item(i).getParentNode(); + if(parentNode.getAttributes().getNamedItem(CONFIG_ATTR_NAME).getNodeValue().equals(actionName)){ + subNodeList.add(nodeList.item(i)); + } + } + for (int i = 0; i < subNodeList.size(); i++) { + Node node = subNodeList.get(i); + if(node.getAttributes().getNamedItem(CONFIG_ATTR_NAME).getNodeValue().equals(result)){ + return node.getTextContent(); + } + } + throw new RuntimeException("result can't be found"); + } + +} diff --git a/group20/404130810/src/com/coderising/litestruts/view/View.java b/group20/404130810/src/com/coderising/litestruts/view/View.java new file mode 100644 index 0000000000..ff1a735948 --- /dev/null +++ b/group20/404130810/src/com/coderising/litestruts/view/View.java @@ -0,0 +1,23 @@ +package com.coderising.litestruts.view; + +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/group20/423184723/Test/\345\215\232\345\256\242\345\234\260\345\235\200" "b/group20/423184723/Test/\345\215\232\345\256\242\345\234\260\345\235\200" new file mode 100644 index 0000000000..b5457af5b6 --- /dev/null +++ "b/group20/423184723/Test/\345\215\232\345\256\242\345\234\260\345\235\200" @@ -0,0 +1 @@ +http://www.cnblogs.com/yyssyh213/p/6442285.html \ No newline at end of file diff --git a/group20/423184723/src/basic/ArrayList.java b/group20/423184723/src/basic/ArrayList.java new file mode 100644 index 0000000000..64a19bea8e --- /dev/null +++ b/group20/423184723/src/basic/ArrayList.java @@ -0,0 +1,78 @@ +package com.coding.basic; + +import com.sun.media.sound.EmergencySoundbank; + +public class ArrayList implements List { + /** + * 鍒楄〃涓厓绱犵殑涓暟 + */ + private int size = 0; + + /** + * 鍒濆鍖栨暟缁勫ぇ灏 + */ + private int arraySize = 100; + /** + * 鍒濆鍖栨暟缁 + */ + private Object[] elementData = new Object[arraySize]; + + /** + * 娣诲姞鏂规硶 + */ + public void add(Object o){ + if(size>=(arraySize*0.75)){ + Object [] target = new Object[(int) (arraySize*1.5)]; + System.arraycopy(elementData,0,target,0,arraySize); + target[size-1] = o; + size++; + }else if(size<(arraySize*0.75)){ + elementData[size-1]=o; + size++; + } + } + /** + * 鏍规嵁绱㈠紩娣诲姞鏂规硶 + */ + public void add(int index, Object o){ + if(size >= arraySize*0.75){ + Object [] target = new Object[(int) (arraySize*1.5)]; + System.arraycopy(elementData,0,target,0,arraySize); + for (int j = target.length;j>=index;j--){ + target[j-1] = target[j-2]; + } + target[index] = o; + size++; + }else if(size < arraySize*0.75){ + for (int j = elementData.length;j>=index;j--){ + elementData[j-1] = elementData[j-2]; + } + elementData[index] = o; + size++; + } + } + /** + * 鏍规嵁绱㈠紩鑾峰彇瀵硅薄 + */ + public Object get(int index){ + return elementData[index]; + } + /** + * 鏍规嵁绱㈠紩绉婚櫎瀵硅薄 + */ + public Object remove(int index){ + for (int i = index; i < elementData.length; i++) { + elementData[i]=elementData[i+1]; + size++; + } + return elementData[index]; + } + /** + * 鑾峰彇鏁扮粍澶у皬 + */ + public int size(){ + return this.size; + } + + +} diff --git a/group20/423184723/src/basic/BinaryTreeNode.java b/group20/423184723/src/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..d7ac820192 --- /dev/null +++ b/group20/423184723/src/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +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/group20/423184723/src/basic/Iterator.java b/group20/423184723/src/basic/Iterator.java new file mode 100644 index 0000000000..06ef6311b2 --- /dev/null +++ b/group20/423184723/src/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group20/423184723/src/basic/LinkedList.java b/group20/423184723/src/basic/LinkedList.java new file mode 100644 index 0000000000..9caf065304 --- /dev/null +++ b/group20/423184723/src/basic/LinkedList.java @@ -0,0 +1,146 @@ +package com.coding.basic; + +import com.coding.basic.LinkedList.Node; + +public class LinkedList implements List { + + private Node head; + private int size; + + public void add(Object o){ + if (head.data == null) { + head.data = o; + head.next = null; + size++; + return; + } + Node node = new Node(o); + Node curr = head; + while (curr.next != null) { + curr = curr.next; + } + curr.next = node; + size++; + } + public void add(int index , Object o){ + if (index < 0 || index > size) { + System.out.println(index + "鏃犳晥鎸囨暟"); + return; + } + if (head.data == null) { + if (index == 0) { + head.data = o; + head.next = null; + size++; + return; + } else { + System.out.println("鏃犳晥鎸囨暟"); + return; + } + } + Node node = new Node(o); + Node curr = head; + for (int i = 0; i < index - 1; i++) { + curr = curr.next; + } + Node temp = curr.next; + curr.next = node; + node.next = temp; + size++; + } + public Object get(int index){ + if (index < 0 || index > size) { + System.out.println(index + " is invalid index!"); + return null; + } + Node result = head; + for (int i = 0; i < index; i++) { + result = result.next; + } + return result; + } + public Object remove(int index){ + if (index < 0 || index > size) { + System.out.println(index + " is invalid index!"); + return null; + } + Node curr = head; + for (int i = 0; i < index - 1; i++) { + curr = curr.next; + } + Node result = curr.next; + curr.next = curr.next.next; + size--; + return result; + } + + public int size(){ + return this.size; + } + + public void addFirst(Object o){ + if (head.data == null) { + head.data = o; + head.next = null; + size++; + return; + } + Node temp = head; + head = new Node(o); + head.next = temp; + size++; + } + public void addLast(Object o){ + if (head.data == null) { + head.data = o; + head.next = null; + size++; + return; + } + Node node = new Node(o); + Node curr = head; + while (curr.next != null) { + curr = curr.next; + } + curr.next = node; + size++; + } + public Object removeFirst(){ + if (head.data == null) { + return null; + } + Node result = head; + head = head.next; + size--; + return result; + } + public Object removeLast(){ + if (head.data == null) { + return null; + } + Node curr = head; + for (int i = 0; i < size - 1; i++) { + curr = curr.next; + } + Node result = curr.next; + curr.next = null; + size--; + return result; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + public Node(Object o) { + data = o; + next = null; + } + + + + } +} diff --git a/group20/423184723/src/basic/List.java b/group20/423184723/src/basic/List.java new file mode 100644 index 0000000000..10d13b5832 --- /dev/null +++ b/group20/423184723/src/basic/List.java @@ -0,0 +1,9 @@ +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/group20/423184723/src/basic/Queue.java b/group20/423184723/src/basic/Queue.java new file mode 100644 index 0000000000..bb4e6bef5c --- /dev/null +++ b/group20/423184723/src/basic/Queue.java @@ -0,0 +1,29 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList list = new LinkedList(); + public void enQueue(Object o){ + list.add(o); + } + + public Object deQueue(){ + int length = list.size(); + if (length == 0) { + return null; + } + return list.removeFirst(); + } + + public boolean isEmpty(){ + if (list.size() == 0) { + return true; + } else { + return false; + } + } + + public int size(){ + return list.size; + } +} diff --git a/group20/423184723/src/basic/Stack.java b/group20/423184723/src/basic/Stack.java new file mode 100644 index 0000000000..6f3def6f0f --- /dev/null +++ b/group20/423184723/src/basic/Stack.java @@ -0,0 +1,35 @@ +package com.coding.basic; + +public class Stack { + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + int length = elementData.size(); + if (length == 0) { + return null; + } + return elementData.remove(length - 1); + } + + public Object peek(){ + int length = elementData.size(); + if (length == 0) { + return null; + } + return elementData.get(length - 1); + } + public boolean isEmpty(){ + if (elementData.size() != 0) { + return false; + } else { + return true; + } + } + public int size(){ + return elementData.size(); + } +} diff --git a/group20/452472201/src/com/coding/basic/ArrayList.java b/group20/452472201/src/com/coding/basic/ArrayList.java index 1af26ce934..80746f1675 100644 --- a/group20/452472201/src/com/coding/basic/ArrayList.java +++ b/group20/452472201/src/com/coding/basic/ArrayList.java @@ -4,10 +4,9 @@ public class ArrayList implements List { private int size=0; - private Object[] elementData =new Object[10]; + private Object[] elementData =new Object[5]; - //鏁扮粍鎵╁ private void ensureCapacityInternal(){ if(size==elementData.length){ Object[] newArray = new Object[size*2]; @@ -33,8 +32,8 @@ public void add(int index, Object o){ } System.arraycopy(elementData, index, elementData, index+1,size-index ); - elementData[index]=o; - size++; + elementData[index]=o; + size++; } public Object get(int index){ @@ -75,24 +74,41 @@ public int size(){ private class Iter implements Iterator { - //璁℃暟鍣 + private int coursor=-1; - //鍒ゆ柇鏄惁瀛樺湪涓嬩竴涓 + public boolean hasNext(){ return coursor+1> 1); i++) { + int num = origin[i]; + origin[i] = origin[origin.length - 1 - i]; + origin[origin.length - 1 - i] = num; + } + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 static int[] removeZero(int[] oldArray) { + int count = 0;// 璁℃暟鍣 + /* + * 鍒╃敤鍐掓场锛屽皢0鍏冪礌鍚戝悗鎺 {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * {1,3,4,5,0,6,6,0,5,4,7,6,7,0,5,0} {1,3,4,5,6,6,0,5,4,7,6,7,0,5,0,0} + * .... + */ + for (int i = 0; i < oldArray.length - count; i++) { + // 绱㈠紩涓篿鐨勫厓绱犱负0锛屽垯渚濇灏嗙储寮昳鐨勫厓绱犱笌i+1鐨勫厓绱犲鎹 + if (oldArray[i] == 0) { + for (int j = i; j < oldArray.length - 1 - count; j++) { + int num = oldArray[j]; + oldArray[j] = oldArray[j + 1]; + oldArray[j + 1] = num; + } + count++;// 璁℃暟鍣+1 + i--;// 闃叉鍘熺储寮昳+1浣嶇疆鐨勫厓绱犱负0, + } + } + // 鍒涘缓鏂版暟缁 + int[] newArray = new int[oldArray.length - count]; + System.arraycopy(oldArray, 0, newArray, 0, newArray.length); + return newArray; + } + + /** + * 鐜板湪鏈夊涓嬬殑涓涓暟缁勶細 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 static int[] removeZero2(int[] oldArray) { + int count = 0;// 璁℃暟鍣 + for (int i = 0; i < oldArray.length; i++) { + if (oldArray[i] == 0) + count++;// 璁℃暟鍣+1 + } + // 鍒涘缓鏂版暟缁 + int[] newArray = new int[oldArray.length - count]; + for (int i = 0, j = 0; i < oldArray.length; i++) { + if (oldArray[i] != 0) { + newArray[j] = oldArray[i]; + j++; + } + } + return newArray; + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 渚嬪 a1 = + * [3, 5, 7,8] a2 = [4, 5, 6,7] 鍒 a3 涓篬3,4,5,6,7,8] , 娉ㄦ剰锛 宸茬粡娑堥櫎浜嗛噸澶 + * + * @param array1 + * @param array2 + * @return + */ + + public static int[] merge(int[] array1, int[] array2) { + int[] newArray = new int[array1.length + array2.length]; + int i = 0, j = 0, k = 0; + while (i < array1.length && j < array2.length) { + // <= 閮藉彇 array1 + if (array1[i] <= array2[j]) { + // 绛変簬鏃讹紝灏哸rray2涓嬫爣++ + if (array1[i] == array2[j]) + j++; + newArray[k++] = array1[i++]; + } else + newArray[k++] = array2[j++]; + + } + // 灏嗘病鏈夊惊鐜畬姣曠殑鍏冪礌鎻掑叆 + while (i < array1.length) + newArray[k++] = array1[i++]; + while (j < array2.length) + newArray[k++] = array2[j++]; + int[] result = newArray; + // 闀垮害缂╃煭鍒欐柊寤烘暟缁 + if (k < newArray.length) { + result = new int[k]; + for (int l = 0; l < result.length; l++) + result[l] = newArray[l]; + } + return result; + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 oldArray鐨勫閲忚繘琛屾墿灞曪紝 鎵╁睍鍚庣殑鏂版暟鎹ぇ灏忎负oldArray.length + size + * 娉ㄦ剰锛岃佹暟缁勭殑鍏冪礌鍦ㄦ柊鏁扮粍涓渶瑕佷繚鎸 渚嬪 oldArray = [2,3,6] , size = 3,鍒欒繑鍥炵殑鏂版暟缁勪负 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static 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; + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細1锛1锛2锛3锛5锛8锛13锛21...... 锛岀粰瀹氫竴涓渶澶у硷紝 杩斿洖灏忎簬璇ュ肩殑鏁板垪 渚嬪锛 max = 15 , + * 鍒欒繑鍥炵殑鏁扮粍搴旇涓 [1锛1锛2锛3锛5锛8锛13] max = 1, 鍒欒繑鍥炵┖鏁扮粍 [] + * + * @param max + * @return + */ + public static int[] fibonacci(int max) { + if (max <= 1) + return new int[] {}; + int[] nums = new int[max]; + nums[0] = nums[1] = 1; + int flag; + for (flag = 0; (flag < max - 2 && nums[flag] + nums[flag + 1] < max); flag++) { + nums[flag + 2] = nums[flag] + nums[flag + 1]; + } + // 鍒涘缓鏂版暟缁 + int[] newArray = nums; + if (newArray.length != flag + 2) { + newArray = new int[flag + 2]; + for (int i = 0; i < newArray.length; i++) { + newArray[i] = nums[i]; + } + } + return newArray; + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + int[] array = new int[max>>1]; + int flag = 0; + for (int i = 2; i < max; i++) { + int j; + for (j = 2; j <= (i >> 1); j++) { + if (i % j == 0) + break; + + } + //濡傛灉澶т簬锛屽垯璇佹槑j++鏈夎繍琛岋紝宸茬粡瀹屾暣瀵规瘮 + if(j > i>>1) + array[flag++] = i; + + } + int[] newArray = array; + if(flag < array.length){ + newArray = new int[flag]; + for (int i = 0; i < newArray.length; i++) { + newArray[i] = array[i]; + } + } + return newArray; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + int[] array = new int[max]; + int flag = 0; + for (int i = 1; i < max; i++) { + int sum = 0; + for (int j = 1; j < i; j++) { + if (i % j == 0) + sum+=j; + } + //濡傛灉澶т簬锛屽垯璇佹槑j++鏈夎繍琛岋紝宸茬粡瀹屾暣瀵规瘮 + if(sum == i) + array[flag++] = i; + + } + int[] newArray = array; + if(flag < array.length){ + newArray = new int[flag]; + for (int i = 0; i < newArray.length; i++) { + newArray[i] = array[i]; + } + } + return newArray; + } + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 渚嬪array= [3,8,9], seperator = "-" 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator) { + String str = ""; + for (int i = 0; i < array.length; i++) + str += i != array.length - 1 ? array[i] + seperator : array[i]; + return str; + } +} diff --git a/group20/592146505/coderising/src/org/wsc/array/ArrayUtilTest.java b/group20/592146505/coderising/src/org/wsc/array/ArrayUtilTest.java new file mode 100644 index 0000000000..009dec2938 --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/array/ArrayUtilTest.java @@ -0,0 +1,98 @@ +package org.wsc.array; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ArrayUtilTest { + + @Test + public void testReverseArray() { + int[] nums = new int[]{7, 9 , 30, 3, 5}; + ArrayUtil.reverseArray(nums); + assertArrayEquals(nums, new int[]{5, 3 ,30, 9, 7}); + } + + /** + * 鍒犻櫎0 + */ + @Test + public void testRemoveZero() { + int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5,0}; + nums = ArrayUtil.removeZero(nums); + assertArrayEquals(nums, new int[]{7, 9 ,30, 3, 5}); + } + + /** + * 鍒犻櫎0 + */ + @Test + public void testRemoveZero2() { + int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5,0}; + nums = ArrayUtil.removeZero2(nums); + assertArrayEquals(nums, new int[]{7, 9 ,30, 3, 5}); + } + + /** + * 鎷兼帴 + */ + @Test + public void testJoin() { + int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5,0}; + String str = ArrayUtil.join(nums,"-"); + assertEquals(str, "0-7-9-0-0-30-0-3-5-0"); + } + + /** + * 鎵╁ + */ + @Test + public void testGrow() { + int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5}; + nums = ArrayUtil.grow(nums,3); + assertTrue(nums.length==12); + } + + /** + * 鍚堝苟 + */ + @Test + public void testMerge() { + int[] nums = new int[]{3, 5, 7,8}; + int[] nums2 = new int[]{4, 5, 6,7,8}; + nums = ArrayUtil.merge(nums,nums2); + assertTrue(nums.length==6); + assertArrayEquals(nums, new int[]{3, 4 ,5, 6, 7,8}); + } + + /** + *鏂愭尝閭e鏁板垪 + */ + @Test + public void testFibonacci() { + int[] nums = ArrayUtil.fibonacci(15); + assertTrue(nums.length==7); + assertArrayEquals(nums, new int[]{1,1,2,3,5,8,13}); + } + + /** + * 绱犳暟 + */ + @Test + public void testGetPrimes() { + int[] nums = ArrayUtil.getPrimes(23); + assertTrue(nums.length==8); + assertArrayEquals(nums, new int[]{2,3,5,7,11,13,17,19}); + } + + /** + * 瀹屾暟 + */ + @Test + public void testGetPerfectNumbers() { + int[] nums = ArrayUtil.getPerfectNumbers(10000); + assertTrue(nums.length==4); + assertArrayEquals(nums, new int[]{6,28,496,8128}); + } + +} diff --git a/group20/592146505/coderising/src/org/wsc/litestruts/Action.java b/group20/592146505/coderising/src/org/wsc/litestruts/Action.java new file mode 100644 index 0000000000..083519bac6 --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/litestruts/Action.java @@ -0,0 +1,21 @@ +package org.wsc.litestruts; + +import java.util.Set; + +public class Action { + + /** 鏍囩鍚 */ + private String tag; + /** 灞炴 */ + private String name; + + /** 绫诲悕 */ + private String className; + + /** 瀛愭爣绛 */ + private Set statusElement; + + /** 鏂囨湰鍐呭 */ + private String textContent; + +} diff --git a/group20/592146505/coderising/src/org/wsc/litestruts/LoginAction.java b/group20/592146505/coderising/src/org/wsc/litestruts/LoginAction.java new file mode 100644 index 0000000000..bb23f5eebb --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package org.wsc.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/group20/592146505/coderising/src/org/wsc/litestruts/Struts.java b/group20/592146505/coderising/src/org/wsc/litestruts/Struts.java new file mode 100644 index 0000000000..30a418979f --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/litestruts/Struts.java @@ -0,0 +1,124 @@ +package org.wsc.litestruts; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +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.wsc.litestruts.util.DocumentUtil; +import org.xml.sax.SAXException; + +public class Struts { + private static final DocumentUtil DOCUMENT_UTIL; + private static Document document; + static{ + /* 0. 璇诲彇閰嶇疆鏂囦欢struts.xml */ + DOCUMENT_UTIL = DocumentUtil.newInstance(); + try { + document = DOCUMENT_UTIL.getDocument("src/struts.xml"); + } catch (ParserConfigurationException | SAXException | IOException e) { + e.printStackTrace(); + } + } + + /** + * + * @param actionName + * @param parameters + * @return + */ + public static View runAction(String actionName, Map parameters) { + String className = null; + String jsp = null; + Map results = new HashMap(); + Node struts = document.getDocumentElement();// 鑾峰彇鏍硅妭鐐 + NodeList actions = struts.getChildNodes();// 鑾峰彇瀛愯妭鐐 + for (int i = 0; i < actions.getLength(); i++) { + // 杩囨护绌鸿妭鐐 + if (actions.item(i).getNodeType() != Node.ELEMENT_NODE) + continue; + Element action = (Element) actions.item(i); + if (!action.getAttribute("name").equals(actionName)) + continue; + className = action.getAttribute("class"); + /* + * 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + * 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 ("name"="test" , + * "password"="1234") , 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + */ + Class clazz = null; + Object instance = null; + try { + clazz = Class.forName(className); + instance = clazz.getConstructor().newInstance(); + Set keySet = parameters.keySet(); + for (String key : keySet) { + Object parameter = parameters.get(key); + Method method = instance.getClass().getMethod( + "set" + (key.substring(0, 1).toUpperCase() + key.substring(1)), parameter.getClass()); + method.invoke(instance, parameter); + } + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } + + /* 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" */ + String rt = null; + try { + Method method = clazz.getMethod("execute"); + rt = (String) method.invoke(instance); + } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + /* + * 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap + * , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , 鏀惧埌View瀵硅薄鐨刾arameters + */ + Field[] fields = instance.getClass().getDeclaredFields(); + Method[] methods = instance.getClass().getMethods(); + for (Field field : fields) { + String fieldName = field.getName(); + for (Method method : methods) { + if (method.getName() + .equals(("get" + (fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1))))) { + try { + results.put(fieldName, (String) method.invoke(instance)); + break; + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + NodeList resultNodes = action.getChildNodes(); + for (int j = 0; j < resultNodes.getLength(); j++) { + if (resultNodes.item(j).getNodeType() != Node.ELEMENT_NODE) + continue; + Element result = (Element) resultNodes.item(j); + if (!result.getAttribute("name").equals(rt)) + continue; + jsp = result.getTextContent(); + } + + } + /* + * 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + * 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + */ + View view = new View(); + view.setJsp(jsp); + view.setParameters(results); + return view; + } + +} diff --git a/group20/592146505/coderising/src/org/wsc/litestruts/StrutsTest.java b/group20/592146505/coderising/src/org/wsc/litestruts/StrutsTest.java new file mode 100644 index 0000000000..8101e95fe3 --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/litestruts/StrutsTest.java @@ -0,0 +1,43 @@ +package org.wsc.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/group20/592146505/coderising/src/org/wsc/litestruts/View.java b/group20/592146505/coderising/src/org/wsc/litestruts/View.java new file mode 100644 index 0000000000..f21906d1a5 --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/litestruts/View.java @@ -0,0 +1,26 @@ +package org.wsc.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/group20/592146505/coderising/src/org/wsc/litestruts/util/DocumentUtil.java b/group20/592146505/coderising/src/org/wsc/litestruts/util/DocumentUtil.java new file mode 100644 index 0000000000..9125b244cd --- /dev/null +++ b/group20/592146505/coderising/src/org/wsc/litestruts/util/DocumentUtil.java @@ -0,0 +1,69 @@ +package org.wsc.litestruts.util; + +import java.io.File; +import java.io.IOException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +/** + * DOM瑙f瀽宸ュ叿绫 + * 鎳掓眽鍗曚緥妯″紡 + * @author Administrator + * @date 2017骞2鏈28鏃ヤ笅鍗9:45:39 + * @version v1.0 + * + */ +public class DocumentUtil { + private static DocumentUtil documentUtil; + + private DocumentUtil() { + super(); + } + + /** + * 鑾峰彇瀹炰緥 + * @return + */ + public static DocumentUtil newInstance(){ + if(documentUtil == null) + synchronized (DocumentUtil.class) { + if(documentUtil == null) + documentUtil = new DocumentUtil(); + } + return documentUtil; + } + + + /** + * 瑙f瀽XML鏂囦欢鑾峰彇DOM鏍 + * @param fileUrl + * XML鏂囦欢璺緞 + * @return + * @throws IOException + * @throws SAXException + * @throws ParserConfigurationException + */ + public Document getDocument(String fileUrl) throws ParserConfigurationException, SAXException, IOException{ + return getDocument(new File(fileUrl)); + } + /** + * 瑙f瀽XML鏂囦欢鑾峰彇DOM鏍 + * @param file + * XML鏂囦欢瀹炰緥 + * @return + * @throws ParserConfigurationException + * @throws IOException + * @throws SAXException + */ + public Document getDocument(File xmlFile) throws ParserConfigurationException, SAXException, IOException{ + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();//瑙f瀽鍣ㄥ伐鍘 + DocumentBuilder db = dbf.newDocumentBuilder();//瑙f瀽鍣 + return db.parse(xmlFile);//DOM鏍 + } + +} diff --git a/group20/592146505/coderising/src/struts.xml b/group20/592146505/coderising/src/struts.xml new file mode 100644 index 0000000000..b709ec6636 --- /dev/null +++ b/group20/592146505/coderising/src/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/group20/592146505/data _structure/src/org/wsc/exception/NullElementException.java b/group20/592146505/data _structure/src/org/wsc/exception/NullElementException.java new file mode 100644 index 0000000000..aa7763ef00 --- /dev/null +++ b/group20/592146505/data _structure/src/org/wsc/exception/NullElementException.java @@ -0,0 +1,23 @@ +package org.wsc.exception; + +/** + * + * 绌哄厓绱犲紓甯 + * @author Administrator + * @date 2017骞2鏈26鏃ヤ笅鍗4:15:49 + * @version v1.0 + * + */ +public class NullElementException extends RuntimeException { + + private static final long serialVersionUID = 4729177529481680909L; + + public NullElementException() { + super(); + } + + public NullElementException(String message) { + super(message); + } + +} diff --git a/group20/592146505/data _structure/src/org/wsc/exception/RepeatingElementException.java b/group20/592146505/data _structure/src/org/wsc/exception/RepeatingElementException.java new file mode 100644 index 0000000000..a5b66c54a1 --- /dev/null +++ b/group20/592146505/data _structure/src/org/wsc/exception/RepeatingElementException.java @@ -0,0 +1,23 @@ +package org.wsc.exception; + +/** + * + * 閲嶅鍏冪礌寮傚父 + * @author Administrator + * @date 2017骞2鏈26鏃ヤ笅鍗4:15:49 + * @version v1.0 + * + */ +public class RepeatingElementException extends RuntimeException { + + private static final long serialVersionUID = 4729177529481680909L; + + public RepeatingElementException() { + super(); + } + + public RepeatingElementException(String message) { + super(message); + } + +} diff --git a/group20/592146505/data _structure/src/cn/wsc/util/ArrayList.java b/group20/592146505/data _structure/src/org/wsc/list/ArrayList.java similarity index 95% rename from group20/592146505/data _structure/src/cn/wsc/util/ArrayList.java rename to group20/592146505/data _structure/src/org/wsc/list/ArrayList.java index 94bb1f3217..28b8db4132 100644 --- a/group20/592146505/data _structure/src/cn/wsc/util/ArrayList.java +++ b/group20/592146505/data _structure/src/org/wsc/list/ArrayList.java @@ -1,9 +1,11 @@ -package cn.wsc.util; +package org.wsc.list; import java.util.Arrays; import java.util.ConcurrentModificationException; import java.util.NoSuchElementException; +import javafx.stage.StageStyle; + /** * ArrayList绫 * @@ -93,17 +95,16 @@ public void remove() { } - // @Override - // public Object[] toArray() { - // // TODO Auto-generated method stub - // return null; - // } - // - // @Override - // public T[] toArray(T[] a) { - // // TODO Auto-generated method stub - // return null; - // } + @SuppressWarnings("unchecked") + @Override + public E[] toArray() { + return (E[]) elementData; + } + + @Override + public T[] toArray(T[] a) { + return null; + } @Override public boolean add(E element) { diff --git a/group20/592146505/data _structure/src/org/wsc/list/Iterator.java b/group20/592146505/data _structure/src/org/wsc/list/Iterator.java new file mode 100644 index 0000000000..59590bbf7e --- /dev/null +++ b/group20/592146505/data _structure/src/org/wsc/list/Iterator.java @@ -0,0 +1,21 @@ +package org.wsc.list; + +public interface Iterator { + /** + * 鏄惁瀛樺湪涓嬩竴涓厓绱 + * @return + */ + boolean hasNext(); + + /** + * 鑾峰彇涓嬩竴涓厓绱 + * @return + */ + E next(); + + /** + * 鍒犻櫎褰撳墠鍏冪礌 + */ + void remove(); + +} diff --git a/group20/592146505/data _structure/src/cn/wsc/util/LinkedList.java b/group20/592146505/data _structure/src/org/wsc/list/LinkedList.java similarity index 90% rename from group20/592146505/data _structure/src/cn/wsc/util/LinkedList.java rename to group20/592146505/data _structure/src/org/wsc/list/LinkedList.java index ccb9ff2783..b909cfeabc 100644 --- a/group20/592146505/data _structure/src/cn/wsc/util/LinkedList.java +++ b/group20/592146505/data _structure/src/org/wsc/list/LinkedList.java @@ -1,18 +1,19 @@ -package cn.wsc.util; +package org.wsc.list; import java.util.ConcurrentModificationException; import java.util.NoSuchElementException; /** * LinkedList绫 - * + * 瀹炵幇List鎺ュ彛鍜孮ueue鎺ュ彛 + * 鍩轰簬閾捐〃鐨勯泦鍚 * @author Administrator * @date 2017骞2鏈25鏃ヤ笂鍗10:52:41 * @version v1.0 * * @param */ -public class LinkedList implements List { +public class LinkedList implements List,Queue { private int size; Node first; // 閾捐〃鐨勫ご鑺傜偣 @@ -224,6 +225,14 @@ public E remove(int index) { return unlink(node(index)); } + public E removeFirst() { + //鑾峰彇澶磋妭鐐 + final Node f = first; + if (f == null) + throw new NoSuchElementException(); + return remove(0); + } + /** * 鍒犻櫎鑺傜偣 * @@ -252,6 +261,16 @@ E unlink(Node x) { return element; } + @Override + public void enQueue(E e) { + linkLast(e); + } + + @Override + public E deQueue() { + return removeFirst(); + } + /** * 浣嶇疆鑼冨洿妫鏌 >0 && <=size * @@ -281,4 +300,17 @@ private void checkElementIndex(int index) { private String outOfBoundsMsg(int index) { return "Index: " + index + ", Size: " + this.size; } + + @Override + public E[] toArray() { + // TODO Auto-generated method stub + return null; + } + + @Override + public T[] toArray(T[] a) { + // TODO Auto-generated method stub + return null; + } + } diff --git a/group20/592146505/data _structure/src/cn/wsc/util/List.java b/group20/592146505/data _structure/src/org/wsc/list/List.java similarity index 83% rename from group20/592146505/data _structure/src/cn/wsc/util/List.java rename to group20/592146505/data _structure/src/org/wsc/list/List.java index 9b61278fc7..2fd90c6395 100644 --- a/group20/592146505/data _structure/src/cn/wsc/util/List.java +++ b/group20/592146505/data _structure/src/org/wsc/list/List.java @@ -1,4 +1,4 @@ -package cn.wsc.util; +package org.wsc.list; /** * List鎺ュ彛 @@ -38,20 +38,21 @@ public interface List { * @return */ Iterator iterator(); - // - // /** - // * 杩斿洖闆嗗悎鏁扮粍瀵硅薄 - // * - // * @return - // */ - // Object[] toArray(); - // - // /** - // * 灏嗛泦鍚堝厓绱犲鍒跺埌鏂版暟缁勪腑 - // * @param a - // * @return - // */ - // T[] toArray(T[] a); + + /** + * 杩斿洖闆嗗悎鏁扮粍瀵硅薄 + * + * @return + */ + Object[] toArray(); + + /** + * 灏嗛泦鍚堝厓绱犲鍒跺埌鏂版暟缁勪腑 + * + * @param a + * @return + */ + T[] toArray(T[] a); /** * 鍦ㄩ泦鍚堟湯灏捐拷鍔犲厓绱 diff --git a/group20/592146505/data _structure/src/org/wsc/list/Queue.java b/group20/592146505/data _structure/src/org/wsc/list/Queue.java new file mode 100644 index 0000000000..2087335e58 --- /dev/null +++ b/group20/592146505/data _structure/src/org/wsc/list/Queue.java @@ -0,0 +1,42 @@ +package org.wsc.list; + +/** + * + * 闃熷垪 + * + * @author Administrator + * @date 2017骞2鏈25鏃ヤ笅鍗6:08:01 + * @version v1.0 + * + * @param + */ +public interface Queue { + + /** + * 鍏ュ垪 + * + * @param e + */ + public void enQueue(E e); + + /** + * 鍑哄垪 + * + * @return + */ + public E deQueue(); + + /** + * 鏄惁涓虹┖ + * + * @return + */ + public boolean isEmpty(); + + /** + * 鍏冪礌闀垮害 + * + * @return + */ + public int size(); +} diff --git a/group20/592146505/data _structure/src/cn/wsc/utils/Stack.java b/group20/592146505/data _structure/src/org/wsc/stack/Stack.java similarity index 83% rename from group20/592146505/data _structure/src/cn/wsc/utils/Stack.java rename to group20/592146505/data _structure/src/org/wsc/stack/Stack.java index 3f156461e8..96b931ca07 100644 --- a/group20/592146505/data _structure/src/cn/wsc/utils/Stack.java +++ b/group20/592146505/data _structure/src/org/wsc/stack/Stack.java @@ -1,4 +1,6 @@ -package cn.wsc.utils; +package org.wsc.stack; + +import org.wsc.list.ArrayList; public class Stack { private ArrayList elementData = new ArrayList(); diff --git a/group20/592146505/data _structure/src/org/wsc/tree_node/BinaryTreeNode.java b/group20/592146505/data _structure/src/org/wsc/tree_node/BinaryTreeNode.java new file mode 100644 index 0000000000..b68784541d --- /dev/null +++ b/group20/592146505/data _structure/src/org/wsc/tree_node/BinaryTreeNode.java @@ -0,0 +1,166 @@ +package org.wsc.tree_node; + +import org.wsc.exception.NullElementException; +import org.wsc.exception.RepeatingElementException; + +/** + * BinaryTreeNode 浜屽弶鏍戠粨鏋 + * + * + * @author Administrator + * @date 2017骞2鏈26鏃ヤ笅鍗5:47:32 + * @version v1.0 + * + * @param + * 蹇呴』瀹炵幇Comparable鎺ュ彛 + */ +@SuppressWarnings("rawtypes") +public class BinaryTreeNode { + + /** 宸﹁妭鐐 */ + private BinaryTreeNode left; + /** 鏁版嵁鍖 */ + private E data; + /** 鍙宠妭鐐 */ + private BinaryTreeNode right; + + /** + * 鎻掑叆 + * + * @param data + * @return + */ + @SuppressWarnings("unchecked") + public BinaryTreeNode insert(E data) { + if (data == null) + throw new NullElementException("Do not insert a null"); + // 褰撳墠鏁版嵁鍖轰负绌,鍒欏皢data鏀惧叆鏁版嵁鍖 + if (this.data == null) { + this.data = data; + return this; + } + // 瀵规瘮褰撳墠鏁版嵁鍖烘暟鎹拰data澶у皬 + int result = this.data.compareTo(data); + // 濡傛灉鐩哥瓑锛屽垯鎶涘嚭寮傚父 + if (result == 0) + throw new RepeatingElementException("Do not insert duplicate element"); + // 褰撳墠鏁版嵁鍖烘暟鎹ぇ浜巇ata锛屽皢data閫掑綊鏀惧叆宸﹁妭鐐 + if (result > 0) { + // 宸﹁妭鐐逛负绌猴紝鍒欏皢鏁版嵁缃叆宸﹁妭鐐 + if (left == null) + left = new BinaryTreeNode(data); + else// 宸﹁妭鐐逛笉涓虹┖锛屽垯灏嗘暟鎹掑綊缃叆宸﹁妭鐐 + left.insert(data); + } else { + // 鍙宠妭鐐逛负绌猴紝鍒欏皢鏁版嵁缃叆鍙宠妭鐐 + if (right == null) + right = new BinaryTreeNode(data); + else// 鍙宠妭鐐逛笉涓虹┖锛屽垯灏嗘暟鎹掑綊缃叆鍙宠妭鐐 + right.insert(data); + } + return this; + } + + /** + * 鏌ヨ + * + * @param data + * @return + */ + @SuppressWarnings("unchecked") + public BinaryTreeNode seek(E data) { + checkCurrElement(); + if (data == null) + return null; + // 瀵规瘮褰撳墠鏁版嵁鍖烘暟鎹拰data澶у皬 + int result = this.data.compareTo(data); + if (result == 0) { + return this; + } else if (result > 0) {// 褰撳墠鏁版嵁鍖烘暟鎹ぇ浜巇ata锛岄掑綊瀵规瘮宸﹁妭鐐 + return left == null ? null : left.seek(data); + } else {// 褰撳墠鏁版嵁鍖烘暟鎹皬浜巇ata锛岄掑綊瀵规瘮鍙宠妭鐐 + return right == null ? null : right.seek(data); + } + + } + + /** + * 鍒犻櫎 + * + * @param data + * @return + */ + public BinaryTreeNode remove(E data) { + return removeChild(null, data); + } + + @SuppressWarnings("unchecked") + public BinaryTreeNode removeChild(BinaryTreeNode supNode, E data) { + checkCurrElement(); + if (data == null) + return null; + // 瀵规瘮褰撳墠鏁版嵁鍖烘暟鎹拰data澶у皬 + int result = this.data.compareTo(data); + // 濡傛灉鐩稿悓锛屽皢閫氳繃鐖惰妭鐐瑰皢瀛愯妭鐐瑰紩鐢ㄧ疆涓簄ull + if (supNode != null && result == 0) { + if (supNode.left == this) + supNode.left = null; + else + supNode.right = null; + } else if (result > 0) {// 褰撳墠鏁版嵁鍖烘暟鎹ぇ浜巇ata锛岄掑綊瀵规瘮宸﹁妭鐐 + return left == null ? null : left.removeChild(this, data); + } else {// 褰撳墠鏁版嵁鍖烘暟鎹皬浜巇ata锛岄掑綊瀵规瘮鍙宠妭鐐 + return right == null ? null : right.removeChild(this, data); + } + return this; + } + + /** + * 妫鏌ュ綋鍓嶈妭鐐瑰厓绱犳槸鍚︽湁鏁 + */ + private void checkCurrElement() { + if (this.data == null) + throw new NullElementException("The current node element is null"); + } + + public BinaryTreeNode() { + super(); + } + + public BinaryTreeNode(E data) { + super(); + this.data = data; + } + + public BinaryTreeNode(BinaryTreeNode left, E data, BinaryTreeNode right) { + super(); + this.left = left; + this.data = data; + this.right = right; + } + + public E getData() { + return data; + } + + public void setData(E 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; + } + +} diff --git "a/group20/755659358/blogs/blog\345\234\260\345\235\200.txt" "b/group20/755659358/blogs/blog\345\234\260\345\235\200.txt" new file mode 100644 index 0000000000..82dd8add37 --- /dev/null +++ "b/group20/755659358/blogs/blog\345\234\260\345\235\200.txt" @@ -0,0 +1 @@ +week02:http://www.jianshu.com/p/22d2cbefdaa1 diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/ArrayList.java b/group20/755659358/week01/src/liuxincourse/ArrayList.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/src/liuxincourse/ArrayList.java rename to group20/755659358/week01/src/liuxincourse/ArrayList.java diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/LinkedList.java b/group20/755659358/week01/src/liuxincourse/LinkedList.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/src/liuxincourse/LinkedList.java rename to group20/755659358/week01/src/liuxincourse/LinkedList.java diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/List.java b/group20/755659358/week01/src/liuxincourse/List.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/src/liuxincourse/List.java rename to group20/755659358/week01/src/liuxincourse/List.java diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/Queue.java b/group20/755659358/week01/src/liuxincourse/Queue.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/src/liuxincourse/Queue.java rename to group20/755659358/week01/src/liuxincourse/Queue.java diff --git a/group20/755659358/liuxincourse_datastructure/src/liuxincourse/Stack.java b/group20/755659358/week01/src/liuxincourse/Stack.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/src/liuxincourse/Stack.java rename to group20/755659358/week01/src/liuxincourse/Stack.java diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/ArrayListTest.java b/group20/755659358/week01/test/liuxincourse/ArrayListTest.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/test/liuxincourse/ArrayListTest.java rename to group20/755659358/week01/test/liuxincourse/ArrayListTest.java diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/LinkedListTest.java b/group20/755659358/week01/test/liuxincourse/LinkedListTest.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/test/liuxincourse/LinkedListTest.java rename to group20/755659358/week01/test/liuxincourse/LinkedListTest.java diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/QueueTest.java b/group20/755659358/week01/test/liuxincourse/QueueTest.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/test/liuxincourse/QueueTest.java rename to group20/755659358/week01/test/liuxincourse/QueueTest.java diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/StackTest.java b/group20/755659358/week01/test/liuxincourse/StackTest.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/test/liuxincourse/StackTest.java rename to group20/755659358/week01/test/liuxincourse/StackTest.java diff --git a/group20/755659358/liuxincourse_datastructure/test/liuxincourse/SuiteTest.java b/group20/755659358/week01/test/liuxincourse/SuiteTest.java similarity index 100% rename from group20/755659358/liuxincourse_datastructure/test/liuxincourse/SuiteTest.java rename to group20/755659358/week01/test/liuxincourse/SuiteTest.java diff --git a/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtil.java b/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtil.java new file mode 100644 index 0000000000..e4985900cb --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtil.java @@ -0,0 +1,219 @@ +package com.coderising.practice.array; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +public class ArrayUtil { + + /** + * 缁欏畾涓涓暣褰㈡暟缁刟 , 瀵硅鏁扮粍鐨勫艰繘琛岀疆鎹 渚嬪锛 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 j = origin.length - 1; + for (int i = 0; i <= j; i++, j--) { + int temp = origin[i]; + origin[i] = origin[j]; + origin[j] = 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) { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < oldArray.length; i++) { + list.add(oldArray[i]); + } + for (Iterator iterator = list.iterator(); iterator.hasNext();) { + Integer integer = (Integer) iterator.next(); + if (integer.equals(0)) { + iterator.remove(); + } + } + + return integerListToArray(list); + } + + /** + * 缁欏畾涓や釜宸茬粡鎺掑簭濂界殑鏁村舰鏁扮粍锛 a1鍜宎2 , 鍒涘缓涓涓柊鐨勬暟缁刟3, 浣垮緱a3 鍖呭惈a1鍜宎2 鐨勬墍鏈夊厓绱狅紝 骞朵笖浠嶇劧鏄湁搴忕殑 渚嬪 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) { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < array1.length; i++) { + if (list.contains(Integer.valueOf(array1[i]))) { + continue; + } + list.add(Integer.valueOf(array1[i])); + } + for (int i = 0; i < array2.length; i++) { + if (list.contains(Integer.valueOf(array2[i]))) { + continue; + } + list.add(Integer.valueOf(array2[i])); + } + Collections.sort(list); + return integerListToArray(list); + } + + /** + * 鎶婁竴涓凡缁忓瓨婊℃暟鎹殑鏁扮粍 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) { + + return Arrays.copyOf(oldArray, oldArray.length + size); + } + + /** + * 鏂愭尝閭e鏁板垪涓猴細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 < 2) { + return new int[0]; + } + ArrayList list = new ArrayList<>(); + list.add(1); + list.add(1); + int nextFibo = list.get(list.size() - 2) + list.get(list.size() - 1); + while (nextFibo < max) { + list.add(nextFibo); + nextFibo = list.get(list.size() - 2) + list.get(list.size() - 1); + } + + return integerListToArray(list); + } + + /** + * 杩斿洖灏忎簬缁欏畾鏈澶у糾ax鐨勬墍鏈夌礌鏁版暟缁 渚嬪max = 23, 杩斿洖鐨勬暟缁勪负[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + if (max<2) { + return new int[]{}; + } + ArrayList list = new ArrayList<>(); + + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + list.add(i); + } + } + return integerListToArray(list); + } + + public int[] integerListToArray(List list){ + int len = list.size(); + int[] result = new int[len]; + for (int i = 0; i < len; i++) { + result[i] = list.get(i); + } + return result; + } + + public boolean isPrime(int a) { + boolean flag = true; + for (int i = 2; i <= Math.sqrt(a); i++) { + if (a % i == 0) { + flag = false; + break; + } + } + return flag; + } + + /** + * 鎵璋撯滃畬鏁扳濓紝 鏄寚杩欎釜鏁版伆濂界瓑浜庡畠鐨勫洜瀛愪箣鍜岋紝渚嬪6=1+2+3 缁欏畾涓涓渶澶у糾ax锛 杩斿洖涓涓暟缁勶紝 鏁扮粍涓槸灏忎簬max 鐨勬墍鏈夊畬鏁 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + ArrayList list=new ArrayList<>(); + for (int i = 2; i < max; i++) { + int [] splits=numSplit(i); + if (sumArray(splits)==i) { + list.add(i); + } + } + + return integerListToArray(list); + } + + public int sumArray(int[] arr){ + int sum=0; + for (int i = 0; i < arr.length; i++) { + sum+=arr[i]; + } + return sum; + } + + public int[] numSplit(int x){ + if (x<=1) { + return new int[]{}; + } + if (x==2) { + return new int[]{1,2}; + } + int k=1; + ArrayList list=new ArrayList<>(); + while (k<=x&&(x/k>=2)) { + if (x%k==0) { + list.add(k); + } + k++; + } + + + return integerListToArray(list); + } + + + /** + * 鐢╯eperator 鎶婃暟缁 array缁欒繛鎺ヨ捣鏉 渚嬪array= [3,8,9], seperator = "-" 鍒欒繑鍥炲间负"3-8-9" + * + * @param array + * @param s + * @return + */ + public String join(int[] array, String seperator) { + StringBuilder sb=new StringBuilder(); + for (int i = 0; i < array.length; i++) { + sb.append(array[i]); + if (i!=array.length-1) { + sb.append(seperator); + } + } + return sb.toString(); + } + +} diff --git a/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtisTest.java b/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtisTest.java new file mode 100644 index 0000000000..2984b2719c --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/array/ArrayUtisTest.java @@ -0,0 +1,73 @@ +package com.coderising.practice.array; + +import static org.junit.Assert.*; + +import java.util.ArrayList; + +import org.junit.Before; +import org.junit.Test; + +import com.coderising.practice.array.ArrayUtil; + +public class ArrayUtisTest { + + private ArrayUtil util; + + @Before + public void init(){ + util=new ArrayUtil(); + } + + + @Test + public void testReverse(){ + int [] origin={1,2,3,4,6}; + ArrayList list=new ArrayList<>(); + list.add(1); + list.add(2); + util.reverseArray(origin); + assertArrayEquals(new int[]{6,4,3,2,1}, origin); + } + + @Test + public void testRomoveZero(){ + int [] origin={1,2,3,0,4,0,6}; + assertArrayEquals(new int[]{1,2,3,4,6}, util.removeZero(origin)); + } + + @Test + public void testMerge(){ + int [] a1={3,0,4,6}; + int [] a2={3,6,8,10}; + assertArrayEquals(new int[]{0,3,4,6,8,10}, util.merge(a1,a2)); + } + + @Test + public void testGrow(){ + int [] a1={3,0,4,6}; + assertArrayEquals(new int[]{3,0,4,6,0,0}, util.grow(a1,2)); + } + + @Test + public void testFibo(){ + assertArrayEquals(new int[]{1,1,2,3}, util.fibonacci(4)); + } + + @Test + public void testGetPrime(){ + assertArrayEquals(new int[]{2,3,5,7,11}, util.getPrimes(13)); + } + + @Test + public void testGetPerfectNum(){ + + assertArrayEquals(new int[]{6,28,496}, util.getPerfectNumbers(1000)); + } + + @Test + public void testJoin(){ + + assertEquals("3-5-8", util.join(new int[]{3,5,8}, "-")); + } + +} diff --git a/group20/755659358/week02/src/com/coderising/practice/litestruts/LoginAction.java b/group20/755659358/week02/src/com/coderising/practice/litestruts/LoginAction.java new file mode 100644 index 0000000000..71f5c939f2 --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/litestruts/LoginAction.java @@ -0,0 +1,39 @@ +package com.coderising.practice.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/group20/755659358/week02/src/com/coderising/practice/litestruts/Struts.java b/group20/755659358/week02/src/com/coderising/practice/litestruts/Struts.java new file mode 100644 index 0000000000..069faad8bb --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/litestruts/Struts.java @@ -0,0 +1,140 @@ +package com.coderising.practice.litestruts; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.xml.sax.SAXException; + + + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 璇诲彇閰嶇疆鏂囦欢struts.xml + + 1. 鏍规嵁actionName鎵惧埌鐩稿搴旂殑class 锛 渚嬪LoginAction, 閫氳繃鍙嶅皠瀹炰緥鍖栵紙鍒涘缓瀵硅薄锛 + 鎹畃arameters涓殑鏁版嵁锛岃皟鐢ㄥ璞$殑setter鏂规硶锛 渚嬪parameters涓殑鏁版嵁鏄 + ("name"="test" , "password"="1234") , + 閭e氨搴旇璋冪敤 setName鍜宻etPassword鏂规硶 + + 2. 閫氳繃鍙嶅皠璋冪敤瀵硅薄鐨別xectue 鏂规硶锛 骞惰幏寰楄繑鍥炲硷紝渚嬪"success" + + 3. 閫氳繃鍙嶅皠鎵惧埌瀵硅薄鐨勬墍鏈塯etter鏂规硶锛堜緥濡 getMessage锛, + 閫氳繃鍙嶅皠鏉ヨ皟鐢紝 鎶婂煎拰灞炴у舰鎴愪竴涓狧ashMap , 渚嬪 {"message": "鐧诲綍鎴愬姛"} , + 鏀惧埌View瀵硅薄鐨刾arameters + + 4. 鏍规嵁struts.xml涓殑 閰嶇疆,浠ュ強execute鐨勮繑鍥炲硷紝 纭畾鍝竴涓猨sp锛 + 鏀惧埌View瀵硅薄鐨刯sp瀛楁涓 + + */ + + + + View view=new View(); + Map viewMap=new HashMap(); + Map xmlMap=parseXML(actionName); + + try { + + Class clazz= Class.forName(xmlMap.get("className")); + + Object object=clazz.newInstance(); + if (object instanceof LoginAction) { + LoginAction action =(LoginAction) object; + action.setName(parameters.get("name")); + action.setPassword(parameters.get("password")); + } + Method execute = clazz.getMethod("execute"); + String executResult=(String) execute.invoke(object); + Field[] fields=clazz.getDeclaredFields(); + Method[] methods=clazz.getDeclaredMethods(); + for (Method method : methods) { + if (method.getName().contains("get")) { + String resultString=(String) method.invoke(object); + for (Field field : fields) { + field.setAccessible(true); + if (field.get(object).equals(resultString)) { + viewMap.put(field.getName(), resultString); + } + } + } + } + + view.setJsp(xmlMap.get(executResult)); + + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + view.setParameters(viewMap); + + + return view; + } + + public static Map parseXML(String actionName){ + SAXParserFactory factory=SAXParserFactory.newInstance(); + StrutsHandler hander=new StrutsHandler(actionName); + SAXParser parser; + try { + parser = factory.newSAXParser(); + parser.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/coderising/practice/litestruts/struts.xml"),hander); + } catch (ParserConfigurationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SAXException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return hander.getResult(); + } + + public static void setPara(Map map){ + + + } + + public static String executAction(String className){ + + return null; + } + + + + + +} diff --git a/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsHandler.java b/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsHandler.java new file mode 100644 index 0000000000..2b3d3ab88b --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsHandler.java @@ -0,0 +1,57 @@ +package com.coderising.practice.litestruts; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +public class StrutsHandler extends DefaultHandler { + + private String actionName; + private String key; + private String value; + + private String currentActionName; + + private Map result = new HashMap(); + + public StrutsHandler(String actionName) { + this.actionName = actionName; + } + + public Map getResult() { + return result; + } + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + if ("action".equals(qName)) { + currentActionName = attributes.getValue(attributes.getIndex("name")); + if (currentActionName.equals(actionName)) { + value = attributes.getValue(attributes.getIndex("class")); + result.put("className", value); + } + + } + if ("result".equals(qName) && actionName.equals(currentActionName)) { + key = attributes.getValue("name"); + } + + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { + if ("result".equals(qName) && actionName.equals(currentActionName)) { + result.put(key, value); + } + } + + public void characters(char[] ch, int start, int length) throws SAXException { + if (actionName.equals(currentActionName)) { + value = new String(ch, start, length); + } + }; +} diff --git a/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsTest.java b/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsTest.java new file mode 100644 index 0000000000..cc57e4c6a8 --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/litestruts/StrutsTest.java @@ -0,0 +1,53 @@ +package com.coderising.practice.litestruts; + +import java.io.File; +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")); + } + + @Test + public void testParse() { + String actionName = "login"; + + System.out.println(Struts.parseXML(actionName)); + } + + +} diff --git a/group20/755659358/week02/src/com/coderising/practice/litestruts/View.java b/group20/755659358/week02/src/com/coderising/practice/litestruts/View.java new file mode 100644 index 0000000000..66643290d4 --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/litestruts/View.java @@ -0,0 +1,23 @@ +package com.coderising.practice.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/group20/755659358/week02/src/com/coderising/practice/litestruts/struts.xml b/group20/755659358/week02/src/com/coderising/practice/litestruts/struts.xml new file mode 100644 index 0000000000..7b2a62ef58 --- /dev/null +++ b/group20/755659358/week02/src/com/coderising/practice/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/group20/872045674/20170226/src/com/coding/basic/ArrayList.java b/group20/872045674/20170226/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..8b495831fe --- /dev/null +++ b/group20/872045674/20170226/src/com/coding/basic/ArrayList.java @@ -0,0 +1,87 @@ +package com.coding.basic; + +import java.util.Arrays; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[7]; + + public void add(Object o){ + if(size>elementData.length-1){ + ensureCapacity(size); + } + elementData[size++] = o; + } + + public void add(int index, Object o){ + System.out.println(elementData.length+" length"); + System.out.println(size+" size"); + size++; + if(index<0||index>size||index>Integer.MAX_VALUE){ + System.out.println("add 浣嶇疆杈撳叆閿欒锛岃杈撳叆鍚堢悊鐨勪綅缃"); + return; + } + if(size>elementData.length-1){ + ensureCapacity(size); + } + System.arraycopy(elementData,index,elementData,index+1,size-index-1); + elementData[index] = o; + } + + public Object get(int index){ + if(index<0||index>size-1){ + System.out.println("get 浣嶇疆杈撳叆閿欒锛岃杈撳叆鍚堢悊鐨勪綅缃"); + return null; + } + + return elementData[index]; + } + + public Object remove(int index){ + if(index<0||index>size-1){ + System.out.println("remove 浣嶇疆杈撳叆閿欒锛岃杈撳叆鍚堢悊鐨勪綅缃"); + return false; + } + System.arraycopy(elementData,index+1,elementData,index,size-index-1); + elementData[--size]=null; + return true; + } + + public int size(){ + return size; + } + + private void ensureCapacity(int nimCapacity){ + int oldCapacity = elementData.length; + int newCapacity = oldCapacity+(oldCapacity/2+1); + if(newCapacity < nimCapacity){ + newCapacity = nimCapacity; + } + if(newCapacity>Integer.MAX_VALUE){ + newCapacity = Integer.MAX_VALUE; + } + elementData = Arrays.copyOf(elementData,newCapacity); + } + + public static void main(String[] args) { + ArrayList list=new ArrayList(); + + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(2,10); + list.remove(3); + for(int i=0;i> { private T data; diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtil.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtil.java" new file mode 100644 index 0000000000..42bda4b210 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtil.java" @@ -0,0 +1,213 @@ +package org.Ralf.ArrayUtil; + +import java.util.ArrayList; +import java.util.Arrays; + +import javax.naming.spi.DirStateFactory.Result; + +public class ArrayUtil { + + public static void reverseArray(int[] origin) { + /** + * 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 + * a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * + * @param origin + * @return + * + */ + + for (int i = 0; i < origin.length / 2; i++) { + int 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 int[] + */ + public static int[] removeZero(int[] oldArr) { + + if (oldArr == null) { + return null; + } + int[] newArr = new int[oldArr.length]; + int size = 0; + + for (int i = 0; i < oldArr.length; i++) { + if (oldArr[i] != 0) { + newArr[size] = oldArr[i]; + size++; + } + } + return Arrays.copyOf(newArr, size); + + } + + /** + * 给定两个已经排序好的整形数组, 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 static int[] merge(int[] array1, int[] array2) { + + // method + ArrayList arrayList = new ArrayList<>(); + for (int i = 0; i < array1.length; i++) { + if (!arrayList.contains(array1[i])) { + arrayList.add(array1[i]); + } + } + for (int i = 0; i < array2.length; i++) { + if (!arrayList.contains(array2[i])) {// 可用list的index找到索引,根据索引判断是否包含该元素 + arrayList.add(array2[i]); + } + } + int[] newArr = new int[arrayList.size()]; + // arrayList.toArray(newArr); + for (int i = 0; i < arrayList.size(); i++) { + newArr[i] = arrayList.get(i); + } + Arrays.sort(newArr);// 可用冒泡排序,插入排序,快速排序法等实现 + return newArr; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * + * @param oldArray + * @param size + * @return + */ + public static 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 static int[] fibonacci(int max) { + int[] newArray = {}; + if (max == 1) { + return newArray; + } + newArray = new int[2 * max]; + int size = 0; + int a = 1; + int b = 1; + newArray[size++] = a; + newArray[size++] = b; + while (b <= max) { + int temp = b; + b = a + b; + newArray[size++] = b; + a = temp; + } + + return Arrays.copyOf(newArray, size - 1); + } + + /** + * 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public static int[] getPrimes(int max) { + if (max < 2) { + return null; + } + int[] aar = new int[max]; + int size = 0; + for (int i = 2; i < max; i++) { + if (isPrime(i)) { + aar[size++] = i; + } + } + return Arrays.copyOf(aar, size); + } + + private static boolean isPrime(int aar) { + boolean flag = true; + for (int i = 2; i <= Math.sqrt(aar); i++) { + if (aar % i == 0) { + flag = false; + break; + } + } + return flag; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max) { + if (max < 1) { + return null; + } + int[] arr = new int[max]; + int size = 0; + for (int i = 1; i <= max; i++) { + if (isPerfectNumber(i)) { + arr[size++] = i; + } + } + return Arrays.copyOf(arr, size); + } + + private static boolean isPerfectNumber(int num) { + int sum = 0; + for (int i = 1; i < num; i++) { + if (num % i == 0) { + sum += i; + } + + } + if (sum == num) { + return true; + } else + return false; + } + + /** + * 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9" + * + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator) { + if (array.length < 1) { + return null; + } + StringBuilder string = new StringBuilder(); + for (int i = 0; i < array.length - 1; i++) { + string.append(array[i]).append(seperator); + } + string.append(array[array.length - 1]); + return string.toString(); + } +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtilTest.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtilTest.java" new file mode 100644 index 0000000000..48f12a3337 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/PracticeOfData/ArrayUtilTest.java" @@ -0,0 +1,100 @@ +package org.Ralf.ArrayUtilTest; + +import static org.junit.Assert.*; + +import org.Ralf.ArrayUtil.ArrayUtil; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ArrayUtilTest { + + + @Before + public void setUp() throws Exception { + } + + @Test + public void reverseArray() { + int[] origin = {9,8,7,6,5,4,3,2,1}; + int[] originCopy = origin; + int[] reverse = {1,2,3,4,5,6,7,8,9}; + ArrayUtil.reverseArray(origin); + Assert.assertArrayEquals(origin, reverse); + ArrayUtil.reverseArray(origin); + Assert.assertArrayEquals(origin, originCopy); + } + + @Test + public void removeZero(){ + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] newarr = ArrayUtil.removeZero(oldArr); + int[] realArr = {1,3,4,5,6,6,5,4,7,6,7,5}; + Assert.assertArrayEquals(newarr, realArr); + } + + @Test + public void merge(){ + + int[] a1 ={3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + int[] newarr = ArrayUtil.merge(a1, a2); + int[] realArr = {3,4,5,6,7,8}; + Assert.assertArrayEquals(newarr, realArr); + } + + @Test + public void grow(){ + int[] oldArray = {2,3,6}; + int[] realArr = {2,3,6,0,0,0}; + int[] newArray = ArrayUtil.grow(oldArray, 3); + Assert.assertArrayEquals(newArray, realArr); + } + + @Test + public void fibonacci(){ + + int[] realArr = {1,1,2,3,5,8,13}; + int[] newArray = ArrayUtil.fibonacci(15); + Assert.assertArrayEquals(newArray, realArr); + } + @Test + public void getPrimes(){ + int[] realArr = {2,3,5,7,11,13,17,19}; + int[] newArray = ArrayUtil.getPrimes(23); + Assert.assertArrayEquals(newArray, realArr); + } + + @Test + public void getPerfectNumbers(){ + int[] realArr = {6, 28, 496, 8128}; + int[] newArray = ArrayUtil.getPerfectNumbers(10000); + Assert.assertArrayEquals(newArray, realArr); + } + + @Test + public void join(){ + int[] realArr = {6, 28, 496, 8128}; + int[] newArray = ArrayUtil.getPerfectNumbers(10000); + Assert.assertArrayEquals(newArray, realArr); + } + + + + + + + + + + + + + + + + + + +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/LoginAction.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/LoginAction.java" new file mode 100644 index 0000000000..40b5de161a --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/LoginAction.java" @@ -0,0 +1,34 @@ +package com.coderising.litestruts; + +public class LoginAction { + + private String name; + private String passWord; + private String message; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getPassWord() { + return passWord; + } + public void setPassWord(String passWord) { + this.passWord = passWord; + } + public String getMessage() { + return message; + } + public void setMessage(String message) { + this.message = message; + } + 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"; + } +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/ReadXml.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/ReadXml.java" new file mode 100644 index 0000000000..e971e779b6 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/ReadXml.java" @@ -0,0 +1,71 @@ +package com.coderising.litestruts; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class ReadXml { + + private Document document = null; + private HashMap hashMap; + + public ReadXml(String filename) { + try { + document = new SAXReader().read((filename)); + hashMap = new HashMap(); + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public String parseXml(String actionName) { + + // List actions = document.selectNodes("//struts/action"); + String className = null; + Element root = document.getRootElement(); + List actions = root.elements("action"); + if (actions.isEmpty()) { + return null; + } + for (Iterator iter = actions.iterator(); iter.hasNext();) { + Element element = (Element) iter.next(); + Attribute attr1 = element.attribute("name"); + if (attr1.getValue().equals(actionName)) { + Attribute attr2 = element.attribute("class"); + className = attr2.getValue(); + //获取子元素的的属性值 + for (Iterator iterator = element.elementIterator(); iterator + .hasNext();) { + Element childElement = (Element) iterator.next(); + Attribute childAttribute = childElement.attribute("name"); + hashMap.put(childAttribute.getValue(), + childElement.getText()); + } + } + + } + return className; + } + + public String getJsp(String result) { + if (result == null) { + return null; + } + String string_jsp = null; + if (!hashMap.isEmpty()) { + for (String string : hashMap.keySet()) { + if (result.equals(string)) { + string_jsp = hashMap.get(string); + } + } + } + return string_jsp; + } +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/Struts.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/Struts.java" new file mode 100644 index 0000000000..2892617845 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/Struts.java" @@ -0,0 +1,93 @@ +package com.coderising.litestruts; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +public class Struts { + + private static final String NAME = "name"; + private static final String PASSWORD = "password"; + private static String excuteString; + private static Object object = null;// 返回反射的实例 + private static Class actionClass = null;// 反射获取的类 + + @SuppressWarnings("unchecked") + public static View runAction(String actionName, + Map parameters) { + // 读取配置文件struts.xml + View view = new View(); + ReadXml readXml = new ReadXml("E:\\struts.xml"); + String classNameString = readXml.parseXml(actionName);//读取xml + object = initAction(classNameString);//通过反射初始化类 + + excuteMethod(parameters);//执行setter和excute方法 + + view.setParameterMap(setMapParameter());//获取所有的getter方法,执行后将方法名和结果保存到view中 + String jspResult = readXml.getJsp(excuteString);//获取jsp类型 + view.setJsp(jspResult); + + return view; + } + + public static Object initAction(String classNameString) { + System.out.println(classNameString); + try { + actionClass = Class.forName(classNameString); + } catch (ClassNotFoundException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + Object newObject = null; + try { + newObject = actionClass.newInstance(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return newObject; + } + + public static void excuteMethod(Map parameters) { + + try { + Method methodOfName = actionClass + .getMethod("setName", String.class); + methodOfName.invoke(object, parameters.get(NAME)); + // + Method methodOfPassword = actionClass.getMethod("setPassWord", + String.class); + methodOfPassword.invoke(object, parameters.get(PASSWORD)); + + Method excuteMethod = actionClass.getMethod("execute"); + excuteString = (String) excuteMethod.invoke(object); + + } catch (Exception e) { + // TODO: handle exception + } + } + + public static Map setMapParameter() { + + Method[] getterMethods = actionClass.getMethods(); + HashMap hashMap = new HashMap<>(); + + for (int i = 0; i < getterMethods.length; i++) { + String getterName = getterMethods[i].getName(); + if (getterName.startsWith("get")) { + try { + String value = (String) getterMethods[i].invoke(object); + hashMap.put(getterName.substring(3).toLowerCase(), value); + //System.out.println("----" + getterName.substring(2)); + } catch (Exception e) { + // TODO: handle exception + } + + } + } + return hashMap; + } +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/StrutsTest.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/StrutsTest.java" new file mode 100644 index 0000000000..b7f0884f41 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/StrutsTest.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 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/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/View.java" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/View.java" new file mode 100644 index 0000000000..bda8419e5f --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/Struts/View.java" @@ -0,0 +1,28 @@ +package com.coderising.litestruts; + +import java.util.Map; + +public class View { + + private String jsp; + private Map parameter; + + public String getJsp() { + return jsp; + } + + public View setJsp(String jsp) { + this.jsp = jsp; + return this; + } + + public Map getParameters() { + return parameter; + } + + public View setParameterMap(Map parameter) { + this.parameter = parameter; + return this; + } + +} diff --git "a/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\226\207\347\253\240.txt" "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\226\207\347\253\240.txt" new file mode 100644 index 0000000000..d4f3154c38 --- /dev/null +++ "b/group20/925290009/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\226\207\347\253\240.txt" @@ -0,0 +1 @@ +http://blog.csdn.net/u011371324/article/details/60329949 \ No newline at end of file diff --git a/group22/group22.md b/group22/group22.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group22/group22.md @@ -0,0 +1 @@ + diff --git a/group23/group23.md b/group23/group23.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group23/group23.md @@ -0,0 +1 @@ + diff --git a/group24/group24.md b/group24/group24.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group24/group24.md @@ -0,0 +1 @@ + diff --git a/group25/group25.md b/group25/group25.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group25/group25.md @@ -0,0 +1 @@ + diff --git a/group26/group26.md b/group26/group26.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group26/group26.md @@ -0,0 +1 @@ + diff --git a/group27/group27.md b/group27/group27.md new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/group27/group27.md @@ -0,0 +1 @@ +