4)车票管理系统 一车站每天有n个发车班次,每个班次都有一班此号(1,2,3,....n),固定的发车时间,固定的路线(起始站,终点站),大致的行车时间,固定的额定载客量。 (1)班次信息的增删改查,支持导入班次信息(格式自定义) (2)浏览班次信息可显示所有班次当前状况,如果当前系时间超过了耨班次的发车时间,则显示“此班已发出”的提示信息。 (3)查询路线:可按班次号查询,可按终点站查询 (4)统计售票情况 (5)售票和退票功能 (6)支持用户导出购票情况设计环境为Windows+Eclipse+JDK+Tomcat+MySQL用代码实现
时间: 2024-01-10 19:03:27 浏览: 100
以下是一个简单的示例代码,使用了Java语言和MySQL数据库:
1. 数据库设计
```sql
CREATE TABLE `ticket` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`train_number` varchar(20) NOT NULL,
`start_station` varchar(50) NOT NULL,
`end_station` varchar(50) NOT NULL,
`departure_time` datetime NOT NULL,
`estimated_time` int(11) NOT NULL,
`capacity` int(11) NOT NULL,
`sold_tickets` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
);
```
2. 后端Java代码
Ticket.java
```java
public class Ticket {
private int id;
private String trainNumber;
private String startStation;
private String endStation;
private Date departureTime;
private int estimatedTime;
private int capacity;
private int soldTickets;
// 构造函数、getter和setter方法省略
}
```
TicketDAO.java
```java
public class TicketDAO {
private Connection conn;
public TicketDAO() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/ticket_management";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
}
public List<Ticket> getAllTickets() {
List<Ticket> tickets = new ArrayList<Ticket>();
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM ticket");
while (rs.next()) {
Ticket ticket = new Ticket();
ticket.setId(rs.getInt("id"));
ticket.setTrainNumber(rs.getString("train_number"));
ticket.setStartStation(rs.getString("start_station"));
ticket.setEndStation(rs.getString("end_station"));
ticket.setDepartureTime(rs.getDate("departure_time"));
ticket.setEstimatedTime(rs.getInt("estimated_time"));
ticket.setCapacity(rs.getInt("capacity"));
ticket.setSoldTickets(rs.getInt("sold_tickets"));
tickets.add(ticket);
}
rs.close();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
return tickets;
}
public List<Ticket> getTicketsByTrainNumber(String trainNumber) {
List<Ticket> tickets = new ArrayList<Ticket>();
try {
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM ticket WHERE train_number = ?");
stmt.setString(1, trainNumber);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Ticket ticket = new Ticket();
ticket.setId(rs.getInt("id"));
ticket.setTrainNumber(rs.getString("train_number"));
ticket.setStartStation(rs.getString("start_station"));
ticket.setEndStation(rs.getString("end_station"));
ticket.setDepartureTime(rs.getDate("departure_time"));
ticket.setEstimatedTime(rs.getInt("estimated_time"));
ticket.setCapacity(rs.getInt("capacity"));
ticket.setSoldTickets(rs.getInt("sold_tickets"));
tickets.add(ticket);
}
rs.close();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
return tickets;
}
public List<Ticket> getTicketsByEndStation(String endStation) {
List<Ticket> tickets = new ArrayList<Ticket>();
try {
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM ticket WHERE end_station = ?");
stmt.setString(1, endStation);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Ticket ticket = new Ticket();
ticket.setId(rs.getInt("id"));
ticket.setTrainNumber(rs.getString("train_number"));
ticket.setStartStation(rs.getString("start_station"));
ticket.setEndStation(rs.getString("end_station"));
ticket.setDepartureTime(rs.getDate("departure_time"));
ticket.setEstimatedTime(rs.getInt("estimated_time"));
ticket.setCapacity(rs.getInt("capacity"));
ticket.setSoldTickets(rs.getInt("sold_tickets"));
tickets.add(ticket);
}
rs.close();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
return tickets;
}
public void addTicket(Ticket ticket) {
try {
PreparedStatement stmt = conn.prepareStatement("INSERT INTO ticket (train_number, start_station, end_station, departure_time, estimated_time, capacity, sold_tickets) VALUES (?, ?, ?, ?, ?, ?, ?)");
stmt.setString(1, ticket.getTrainNumber());
stmt.setString(2, ticket.getStartStation());
stmt.setString(3, ticket.getEndStation());
stmt.setDate(4, new java.sql.Date(ticket.getDepartureTime().getTime()));
stmt.setInt(5, ticket.getEstimatedTime());
stmt.setInt(6, ticket.getCapacity());
stmt.setInt(7, ticket.getSoldTickets());
stmt.executeUpdate();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void updateTicket(Ticket ticket) {
try {
PreparedStatement stmt = conn.prepareStatement("UPDATE ticket SET train_number = ?, start_station = ?, end_station = ?, departure_time = ?, estimated_time = ?, capacity = ?, sold_tickets = ? WHERE id = ?");
stmt.setString(1, ticket.getTrainNumber());
stmt.setString(2, ticket.getStartStation());
stmt.setString(3, ticket.getEndStation());
stmt.setDate(4, new java.sql.Date(ticket.getDepartureTime().getTime()));
stmt.setInt(5, ticket.getEstimatedTime());
stmt.setInt(6, ticket.getCapacity());
stmt.setInt(7, ticket.getSoldTickets());
stmt.setInt(8, ticket.getId());
stmt.executeUpdate();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void deleteTicket(int id) {
try {
PreparedStatement stmt = conn.prepareStatement("DELETE FROM ticket WHERE id = ?");
stmt.setInt(1, id);
stmt.executeUpdate();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void sellTicket(int id) {
try {
PreparedStatement stmt = conn.prepareStatement("UPDATE ticket SET sold_tickets = sold_tickets + 1 WHERE id = ?");
stmt.setInt(1, id);
stmt.executeUpdate();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void refundTicket(int id) {
try {
PreparedStatement stmt = conn.prepareStatement("UPDATE ticket SET sold_tickets = sold_tickets - 1 WHERE id = ?");
stmt.setInt(1, id);
stmt.executeUpdate();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void exportTicketSales() {
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM ticket");
FileWriter writer = new FileWriter("ticket_sales.csv");
writer.append("id,train_number,start_station,end_station,departure_time,estimated_time,capacity,sold_tickets\n");
while (rs.next()) {
writer.append(String.valueOf(rs.getInt("id")));
writer.append(",");
writer.append(rs.getString("train_number"));
writer.append(",");
writer.append(rs.getString("start_station"));
writer.append(",");
writer.append(rs.getString("end_station"));
writer.append(",");
writer.append(rs.getDate("departure_time").toString());
writer.append(",");
writer.append(String.valueOf(rs.getInt("estimated_time")));
writer.append(",");
writer.append(String.valueOf(rs.getInt("capacity")));
writer.append(",");
writer.append(String.valueOf(rs.getInt("sold_tickets")));
writer.append("\n");
}
writer.flush();
writer.close();
rs.close();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
TicketController.java
```java
public class TicketController extends HttpServlet {
private TicketDAO ticketDAO;
public void init() {
ticketDAO = new TicketDAO();
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if (action == null) {
action = "list";
}
switch (action) {
case "list":
listTickets(request, response);
break;
case "add":
addTicket(request, response);
break;
case "edit":
editTicket(request, response);
break;
case "update":
updateTicket(request, response);
break;
case "delete":
deleteTicket(request, response);
break;
case "sell":
sellTicket(request, response);
break;
case "refund":
refundTicket(request, response);
break;
case "export":
exportTicketSales(request, response);
break;
default:
listTickets(request, response);
break;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
private void listTickets(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Ticket> tickets = ticketDAO.getAllTickets();
request.setAttribute("tickets", tickets);
request.getRequestDispatcher("list_tickets.jsp").forward(request, response);
}
private void addTicket(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Ticket ticket = new Ticket();
ticket.setTrainNumber(request.getParameter("train_number"));
ticket.setStartStation(request.getParameter("start_station"));
ticket.setEndStation(request.getParameter("end_station"));
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date departureTime = dateFormat.parse(request.getParameter("departure_time"));
ticket.setDepartureTime(departureTime);
} catch (Exception e) {
e.printStackTrace();
}
ticket.setEstimatedTime(Integer.parseInt(request.getParameter("estimated_time")));
ticket.setCapacity(Integer.parseInt(request.getParameter("capacity")));
ticket.setSoldTickets(Integer.parseInt(request.getParameter("sold_tickets")));
ticketDAO.addTicket(ticket);
response.sendRedirect("ticket?action=list");
}
private void editTicket(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Ticket ticket = ticketDAO.getTicketById(id);
request.setAttribute("ticket", ticket);
request.getRequestDispatcher("edit_ticket.jsp").forward(request, response);
}
private void updateTicket(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Ticket ticket = new Ticket();
ticket.setId(Integer.parseInt(request.getParameter("id")));
ticket.setTrainNumber(request.getParameter("train_number"));
ticket.setStartStation(request.getParameter("start_station"));
ticket.setEndStation(request.getParameter("end_station"));
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm
阅读全文