任务描述
本项目使用图形用户界面与数据库技术,完成公交查询系统。
本项目主要有4个功能:线路查询、站点查询、换乘查询和后台管理。
(1)线路查询:获得需要查询的公交的信息。
(2)站点查询:获得通过输入的经过该站点的指公交信息。
(3)换乘查询:判断公交直达或公交一次换乘,并获得所有换乘方法。
(4)后台管理:管理员登录,添加、删除、修改公交线路功能。
程序需要提取各公交信息,文件存储信息的形式如下:
1s%……站%……站%……站%6:00-18:00 票价1元 ABCD卡有效
1x%……站%……站%……站%6:00-18:00 票价1元 ABCD卡有效
……
其中1s为上行,1x为下行,站点信息之间使用“%”分割。
程序需要存储各公交信息,数据库存储信息的形式如下:
route,station,time,price,card。其中station为text格式,并且站点之间使用%隔开。
注意:请自行下载数据库连接文件,使用前需要提前建立数据表,路径需要替换。
需求分析
公交查询系统需要将公交信息文件提取到数据库中,然后将数据库中的数据提取转化使用。需要对线路信息进行抽象,创建类busInformation,成员变量包括route、station、time、price、card和相应的get方法和set方法。由类busInformationTransform实现数据提取转化判断保存功能,成员方法包括extract、transform、tableIsEmpty、save。由类busEnquirySystem实现用户端功能,成员方法包括exist、routeInquiry、stationInquiry、transferInquiry。由类backEndManagement实现后台端管理员功能,成员方法包括exist、index、addBusInformation、updateBusInformation、deleteBusInformation。由类GUI实现图形用户界面功能,成员方法包括main。
设计思路
公交查询系统首先将公交信息文件中的数据分割提取存储到数据库中,然后将数据库中的数据提取到ArrayList中供用户端功能和后台端管理员功能使用,线路信息需要进行抽象。设计GUI界面,结合用户端供能和后台端管理员功能。
对数据库中的数据分割提取并使用ArrayList存储。创建类busInformation存储,包含路线号、站点名、路线时间、路线票价、路线公交卡,存储于ArrayList中,其中站点名由AraayList存储。
数据提取转化判断保存功能由类busInformationTransform中的成员方法实现。包含成员方法如下:
(1)extract将公交信息文件中的数据提取到数据库中。先连接数据库,创建输入流,读取公交信息文件的每一行,将文件中的数据分割提取按设定的表格形式存储到数据库中。
(2)transform将数据库的数据中转化到ArrayList中。先连接数据库,提取数据库每一行并进行分割存放,创建busInformation实例,将数据按要求存放其中,然后存储到ArrayList中。
(3)tableIsEmpty判断公交信息文件中的数据在数据库中是否存在。连接数据库,执行SQL查询语句,统计数据表中的记录数,判断数据库数据是否为空。
(4)save将修改后的ArrayList的数据同步保存到数据库中。当对Array List中的数据进行修改后,将数据库中的数据清空替换。
用户端功能由类busEnquirySystem中的成员方法实现,各成员方法对公交线路信息的操作均为对ArrayList的操作。包含成员方法如下:
(1)exist使用循环遍历判断公交线路信息是否存在,是否重复。
(2)routeInquiry线路查询。接收并判断公交线路号,循环遍历获得要查询的公交线路的信息。
(3)stationInquiry站点查询。接收并判断公交线路站点,循环遍历指定站点查询经过该站点的所有公交线路。
(4)transferInquiry换乘查询。接收并判断公交线路起点和终点,判断直达与公交一次换乘,并输出相应线路。
后台端管理员功能由类backEndManagement中的成员方法实现,包含管理员账号密码验证和后台端管理员功能操作界面,各成员方法对公交线路信息的操作均为对文件的操作。包含成员方法如下:
(1)exist使用循环遍历判断公交线路信息是否存在,是否重复。
(2)index使用循环遍历判断公交线路在ArrayList中的索引。
(3)addBusInformation添加公交线路。接收并判断公交线路号和公交线路信息,将输入内容写入ArrayList。
(4)updateBusInformation修改公交线路。接收并判断公交线路号和公交线路信息,修改文件某一行,将文件信息按行拆分进行操作,然后将修改后的内容写回ArrayList。
(6)deleteBusInformation删除公交线路。接收并判断公交线路号,将文件信息按行拆分进行操作,删除文件某一行,然后将修改后的内容写回ArrayList。
GUI界面功能由类GUI中的成员方法实现,结合结合用户端供能和后台端管理员功能。包含成员方法如下:
(1)main调用所有功能方法
实现过程
(1)对公交路线信息进行抽象建类
public class busInformation {
//线路
public String route;
//站点
public ArrayList<String> station;
//ArrayList类是一个可以动态修改的数组,没有固定大小的限制,继承了AbstractList并实现了List接口
//时间
public String time;
//价格
public String price;
//卡
public String card;
//get与set方法
public String getRoute() { return route; }
public void setRoute(String route) { this.route = route; }
public ArrayList<String> getStation() { return station; }
public void setStation(ArrayList<String> station) { this.station = station; }
public String getTime() { return time; }
public void setTime(String time) { this.time = time; }
public String getPrice() { return price; }
public void setPrice(String price) { this.price = price; }
public String getCard() { return card; }
public void setCard(String card) { this.card = card; }
}
(2)数据处理
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
public class busInformationTransform {
//将公交信息文件中的数据提取到数据库中
public static void extract() throws ClassNotFoundException, SQLException, IOException {
//连接数据库
Class.forName("com.mysql.cj.jdbc.Driver");
//使用Class类的forName(driverClass)静态方法加载数据库驱动
//driverClass是数据库驱动类所对应的字符串,加载MySQL的驱动采用的字符串为"com.mysql.cj.jdbc.Driver"
String url = "jdbc:mysql://localhost:3306/busInformation";
//url访问数据库的路径,数据库URL规则遵循"jdbc:sub_protocol:sub_name"
//请替换为自己的数据库路径
Connection connection = DriverManager.getConnection(url, "root", "root");
//加载数据库驱动后,通过DriverManager提供的getConnection()方法获取数据库连接,并返回该数据库的Connection对象
//创建输入流
String filePath = "src/busDatabase/busInformation.txt";
FileInputStream fileInputStream = new FileInputStream(filePath);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line_f;
while ((line_f = bufferedReader.readLine()) != null) {
String[] split = line_f.split("[ %]+");
String sql = "REPLACE INTO information (route, station, time, price, card) VALUES (?, ?, ?, ?, ?)";
//REPLACE语句将在冲突发生时替换现有行
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//PreparedStatement用于执行预编译SQL语句的对象
preparedStatement.setString(1, split[0]);
preparedStatement.setString(2, String.join("%", Arrays.copyOfRange(split, 1, split.length - 4)));
preparedStatement.setString(3, split[split.length - 3]);
preparedStatement.setString(4, split[split.length - 2]);
preparedStatement.setString(5, split[split.length - 1]);
preparedStatement.executeUpdate();
}
connection.close();
}
//将数据库的数据中转化到ArrayList中
public static ArrayList transform() throws ClassNotFoundException, SQLException {
ArrayList<busInformation> array = new ArrayList<>();
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/busInformation";
Connection connection = DriverManager.getConnection(url, "root", "root");
Statement statement = connection.createStatement();
ResultSet line_d = statement.executeQuery("SELECT * FROM information");
//遍历结果,将数据转化到ArrayList中
while (line_d.next()) {
busInformation busInformation = new busInformation();
busInformation.setRoute(line_d.getString("route"));
String[] station = line_d.getString("station").split("%");
ArrayList<String> setStation = new ArrayList<>(Arrays.asList(station));
busInformation.setStation(setStation);
busInformation.setTime(line_d.getString("time"));
busInformation.setPrice(line_d.getString("price"));
busInformation.setCard(line_d.getString("card"));
array.add(busInformation);
}
statement.close();
connection.close();
return array;
}
//判断公交信息文件中的数据在数据库中是否存在
public static int tableIsEmpty() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/busInformation";
Connection connection = DriverManager.getConnection(url, "root", "root");
String sql = "SELECT COUNT(*) FROM " + "information";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
resultSet.next();
int tableIsEmpty = resultSet.getInt(1);
resultSet.close();
statement.close();
connection.close();
return tableIsEmpty;
}
//将修改后的ArrayList的数据同步保存到数据库中
public static void save(ArrayList<busInformation> array) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/busInformation";
Connection connection = DriverManager.getConnection(url, "root", "root");
String truncateSql = "TRUNCATE TABLE information";
PreparedStatement truncateStatement = connection.prepareStatement(truncateSql);
truncateStatement.executeUpdate();
for (busInformation i : array) {
String sql = "REPLACE INTO information (route, station, time, price, card) VALUES (?, ?, ?, ?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, i.getRoute());
preparedStatement.setString(2, String.join("%", i.getStation()));
preparedStatement.setString(3, i.getTime());
preparedStatement.setString(4, i.getPrice());
preparedStatement.setString(5, i.getCard());
preparedStatement.executeUpdate();
}
connection.close();
}
}
(3)用户端功能
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Objects;
public class busEnquirySystem {
//判断存在
public static int exist(ArrayList<busInformation> array, String route) {
int exist = 0;
//此公交路线不存在
//使用线路号进行判断
for (busInformation i :array) {
if (Objects.equals(i.getRoute(), route)) {
exist = 1;
//此公交路线存在
break;
}
//使用站点进行判断
for (String j : i.getStation()) {
if (Objects.equals(j, route)) {
exist = 1;
//此公交路线存在
break;
}
}
}
return exist;
}
//线路查询:获得需要查询的公交的信息
public static void routeInquiry(ArrayList<busInformation> array, String route, JFrame jFrame) {
if (exist(array, route) == 0) {
JOptionPane.showMessageDialog(jFrame, "公交路线号不存在,请重新操作!", "输入错误", JOptionPane.ERROR_MESSAGE);
} else {
StringBuilder result = new StringBuilder();
//StringBuilder用于处理可变的字符串
for (busInformation i : array) {
if (Objects.equals(i.getRoute(), route)) {
result.append("站点:").append(i.getStation()).append("\n");
result.append("时间:").append(i.getTime()).append("\n");
result.append("票价:").append(i.getPrice()).append("\n");
result.append("公交卡:").append(i.getCard()).append("\n");
}
}
//创建一个带有滚动条的文本区域并将其添加到一个滚动面板中
JTextArea textArea = new JTextArea(result.toString());
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(1000, 300));
JOptionPane.showMessageDialog(jFrame, scrollPane);
}
}
//站点查询:获得通过输入的经过该站点的指公交信息
public static void stationInquiry(ArrayList<busInformation> array, String choice, JFrame frame) {
if (exist(array, choice) == 0) {
JOptionPane.showMessageDialog(frame, "公交站点名不存在,请重新操作!", "输入错误", JOptionPane.ERROR_MESSAGE);
} else {
StringBuilder result = new StringBuilder();
for (busInformation i : array) {
for (String j : i.getStation()) {
if (Objects.equals(j, choice)) {
result.append("路线号:").append(i.getRoute()).append("\n");
result.append("站点:").append(i.getStation()).append("\n");
result.append("时间:").append(i.getTime()).append("\n");
result.append("票价:").append(i.getPrice()).append("\n");
result.append("公交卡:").append(i.getCard()).append("\n");
}
}
}
JTextArea textArea = new JTextArea(result.toString());
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(1000, 600));
JOptionPane.showMessageDialog(frame, scrollPane);
}
}
//换乘查询:判断公交直达或公交一次换乘,并获得所有换乘方法
public static void transferInquiry(ArrayList<busInformation> array, String station_1, String station_2, JFrame jFrame) {
if (exist(array, station_1) == 0) {
JOptionPane.showMessageDialog(jFrame, "起点公交站点名不存在,请重新操作!", "输入错误", JOptionPane.ERROR_MESSAGE);
} else {
if (exist(array, station_2) == 0) {
JOptionPane.showMessageDialog(jFrame, "终点公交站点名不存在,请重新操作!", "输入错误", JOptionPane.ERROR_MESSAGE);
} else {
//标记区分直达和一次换乘
boolean transferInquiry = false;
//公交一次换乘
ArrayList<String> station = new ArrayList<>();
ArrayList<busInformation> station_t1 = new ArrayList<>();
ArrayList<busInformation> station_t2 = new ArrayList<>();
ArrayList<String> a = new ArrayList<>();
ArrayList<String> b = new ArrayList<>();
for (busInformation i : array) {
if (i.getStation().contains(station_1) && i.getStation().contains(station_2)) {
transferInquiry = true;
//公交直达
station.add(i.getRoute());
break;
}
}
if (transferInquiry) {
JTextArea textArea = new JTextArea("直达线路:\n" + station);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(250, 300));
JOptionPane.showMessageDialog(jFrame, scrollPane);
}
if (! transferInquiry) {
for (busInformation i : array) {
for (String j : i.getStation()) {
if (Objects.equals(j, station_1)) {
station_t1.add(i);
}
}
}
for (busInformation i : array) {
for (String j : i.getStation()) {
if (Objects.equals(j, station_2)) {
station_t2.add(i);
}
}
}
for (busInformation i : station_t1) {
for (String j : i.getStation()) {
for (busInformation k : station_t2) {
for (String l : k.getStation()) {
if (Objects.equals(j, l)) {
a.add(i.getRoute());
b.add(k.getRoute());
}
}
}
}
}
StringBuilder transferInfo = new StringBuilder();
for (int i = 0; i < a.size(); i++) {
transferInfo.append("方案").append(i + 1).append(": ").append(a.get(i)).append("转").append(b.get(i)).append("\n");
}
JTextArea textArea = new JTextArea("换乘一次线路:\n" + transferInfo);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(250, 300));
JOptionPane.showMessageDialog(jFrame, scrollPane);
}
}
}
}
}
(4)后台端管理员功能
import javax.swing.*;
import java.util.*;
public class backEndManagement {
//判断公交线路在ArrayList中是否存在
public static int exist(ArrayList<busInformation> array, String route) {
int exist = 0;
//此公交路线不存在
for (busInformation i :array) {
if (Objects.equals(i.getRoute(), route)) {
exist = 1;
//此公交路线存在
break;
}
}
return exist;
}
//判断公交线路在ArrayList中的索引
public static int index(ArrayList<busInformation> array, String route) {
for (int i = 0; i < array.size(); i++) {
if (Objects.equals(array.get(i).getRoute(), route)) {
return i;
}
}
return -1;
//公交路线号不存在
}
public static void addBusInformation(ArrayList<busInformation> array, String inputRoute, ArrayList<String> station, String inputTime, String inputPrice, String inputCard, JFrame jFrame_b) {
busInformation busInformation = new busInformation();
if (exist(array, inputRoute) == 1) {
JOptionPane.showMessageDialog(jFrame_b, "公交路线号已存在,请重新操作!", "输入错误", JOptionPane.ERROR_MESSAGE);
} else {
busInformation.setRoute(inputRoute);
busInformation.setStation(station);
busInformation.setTime(inputTime);
busInformation.setPrice(inputPrice);
busInformation.setCard(inputCard);
array.add(busInformation);
JOptionPane.showMessageDialog(jFrame_b, "添加成功!");
}
}
public static void updateBusInformation(ArrayList<busInformation> array, String route, String inputRoute, ArrayList<String> station, String inputTime, String inputPrice, String inputCard, JFrame jFrame_b) {
busInformation busInformation = new busInformation();
int index = index(array, route);
if (index == -1) {
JOptionPane.showMessageDialog(jFrame_b, "公交路线号不存在,请重新操作!", "输入错误", JOptionPane.ERROR_MESSAGE);
} else {
if (exist(array, inputRoute) == 1) {
JOptionPane.showMessageDialog(jFrame_b, "公交路线号已存在,请重新操作!", "输入错误", JOptionPane.ERROR_MESSAGE);
} else {
busInformation.setRoute(inputRoute);
busInformation.setStation(station);
busInformation.setTime(inputTime);
busInformation.setPrice(inputPrice);
busInformation.setCard(inputCard);
array.set(index, busInformation);
//set用来设置数组中指定索引位置的元素值
JOptionPane.showMessageDialog(jFrame_b, "修改成功!");
}
}
}
public static void deleteBusInformation(ArrayList<busInformation> array, String route, JFrame jFrame_b) {
int index = index(array, route);
if (index == -1) {
JOptionPane.showMessageDialog(jFrame_b, "公交路线号不存在,请重新操作!", "输入错误", JOptionPane.ERROR_MESSAGE);
} else {
array.remove(index);
//remove用来移除指定索引位置的元素
JOptionPane.showMessageDialog(jFrame_b, "删除成功!", "输入错误", JOptionPane.ERROR_MESSAGE);
}
}
}
(5)GUI界面设计
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import static busDatabase.backEndManagement.*;
import static busDatabase.busEnquirySystem.*;
import static busDatabase.busInformationTransform.*;
public class GUI {
public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
//异常声明的作用是告诉编译器该方法可能会抛出哪些异常,以便在调用该方法时进行异常处理
//如果一个方法可能会抛出异常,那么在使用该方法的地方就需要捕获并处理这些异常,以避免程序崩溃
//SQLException:当与数据库交互时出现问题时抛出
//ClassNotFoundException:当程序试图加载一个不存在的类时抛出
//IOException:当程序在进行I/O操作出现问题时抛出
//判断公交信息文件中的数据在数据库中是否存在,避免数据反复提取
if (tableIsEmpty() == 0) {
//公交信息文件中的数据在数据库中不存在
//将公交信息文件中的数据提取到数据库中
extract();
}
//将数据库中的数据中转化到ArrayList中
ArrayList<busInformation> arrayList = transform();
//图形用户界面
JFrame jFrame = new JFrame("公交查询系统");
//JFrame用于创建图形用户界面(GUI)窗口,是Java应用程序的主窗口,可以包含其他组件
//通过使用JFrame,可以轻松地创建和管理窗口,设置窗口的大小、位置和可见性等属性
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置JFrame窗口关闭时的操作,当用户点击窗口的关闭按钮时,程序将执行EXIT_ON_CLOSE操作,即退出程序
jFrame.setSize(500, 300);
//设置JFrame窗口的大小
jFrame.setLocationRelativeTo(null);
//设置JFrame窗口的位置,将窗口相对于屏幕的中央位置进行定位
JPanel jPanel = new JPanel(new BorderLayout());
//JPanel用于创建面板。它可以包含其他组件并可以设置布局管理器来控制组件的排列方式
JPanel jPanelTop = new JPanel();
JLabel jLabelTop1 = new JLabel("欢迎使用公交查询系统!", SwingConstants.CENTER);
//JLabel用于创建标签,它可以显示文本、图像或其他组件,并可以设置字体、颜色等样式
JLabel jLabelTop2 = new JLabel("请选择您需要的操作", SwingConstants.CENTER);
jPanelTop.setLayout(new BoxLayout(jPanelTop, BoxLayout.Y_AXIS));
//将jPanelTop的布局管理器设置为BoxLayout,并指定为垂直排列
jPanelTop.add(jLabelTop1);
jPanelTop.add(jLabelTop2);
JPanel buttonBottom = new JPanel(new GridLayout(1, 4));
//创建一个名为buttonBottom的JPanel对象,并使用GridLayout布局管理器进行初始化,将buttonBottom分为1行4列的网格,每个网格中可以放置一个组件
JButton button1 = new JButton("线路查询");
//JButton用于创建按钮,它可以显示文本或图像,并可以响应用户的点击事件
JButton button2 = new JButton("站点查询");
JButton button3 = new JButton("换乘查询");
JButton button4 = new JButton("后台管理");
buttonBottom.add(button1);
buttonBottom.add(button2);
buttonBottom.add(button3);
buttonBottom.add(button4);
jPanel.add(jPanelTop, BorderLayout.NORTH);
jPanel.add(buttonBottom, BorderLayout.SOUTH);
jFrame.add(jPanel);
button1.addActionListener(e -> {
//一个按钮的点击事件监听器,当用户点击时会触发该事件
//e -> {...}表示一个Lambda表达式,它定义了按钮点击事件的处理逻辑
String route = JOptionPane.showInputDialog(jFrame, "请输入需要查询的公交路线号:");
//JOptionPane.showInputDialog是一个用于弹出输入对话框的方法,它可以让用户输入一些文本信息
//该方法返回一个字符串类型的值,表示用户在输入框中输入的内容。如果用户单击“取消”按钮或关闭对话框,则返回null
if (route != null && !route.trim().isEmpty()) {
//检查字符串是否为空或仅包含空格的方法
//首先,使用trim方法去除字符串两端的空格,然后使用isEmpty()方法判断处理后的字符串是否为空
routeInquiry(arrayList, route, jFrame);
} else {
JOptionPane.showMessageDialog(jFrame, "输入不能为空!", "输入错误", JOptionPane.ERROR_MESSAGE);
//JOptionPane.showMessageDialog用于在图形用户界面(GUI)中显示一个消息对话框
}
});
button2.addActionListener(e -> {
String station = JOptionPane.showInputDialog(jFrame, "请输入需要查询的公交站点名:");
if (station != null && !station.trim().isEmpty()) {
stationInquiry(arrayList, station, jFrame);
} else {
JOptionPane.showMessageDialog(jFrame, "输入不能为空!", "输入错误", JOptionPane.ERROR_MESSAGE);
}
});
button3.addActionListener(e -> {
String station_1 = JOptionPane.showInputDialog(jFrame, "请输入起点的公交站点名:");
String station_2 = JOptionPane.showInputDialog(jFrame, "请输入终点的公交站点名:");
if (station_1 != null && !station_1.trim().isEmpty()
&& station_2 != null && !station_2.trim().isEmpty()) {
transferInquiry(arrayList, station_1, station_2, jFrame);
} else {
JOptionPane.showMessageDialog(jFrame, "输入不能为空!", "输入错误", JOptionPane.ERROR_MESSAGE);
}
});
button4.addActionListener(e -> {
//管理员登录
String expectedAccount = "12345";
String expectedPassword = "12345";
String account = JOptionPane.showInputDialog(jFrame, "请输入账号:");
String password = JOptionPane.showInputDialog(jFrame, "请输入密码:");
if (account == null || password == null || !account.equals(expectedAccount) || !password.equals(expectedPassword)) {
//equals用于比较两个对象是否相等的方法
JOptionPane.showMessageDialog(jFrame, "输入账号或密码错误,请重新操作!", "输入错误", JOptionPane.ERROR_MESSAGE);
} else {
//后台管理图形用户界面
JFrame jFrame_b = new JFrame("后台管理");
jFrame_b.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//设置JFrame窗口关闭时的操作,当用户点击窗口的关闭按钮时,程序将将执行JFrame.DISPOSE_ON_CLOSE操作,即释放与该窗口相关的所有资源并销毁它
jFrame_b.setSize(400, 150);
jFrame_b.setLocationRelativeTo(null);
JPanel jPanel_b = new JPanel();
jFrame_b.add(jPanel_b);
JButton button_b1 = new JButton("添加线路");
JButton button_b2 = new JButton("修改线路");
JButton button_b3 = new JButton("删除线路");
jPanel_b.add(button_b1);
jPanel_b.add(button_b2);
jPanel_b.add(button_b3);
button_b1.addActionListener(e14 -> {
String inputRoute = JOptionPane.showInputDialog(jFrame_b, "请输入需要添加的公交路线号:");
String inputStation = JOptionPane.showInputDialog(jFrame_b, "请输入需要添加的公交路线站点(用空格分隔):");
String inputTime = JOptionPane.showInputDialog(jFrame_b, "请输入需要添加的公交路线时间:");
String inputPrice = JOptionPane.showInputDialog(jFrame_b, "请输入需要添加的公交路线票价:");
String inputCard = JOptionPane.showInputDialog(jFrame_b, "请输入需要添加的公交路线卡:");
if (inputRoute != null && !inputRoute.trim().isEmpty()
&& inputStation != null && !inputStation.trim().isEmpty()
&& inputTime != null && !inputTime.trim().isEmpty()
&& inputPrice != null && !inputPrice.trim().isEmpty()
&& inputCard != null && !inputCard.trim().isEmpty()) {
String[] split = inputStation.split(" ");
//使用split()方法将字符串按空格分隔成一个字符串数组
ArrayList<String> station = new ArrayList<>(Arrays.asList(split));
//将数组转换为列表
addBusInformation(arrayList, inputRoute, station, inputTime, inputPrice, inputCard, jFrame_b);
} else {
JOptionPane.showMessageDialog(jFrame_b, "输入不能为空!", "输入错误", JOptionPane.ERROR_MESSAGE);
}
});
button_b2.addActionListener(e1 -> {
String route = JOptionPane.showInputDialog(jFrame_b, "请输入需要修改的公交路线号:");
String inputRoute = JOptionPane.showInputDialog(jFrame_b, "请输入新的公交路线号:");
String inputStation = JOptionPane.showInputDialog(jFrame_b, "请输入新的公交路线站点(请使用空格分隔):");
String inputTime = JOptionPane.showInputDialog(jFrame_b, "请输入新的公交路线时间:");
String inputPrice = JOptionPane.showInputDialog(jFrame_b, "请输入新的公交路线票价:");
String inputCard = JOptionPane.showInputDialog(jFrame_b, "请输入新的公交路线卡:");
if (route != null && !route.trim().isEmpty()
&& inputRoute != null && !inputRoute.trim().isEmpty()
&& inputStation != null && !inputStation.trim().isEmpty()
&& inputTime != null && !inputTime.trim().isEmpty()
&& inputPrice != null && !inputPrice.trim().isEmpty()
&& inputCard != null && !inputCard.trim().isEmpty()) {
String[] split = inputStation.split(" ");
//使用split()方法将字符串按空格分隔成一个字符串数组
ArrayList<String> station = new ArrayList<>(Arrays.asList(split));
updateBusInformation(arrayList, route, inputRoute, station, inputTime, inputPrice, inputCard, jFrame_b);
} else {
JOptionPane.showMessageDialog(jFrame_b, "输入不能为空!", "输入错误", JOptionPane.ERROR_MESSAGE);
}
});
button_b3.addActionListener(e13 -> {
String route = JOptionPane.showInputDialog(jFrame_b, "请输入需要删除的公交路线号:");
if (route != null && !route.trim().isEmpty()) {
deleteBusInformation(arrayList, route, jFrame_b);
} else {
JOptionPane.showMessageDialog(jFrame_b, "输入不能为空!", "输入错误", JOptionPane.ERROR_MESSAGE);
}
});
//当用户尝试关闭窗口时,会触发windowClosing事件。
jFrame_b.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
try {
//将修改后的ArrayList的数据同步保存到数据库中
save(arrayList);
} catch (ClassNotFoundException | SQLException ex) {
throw new RuntimeException(ex);
}
}
});
jFrame_b.setVisible(true);
}
});
jFrame.setVisible(true);
}
}