Java實現航空航班管理系統

本文實例為大傢分享瞭Java實現航空航班管理系統的具體代碼,供大傢參考,具體內容如下

Plane對象:

在Plane對象裡定義瞭飛機的編號id、航班號、目的地、起飛日期。生成瞭構造方法和toString()方法;以及getting()和setting()方法,但在程序裡沒用到。

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()函數中註冊驅動、獲取連接

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.退出系統。

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);
 }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: