diff --git a/group24/com/github/CJ-chen/coding2017/basic/ArrayList.java b/group24/120509419/ArrayList.java similarity index 100% rename from group24/com/github/CJ-chen/coding2017/basic/ArrayList.java rename to group24/120509419/ArrayList.java diff --git a/group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java b/group24/120509419/BinaryTreeNode.java similarity index 100% rename from group24/com/github/CJ-chen/coding2017/basic/BinaryTreeNode.java rename to group24/120509419/BinaryTreeNode.java diff --git a/group24/com/github/CJ-chen/coding2017/basic/Iterator.java b/group24/120509419/Iterator.java similarity index 100% rename from group24/com/github/CJ-chen/coding2017/basic/Iterator.java rename to group24/120509419/Iterator.java diff --git a/group24/120509419/JUnitTest/ArrayUtilTest.java b/group24/120509419/JUnitTest/ArrayUtilTest.java new file mode 100644 index 0000000000..bef91785bc --- /dev/null +++ b/group24/120509419/JUnitTest/ArrayUtilTest.java @@ -0,0 +1,164 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author CJ + */ +public class ArrayUtilTest { + + public ArrayUtilTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of reverseArray method, of class ArrayUtil. + */ + @Test + public void testReverseArray() { + System.out.println("reverseArray"); + int[] origin = new int[]{1,2,3,4,5}; + int[] expecteds = new int[]{5,4,3,2,1}; + ArrayUtil instance = new ArrayUtil(); + instance.reverseArray(origin); + Assert.assertArrayEquals(expecteds, origin); + // TODO review the generated test code and remove the default call to fail. + //fail("The test case is a prototype."); + } + + /** + * Test of removeZero method, of class ArrayUtil. + */ + @Test + public void testRemoveZero() { + System.out.println("removeZero"); + int[] oldArray = new int[]{1,2,3,4,5,6,0,0,0,0}; + ArrayUtil instance = new ArrayUtil(); + int[] expResult = new int[]{1,2,3,4,5,6}; + int[] result = instance.removeZero(oldArray); + assertArrayEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + // fail("The test case is a prototype."); + } + + /** + * Test of merge method, of class ArrayUtil. + */ + @Test + public void testMerge() { + System.out.println("merge"); + int[] array1 = new int[]{3, 5, 7,8}; + int[] array2 = new int[]{4, 5, 6,7}; + ArrayUtil instance = new ArrayUtil(); + int[] expResult = new int[]{3,4,5,6,7,8}; + int[] result = instance.merge(array1, array2); + assertArrayEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + // fail("The test case is a prototype."); + } + + /** + * Test of grow method, of class ArrayUtil. + */ + @Test + public void testGrow() { + System.out.println("grow"); + int[] oldArray = new int[]{1,2,3,4,5}; + int size = 5; + ArrayUtil instance = new ArrayUtil(); + int[] expResult = new int[]{1,2,3,4,5,0,0,0,0,0}; + int[] result = instance.grow(oldArray, size); + assertArrayEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + // fail("The test case is a prototype."); + } + + /** + * Test of fibonacci method, of class ArrayUtil. + */ + @Test + public void testFibonacci() { + System.out.println("fibonacci"); + int max = 15; + ArrayUtil instance = new ArrayUtil(); + int[] expResult = new int[]{1,1,2,3,5,8,13}; + int[] result = instance.fibonacci(max); + assertArrayEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + // fail("The test case is a prototype."); + } + + /** + * Test of getPrimes method, of class ArrayUtil. + */ + @Test + public void testGetPrimes() { + System.out.println("getPrimes"); + int max = 23; + ArrayUtil instance = new ArrayUtil(); + int[] expResult = new int[]{2,3,5,7,11,13,17,19}; + int[] result = instance.getPrimes(max); + assertArrayEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + // fail("The test case is a prototype."); + } + + /** + * Test of getPerfectNumbers method, of class ArrayUtil. + */ + @Test + public void testGetPerfectNumbers() { + System.out.println("getPerfectNumbers"); + int max = 10; + ArrayUtil instance = new ArrayUtil(); + int[] expResult = new int[]{6}; + int[] result = instance.getPerfectNumbers(max); + assertArrayEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + // fail("The test case is a prototype."); + } + + /** + * Test of join method, of class ArrayUtil. + */ + @Test + public void testJoin() { + System.out.println("join"); + int[] array = new int[]{1,2,3,4,5}; + String seperator = ""; + ArrayUtil instance = new ArrayUtil(); + String expResult = "1-2-3-4-5"; + String result = instance.join(array, seperator); + assertEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + // fail("The test case is a prototype."); + } + +} diff --git a/group24/120509419/JUnitTest/struts/StrutsTest.java b/group24/120509419/JUnitTest/struts/StrutsTest.java new file mode 100644 index 0000000000..ebeafa858f --- /dev/null +++ b/group24/120509419/JUnitTest/struts/StrutsTest.java @@ -0,0 +1,46 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass.struts; + +import java.io.FileNotFoundException; +import java.io.IOException; +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 IOException, FileNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, 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 IOException, FileNotFoundException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, 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/group24/com/github/CJ-chen/coding2017/basic/LinkedList.java b/group24/120509419/LinkedList.java similarity index 100% rename from group24/com/github/CJ-chen/coding2017/basic/LinkedList.java rename to group24/120509419/LinkedList.java diff --git a/group24/com/github/CJ-chen/coding2017/basic/List.java b/group24/120509419/List.java similarity index 100% rename from group24/com/github/CJ-chen/coding2017/basic/List.java rename to group24/120509419/List.java diff --git a/group24/com/github/CJ-chen/coding2017/basic/Queue.java b/group24/120509419/Queue.java similarity index 100% rename from group24/com/github/CJ-chen/coding2017/basic/Queue.java rename to group24/120509419/Queue.java diff --git a/group24/com/github/CJ-chen/coding2017/basic/Stack.java b/group24/120509419/Stack.java similarity index 100% rename from group24/com/github/CJ-chen/coding2017/basic/Stack.java rename to group24/120509419/Stack.java diff --git a/group24/120509419/javaclass/ArrayUtil.java b/group24/120509419/javaclass/ArrayUtil.java new file mode 100644 index 0000000000..fc460454be --- /dev/null +++ b/group24/120509419/javaclass/ArrayUtil.java @@ -0,0 +1,237 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass; + +import java.util.Arrays; + +/** + * + * @author CJ + */ +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 totalLen = origin.length; + int[] revIntArr = new int[totalLen]; + for (int i = 0; i < totalLen; i++) { + revIntArr[i] = origin[totalLen - 1 - i]; + } + for (int i = 0; i < totalLen; i++) { + origin[i] = revIntArr[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 totalLen = oldArray.length; + + int[] tmpArr = new int[totalLen]; + int nonZeroCount = 0; + for (int i : oldArray) { + if (i != 0) { + tmpArr[nonZeroCount++] = i; + } + } + return Arrays.copyOfRange(tmpArr, 0, nonZeroCount); + } + + /** + * 给定两个已经排序好的整形数组, 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 arrLen1 = array1.length; + int arrLen2 = array2.length; + int maxLen = arrLen1+arrLen2; + int[] mergedArr = new int[maxLen]; + int mergedC = 0; + int i = 0; + int j = 0; + while(i 1; + } + for(int i=2;i<=Math.sqrt(n);i++){ + if(n%i == 0) + return false; + } + return true; + } + + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int[] perfectArr = new int[max]; + // int growSize = 100; + int Count = 0; + for(int i=2;iinNum){ + return false; + } + } + } + return sum == inNum; + } + + + + /** + * 用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(); + int loopMax = array.length-1; + for(int i=0;i parameters) throws FileNotFoundException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { + + /* + 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字段中。 + + */ + String strutsFilePath = getProRealPath(Struts.class) + File.separator + "struts.xml"; + System.out.println("Reading File: " + strutsFilePath); + + BufferedReader br = new BufferedReader(new FileReader(strutsFilePath)); + String inline; + String curActionName = null; + String className = null; + HashMap status2action = new HashMap(); + while ((inline = br.readLine()) != null) { + if (inline.contains("(.*?).*$", "$1\t$2").split("\t"); + status2action.put(info[0], info[1]); + } else if (inline.contains("")) { + if (actionName.equals(curActionName)) { + Class c = Class.forName(className); + Object curInstance = c.newInstance(); + + Method setNameMethod = c.getMethod("setName", String.class); + setNameMethod.invoke(curInstance, parameters.get("name")); + Method setPasswordMethod = c.getMethod("setPassword", String.class); + setPasswordMethod.invoke(curInstance, parameters.get("password")); + + Method excuteMethod = c.getMethod("execute"); + // 返回 sucess 或者 fail + String status =(String) excuteMethod.invoke(curInstance); + + String curJsp = status2action.get(status); + + Method getMessageMethod = c.getMethod("getMessage"); + + View curView = new View(); + curView.setJsp(curJsp); + Map curMap = new HashMap(); + curMap.put("message",(String) getMessageMethod.invoke(curInstance)); + curView.setParameters(curMap); + return curView; + + } + } + } + System.err.println("No Match Aciton"); + return null; + } + + public static String getProRealPath(Class inClass) { + URL url = inClass.getProtectionDomain().getCodeSource().getLocation(); + + String filePath = null; + try { + filePath = URLDecoder.decode(url.getPath(), "utf-8"); + } catch (Exception e) { + e.printStackTrace(); + } + if (filePath.endsWith(".jar")) { + filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1); + } + File file = new File(filePath); + filePath = file.getAbsolutePath(); + return filePath; + } + +} diff --git a/group24/120509419/javaclass/struts/View.java b/group24/120509419/javaclass/struts/View.java new file mode 100644 index 0000000000..700c376eaa --- /dev/null +++ b/group24/120509419/javaclass/struts/View.java @@ -0,0 +1,32 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javaclass.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/group24/120509419/javaclass/struts/struts.xml b/group24/120509419/javaclass/struts/struts.xml new file mode 100644 index 0000000000..a6cdbfc4a4 --- /dev/null +++ b/group24/120509419/javaclass/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