Java理发店会员充值卡消费管理小程序编写代码。运用Java Web 要求: (1)使用图形用户界面设计, 结合MySQL数据; (2)可以针对理发店与浴池为例进行设计充值卡的充值与消费管理。 (3)本题重点在数据库的表的建立与管理,管理主要完成增、删、改、查 的四大功能
时间: 2024-02-12 14:03:27 浏览: 101
由于本题要求使用Java Web,因此需要使用Java Servlet和JSP技术实现。以下是可能的代码实现:
1. 创建数据库表格:
```sql
CREATE TABLE member (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
phone VARCHAR(20) NOT NULL,
balance DOUBLE DEFAULT 0
);
CREATE TABLE recharge_card (
id INT PRIMARY KEY AUTO_INCREMENT,
card_no VARCHAR(20) NOT NULL,
balance DOUBLE DEFAULT 0
);
CREATE TABLE consume (
id INT PRIMARY KEY AUTO_INCREMENT,
member_id INT NOT NULL,
card_id INT NOT NULL,
amount DOUBLE NOT NULL,
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```
2. 创建Java Servlet类实现增删改查功能:
```java
@WebServlet("/member")
public class MemberServlet extends HttpServlet {
private MemberDAO memberDAO = new MemberDAOImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if (action == null) {
List<Member> members = memberDAO.list();
request.setAttribute("members", members);
request.getRequestDispatcher("member.jsp").forward(request, response);
} else if (action.equals("add")) {
request.getRequestDispatcher("member_add.jsp").forward(request, response);
} else if (action.equals("edit")) {
int id = Integer.parseInt(request.getParameter("id"));
Member member = memberDAO.get(id);
request.setAttribute("member", member);
request.getRequestDispatcher("member_edit.jsp").forward(request, response);
} else if (action.equals("delete")) {
int id = Integer.parseInt(request.getParameter("id"));
memberDAO.delete(id);
response.sendRedirect("member");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if (action == null) {
String name = request.getParameter("name");
String phone = request.getParameter("phone");
memberDAO.add(name, phone);
response.sendRedirect("member");
} else if (action.equals("edit")) {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String phone = request.getParameter("phone");
memberDAO.update(id, name, phone);
response.sendRedirect("member");
}
}
}
```
3. 创建JSP页面实现图形用户界面:
member.jsp:
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>会员管理</title>
</head>
<body>
<h1>会员管理</h1>
<table border="1">
<tr>
<th>ID</th>
<th>姓名</th>
<th>电话</th>
<th>余额</th>
<th>操作</th>
</tr>
<c:forEach items="${members}" var="member">
<tr>
<td>${member.getId()}</td>
<td>${member.getName()}</td>
<td>${member.getPhone()}</td>
<td>${member.getBalance()}</td>
<td>
<a href="member?action=edit&id=${member.getId()}">编辑</a>
<a href="member?action=delete&id=${member.getId()}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<br>
<a href="member?action=add">添加会员</a>
</body>
</html>
```
member_add.jsp:
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加会员</title>
</head>
<body>
<h1>添加会员</h1>
<form action="member" method="post">
<input type="hidden" name="action" value="add">
姓名:<input type="text" name="name"><br>
电话:<input type="text" name="phone"><br>
<input type="submit" value="添加">
</form>
</body>
</html>
```
member_edit.jsp:
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>编辑会员</title>
</head>
<body>
<h1>编辑会员</h1>
<form action="member" method="post">
<input type="hidden" name="action" value="edit">
<input type="hidden" name="id" value="${member.getId()}">
姓名:<input type="text" name="name" value="${member.getName()}"><br>
电话:<input type="text" name="phone" value="${member.getPhone()}"><br>
<input type="submit" value="保存">
</form>
</body>
</html>
```
4. 实现充值和消费功能类似于上述步骤2和步骤3的代码实现,只是需要调用RechargeCardDAO和ConsumeDAO。
阅读全文