3.17航空服务查询
基于决策表的可视化实现
根据提供的图片和需求,我将实现一个完整的航空服务查询系统,包括可视化界面和决策表测试。
完整Java Swing实现代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections; // 添加这行导入
import javax.swing.AbstractButton;
public class AirlineServiceSystem extends JFrame {
private ButtonGroup routeGroup;
private ButtonGroup cabinGroup;
private ButtonGroup timeGroup;
public AirlineServiceSystem() {
setTitle("航空服务查询系统");
setSize(400, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(6, 1, 10, 10));
// 航线选择
JPanel routePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
routePanel.add(new JLabel("航线:"));
routeGroup = new ButtonGroup();
JRadioButton euUsRoute = new JRadioButton("欧美");
JRadioButton nonEuUsRoute = new JRadioButton("国外非欧美");
JRadioButton domesticRoute = new JRadioButton("国内");
routeGroup.add(euUsRoute);
routeGroup.add(nonEuUsRoute);
routeGroup.add(domesticRoute);
routePanel.add(euUsRoute);
routePanel.add(nonEuUsRoute);
routePanel.add(domesticRoute);
add(routePanel);
// 舱位选择
JPanel cabinPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
cabinPanel.add(new JLabel("舱位:"));
cabinGroup = new ButtonGroup();
JRadioButton businessClass = new JRadioButton("商务舱");
JRadioButton economyClass = new JRadioButton("经济舱");
cabinGroup.add(businessClass);
cabinGroup.add(economyClass);
cabinPanel.add(businessClass);
cabinPanel.add(economyClass);
add(cabinPanel);
// 飞行时间选择
JPanel timePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
timePanel.add(new JLabel("飞行时间:"));
timeGroup = new ButtonGroup();
JRadioButton under2h = new JRadioButton("两小时以内");
JRadioButton over2h = new JRadioButton("超过两小时");
timeGroup.add(under2h);
timeGroup.add(over2h);
timePanel.add(under2h);
timePanel.add(over2h);
add(timePanel);
// 查询按钮
JButton queryButton = new JButton("查询");
queryButton.addActionListener(new QueryListener());
add(queryButton);
// 退出按钮
JButton exitButton = new JButton("退出");
exitButton.addActionListener(e -> System.exit(0));
add(exitButton);
// 结果标签
JLabel resultLabel = new JLabel("", JLabel.CENTER);
resultLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
add(resultLabel);
setVisible(true);
}
private class QueryListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// 获取航线选择
String route = "";
for (AbstractButton button : Collections.list(routeGroup.getElements())) {
if (button.isSelected()) {
route = button.getText();
break;
}
}
// 获取舱位选择
String cabin = "";
for (AbstractButton button : Collections.list(cabinGroup.getElements())) {
if (button.isSelected()) {
cabin = button.getText();
break;
}
}
// 获取飞行时间选择
String time = "";
for (AbstractButton button : Collections.list(timeGroup.getElements())) {
if (button.isSelected()) {
time = button.getText();
break;
}
}
// 验证是否所有选项都已选择
if (route.isEmpty() || cabin.isEmpty() || time.isEmpty()) {
JOptionPane.showMessageDialog(null, "请选择所有选项", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
// 确定服务
boolean hasFood = false;
boolean hasMovie = false;
// 决策表实现
if (route.equals("欧美")) {
hasFood = true;
hasMovie = true;
} else if (route.equals("国外非欧美")) {
hasFood = true;
hasMovie = cabin.equals("商务舱");
} else if (route.equals("国内")) {
if (cabin.equals("商务舱")) {
hasFood = true;
hasMovie = false;
} else { // 经济舱
hasFood = time.equals("超过两小时");
hasMovie = false;
}
}
// 显示结果
String result = "服务结果:\n";
result += "餐饮服务: " + (hasFood ? "提供" : "不提供") + "\n";
result += "影视娱乐: " + (hasMovie ? "提供" : "不提供");
JLabel resultLabel = (JLabel) getContentPane().getComponent(5);
resultLabel.setText(result);
// 显示可视化结果
showServiceVisualization(hasFood, hasMovie);
}
}
private void showServiceVisualization(boolean hasFood, boolean hasMovie) {
JFrame visualFrame = new JFrame("服务详情");
visualFrame.setSize(300, 200);
visualFrame.setLayout(new GridLayout(2, 1));
// 餐饮服务面板
JPanel foodPanel = new JPanel();
foodPanel.setBorder(BorderFactory.createTitledBorder("餐饮服务"));
JLabel foodLabel = new JLabel(hasFood ? "🍽️ 提供" : "🚫 不提供");
foodLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
foodLabel.setForeground(hasFood ? Color.GREEN : Color.RED);
foodPanel.add(foodLabel);
// 影视娱乐面板
JPanel moviePanel = new JPanel();
moviePanel.setBorder(BorderFactory.createTitledBorder("影视娱乐"));
JLabel movieLabel = new JLabel(hasMovie ? "🎬 提供" : "🚫 不提供");
movieLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
movieLabel.setForeground(hasMovie ? Color.GREEN : Color.RED);
moviePanel.add(movieLabel);
visualFrame.add(foodPanel);
visualFrame.add(moviePanel);
visualFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new AirlineServiceSystem());
}
}
决策表测试用例设计
根据需求分析,我们可以构建以下决策表:
条件\规则 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
---|---|---|---|---|---|---|---|---|
航线=欧美 | Y | Y | Y | Y | N | N | N | N |
航线=国外非欧美 | N | N | N | N | Y | Y | Y | Y |
航线=国内 | N | N | N | N | N | N | N | N |
舱位=商务舱 | Y | Y | N | N | Y | Y | N | N |
舱位=经济舱 | N | N | Y | Y | N | N | Y | Y |
时间>2小时 | - | - | Y | N | - | - | Y | N |
动作 | ||||||||
提供食物 | Y | Y | Y | Y | Y | Y | Y | N |
提供电影 | Y | Y | Y | Y | Y | Y | N | N |
注:Y=是,N=否,-=不适用
测试用例表
测试用例ID | 航线 | 舱位 | 飞行时间 | 预期结果(食物) | 预期结果(电影) |
---|---|---|---|---|---|
TC1 | 欧美 | 商务舱 | 任意 | 提供 | 提供 |
TC2 | 欧美 | 经济舱 | 任意 | 提供 | 提供 |
TC3 | 国外非欧美 | 商务舱 | 任意 | 提供 | 提供 |
TC4 | 国外非欧美 | 经济舱 | 任意 | 提供 | 不提供 |
TC5 | 国内 | 商务舱 | 任意 | 提供 | 不提供 |
TC6 | 国内 | 经济舱 | >2小时 | 提供 | 不提供 |
TC7 | 国内 | 经济舱 | ≤2小时 | 不提供 | 不提供 |
测试执行步骤
-
运行程序,显示航空服务查询界面
-
按照测试用例表中的组合选择航线、舱位和飞行时间
-
点击"查询"按钮
-
验证结果显示和可视化窗口是否符合预期
-
重复步骤2-4,执行所有测试用例
测试结果验证
对于每个测试用例,系统应该:
-
在主窗口显示正确的餐饮和影视服务状态
-
在可视化窗口显示对应的图标和颜色:
-
绿色✅表示提供该服务
-
红色🚫表示不提供该服务
-
-
结果与决策表的预期结果一致