Java实现航空航班管理系统
程序员文章站
2022-06-26 09:30:01
本文实例为大家分享了java实现航空航班管理系统的具体代码,供大家参考,具体内容如下plane对象:在plane对象里定义了飞机的编号id、航班号、目的地、起飞日期。生成了构造方法和tostring(...
本文实例为大家分享了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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: 创业如何设计一个成熟的商业模式?