<项目><day12>通讯录(视频)

需求分析(需求分析师)

功能分析:

1)添加联系人

2)修改联系人

3)删除联系人

4)查询所有联系人

 

需求设计(系统分析师/架构师/资深开发人员)

 

2.1设计实体(抽象实体)

 

联系人实体:

class Contact{
    private String id;
    private String name;
    private String gender;
    private int age;
    private String phone;
    private String email;
    private String qq;
  public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getQq() {
        return qq;
    }
    public void setQq(String qq) {
        this.qq = qq;
    }
    @Override
    public String toString() {
        return "Contact [age=" + age + ", email=" + email + ", gender="
                + gender + ", id=" + id + ", name=" + name + ", phone=" + phone
                + ", qq=" + qq + "]";
    }
}

 

 

 

2.2设计“数据库”,(xml代替"数据库"

XML格式

 

contact.xml
<contactList>
<contact id="1">
<name>张三</name>
<gender></gender>
<age>20</age>
<phone>13433334444</phone>
<email>zs@qq.com</email>
<qq>43222222<qq>
</contact>
</contactList>

 

XML工具类

 1 public class XMLUtil {
 2     
 3     /**
 4      * 读取xml文档方法
 5      * @return
 6      */
 7     public static Document getDocument(){
 8         try {
 9             Document doc = new SAXReader().read(new File("e:/contact.xml"));
10             return doc;
11         } catch (DocumentException e) {
12             e.printStackTrace();
13             throw new RuntimeException(e);
14         }
15     }
16     
17 
18     /**
19      * 写出到xml文档中
20      */
21     public static void write2xml(Document doc){
22         try {
23             FileOutputStream out = new FileOutputStream("e:/contact.xml");
24             OutputFormat format = OutputFormat.createPrettyPrint();
25             format.setEncoding("utf-8");
26             XMLWriter writer = new XMLWriter(out,format);
27             writer.write(doc);
28             writer.close();
29         } catch (Exception e) {
30             e.printStackTrace();
31             throw new RuntimeException(e);
32         }
33     }
34 }

 

2.3设计涉及的接口

 

DAO接口(数据访问对象):实体对象的CRUD方法。

 

项目原则: 通常一个实体对象就会对应一个DAO接口和一个DAO实现类

DAO接口

 

interface ContactDao{
    public void addContact(Contact contact);//添加联系人
    public void updateContact(Contact contact);//修改联系人
    public void deleteContact(String id);//删除联系人
    public List<Contact> findAll();  //查询所有联系人
    public Contact findById(String id);//根据编号查询联系人
}

 

DAO实现类

  1 public class ContactDaoImpl implements ContactDao {
  2 
  3     /**
  4      * 添加联系人
  5      */
  6     public void addContact(Contact contact) {
  7         try {
  8             File file = new File("e:/contact.xml");
  9             Document doc = null;
 10             Element rootElem = null;
 11             if(!file.exists()){
 12                 /**
 13                  * 需求: 把contact对象保存到xml文件中
 14                  */
 15                 //如果没有xml文件,则创建xml文件
 16                 doc = DocumentHelper.createDocument();
 17                 //创建根标签
 18                 rootElem = doc.addElement("contactList");
 19             }else{
 20                 //如果有xml文件,则读取xml文件
 21                 doc = XMLUtil.getDocument();
 22                 //如果有xml文件,读取根标签
 23                 rootElem = doc.getRootElement();
 24             }
 25 
 26             //添加contact标签
 27             /**
 28              * <contact id="1">
 29                     <name>eric</name>
 30                     <gender>男</gender>
 31                     <age>20</age>
 32                     <phone>1343333</phone>
 33                     <email>eric@qq.com</email>
 34                     <qq>554444</qq>
 35                 </contact>
 36              */
 37             Element contactElem = rootElem.addElement("contact");
 38             
 39             /**
 40              * 由系统自动生成随机且唯一的ID值,赋值给联系人
 41              */
 42             String uuid = UUID.randomUUID().toString().replace("-","");
 43         
 44             contactElem.addAttribute("id", uuid);
 45             contactElem.addElement("name").setText(contact.getName());
 46             contactElem.addElement("gender").setText(contact.getGender());
 47             contactElem.addElement("age").setText(contact.getAge()+"");
 48             contactElem.addElement("phone").setText(contact.getPhone());
 49             contactElem.addElement("email").setText(contact.getEmail());
 50             contactElem.addElement("qq").setText(contact.getQq()); 51             
 52             //把Document写出到xml文件
 53             XMLUtil.write2xml(doc);
 54         } catch (Exception e) {
 55             e.printStackTrace();
 56             throw new RuntimeException(e);
 57         }
 58     }
 59 
 60     /**
 61      * 删除联系人
 62      */
 63     public void deleteContact(String id) {
 64         try {
 65             //1.读取xml文件
 66             Document doc = XMLUtil.getDocument();
 67             //2.查询需要删除id的contact
 68             Element contactElem = (Element)doc.selectSingleNode("//contact[@id='"+id+"']");
 69             //删除标签
 70             if(contactElem!=null){
 71                 contactElem.detach();
 72             }
 73             
 74             //3.把Document写出到xml文件
 75             XMLUtil.write2xml(doc);
 76         } catch (Exception e) {
 77             e.printStackTrace();
 78             throw new RuntimeException(e);
 79         }
 80     }
 81 
 82     /**
 83      * 查询所有联系人
 84      */
 85     public List<Contact> findAll() {
 86         //1.读取xml文件
 87         Document doc = XMLUtil.getDocument();
 88         
 89         //2.创建List对象
 90         List<Contact> list = new ArrayList<Contact>();
 91         //3.读取contact标签
 92         List<Element> conList = (List<Element>)doc.selectNodes("//contact");
 93         for(Element e:conList){
 94             //创建COntact对象
 95             Contact c = new Contact();
 96             c.setId(e.attributeValue("id"));
 97             c.setName(e.elementText("name"));
 98             c.setGender(e.elementText("gender"));
 99             c.setAge(Integer.parseInt(e.elementText("age")));
100             c.setPhone(e.elementText("phone"));
101             c.setEmail(e.elementText("email"));
102             c.setQq(e.elementText("qq"));
103             //把Contact放入list中
104             list.add(c);
105         }
106         return list;
107     }
108 
109     /**
110      * 根据编号查询联系人
111      */
112     public Contact findById(String id) {
113         Document doc = XMLUtil.getDocument();
114         Element e = (Element)doc.selectSingleNode("//contact[@id='"+id+"']");
115         
116         Contact c = null;
117         if(e!=null){
118             //创建COntact对象
119             c = new Contact();
120             c.setId(e.attributeValue("id"));
121             c.setName(e.elementText("name"));
122             c.setGender(e.elementText("gender"));
123             c.setAge(Integer.parseInt(e.elementText("age")));
124             c.setPhone(e.elementText("phone"));
125             c.setEmail(e.elementText("email"));
126             c.setQq(e.elementText("qq"));
127         }
128         return c;
129     }
130 
131     /**
132      * 修改联系人
133      */
134     public void updateContact(Contact contact) {
135         /**
136          * 需求: 修改id值为2的联系人
137          *     1)查询id值为2的contact标签
138          *  2)修改contact标签的内容
139          */
140         try {
141             //1.读取xml文件
142             Document doc = XMLUtil.getDocument();
143             
144             Element contactElem = (Element)doc.selectSingleNode("//contact[@id='"+contact.getId()+"']");
145             
146             //2.修改contact标签内容            contactElem.element("name").setText(contact.getName());
147             contactElem.element("gender").setText(contact.getGender());
148             contactElem.element("age").setText(contact.getAge()+"");
149             contactElem.element("phone").setText(contact.getPhone());
150             contactElem.element("email").setText(contact.getEmail());
151             contactElem.element("qq").setText(contact.getQq());            
152             //3.把Document写出到xml文件
153             XMLUtil.write2xml(doc);
154         } catch (Exception e) {
155             e.printStackTrace();
156             throw new RuntimeException(e);
157         }
158     }
159     public static void main(String[] args) {
160         //测试UUID
161         String uuid = UUID.randomUUID().toString().replace("-","");
162         System.out.println(uuid);
163     }
164 
165 }

 

 

2.4设计项目的目录结构

 

项目名称: contactSys_web

 

目录结构:

 

|- contactSys_web

 

  |-src

 

    |-gz.itcast.contactSys_web.entity : 存放实体对象

 

    |-gz.itcast.contactSys_web.dao : 存放dao的接口

 

    |-gz.itcast.contactSys_web.dao.impl: 存放dao的实现类

 

    |-gz.itcast.contactSys_web.servlet: 存放servlet的类

 

    |-gz.itcast.contactSys_web.test: 存放单元测试类

 

    |-gz.itcast.contactSys_web.util: 存放工具类

 

    |-gz.itcast.contactSys_web.exception: 存放自定义异常类

 

  |-WebRoot

 

    |-html文件

 

    |-images:目录。存放图片资源

 

    |-css:目录。存放css资源

 

    |-js:目录。存放js资源

 

编码实现(软件开发工程师/攻城狮)

 

开发顺序:

 

设计数据库-> 实体 -> DAO接口,DAO实现-> Servlet+html页面

3.1显示所有人的逻辑

public class ListContactServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //1.从xml中读取出联系人数据
        ContactDao dao = new ContactDaoImpl();
        List<Contact> list = dao.findAll();
        
        //2.显示到浏览器
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        
        String html = "";
        
        //shift+alt+A   ^(.*)$  \1";
        html += "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
        html += "<html xmlns='http://www.w3.org/1999/xhtml'>";
        html += "<head>";
        html += "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
        html += "<title>查询所有联系人</title>";
        html += "<style type='text/css'>";
        html += "    table td{";
        html += "        /*文字居中*/";
        html += "        text-align:center;";
        html += "    }";
        html += "    ";
        html += "    /*合并表格的边框*/";
        html += "    table{";
        html += "        border-collapse:collapse;";
        html += "    }";
        html += "</style>";
        html += "</head>";
        html += "";
        html += "<body>";
        html += "<center><h3>查询所有联系人</h3></center>";
        html += "<table align='center' border='1' width='800px'>";
        html += "    <tr>";
        html += "        <th>编号</th>";
        html += "        <th>姓名</th>";
        html += "        <th>性别</th>";
        html += "        <th>年龄</th>";
        html += "        <th>电话</th>";
        html += "        <th>邮箱</th>";
        html += "        <th>QQ</th>";
        html += "        <th>操作</th>";
        html += "    </tr>";
        if(list!=null){
            for (Contact contact : list) {
                html += "    <tr>";
                html += "        <td>"+contact.getId()+"</td>";
                html += "        <td>"+contact.getName()+"</td>";
                html += "        <td>"+contact.getGender()+"</td>";
                html += "        <td>"+contact.getAge()+"</td>";
                html += "        <td>"+contact.getPhone()+"</td>";
                html += "        <td>"+contact.getEmail()+"</td>";
                html += "        <td>"+contact.getQq()+"</td>";
                html += "        <td><a href='"+request.getContextPath()+"/QueryContactServlet?id="+contact.getId()+"'>修改</a>&nbsp;<a href='"+request.getContextPath()+"/DeleteContactServlet?id="+contact.getId()+"'>删除</a></td>";
                html += "    </tr>";
            }
        }
        html += "    <tr>";
        html += "        <td colspan='8' align='center'><a href='"+request.getContextPath()+"/addContact.html'>[添加联系人]</a></td>";
        html += "    </tr>";
        html += "</table>";
        html += "</body>";
        html += "</html>";

        
        writer.write(html);
        
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

 

3.2添加联系人的逻辑

 1 public class AddContactServlet extends HttpServlet {
 2 
 3     public void doGet(HttpServletRequest request, HttpServletResponse response)
 4             throws ServletException, IOException {
 5         request.setCharacterEncoding("utf-8");
 6         //1.接收参数
 7         String name = request.getParameter("name");
 8         String gender = request.getParameter("gender");
 9         String age = request.getParameter("age");
10         String phone = request.getParameter("phone");
11         String email = request.getParameter("email");
12         String qq = request.getParameter("qq");
13         
14         //封装成Contact对象
15         Contact contact = new Contact();
16         contact.setName(name);
17         contact.setGender(gender);
18         contact.setAge(Integer.parseInt(age));
19         contact.setPhone(phone);
20         contact.setEmail(email);
21         contact.setQq(qq);
22         
23         //2.调用dao类的添加联系人的方法
24         ContactDao dao = new ContactDaoImpl();
25         dao.addContact(contact);
26         
27         //3.跳转到查询联系人的页面
28         response.sendRedirect(request.getContextPath()+"/ListContactServlet");
29     }
30 
31     public void doPost(HttpServletRequest request, HttpServletResponse response)
32             throws ServletException, IOException {
33         doGet(request, response);
34     }
35 
36 }

3.3修改前查询联系人的逻辑

 1 public class QueryContactServlet extends HttpServlet {
 2 
 3     public void doGet(HttpServletRequest request, HttpServletResponse response)
 4             throws ServletException, IOException {
 5         //1.接收id
 6         String id = request.getParameter("id");
 7         
 8         //2.调用dao根据id查询联系人的方法
 9         ContactDao dao = new ContactDaoImpl();
10         Contact contact = dao.findById(id);
11         
12         //3.把联系人显示到浏览器中
13         response.setContentType("text/html;charset=utf-8");
14         PrintWriter writer = response.getWriter();
15         
16         String html = "";
17         
18         html += "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
19         html += "<html xmlns='http://www.w3.org/1999/xhtml'>";
20         html += "<head>";
21         html += "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
22         html += "<title>修改联系人</title>";
23         html += "</head>";
24         html += "";
25         html += "<body>";
26         html += "<center><h3>修改联系人</h3></center>";
27         html += "<form action='"+request.getContextPath()+"/UpdateContactServlet' method='post'>";
28         //注意:添加id的隐藏域
29         html += "<input type='hidden' name='id' value='"+contact.getId()+"'/>";
30         html += "<table align='center' border='1' width='300px'>";
31         html += "    <tr>";
32         html += "        <th>姓名</th>";
33         html += "        <td><input type='text' name='name' value='"+contact.getName()+"'/></td>";
34         html += "    </tr>";
35         html += "    <tr>";
36         html += "        <th>性别</th>";
37         html += "        <td>";
38         
39         if(contact.getGender().equals("男")){
40             html += "        <input type='radio' name='gender' value='男' checked='checked'/>男";
41             html += "        <input type='radio' name='gender' value='女'/>女";
42         }else if(contact.getGender().equals("女")){
43             html += "        <input type='radio' name='gender' value='男'/>男";
44             html += "        <input type='radio' name='gender' value='女' checked='checked'/>女";
45         }else{
46             html += "        <input type='radio' name='gender' value='男' checked='checked'/>男";
47             html += "        <input type='radio' name='gender' value='女'/>女";
48         }
49     
50         html += "        </td>";
51         html += "    </tr>";
52         html += "    <tr>";
53         html += "        <th>年龄</th>";
54         html += "        <td><input type='text' name='age' value='"+contact.getAge()+"'/></td>";
55         html += "    </tr>";
56         html += "    <tr>";
57         html += "        <th>电话</th>";
58         html += "        <td><input type='text' name='phone' value='"+contact.getPhone()+"'/></td>";
59         html += "    </tr>";
60         html += "    <tr>";
61         html += "        <th>邮箱</th>";
62         html += "        <td><input type='text' name='email' value='"+contact.getEmail()+"'/></td>";
63         html += "    </tr>";
64         html += "    <tr>";
65         html += "        <th>QQ</th>";
66         html += "        <td><input type='text' name='qq' value='"+contact.getQq()+"'/></td>";
67         html += "    </tr>";
68         html += "    <tr>";
69         html += "        <td colspan='2' align='center'>";
70         html += "        <input type='submit' value='保存'/>&nbsp;";
71         html += "        <input type='reset' value='重置'/></td>";
72         html += "    </tr>";
73         html += "</table>";
74         html += "</form>";
75         html += "</body>";
76         html += "</html>";
77 
78         
79         
80         writer.write(html);
81     }
82 
83     public void doPost(HttpServletRequest request, HttpServletResponse response)
84             throws ServletException, IOException {
85         doGet(request, response);
86     }
87 
88 }

3.4修改联系人的逻辑

 1 public class UpdateContactServlet extends HttpServlet {
 2 
 3     public void doGet(HttpServletRequest request, HttpServletResponse response)
 4             throws ServletException, IOException {
 5         request.setCharacterEncoding("utf-8");
 6         //1.接收参数
 7         String id = request.getParameter("id");
 8         String name = request.getParameter("name");
 9         String gender = request.getParameter("gender");
10         String age = request.getParameter("age");
11         String phone = request.getParameter("phone");
12         String email = request.getParameter("email");
13         String qq = request.getParameter("qq");
14         
15         //封装成Contact对象
16         Contact contact = new Contact();
17         contact.setId(id);
18         contact.setName(name);
19         contact.setGender(gender);
20         contact.setAge(Integer.parseInt(age));
21         contact.setPhone(phone);
22         contact.setEmail(email);
23         contact.setQq(qq);
24         
25         //2.调用dao修改联系人的方法
26         ContactDao dao = new ContactDaoImpl();
27         dao.updateContact(contact);
28         
29         //3.跳转到查询联系人的页面
30         response.sendRedirect(request.getContextPath()+"/ListContactServlet");
31         
32     }
33 
34     public void doPost(HttpServletRequest request, HttpServletResponse response)
35             throws ServletException, IOException {
36         doGet(request, response);
37     }
38 
39 }

3.5删除联系人的逻辑

 1 public class DeleteContactServlet extends HttpServlet {
 2 
 3     public void doGet(HttpServletRequest request, HttpServletResponse response)
 4             throws ServletException, IOException {
 5         //在火狐浏览器中以Get方式提交带参数的数据,会重复提交两次。
 6         System.out.println("删除联系人");
 7         //1.接收id
 8         String id = request.getParameter("id");
 9         
10         //2.调用dao删除联系人的方法
11         ContactDao dao = new ContactDaoImpl();
12         dao.deleteContact(id);
13         
14         //3.跳转到查询联系人的页面
15         response.sendRedirect(request.getContextPath()+"/ListContactServlet");
16     }
17 
18     public void doPost(HttpServletRequest request, HttpServletResponse response)
19             throws ServletException, IOException {
20         doGet(request, response);
21     }
22 
23 }

 

 

功能测试(测试攻城狮)

 

性能测试(测试攻城狮)

 

部署上线(实施攻城狮) 

 

维护阶段(实施攻城狮)

 

posted @ 2016-05-02 17:03  双子座的皮卡丘  阅读(271)  评论(0编辑  收藏  举报