Skip to content

Commit 7ec2149

Browse files
authored
Merge pull request onlyliuxin#23 from Weiyin-Chen/master
assignment2 by Weiyin-Chen
2 parents a7917ef + ec22900 commit 7ec2149

File tree

6 files changed

+343
-0
lines changed

6 files changed

+343
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package array;
2+
3+
public class ArrayUtil {
4+
5+
/**
6+
* 给定一个整形数组a , 对该数组的值进行置换
7+
例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7]
8+
如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7]
9+
* @param origin
10+
* @return
11+
*/
12+
public void reverseArray(int[] origin){
13+
if (origin.length == 0) {
14+
return;
15+
}
16+
int n = origin.length - 1;
17+
int temp;
18+
for (int i = 0; i < n/2; i++) {
19+
temp = origin[i];
20+
origin[i] = origin[n - i];
21+
origin[n - i] = temp;
22+
}
23+
}
24+
25+
/**
26+
* 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}
27+
* 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为:
28+
* {1,3,4,5,6,6,5,4,7,6,7,5}
29+
* @param oldArray
30+
* @return
31+
*/
32+
33+
public int[] removeZero(int[] oldArray){
34+
if (oldArray.length == 0) {
35+
return null;
36+
}
37+
int n = oldArray.length;
38+
int zeros = 0;
39+
for (int i = 0; i < n - 1; i++) {
40+
if (oldArray[i] == 0) {
41+
zeros++;
42+
}
43+
}
44+
int[] result = new int[n - zeros];
45+
int j = 0;
46+
for (int i = 0; i < n - zeros - 1; i++) {
47+
while (oldArray[j] == 0) {
48+
j++;
49+
}
50+
result[i] = oldArray[j];
51+
j++;
52+
}
53+
return result;
54+
}
55+
56+
/**
57+
* 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的
58+
* 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复
59+
* @param array1
60+
* @param array2
61+
* @return
62+
*/
63+
64+
public int[] merge(int[] array1, int[] array2){
65+
return null;
66+
}
67+
/**
68+
* 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size
69+
* 注意,老数组的元素在新数组中需要保持
70+
* 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为
71+
* [2,3,6,0,0,0]
72+
* @param oldArray
73+
* @param size
74+
* @return
75+
*/
76+
public int[] grow(int [] oldArray, int size){
77+
return null;
78+
}
79+
80+
/**
81+
* 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列
82+
* 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13]
83+
* max = 1, 则返回空数组 []
84+
* @param max
85+
* @return
86+
*/
87+
public static int[] fibonacci(int max){
88+
if (max <= 1) {
89+
return new int[0];
90+
}
91+
int n = 1;
92+
int init = helpFibonacci(n);
93+
while (init < max) {
94+
n++;
95+
init = helpFibonacci(n);
96+
}
97+
int[] result = new int[n - 1];
98+
for (int i = 0; i < n - 1; i++) {
99+
result[i] = helpFibonacci(i + 1);
100+
}
101+
return result;
102+
}
103+
104+
public static int helpFibonacci(int n) {
105+
if(n == 0)
106+
return 0;
107+
else if(n == 1)
108+
return 1;
109+
else
110+
return helpFibonacci(n -1 ) + helpFibonacci(n - 2);
111+
}
112+
/**
113+
* 返回小于给定最大值max的所有素数数组
114+
* 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19]
115+
* @param max
116+
* @return
117+
*/
118+
public int[] getPrimes(int max){
119+
return null;
120+
}
121+
122+
/**
123+
* 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3
124+
* 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数
125+
* @param max
126+
* @return
127+
*/
128+
public int[] getPerfectNumbers(int max){
129+
return null;
130+
}
131+
132+
/**
133+
* 用seperator 把数组 array给连接起来
134+
* 例如array= [3,8,9], seperator = "-"
135+
* 则返回值为"3-8-9"
136+
* @param array
137+
* @param
138+
* @return
139+
*/
140+
public String join(int[] array, String seperator){
141+
return null;
142+
}
143+
144+
public static void main(String[] args) {
145+
int[] a = fibonacci(15);
146+
for (int i = 0; i < a.length; i++) {
147+
System.out.println(a[i]);
148+
}
149+
}
150+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package litestruts;
2+
3+
/**
4+
* 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。
5+
* @author liuxin
6+
*
7+
*/
8+
public class LoginAction{
9+
private String name ;
10+
private String password;
11+
private String message;
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public String getPassword() {
18+
return password;
19+
}
20+
21+
public String execute(){
22+
if("test".equals(name) && "1234".equals(password)){
23+
this.message = "login successful";
24+
return "success";
25+
}
26+
this.message = "login failed,please check your user/pwd";
27+
return "fail";
28+
}
29+
30+
public void setName(String name){
31+
this.name = name;
32+
}
33+
public void setPassword(String password){
34+
this.password = password;
35+
}
36+
public String getMessage(){
37+
return this.message;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package litestruts;
2+
3+
import org.w3c.dom.Document;
4+
import org.w3c.dom.Element;
5+
import org.w3c.dom.Node;
6+
import org.w3c.dom.NodeList;
7+
8+
import javax.xml.parsers.DocumentBuilder;
9+
import javax.xml.parsers.DocumentBuilderFactory;
10+
import java.io.File;
11+
import java.lang.reflect.Method;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
16+
17+
public class Struts {
18+
19+
public static View runAction(String actionName, Map<String,String> parameters) {
20+
21+
try {
22+
// parse xml file
23+
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
24+
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
25+
Document doc = dBuilder.parse("src/litestruts/struts.xml");
26+
doc.getDocumentElement().normalize();
27+
28+
// get action items
29+
NodeList list = doc.getElementsByTagName("action");
30+
for (int i = 0; i < list.getLength(); i++) {
31+
Node node = list.item(i);
32+
Element element = (Element) node;
33+
// look for action-related class
34+
if (element.getAttribute("name").equals(actionName)){
35+
36+
Class<?> c = Class.forName(element.getAttribute("class"));
37+
Object o = c.newInstance();
38+
39+
//set name
40+
Method setName = c.getDeclaredMethod("setName", String.class);
41+
setName.invoke(o, parameters.get("name"));
42+
43+
//set password
44+
Method setPassword = c.getDeclaredMethod("setPassword", String.class);
45+
setPassword.invoke(o, parameters.get("password"));
46+
47+
//execute
48+
Method execute = c.getDeclaredMethod("execute", null);
49+
// login result
50+
String result = execute.invoke(o).toString();
51+
52+
//get login messsage
53+
Method getMessage = c.getDeclaredMethod("getMessage", null);
54+
HashMap<String, String> map = new HashMap<>();
55+
map.put("message", getMessage.invoke(o).toString());
56+
57+
// new view with parameter map
58+
View view = new View();
59+
view.setParameters(map);
60+
NodeList list1 = element.getElementsByTagName("result");
61+
for (int j = 0; j < list1.getLength(); j++) {
62+
Node node1 = list1.item(j);
63+
Element element1 = (Element) node1;
64+
if (element1.getAttribute("name").equals(result)) {
65+
view.setJsp(node1.getTextContent());
66+
}
67+
}
68+
return view;
69+
}
70+
}
71+
} catch (Exception e) {
72+
System.out.println("parse error");
73+
}
74+
return null;
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package litestruts;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
10+
11+
12+
13+
public class StrutsTest {
14+
15+
@Test
16+
public void testLoginActionSuccess() {
17+
18+
String actionName = "login";
19+
20+
Map<String,String> params = new HashMap<String,String>();
21+
params.put("name", "test");
22+
params.put("password","1234");
23+
24+
25+
View view = Struts.runAction(actionName,params);
26+
27+
Assert.assertEquals("/jsp/homepage.jsp", view.getJsp());
28+
Assert.assertEquals("login successful", view.getParameters().get("message"));
29+
}
30+
31+
@Test
32+
public void testLoginActionFailed() {
33+
String actionName = "login";
34+
Map<String,String> params = new HashMap<String,String>();
35+
params.put("name", "test");
36+
params.put("password","123456"); //密码和预设的不一致
37+
38+
View view = Struts.runAction(actionName,params);
39+
40+
Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp());
41+
Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message"));
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package litestruts;
2+
3+
import java.util.Map;
4+
5+
public class View {
6+
private String jsp;
7+
private Map parameters;
8+
9+
public String getJsp() {
10+
return jsp;
11+
}
12+
public View setJsp(String jsp) {
13+
this.jsp = jsp;
14+
return this;
15+
}
16+
public Map getParameters() {
17+
return parameters;
18+
}
19+
public View setParameters(Map parameters) {
20+
this.parameters = parameters;
21+
return this;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<struts>
3+
<action name="login" class="litestruts.LoginAction">
4+
<result name="success">/jsp/homepage.jsp</result>
5+
<result name="fail">/jsp/showLogin.jsp</result>
6+
</action>
7+
<action name="logout" class="litestruts.LogoutAction">
8+
<result name="success">/jsp/welcome.jsp</result>
9+
<result name="error">/jsp/error.jsp</result>
10+
</action>
11+
</struts>

0 commit comments

Comments
 (0)