diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/ArrayList.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/ArrayList.java new file mode 100644 index 0000000000..8ae862da33 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/ArrayList.java @@ -0,0 +1,115 @@ +package basic.dataStructure; + +/** + * Created by macvi on 2017/4/2. + */ +public class ArrayList implements List { + private int size = 10; + //每次扩容的长度,默认为10 + private int extendSize = 10; + + private Object[] data = new Object[size]; + + public ArrayList(Object o) { + this.add(o); + } + + public ArrayList(){} + + public void add(Object o) { + if (this.size() == this.size) { + this.size += extendSize; + Object[] newData = new Object[this.size]; + System.arraycopy(this.data, 0, newData, 0, this.data.length); + this.data = newData; + } + + for (int i = 0; i < this.data.length; i++) { + if (data[i] == null) { + data[i] = o; + break; + } else continue; + } + } + + public void add(int index, Object o) { + if (index > this.size() || index < 0) { + throw new IndexOutOfBoundsException(); + } + + if(this.size() == this.size){ + this.size += extendSize; + } + + Object[] newData = new Object[this.size]; + + System.arraycopy(this.data, 0, newData, 0, index); + newData[index] = o; + System.arraycopy(this.data, index, newData, index + 1, this.size() - index); + + this.data = newData; + } + + public Object get(int index) { + if(index > this.size() || index < 0){ + throw new IndexOutOfBoundsException(); + } + for(int i = 0; i < this.size(); i ++){ + if(index == i){ + return this.data[i]; + } + } + + return null; + } + + public Object remove(int index) { + if(index > this.size() || index < 0){ + throw new IndexOutOfBoundsException(); + } + + Object[] newData = new Object[this.size]; + Object removed = this.get(index); + + System.arraycopy(this.data, 0, newData, 0, index); + System.arraycopy(this.data, index + 1, newData, index, this.size() - index); + this.data = newData; + return removed; + } + + public int size() { + int size = 0; + for(Object obj : this.data){ + if(obj != null){ + size += 1; + } + } + + return size; + } + + public boolean contains(Object obj){ + for(int i = 0; i < this.size(); i++){ + if(obj == this.get(i)){ + return true; + } + } + + return false; + } + + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + for(Object obj : data){ + if(obj != null){ + sb.append(obj.toString()).append(","); + }else { +// sb.append("null,"); + continue; + } + } + + return sb.toString(); + } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/ArrayUtil.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/ArrayUtil.java new file mode 100644 index 0000000000..22bccaaf5b --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/ArrayUtil.java @@ -0,0 +1,245 @@ +package basic.dataStructure; + +/** + * @author : 温友朝 + * @date : 2017/4/5 + */ +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 length = origin.length; + int[] reversed = new int[length]; + for (int i = length - 1; i >= 0; i--) { + reversed[length - i - 1] = 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 int[] removeZero(int[] oldArray) { + int length = oldArray.length; + int[] arr = new int[length]; + int index = 0; + for (int i = 0; i < length; i++) { + if (oldArray[i] != 0) { + arr[index] = oldArray[i]; + index++; + } + } + //非0的数据个数 + int[] newArr = new int[index]; + System.arraycopy(arr, 0, newArr, 0, index); + return newArr; + } + + public static Object[] remove(Object[] oldArray, Object value){ + int length = oldArray.length; + Object[] arr = new Object[length]; + int index = 0; + for (int i = 0; i < length; i++) { + if (oldArray[i] != value) { + arr[index] = oldArray[i]; + index++; + } + } + Object[] newArr = new Object[index]; + System.arraycopy(arr, 0, newArr, 0, index); + 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] , 注意: 已经消除了重复 + * + * @param array1 + * @param array2 + * @return + */ + + public int[] merge(int[] array1, int[] array2) { + int length1 = array1.length; + int length2 = array2.length; + int[] arr = new int[length1 + length2]; + + System.arraycopy(array1, 0, arr, 0, length1); + System.arraycopy(array2, 0, arr, length1, length2); + + //去重 + for(int i = 0; i < arr.length; i++){ + for(int j = 0; j < arr.length; j++){ + if(i != j && arr[i] == arr[j]){ + arr[j] = 0; + } + } + } + + + int[] data = removeZero(arr); + int length = data.length; + + //排序 + for (int i = 0; i < length; i++) { + for(int j = 0; j < length; j++){ + if(data[i] < data[j]){ + int tmp = data[i]; + data[i] = data[j]; + data[j] = tmp; + } + } + } + return data; + } + + + /** + * 把一个已经存满数据的数组 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[] arr = new int[oldArray.length + size]; + System.arraycopy(oldArray, 0, arr, 0, oldArray.length); + return arr; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * F(0)=0,F(1)=1, F(n)=F(n-1)+F(n-2)(n>=2,n∈N*) + * @param max + * @return + */ + public int[] fibonacci(int max) { + int[] empty = {}; + int[] arr2 = {1, 1}; + + switch (max){ + case 0 : return empty; + case 1 : return empty; + case 2 : return arr2; + default: { + int[] data = arr2; + int d = data[0] + data[1]; + while (d < max){ + int length = data.length; + d = data[length - 1] + data[length - 2]; + if(d > max){ + return data; + } + int[] temp = new int[data.length + 1]; + System.arraycopy(data, 0, temp, 0, length); + temp[length] = d; + + data = temp; + } + } + } + + return null; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * + * @param max + * @return + */ + public int[] getPrimes(int max) { + int[] data = new int[max]; + int index = 0; + for(int i = 1; i < max; i++){ + int divided = 0; + for(int j = i; j >= 1; j--){ + if(i % j == 0){ + divided++; + } + if(divided > 2){ + break; + }else if(j == 1 && divided == 2){ + data[index] = i; + index ++; + } + } + } + + int[] result = new int[index]; + System.arraycopy(data, 0, result, 0, index); + return result; + + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * + * @param max + * @return + */ + public int[] getPerfectNumbers(int max) { + int[] perfd = new int[max]; + int perfIndex = 0; + for(int i = 1; i <= max; i++){ + int index = 0; + int[] data = new int[i]; + for(int j = i - 1; j >= 1; j--){ + if(i % j == 0){ + data[index] = j; + index ++; + } + + if(j == 1 && getSum(data) == i){ + perfd[perfIndex] = i; + perfIndex++; + } + } + } + + return removeZero(perfd); + } + + private int getSum(int[] arr){ + int sum = 0; + for(int i : arr){ + sum += i; + } + return sum; + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * + * @param array + * @param seperator + * @return + */ + public String join(int[] array, String seperator) { + StringBuffer sb = new StringBuffer(); + for(int i : array){ + sb.append(i).append(seperator); + } + return sb.substring(0, sb.length() - 1).toString(); + } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/BinaryTreeNode.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/BinaryTreeNode.java new file mode 100644 index 0000000000..5050ae3c95 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/BinaryTreeNode.java @@ -0,0 +1,58 @@ +package basic.dataStructure; + +/** + * Created by macvi on 2017/4/4. + */ +public class BinaryTreeNode { + private int data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + private BinaryTreeNode(){} + + public BinaryTreeNode(int data){ + this.data = data; + this.left = null; + this.right = null; + } + + public void setData(int data){ + BinaryTreeNode node = new BinaryTreeNode(data); + if(compareTo(data)){ + if(this.left == null){ + this.left = node; + }else{ + this.left.setData(data); + } + }else{ + if(this.right == null){ + this.right = node; + }else{ + this.right.setData(data); + } + } + } + + public int getData(){ + return data; + } + + private boolean compareTo(int d) { + System.out.println("data=" + this.data + ", d=" + d); + return this.data > d; + } + + private StringBuffer dataStr = new StringBuffer(); + private int index = 0; +// public String toString(BinaryTreeNode node) { +// while (node.left != null || node.right != null){ +// dataStr.append(index + "层,数据=").append(node.data).append("|"); +// if(node.left != null){ +// dataStr.append(node.left.data) +// } +// index ++; +// } +// +// return dataStr.toString(); +// } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/LinkedList.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/LinkedList.java new file mode 100644 index 0000000000..3ac85ad37b --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/LinkedList.java @@ -0,0 +1,341 @@ +package basic.dataStructure; + +/** + * Created by macvi on 2017/4/3. + */ +public class LinkedList implements List { + private Node head; + + public LinkedList() { + this.head = new Node(); + } + + public void add(Object o) { + if (this.head.data == null) { + this.head = new Node(o, null); + } else { + Node temp = this.head; + while (temp.next != null) { + temp = temp.next; + } + temp.next = new Node(o, null); + } + } + + public void add(int index, Object o) { + if (index > this.size() || index < 0) { + throw new IndexOutOfBoundsException(); + } + + if(index == 0){ + Node newNode = new Node(o, this.head); + this.head = newNode; + return; + } + + if(index == this.size()){ + this.add(o); + return; + } + + Node before = getNode(index - 1); + Node next = getNode(index); + Node newNode = new Node(o, next); + before.next = newNode; + + } + + private Node getNode(int index) { + int i = 0; + Node temp = this.head; + while (temp.data != null) { + if (index == i) { + return temp; + } + + if (temp.next != null) { + temp = temp.next; + } else break; + + i++; + } + + return null; + } + + public Object get(int index) { + if (index > this.size() || index < 0) { + throw new IndexOutOfBoundsException(); + } + + return this.getNode(index).data; + } + + public Object remove(int index) { + if(index > this.size() || index < 0){ + throw new IndexOutOfBoundsException(); + } + + Object removed = get(index); + + Node before = getNode(index - 1); + Node next = getNode(index + 1); + before.next = next; + + return removed; + } + + public int size() { + int size = 0; + Node temp = this.head; + while (temp.data != null) { + size++; + if (temp.next != null) { + temp = temp.next; + } else break; + } + + return size; + } + + public void asList(Object[] array){ + LinkedList list = new LinkedList(); + for(int i = 0; i < array.length; i++){ + list.add(array[i]); + } + + this.head = list.head; + } + + public Object[] toArray(LinkedList list){ + int size = list.size(); + Object[] arr = new Object[size]; + for(int i = 0; i < size; i++){ + arr[i] = list.get(i); + } + + return arr; + } + + public String toString() { + StringBuffer sb = new StringBuffer(); + Node temp = this.head; + while (temp.data != null) { + sb.append(temp.data.toString()).append(","); + if (temp.next != null) { + temp = temp.next; + } else break; + } + + return sb.toString(); + } + + private static class Node { + Object data; + Node next; + + public Node() {} + + public Node(Object obj, Node next) { + this.data = obj; + this.next = next; + } + + } + + + /** + * 把该链表逆置 + * 例如链表为 3->7->10 , 逆置后变为 10->7->3 + */ + public void reverse(){ + int size = this.size(); + + if(size == 1){ + return; + } + + Object[] data = new Object[size]; + for(int i = 0; i < size; i++){ + data[i] = this.get(i); + } + + this.head = new Node(); + + for(int i = size - 1; i >= 0; i--){ + this.add(data[i]); + } + } + + /** + * 删除一个单链表的前半部分 + * 例如:list = 2->5->7->8 , 删除以后的值为 7->8 + * 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10 + + */ + public void removeFirstHalf(){ + int size = this.size(); + int index = this.size()/2; + ArrayList al = new ArrayList(); + for(int i = index; i < size; i++){ + al.add(this.get(i)); + } + + this.head = new Node(); + + for(int i = 0; i < al.size(); i++){ + this.add(al.get(i)); + } + } + + /** + * 从第i个元素开始, 删除length 个元素 , 注意i从0开始 + * @param i + * @param length + */ + public void remove(int i, int length){ + for(int j = i; j < i + length; j++){ + this.remove(i); + } + } + /** + * 假定当前链表和listB均包含已升序排列的整数 + * 从当前链表中取出那些listB所指定的元素 + * 例如当前链表 = 11->101->201->301->401->501->601->701 + * listB = 1->3->4->6 + * 返回的结果应该是[101,301,401,601] + * @param list + */ + public int[] getElements(LinkedList list){ + int size = list.size(); + int[] arr = new int[size]; + for(int i = 0; i < size; i++){ + int index = (Integer) list.get(i); + arr[i] = (Integer) this.get(index); + } + + return arr; + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 从当前链表中中删除在listB中出现的元素 + + * @param list + */ + + public void subtract(LinkedList list){ + Object[] arr1 = toArray(this); + Object[] arr2 = toArray(list); + for(int i = 0; i < arr2.length; i++){ + arr1 = ArrayUtil.remove(arr1, arr2[i]); + } + + asList(arr1); + } + + /** + * 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同) + */ + public void removeDuplicateValues(){ + int size = this.size(); + ArrayList indexList = new ArrayList(); + ArrayList valueList = new ArrayList(); + for(int i = 0; i < size; i ++){ + int valueI = (Integer)this.get(i); + int index = 0; + for(int j = i + 1; j < size; j++){ + if(valueList.contains(valueI)){ + continue; + } + int valueJ = (Integer) this.get(j); + if(valueJ == valueI){ + index++; + } + + if(index > 0){ + indexList.add(j); + valueList.add(valueJ); + } + } + } + + Object[] arr = new Object[size]; + for(int i = 0; i < size; i++){ + arr[i] = indexList.contains(i) ? false : this.get(i); + } + + ArrayUtil au = new ArrayUtil(); + arr = au.remove(arr, false); + + asList(arr); + } + + /** + * 已知链表中的元素以值递增有序排列,并以单链表作存储结构。 + * 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素) + * @param min + * @param max + */ + public void removeRange(int min, int max){ + int size = this.size(); + int[] range = new int[max - min]; + int index = 0; + for(int i = 0; i < size; i++){ + int value = (Integer) this.get(i); + if(value > min && value < max){ + range[index] = value; + index++; + } + } + + Object[] arr = new Object[size]; + for(int i = 0; i < size; i++){ + arr[i] = this.get(i); + } + + for(int i = 0; i < range.length; i++){ + arr = ArrayUtil.remove(arr, range[i]); + } + + asList(arr); + } + + /** + * 假设当前链表和参数list指定的链表均以元素依值递增有序排列(同一表中的元素值各不相同) + * 现要求生成新链表C,其元素为当前链表和list中元素的交集,且表C中的元素有依值递增有序排列 + * @param list + */ + public LinkedList intersection( LinkedList list){ + //组合成新的链表 + int listSize = list.size(); + for(int i = 0 ; i < listSize; i ++){ + this.add(list.get(i)); + } + + //转化成数组 + int size = this.size(); + int[] arr = new int[size]; + for(int i = 0; i < size; i++){ + arr[i] = (Integer)this.get(i); + } + //排序 + for(int i = 0; i < size - 1; i ++){ + for(int j = 0; j < size - i - 1; j ++){ + if(arr[j] >= arr[j + 1]){ + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + + //组装 + LinkedList li = new LinkedList(); + for(int i = 0; i < size; i ++){ + li.add(arr[i]); + } + return li; + } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/List.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/List.java new file mode 100644 index 0000000000..dc2a62aab3 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/List.java @@ -0,0 +1,12 @@ +package basic.dataStructure; + +/** + * Created by macvi on 2017/4/2. + */ +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/group24/75939388/learning2017/src/main/java/basic/dataStructure/Queue.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/Queue.java new file mode 100644 index 0000000000..36ca7e9647 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/Queue.java @@ -0,0 +1,72 @@ +package basic.dataStructure; + +/** + * Created by macvi on 2017/4/4. + */ +public class Queue { + private Object[] data; + + private int size = 10; + + private int extendedSize = 10; + + public Queue(){ + this.data = new Object[size]; + } + + public Queue(Object o){ + this.data = new Object[size]; + data[0] = o; + } + + public void enQueue(Object o){ + //被添加的位置 + int index = this.size(); + if(this.size() == this.size){ + this.size += extendedSize; + Object[] newData = new Object[this.size]; + System.arraycopy(this.data, 0, newData, 0, index); + newData[index] = o; + this.data = newData; + }else{ + this.data[index] = o; + } + } + + public Object deQueue(){ + Object[] newData = new Object[this.size]; + Object d = this.data[0]; + System.arraycopy(this.data, 1, newData, 0, this.size - 1); + this.data = newData; + + return d; + } + + public Object peek(){ + return this.data[0]; + } + + public boolean isEmpty(){ + return peek() == null; + } + + public int size(){ + int size = 0; + for(Object obj : this.data){ + size += obj == null ? 0 : 1; + } + + return size; + } + + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + for(Object obj : this.data){ + if(obj != null){ + sb.append(obj.toString()).append(","); + }else break; + } + return sb.toString(); + } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/Stack.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/Stack.java new file mode 100644 index 0000000000..bea16033fa --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/Stack.java @@ -0,0 +1,41 @@ +package basic.dataStructure; + +/** + * Created by macvi on 2017/4/4. + */ +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + this.elementData.add(o); + } + + public Object pop(){ + int index = elementData.size() - 1; + Object obj = elementData.remove(index); + + return obj; + } + + public Object peek(){ + int index = elementData.size() - 1; + return elementData.get(index); + } + public boolean isEmpty(){ + return peek() == null; + } + public int size(){ + return elementData.size(); + } + + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + for(int i = this.size() - 1; i >= 0; i--){ + sb.append(elementData.get(i).toString()).append(","); + } + + return sb.toString(); + } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/liteStruts/LoginAction.java b/group24/75939388/learning2017/src/main/java/basic/liteStruts/LoginAction.java new file mode 100644 index 0000000000..14b8aba8e2 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/liteStruts/LoginAction.java @@ -0,0 +1,39 @@ +package basic.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/group24/75939388/learning2017/src/main/java/basic/liteStruts/ReadXML.java b/group24/75939388/learning2017/src/main/java/basic/liteStruts/ReadXML.java new file mode 100644 index 0000000000..458b247e18 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/liteStruts/ReadXML.java @@ -0,0 +1,28 @@ +package basic.liteStruts; + +import org.dom4j.Document; +import org.dom4j.io.SAXReader; + +import java.io.File; + +/** + * @author : 温友朝 + * @date : 2017/4/10 + */ +public class ReadXML { + Document document; + + public ReadXML(String filePath) throws Exception{ + SAXReader reader = new SAXReader(); + document = reader.read(new File(ReadXML.class.getResource("/").getFile()) + filePath); + } + + public String getActionClass(String actionName){ + return document.selectSingleNode("//action[@name='" + actionName + "']").valueOf("@class"); + } + + public String getJspPage(String actionName, String result){ + return document.selectSingleNode("//action[@name='" + actionName + "']") + .selectSingleNode("//result[@name='" + result + "']").getText(); + } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/liteStruts/Struts.java b/group24/75939388/learning2017/src/main/java/basic/liteStruts/Struts.java new file mode 100644 index 0000000000..3b28f2bf60 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/liteStruts/Struts.java @@ -0,0 +1,78 @@ +package basic.liteStruts; + +import java.lang.reflect.Method; +import java.util.Map; + + +public class Struts { + /** + * 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字段中。 + */ + public static View runAction(String actionName, Map parameters) { + try{ + //0. 读取配置文件struts.xml + ReadXML read = new ReadXML("/resources/struts.xml"); + //1. 找到对应的class + String className = read.getActionClass(actionName); + Class clz = Class.forName(className); + //得到对象 + Object la = clz.newInstance(); + setNameAndPassword(clz, la, parameters); + //2. 调用execute方法 + String result = invokeExecute(clz, la); + //3. 找到对象的所有getter方法 + getResultMap(clz, la, parameters); + //4. 确定使用哪一个jsp + String viewName = read.getJspPage(actionName, result); + View view = new View(); + view.setJsp(viewName); + view.setParameters(parameters); + return view; + }catch(Exception e){ + e.printStackTrace(); + return null; + } + } + + private static void setNameAndPassword(Class clz, Object la, Map parameters) throws Exception { + Method setName = clz.getDeclaredMethod("setName", String.class); + setName.invoke(la, parameters.get("name")); + + Method setPassword = clz.getDeclaredMethod("setPassword", String.class); + setPassword.invoke(la, parameters.get("password")); + } + + private static String invokeExecute(Class clz, Object la)throws Exception{ + Method execute = clz.getDeclaredMethod("execute", null); + Method getMessage = clz.getDeclaredMethod("getMessage", null); + execute.invoke(la, null); + return getMessage.invoke(la, null).toString(); + } + + private static void getResultMap(Class clz, Object la, Map parameters) throws Exception{ + Method[] methods = clz.getMethods(); + for(Method me : methods){ + if(me.getName().startsWith("get")){ + String info = me.invoke(la, null).toString(); + String method= me.getName(); + String key = method.substring(3, method.length()).toLowerCase(); + parameters.put(key, info); + }else continue; + } + + } +} diff --git a/group24/75939388/learning2017/src/main/java/basic/liteStruts/View.java b/group24/75939388/learning2017/src/main/java/basic/liteStruts/View.java new file mode 100644 index 0000000000..777380b8f9 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/liteStruts/View.java @@ -0,0 +1,23 @@ +package basic.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/group24/75939388/learning2017/src/main/java/miniJVM/Demo.java b/group24/75939388/learning2017/src/main/java/miniJVM/Demo.java new file mode 100644 index 0000000000..565983ab06 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/miniJVM/Demo.java @@ -0,0 +1,7 @@ +package miniJVM; + +/** + * Created by macvi on 2017/4/11. + */ +public class Demo { +} diff --git a/group24/75939388/learning2017/src/main/java/thread/download/DownloadThread.java b/group24/75939388/learning2017/src/main/java/thread/download/DownloadThread.java new file mode 100644 index 0000000000..190cae6423 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/thread/download/DownloadThread.java @@ -0,0 +1,39 @@ +package thread.download; + + +import thread.download.api.Connection; + +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +public class DownloadThread extends Thread{ + + Connection conn; + int startPos; + int endPos; + + String localFile = ""; + + CyclicBarrier barrier; + + public DownloadThread(Connection conn, int startPos, int endPos, String localFileName, CyclicBarrier barrier){ + this.localFile = localFileName; + this.conn = conn; + this.startPos = startPos; + this.endPos = endPos; + this.barrier = barrier; + } + public void run(){ + try{ + RandomAccessFile file = new RandomAccessFile(localFile, "rw"); + byte[] buffer = this.conn.read(this.startPos, this.endPos); + file.seek(startPos); + file.write(buffer); + file.close(); + this.conn.close(); + barrier.await(); + }catch(Exception e){ + e.printStackTrace(); + } + } +} diff --git a/group24/75939388/learning2017/src/main/java/thread/download/FileDownloader.java b/group24/75939388/learning2017/src/main/java/thread/download/FileDownloader.java new file mode 100644 index 0000000000..f50560b8be --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/thread/download/FileDownloader.java @@ -0,0 +1,106 @@ +package thread.download; + +import thread.download.api.Connection; +import thread.download.api.ConnectionManager; +import thread.download.api.DownloadListener; + +import java.io.File; +import java.io.RandomAccessFile; +import java.util.concurrent.CyclicBarrier; + +public class FileDownloader { + + String url; + String fileLocation; + DownloadListener listener; + ConnectionManager cm; + + RandomAccessFile file; + + int length; + + private static final int MAX_THREAD_NUM = 5; + + + public FileDownloader(String _url, String fileLocation) { + this.url = _url; + this.fileLocation = fileLocation; + } + + public void execute(){ + // 在这里实现你的代码, 注意: 需要用多线程实现下载 + // 这个类依赖于其他几个接口, 你需要写这几个接口的实现代码 + // (1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) + // (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 + // 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 + // 具体的实现思路: + // 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 + // 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 + // 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 + // 3. 把byte数组写入到文件中 + // 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法 + + // 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。 + Connection conn = null; + + try { + CyclicBarrier barrier = new CyclicBarrier(MAX_THREAD_NUM, new Runnable() { + public void run() { + listener.notifyFinished(); + } + }); + + conn = cm.open(this.url); + this.length = conn.getContentLength(); + + file = getEmptyFile(); + + int divided = length/MAX_THREAD_NUM; + int[] pos = new int[MAX_THREAD_NUM + 1]; + for(int i = 0; i < MAX_THREAD_NUM; i++){ + pos[i] = i == 0 ? 0 : divided * i; + } + pos[MAX_THREAD_NUM] = length; + + for(int i = 0; i < MAX_THREAD_NUM; i++){ + new DownloadThread(conn, pos[i], pos[i + 1] - 1, this.fileLocation, barrier).start(); + } + + + File file2 = new File(fileLocation); + System.out.println("file.length=" + file2.length()); + } catch (Exception e) { + e.printStackTrace(); + }finally{ + if(conn != null){ + conn.close(); + } + } + } + + public RandomAccessFile getEmptyFile(){ + try{ + RandomAccessFile file = new RandomAccessFile(this.fileLocation, "rw"); + byte[] empty = new byte[this.length]; + file.write(empty); + file.close(); + return file; + }catch(Exception e){ + e.printStackTrace(); + } + return null; + } + + 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/group24/75939388/learning2017/src/main/java/thread/download/api/Connection.java b/group24/75939388/learning2017/src/main/java/thread/download/api/Connection.java new file mode 100644 index 0000000000..08a85b35dd --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/thread/download/api/Connection.java @@ -0,0 +1,23 @@ +package thread.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/group24/75939388/learning2017/src/main/java/thread/download/api/ConnectionException.java b/group24/75939388/learning2017/src/main/java/thread/download/api/ConnectionException.java new file mode 100644 index 0000000000..e8fac91d1e --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/thread/download/api/ConnectionException.java @@ -0,0 +1,8 @@ +package thread.download.api; + +public class ConnectionException extends Exception { + + public ConnectionException(String msg){ + super(msg); + } +} diff --git a/group24/75939388/learning2017/src/main/java/thread/download/api/ConnectionManager.java b/group24/75939388/learning2017/src/main/java/thread/download/api/ConnectionManager.java new file mode 100644 index 0000000000..f5f6c2ff70 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/thread/download/api/ConnectionManager.java @@ -0,0 +1,10 @@ +package thread.download.api; + +public interface ConnectionManager { + /** + * 给定一个url , 打开一个连接 + * @param url + * @return + */ + Connection open(String url) throws ConnectionException; +} diff --git a/group24/75939388/learning2017/src/main/java/thread/download/api/DownloadListener.java b/group24/75939388/learning2017/src/main/java/thread/download/api/DownloadListener.java new file mode 100644 index 0000000000..16393c4dd9 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/thread/download/api/DownloadListener.java @@ -0,0 +1,5 @@ +package thread.download.api; + +public interface DownloadListener { + void notifyFinished(); +} diff --git a/group24/75939388/learning2017/src/main/java/thread/download/impl/ConnectionImpl.java b/group24/75939388/learning2017/src/main/java/thread/download/impl/ConnectionImpl.java new file mode 100644 index 0000000000..2e2544ab27 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/thread/download/impl/ConnectionImpl.java @@ -0,0 +1,93 @@ +package thread.download.impl; + +import thread.download.api.Connection; +import thread.download.api.ConnectionException; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.ConnectException; +import java.net.HttpURLConnection; +import java.net.URL; + +public class ConnectionImpl implements Connection { + + private int length = 0; + + private URL url; + + private HttpURLConnection conn; + + private InputStream is; + + private ByteArrayOutputStream baos; + + private ConnectionImpl() {} + + public ConnectionImpl(URL url) { + this.url = url; + try { + this.conn = (HttpURLConnection) url.openConnection(); + this.conn.setRequestMethod("GET"); + this.conn.setReadTimeout(5000); + int responseCode = this.conn.getResponseCode(); + System.out.println("连接状态=" + responseCode); + if (responseCode != 200) { + throw new ConnectionException("连接到" + url.toURI() + "失败"); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public byte[] read(int startPos, int endPos) throws IOException { + try { + //设置读取段落 + this.conn = (HttpURLConnection) url.openConnection(); + this.conn.setRequestMethod("GET"); + this.conn.setReadTimeout(5000); + this.conn.setRequestProperty("Range", "bytes=" + startPos + "-" + endPos); + //获取返回值 + int response = conn.getResponseCode(); + if(response != 200 && response != 206){ + throw new ConnectException("没有连接上" + url.toURI() + ", 状态码为" + response); + } + //开始读取 + int length = endPos - startPos + 1; + this.is = conn.getInputStream(); + byte[] buffer = new byte[1024]; + baos = new ByteArrayOutputStream(length); + while(-1 != is.read(buffer)){ + baos.write(buffer); + } + System.out.println(startPos + "-" + endPos + "文件段读取完成"); + return baos.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } finally { + this.close(); + } + } + + public int getContentLength() { + try { + this.length = this.conn.getContentLength(); + System.out.println("获取的文件长度=" + length); + return this.length; + } catch (Exception e) { + e.printStackTrace(); + return -1; + } + } + + public void close() { + try { + if(is != null){ + is.close(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/group24/75939388/learning2017/src/main/java/thread/download/impl/ConnectionManagerImpl.java b/group24/75939388/learning2017/src/main/java/thread/download/impl/ConnectionManagerImpl.java new file mode 100644 index 0000000000..ede8ccac00 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/thread/download/impl/ConnectionManagerImpl.java @@ -0,0 +1,20 @@ +package thread.download.impl; + +import thread.download.api.Connection; +import thread.download.api.ConnectionException; +import thread.download.api.ConnectionManager; + +import java.net.URL; + +public class ConnectionManagerImpl implements ConnectionManager { + + public Connection open(String url) throws ConnectionException { + Connection conn = null; + try{ + conn = new ConnectionImpl(new URL(url)); + }catch(Exception e){ + e.printStackTrace(); + } + return conn; + } +} diff --git a/group24/75939388/learning2017/src/test/java/data_structure/ArrayListTest.java b/group24/75939388/learning2017/src/test/java/data_structure/ArrayListTest.java new file mode 100644 index 0000000000..ee8ee6b0d0 --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/data_structure/ArrayListTest.java @@ -0,0 +1,69 @@ +package data_structure; + +import org.junit.Test; +import basic.dataStructure.ArrayList; + +/** + * Created by macvi on 2017/4/2. + */ +public class ArrayListTest { + + @Test + public void TestAdd(){ + ArrayList al = new ArrayList(); + for(int i = 0; i < 32; i++){ + al.add(i + ""); + } + + System.out.println("ArrayList.content-->" + al.toString()); + } + + + @Test + public void testIndexAdd(){ + ArrayList al = new ArrayList(); + for(int i = 0; i < 17; i ++){ + al.add(i + ""); + } + + al.add(3, "xxoo"); + al.add(11, "abcd"); + al.add(0, "efgh"); + al.add(al.size(), "ijkl"); + + System.out.println("al.toString-->" + al.toString()); + System.out.println("size-->" + al.size()); + } + + @Test + public void testGet(){ + ArrayList al = new ArrayList(); + for(int i = 0; i < 18; i ++){ + al.add(i + "zxcd"); + } + + System.out.println("get-->" + al.get(13)); + } + + @Test + public void testRemove(){ + ArrayList al = new ArrayList(); + for(int i = 0; i < 18; i ++){ + al.add(i + ""); + } + System.out.println("size1-->" + al.size()); + System.out.println("al.toString1-->" + al.toString()); + String re = (String)al.remove(12); + System.out.println("remove index=12 :"); + System.out.println("re-->" + re); + System.out.println("size2-->" + al.size()); + System.out.println("al.toString2-->" + al.toString()); + + String re1 = (String)al.remove(1); + System.out.println("remove index=1 :"); + System.out.println("re-->" + re1); + System.out.println("size2-->" + al.size()); + System.out.println("al.toString2-->" + al.toString()); + } + +} diff --git a/group24/75939388/learning2017/src/test/java/data_structure/ArrayUtilTest.java b/group24/75939388/learning2017/src/test/java/data_structure/ArrayUtilTest.java new file mode 100644 index 0000000000..1dc1a6f263 --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/data_structure/ArrayUtilTest.java @@ -0,0 +1,67 @@ +package data_structure; + +import org.junit.Test; +import basic.dataStructure.ArrayUtil; + +import java.util.Arrays; + +/** + * @author : 温友朝 + * @date : 2017/4/5 + */ +public class ArrayUtilTest { + ArrayUtil au = new ArrayUtil(); + + @Test + public void testReverse(){ + int[] arr = {1, 2, 3, 4, 5}; + this.au.reverseArray(arr); + } + + @Test + public void testTrim(){ + int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; + int[] arr = this.au.removeZero(oldArr); + System.out.println(Arrays.toString(arr)); + } + + @Test + public void testMerge(){ + int[] a1 = {3, 5, 7,8}; + int[] a2 = {4, 5, 6,7}; + + int[] arr = this.au.merge(a1, a2); + System.out.println(Arrays.toString(arr)); + } + + @Test + public void testGrow(){ + int[] arr = {1, 2, 3, 4, 5}; + int[] arr2 = this.au.grow(arr, 4); + System.out.println(Arrays.toString(arr2)); + } + + @Test + public void testFibonacci(){ + int[] arr = this.au.fibonacci(100); + System.out.println(Arrays.toString(arr)); + } + + @Test + public void testPrimes(){ + int[] arr = this.au.getPrimes(100000); + System.out.println(Arrays.toString(arr)); + } + + @Test + public void testPerfectNumbers(){ + int[] arr = this.au.getPerfectNumbers(10000); + System.out.println(Arrays.toString(arr)); + } + + @Test + public void testJoin(){ + int[] arr = this.au.getPerfectNumbers(10000); + System.out.println(this.au.join(arr, "-")); + } +} diff --git a/group24/75939388/learning2017/src/test/java/data_structure/BinaryNodeTreeTest.java b/group24/75939388/learning2017/src/test/java/data_structure/BinaryNodeTreeTest.java new file mode 100644 index 0000000000..df976147e3 --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/data_structure/BinaryNodeTreeTest.java @@ -0,0 +1,29 @@ +package data_structure; + +import org.junit.Test; +import basic.dataStructure.BinaryTreeNode; + +/** + * @author : 温友朝 + * @date : 2017/4/5 + */ +public class BinaryNodeTreeTest { + +// @Test +// public BinaryTreeNode getTree(){ +// BinaryTreeNode btn = new BinaryTreeNode(5); +// btn.setData(3); +// +// return btn; +// } + + @Test + public void testAdd(){ + BinaryTreeNode btn = new BinaryTreeNode(5); + btn.setData(3); + btn.setData(7); + btn.setData(10); + btn.setData(6); + btn.setData(4); + } +} diff --git a/group24/75939388/learning2017/src/test/java/data_structure/LinkedListTest.java b/group24/75939388/learning2017/src/test/java/data_structure/LinkedListTest.java new file mode 100644 index 0000000000..c98a305623 --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/data_structure/LinkedListTest.java @@ -0,0 +1,151 @@ +package data_structure; + +import basic.dataStructure.LinkedList; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; + +/** + * Created by macvi on 2017/4/3. + */ +public class LinkedListTest { + + LinkedList ll = new LinkedList(); + + @Before + public void init(){ + for(int i = 0; i < 10; i++){ + ll.add(i); + } + } + + @After + public void print(){ + + } + + + @Test + public void testLinkedListAdd(){ + LinkedList ll = new LinkedList(); + ll.add("123"); + ll.add("456"); + ll.add("asdf"); + ll.add("zxcv"); + + + System.out.println("ll.toString-->" + ll); + System.out.println("ll.size--->" + ll.size()); + } + + @Test + public void testLinkedListIndexAdd(){ + System.out.println("12345"); + } + + @Test + public void testGet(){ + LinkedList ll = new LinkedList(); + for(int i = 0; i < 10; i ++){ + ll.add(i + ""); + } + + System.out.println("get-->" + ll.get(9)); + System.out.println("ll.toString-->" + ll.toString() + "\nsize-->" + ll.size()); + } + + @Test + public void testIndexAdd(){ + LinkedList ll = new LinkedList(); + for(int i = 0; i < 5; i ++){ + ll.add(i + ""); + } + + ll.add(5, "xxoo"); + System.out.println("index get-->" + ll.get(0)); + System.out.println("ll.toString2-->" + ll.toString() + "\nsize-->" + ll.size()); + } + + @Test + public void testRemove(){ + LinkedList ll = new LinkedList(); + for(int i = 0; i < 6; i ++){ + ll.add(i + ""); + } + + Object removed = ll.remove(-1); + System.out.println("ll.toString-->" + ll.toString() + "\nsize-->" + ll.size()); + System.out.println("removed-->" + removed.toString()); + } + + @Test + public void testReverse(){ + ll.reverse(); + System.out.println("ll.reverse-->" + ll.toString()); + } + + @Test + public void testRemoveFirstHalf(){ + ll.removeFirstHalf(); + System.out.println("ll.removeFirstHalf-->" + ll.toString()); + } + + @Test + public void testRemoveL(){ + ll.remove(2, 5); + System.out.println("ll.toString-->" + ll.toString()); + } + + @Test + public void testGetElements(){ + LinkedList l2 = new LinkedList(); + l2.add(3); + l2.add(5); + l2.add(9); + l2.add(0); + + int[] arr = ll.getElements(l2); + System.out.println("arr->" + Arrays.toString(arr)); + } + + @Test + public void testRemoveDuplicate(){ + ll.add(1); + ll.add(3); + ll.add(4); + ll.add(10); + ll.add(11); + ll.removeDuplicateValues(); + System.out.println("ll.toString-->" + ll.toString()); + } + + @Test + public void testRemoveRange(){ + ll.removeRange(2, 6); + System.out.println("ll.toString-->" + ll.toString()); + } + + @Test + public void testSubtract(){ + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(5); + + ll.subtract(list); + System.out.println("ll.toString-->" + ll); + } + + @Test + public void testIntersection(){ + LinkedList list = new LinkedList(); + list.add(1); + list.add(2); + list.add(5); + + LinkedList list2 = ll.intersection(list); + System.out.println(list2); + } +} diff --git a/group24/75939388/learning2017/src/test/java/data_structure/QueueTest.java b/group24/75939388/learning2017/src/test/java/data_structure/QueueTest.java new file mode 100644 index 0000000000..3db6d82e49 --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/data_structure/QueueTest.java @@ -0,0 +1,50 @@ +package data_structure; + +import org.junit.Assert; +import org.junit.Test; +import basic.dataStructure.Queue; + +/** + * Created by macvi on 2017/4/4. + */ +public class QueueTest { + + private Queue newQueue(){ + Queue q = new Queue(); + for(int i = 0; i < 13; i++){ + q.enQueue(i + ""); + } + + return q; + } + + @Test + public void testEnqueue(){ + Queue q = newQueue(); + q.enQueue(10 + ""); + q.enQueue( "xxoo"); + System.out.println("queue-->" + q.toString()); + } + + @Test + public void testSize(){ + Queue q = newQueue(); + + Assert.assertEquals(13, q.size()); + } + + @Test + public void testDequeue(){ + Queue q = newQueue(); + Object obj = q.deQueue(); + + Assert.assertEquals("0", obj); + } + + @Test + public void testIsEmpty(){ + Queue q = newQueue(); + + Assert.assertEquals(false, q.isEmpty()); + } +} diff --git a/group24/75939388/learning2017/src/test/java/data_structure/StackTest.java b/group24/75939388/learning2017/src/test/java/data_structure/StackTest.java new file mode 100644 index 0000000000..b933b8b63e --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/data_structure/StackTest.java @@ -0,0 +1,49 @@ +package data_structure; + +import org.junit.Assert; +import org.junit.Test; +import basic.dataStructure.Stack; + +/** + * Created by macvi on 2017/4/4. + */ +public class StackTest { + + private Stack getStack(){ + Stack s = new Stack(); + for(int i = 0; i < 14; i ++){ + s.push(i + ""); + } + + return s; + } + + @Test + public void pushTest(){ + Stack s = getStack(); + + System.out.println("stack-->" + s.toString()); + } + + @Test + public void testSize(){ + Stack s = getStack(); + + Assert.assertEquals(14, s.size()); + } + + @Test + public void testPeek(){ + Stack s = getStack(); + + Assert.assertEquals("13", s.peek()); + } + + @Test + public void testPop(){ + Stack s = getStack(); + + Assert.assertEquals("13", s.pop()); + Assert.assertEquals(13, s.size()); + } +} diff --git a/group24/75939388/learning2017/src/test/java/download/FileDownloaderTest.java b/group24/75939388/learning2017/src/test/java/download/FileDownloaderTest.java new file mode 100644 index 0000000000..0dc8536269 --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/download/FileDownloaderTest.java @@ -0,0 +1,54 @@ +package download; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import thread.download.FileDownloader; +import thread.download.api.ConnectionManager; +import thread.download.api.DownloadListener; +import thread.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://www.baidu.com/img/bd_logo1.png"; + String fileLocation = "D:\\Tee\\JavaLearnin\\test.png"; + FileDownloader downloader = new FileDownloader(url, fileLocation); + + ConnectionManager cm = new ConnectionManagerImpl(); + downloader.setConnectionManager(cm); + + downloader.setListener(new DownloadListener() { + 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/group24/75939388/learning2017/src/test/java/liteStruts/StrutsTest.java b/group24/75939388/learning2017/src/test/java/liteStruts/StrutsTest.java new file mode 100644 index 0000000000..4477c5f51b --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/liteStruts/StrutsTest.java @@ -0,0 +1,42 @@ +package liteStruts; + +import org.junit.Assert; +import org.junit.Test; +import basic.liteStruts.Struts; +import basic.liteStruts.View; + +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")); + } +}