用Java语言设计一个公交管理系统,要求可以实现管理员可以添加、修改、删除公交线路、站点、车辆等信息。 用户可以查询公交线路、站点、车辆等信息
时间: 2023-08-09 15:10:18 浏览: 45
好的,以下是一个简单的公交管理系统的Java代码实现,主要实现管理员可以添加、修改、删除公交线路、站点、车辆等信息,用户可以查询公交线路、站点、车辆等信息的功能:
1. 首先,我们需要定义一些基本的数据结构,如公交线路、站点、车辆等信息的类:
```java
// 公交线路类
public class BusLine {
private String lineName; // 线路名称
private List<BusStation> stations; // 站点列表
// 构造函数
public BusLine(String lineName) {
this.lineName = lineName;
this.stations = new ArrayList<>();
}
// 添加站点
public void addStation(BusStation station) {
stations.add(station);
}
// 删除站点
public void removeStation(BusStation station) {
stations.remove(station);
}
// 获取站点列表
public List<BusStation> getStations() {
return stations;
}
}
// 公交站点类
public class BusStation {
private String name; // 站点名称
private double longitude; // 经度
private double latitude; // 纬度
// 构造函数
public BusStation(String name, double longitude, double latitude) {
this.name = name;
this.longitude = longitude;
this.latitude = latitude;
}
// 获取站点名称
public String getName() {
return name;
}
// 获取经度
public double getLongitude() {
return longitude;
}
// 获取纬度
public double getLatitude() {
return latitude;
}
}
// 公交车辆类
public class Bus {
private String plateNumber; // 车牌号
private BusLine line; // 所属线路
private int capacity; // 容量
private List<BusStation> currentRoute; // 当前行驶的路线
private int currentPosition; // 当前位置
// 构造函数
public Bus(String plateNumber, BusLine line, int capacity) {
this.plateNumber = plateNumber;
this.line = line;
this.capacity = capacity;
this.currentRoute = new ArrayList<>();
this.currentPosition = 0;
}
// 获取车牌号
public String getPlateNumber() {
return plateNumber;
}
// 获取所属线路
public BusLine getLine() {
return line;
}
// 获取容量
public int getCapacity() {
return capacity;
}
// 获取当前路线
public List<BusStation> getCurrentRoute() {
return currentRoute;
}
// 获取当前位置
public int getCurrentPosition() {
return currentPosition;
}
// 设置当前路线
public void setCurrentRoute(List<BusStation> currentRoute) {
this.currentRoute = currentRoute;
}
// 设置当前位置
public void setCurrentPosition(int currentPosition) {
this.currentPosition = currentPosition;
}
}
```
2. 接下来,我们需要实现一个公交管理系统类,用于管理公交线路、站点、车辆等信息的添加、修改、删除和查询功能:
```java
public class BusManagementSystem {
private List<BusLine> busLines; // 线路列表
private List<BusStation> busStations; // 站点列表
private List<Bus> buses; // 车辆列表
// 构造函数
public BusManagementSystem() {
this.busLines = new ArrayList<>();
this.busStations = new ArrayList<>();
this.buses = new ArrayList<>();
}
// 添加线路
public void addBusLine(BusLine busLine) {
busLines.add(busLine);
}
// 删除线路
public void removeBusLine(BusLine busLine) {
busLines.remove(busLine);
}
// 添加站点
public void addBusStation(BusStation busStation) {
busStations.add(busStation);
}
// 删除站点
public void removeBusStation(BusStation busStation) {
busStations.remove(busStation);
}
// 添加车辆
public void addBus(Bus bus) {
buses.add(bus);
}
// 删除车辆
public void removeBus(Bus bus) {
buses.remove(bus);
}
// 查询线路
public BusLine getBusLine(String lineName) {
for (BusLine busLine : busLines) {
if (busLine.getLineName().equals(lineName)) {
return busLine;
}
}
return null;
}
// 查询站点
public BusStation getBusStation(String stationName) {
for (BusStation busStation : busStations) {
if (busStation.getName().equals(stationName)) {
return busStation;
}
}
return null;
}
// 查询车辆
public Bus getBus(String plateNumber) {
for (Bus bus : buses) {
if (bus.getPlateNumber().equals(plateNumber)) {
return bus;
}
}
return null;
}
}
```
以上是一个简单的公交管理系统的Java代码实现,可以实现管理员可以添加、修改、删除公交线路、站点、车辆等信息,用户可以查询公交线路、站点、车辆等信息的功能。需要注意的是,为了简化代码实现,我在以上代码中省略了一些必要的函数和参数,具体实现还需要根据实际情况进行调整和完善。