服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java实现航空航班管理系统

Java实现航空航班管理系统

2021-10-28 10:38onpine Java教程

这篇文章主要为大家详细介绍了Java实现航空航班管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Java实现航空航班管理系统的具体代码,供大家参考,具体内容如下

Plane对象:

在Plane对象里定义了飞机的编号id、航班号、目的地、起飞日期。生成了构造方法和toString()方法;以及getting()和setting()方法,但在程序里没用到。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.hangkong;
 
public class Plane {
 
 private int id;//编号
 private String planeNum;//航班号
 private String address;//目的地
 private String date;//日期
 
 
 
 public Plane(int id, String planeNum, String address, String date) {
  super();
  this.id = id;
  this.planeNum = planeNum;
  this.address = address;
  this.date = date;
 }
 
 public Plane(){
  super();
 }
 //Alt+Shift+s
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getPlaneNum() {
  return planeNum;
 }
 public void setPlaneNum(String planeNum) {
  this.planeNum = planeNum;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 public String getDate() {
  return date;
 }
 public void setDate(String date) {
  this.date = date;
 }
 
 @Override
 public String toString() {
  return "Plane" + id + "\t\t" + planeNum + "\t\t" + address + "\t\t" + date;
  //return "Plane ID:" + id + "\t航班编号:" + planeNum + "\t目的地:" + address + "\t起飞时间:" + date;
 }
}

MySQL数据库:

数据库名字是Fly,数据表是plane;在getcon()函数中注册驱动、获取连接

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
SET FOREIGN_KEY_CHECKS=0;
 
-- ----------------------------
-- Table structure for `plane`
-- ----------------------------
DROP TABLE IF EXISTS `plane`;
CREATE TABLE `plane` (
  `id` int(20) NOT NULL AUTO_INCREMENT,
  `planeNum` varchar(20) DEFAULT NULL,
  `address` varchar(20) DEFAULT NULL,
  `date` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
 
-- ----------------------------
-- Records of plane
-- ----------------------------
INSERT INTO `plane` VALUES ('1', 'DZ001', '东京', '2019-9-1');
INSERT INTO `plane` VALUES ('2', 'DZ002', '上海', '2019-8-28');
INSERT INTO `plane` VALUES ('3', 'DZ003', '广州', '2019-8-29');
INSERT INTO `plane` VALUES ('4', 'DZ004', '深圳', '2019-8-29');
INSERT INTO `plane` VALUES ('5', 'DZ005', '厦门', '2019-8-30');
INSERT INTO `plane` VALUES ('6', 'DZ006', '杭州', '2019-8-30');
INSERT INTO `plane` VALUES ('7', 'DZ007', '武汉', '2019-8-30');
INSERT INTO `plane` VALUES ('8', 'DZ008', '成都', '2019-8-30');
INSERT INTO `plane` VALUES ('9', 'DZ009', '西安', '2019-8-30');
INSERT INTO `plane` VALUES ('10', 'DZ0010', '郑州', '2019-8-30');
INSERT INTO `plane` VALUES ('11', 'DZ0011', '长沙', '2019-8-30');
INSERT INTO `plane` VALUES ('12', 'DZ0012', '民权', '2019-8-31');
INSERT INTO `plane` VALUES ('13', 'DZ0013', '莫斯科', '2019-9-1');
INSERT INTO `plane` VALUES ('14', 'DZ0014', '曼谷', '2019-9-2');
INSERT INTO `plane` VALUES ('15', 'DZ0015', '阿布扎比', '2019-9-2');

主程序TestFly:

TestFly类中有实现各种功能的函数,包括 1.列出所有航班,2.按起飞时间查询,3.按目的地查询,4.删除航班,5.更新航班,6.增加航班,7.退出系统。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package com.hangkong;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
 
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
 
import com.mysql.jdbc.ExceptionInterceptor;
import com.sun.javafx.runtime.VersionInfo;
import com.sun.xml.internal.ws.api.pipe.NextAction;
 
public class TestFly {
 
 static Connection con = null;//连接
 static PreparedStatement ps = null;//模板
 static ResultSet rs = null;//结果集
 
 public static void main(String[] args) throws Exception {
  System.out.println("******************************************大壮航空航班信息管理系统********************************************\n");
  //show();
  boolean bool = Dome();
  while(bool){
   bool = Dome();
  }
  if(!bool){
   System.out.println("**************************************已成功退出大壮航空航班信息管理系统**************************************\n");
   System.exit(0);
  }
 }
 
 //流程
 public static boolean Dome() throws Exception{
  Scanner scan = new Scanner(System.in);
  show();
  int key = scan.nextInt();
  switch (key) {
  case 1:
   showMessage(listMessage());
   break;
   
  case 2:{
   System.out.println("输入起飞时间:");
   String date = scan.next();
   showMessage(selectDate(date));
  }break;
  
  case 3:{
   System.out.println("输入目的地:");
   String Address = scan.next();
   showMessage(selectAddress(Address));
  }break;
  
  case 4:{
   System.out.println("输入航班编号:");
   String planeNum = scan.next();
   deleteFly(planeNum);
  }break;
  
  case 5:{
   System.out.println("输入航班编号和更改后目的地和时间:");
   String planeNum = scan.next();
   String Address = scan.next();
   String date = scan.next();
   updateFly(Address,date,planeNum);
  }break;
  
  case 6:{
   System.out.println("输入航班编号、目的地、起飞时间:");
   String planeNum = scan.next();
   String Address = scan.next();
   String date = scan.next();
   creatPlane(planeNum,Address,date);
  }break;
  
  default:
   //scan.close();
   return false;
  }
  //scan.close();
  return true;
 }
 
 //注册驱动,获取连接
 public static Connection getCon() throws Exception{
  Class.forName("com.mysql.jdbc.Driver");
  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/fly", "root", "");
  return con;
 }
 
 //创建初始信息,插入信息
 public static void creatPlane(String planeNum,String address, String date) throws Exception{
  getCon();
  String sql = "insert into plane values (null,?,?,?)";
  ps = con.prepareStatement(sql);
  ps.setString(1, planeNum);
  ps.setString(2, address);
  ps.setString(3, date);
  ps.executeUpdate();
  ps.close();
  con.close();
  selectPlaneNum(planeNum);
 }
 
 //系统主菜单
 public static void show(){
  System.out.println("请选择操作:(1.列出所有航班,2.按起飞时间查询,3.按目的地查询,4.删除航班,5.更新航班,6.增加航班,7.退出系统)");
 }
 
 //获取结果集合输出
 public static void showMessage(Set<Plane> set){
  System.out.println("\n********************************大壮航空***********************************\n");
  if(set.size() == 0){
   System.out.println("未匹配到任何数据!");
   System.out.println("\n********************************大壮航空***********************************\n");
   return;
  }
  System.out.println("Plane\t\t航班编号\t目的地\t\t起飞时间");
  for( Plane value : set){
   System.out.println(value);
  }
  System.out.println("\n********************************大壮航空***********************************\n");
 }
 
 //列出所有航班信息
 public static Set<Plane> listMessage() throws Exception{
  getCon();
  String sql = "select * from plane";
  ps = con.prepareStatement(sql);
  rs = ps.executeQuery();
  
  Set<Plane> set = new HashSet<>();
  
  while(rs.next()){
   int id = rs.getInt("id");
   String planeNum = rs.getString("planeNum");
   String address = rs.getString("address");
   String dateTime = rs.getString("date");
   Plane plane = new Plane(id, planeNum, address, dateTime);
   set.add(plane);
  }
  ps.close();
  con.close();
  return set;
 }
 
 //按起飞时间查询
 public static Set<Plane> selectDate(String date) throws Exception{
  getCon();
  String sql = "select * from plane where date = ? ";
  ps = con.prepareStatement(sql);
  ps.setString(1, date);
  rs = ps.executeQuery();
  
  Set<Plane> set = new HashSet<>();
  //String planes = "";
 
  while(rs.next()){
   int id = rs.getInt("id");
   String planeNum = rs.getString("planeNum");
   String address = rs.getString("address");
   String dateTime = rs.getString("date");
   Plane plane = new Plane(id, planeNum, address, dateTime);
   set.add(plane);
   //planes += plane.toString() + "\n";
  }
  ps.close();
  con.close();
  return set;
 }
 
 //按目的地查询
 public static Set<Plane> selectAddress(String Address) throws Exception{
  getCon();
  String sql = "select * from plane where address = ? ";
  ps = con.prepareStatement(sql);
  ps.setString(1, Address);
  rs = ps.executeQuery();
  
  Set<Plane> set = new HashSet<>();
  //String planes = "";
  
  while(rs.next()){
   int id = rs.getInt("id");
   String planeNum = rs.getString("planeNum");
   String address = rs.getString("address");
   String dateTime = rs.getString("date");
   Plane plane = new Plane(id, planeNum, address, dateTime);
   set.add(plane);
   //planes += plane.toString() + "\n";
  }
  ps.close();
  con.close();
  return set;
 }
 
 //按航班编号
 public static void selectPlaneNum(String planeNum) throws Exception{
  getCon();
  String sql = "select * from plane where planeNum = ? ";
  ps = con.prepareStatement(sql);
  ps.setString(1, planeNum);
  rs = ps.executeQuery();
  boolean x = true;
  while(rs.next()){
   if(x){
    System.out.println("\n********************************大壮航空***********************************\n");
    System.out.println("Plane\t\t航班编号\t目的地\t\t起飞时间");
   }
   int id = rs.getInt("id");
   String planenum = rs.getString("planeNum");
   String address = rs.getString("address");
   String date = rs.getString("date");
   System.out.println("Plane" + id + "\t\t" + planenum + "\t\t" + address + "\t\t" + date);
   x = false;
  }
  System.out.println("\n********************************大壮航空***********************************\n");
 }
 //按航班编号删除航班
 public static void deleteFly(String planeNum) throws Exception{
  getCon();
  String sql = "delete from plane where planeNum = ? ";
  ps = con.prepareStatement(sql);
  ps.setString(1, planeNum);
  ps.executeUpdate();
  ps.close();
  con.close();
  System.out.println("\n********************************大壮航空***********************************\n");
  System.out.println("已删除!");
  System.out.println("\n********************************大壮航空***********************************\n");
 }
 
 //按航班编号更新航班目的地和时间
 public static void updateFly(String Address,String date,String planeNum) throws Exception{
  getCon();
  String sql = "update plane set address = ?,date = ? where planeNum = ? ";
  ps = con.prepareStatement(sql);
  ps.setString(1, Address);
  ps.setString(2, date);
  ps.setString(3, planeNum);
  ps.executeUpdate();
  ps.close();
  con.close();
  selectPlaneNum(planeNum);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_43736435/article/details/100145260

延伸 · 阅读

精彩推荐