Java实现停车场管理系统攻略
1.需求分析
停车场管理系统需要实现以下功能:
- 停车:可以记录车辆的停放时间和位置(车位号)
- 取车:可以计算车辆停放的费用并将车位号记录,同时从停车记录中删除该车辆
- 车位管理:对车位进行增删改查,可以查询所有车位和空闲车位
- 停车记录查询:可以查询所有停车记录以及某个时间段的停车记录
2.数据库设计
使用MySQL数据库存储停车场管理系统的相关数据:
- vehicle_record表:记录所有车辆的停车信息,包含车牌号、停车开始时间、停车车位号、停车费用等字段。
- parking_space表:记录所有车位的信息,包含车位号、该车位是否空闲等字段。
3.代码实现
3.1 数据库连接与操作
使用JDBC连接和操作MySQL数据库,具体步骤如下:
- 引入MySQL JDBC驱动jar包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
- 建立数据库连接,获取Connection对象
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/parking_system";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
}
- 定义公共的执行SQL语句的方法并获取PreparedStatement对象
public class DBUtil {
// 省略getConnection()方法
public static PreparedStatement getPreparedStatement(String sql) throws SQLException {
Connection conn = getConnection();
return conn.prepareStatement(sql);
}
}
3.2 实现停车功能
当有车辆进入停车场时,调用以下方法记录他的停车信息:
public class ParkingService {
public void park(String plate, String spaceNum) {
// 获取停车开始时间
Date beginTime = new Date();
// 将车位设置为占用状态
setSpaceStatus(spaceNum, 1);
// 保存停车记录
saveVehicleRecord(plate, beginTime, spaceNum);
}
}
3.3 实现取车功能
当有车辆驶离停车场时,调用以下方法计算停车费用并记录车位号:
public class ParkingService {
public void checkout(String plate, String spaceNum) {
// 获取停车开始时间
Date beginTime = getBeginTimeByPlate(plate);
// 计算停车费用
double cost = calculateCost(beginTime);
// 将车位设置为空闲状态
setSpaceStatus(spaceNum, 0);
// 记录车位号
updateSpaceNumByPlate(plate, spaceNum);
// 删除停车记录
deleteVehicleRecord(plate);
}
}
3.4 实现车位管理功能
对车位进行增删改查操作,具体实现如下:
- 增加车位
public class ParkingSpaceService {
public void addSpace(String spaceNum) {
String sql = "INSERT INTO parking_space (space_num, status) VALUES (?, ?)";
try (PreparedStatement ps = DBUtil.getPreparedStatement(sql)) {
ps.setString(1, spaceNum);
ps.setInt(2, 0); // 新增车位默认为空闲状态
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
- 删除车位
public class ParkingSpaceService {
public void deleteSpace(String spaceNum) {
String sql = "DELETE FROM parking_space WHERE space_num = ?";
try (PreparedStatement ps = DBUtil.getPreparedStatement(sql)) {
ps.setString(1, spaceNum);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
- 修改车位状态
public class ParkingSpaceService {
public void updateStatus(String spaceNum, int status) {
String sql = "UPDATE parking_space SET status = ? WHERE space_num = ?";
try (PreparedStatement ps = DBUtil.getPreparedStatement(sql)) {
ps.setInt(1, status);
ps.setString(2, spaceNum);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
- 查询车位
public class ParkingSpaceService {
public List<ParkingSpace> getAllSpaces() {
String sql = "SELECT space_num, status FROM parking_space";
List<ParkingSpace> parkingSpaces = new ArrayList<>();
try (PreparedStatement ps = DBUtil.getPreparedStatement(sql)) {
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
String spaceNum = rs.getString("space_num");
int status = rs.getInt("status");
parkingSpaces.add(new ParkingSpace(spaceNum, status));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return parkingSpaces;
}
}
3.5 实现停车记录查询功能
实现查询所有停车记录和某个时间段停车记录的方法如下:
- 查询所有停车记录
public class VehicleRecordService {
public List<VehicleRecord> getAllRecords() {
String sql = "SELECT plate, begin_time, space_num, cost FROM vehicle_record";
List<VehicleRecord> vehicleRecords = new ArrayList<>();
try (PreparedStatement ps = DBUtil.getPreparedStatement(sql)) {
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
String plate = rs.getString("plate");
Date beginTime = rs.getTime("begin_time");
String spaceNum = rs.getString("space_num");
double cost = rs.getDouble("cost");
vehicleRecords.add(new VehicleRecord(plate, beginTime, spaceNum, cost));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return vehicleRecords;
}
}
- 查询某个时间段停车记录
public class VehicleRecordService {
public List<VehicleRecord> getRecordsByTime(Date startTime, Date endTime) {
String sql = "SELECT plate, begin_time, space_num, cost FROM vehicle_record WHERE begin_time BETWEEN ? AND ?";
List<VehicleRecord> vehicleRecords = new ArrayList<>();
try (PreparedStatement ps = DBUtil.getPreparedStatement(sql)) {
ps.setTime(1, new java.sql.Time(startTime.getTime()));
ps.setTime(2, new java.sql.Time(endTime.getTime()));
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
String plate = rs.getString("plate");
Date beginTime = rs.getTime("begin_time");
String spaceNum = rs.getString("space_num");
double cost = rs.getDouble("cost");
vehicleRecords.add(new VehicleRecord(plate, beginTime, spaceNum, cost));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return vehicleRecords;
}
}
4.示例说明
以下为停车场管理系统的两个示例说明:
示例1:进入停车场
当车辆进入停车场时,调用park()方法记录停车信息。例如,当一辆车进入停车场,并且空闲车位为"A01"时,可以这样调用方法:
ParkingService parkingService = new ParkingService();
parkingService.park("京A12345", "A01");
示例2:查询停车记录
可以调用VehicleRecordService类的getAllRecords()方法查询所有停车记录,例如:
VehicleRecordService vehicleRecordService = new VehicleRecordService();
List<VehicleRecord> records = vehicleRecordService.getAllRecords();
for (VehicleRecord record : records) {
System.out.println(record);
}
输出结果如下:
VehicleRecord{plate='京A12345', beginTime=09:30:15, spaceNum='A01', cost=2.5}
VehicleRecord{plate='京A67890', beginTime=11:20:10, spaceNum='B02', cost=4.5}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java实现停车场管理系统 - Python技术站