开发基于控制台的航班信息系统。具体要求如下:crud
(1)显示航班信息系统主菜单,如图-1所示,包括:
1)列出所有航班
2)按起飞时间查询
3)按目的地查询
4)删除航班
5)更新航班
6)退出系统
(2)列出所有航班:查出所有航班的信息,以列表形式显示,包括:编号,航班号,目的地,起飞日期。如图-2所示。
(3)按起飞时间查询:输入起飞时间(格式如2011-2-25),查出所有这一天的航班。如图-3所示。
(4)按目的地查询:输入目的地,查出所有飞往此地的航班。如图-4所示。
(5)删除航班:删除指定编号的航班。如图-5所示。
(6)更新航班:更新指定编号的航班。如图-6所示。
(7)退出系统。
系统运行界面如图-1所示。
package com.aircraftflight;
/*
* 航班类
* */
public class Flight {
//编号
int number;
//航班号
int flightNumber;
//目的地
String destination;
//起飞日期(格式如2011-2-25)
String dataFlight;
//构造器(对所有参数进行构造)
public Flight(int number, int flightNumber, String destination, String dataFlight) {
this.number = number;
this.flightNumber = flightNumber;
this.destination = destination;
this.dataFlight = dataFlight;
}
}
package com.aircraftflight;
import jdk.nashorn.internal.ir.WhileNode;
import java.awt.*;
import java.util.Scanner;
/*
* 操作类
* */
public class Operationfly {
//声明一个航班的数组
Flight[] flights = new Flight[10];
Scanner input = new Scanner(System.in);
Flight flights1 = new Flight(1, 001,"beijng","2010-1-1");
Flight flights2 = new Flight(2, 002,"shanghai","2011-2-10");
Flight flights3 = new Flight(3, 003,"guangzhou","2011-2-24");
//初始化航班
public void initial(){
flights[0] = flights1;
flights[1] = flights2;
flights[2] = flights3;
}
boolean flag = true;
public void intoSystem(){
boolean flag = true;
while (flag){
System.out.print("请选择操作(1.列出所有航班,2按起飞时间查询,3.按目的地查询,4.删除航班,5更新航班,6离开系统) :");
int s = input.nextInt();
switch (s){
case 1:
showAll();
break;
case 2:
takeoffTime();
break;
case 3:
destination();
break;
case 4:
delete();
break;
case 5:
add();
break;
case 6:
flag = false;
break;
default:
System.out.println("输入操作序号失败");
break;
}
}
}
//列出所有航班
public void showAll(){
System.out.println("编号 航班号 目的地 起飞日期");
for (int i = 0; i < flights.length; i++) {
if (flights[i]==null){
break;
}
System.out.println(flights[i].number+" "+flights[i].flightNumber+" "+flights[i].destination+" "+flights[i].dataFlight);
}
}
//按起飞时间查询
public void takeoffTime(){
System.out.print("请输入日期:");
String data = input.next();
for (int i = 0; i <flights.length ; i++) {
if (flights[i]==null){
System.out.println("没有"+data+"时间的飞机起飞");
break;
}
if (data.equals(flights[i].dataFlight)){
System.out.println("编号 航班号 目的地 起飞日期");
System.out.println(flights[i].number+" "+flights[i].flightNumber+" "+flights[i].destination+" "+flights[i].dataFlight);
break;
}
}
}
//按目的地查询
public void destination(){
System.out.print("请输入目的地:");
String address = input.next();
for (int i = 0; i < flights.length; i++) {
if (flights[i]==null){
System.out.println("没有去往"+address+"的飞机");
break;
}
if (address.equals(flights[i].destination)){
System.out.println("编号 航班号 目的地 起飞日期");
System.out.println(flights[i].number+" "+flights[i].flightNumber+" "+flights[i].destination+" "+flights[i].dataFlight);
break;
}
}
}
//删除指定航班界面
public void delete(){
System.out.print("请输入需要删除的航班编号:");
int number = input.nextInt();
for (int i = 0; i < flights.length; i++) {
if (flights[i].number==number){
for (int j = i; j <flights.length-1 ; j++) {
flights[j]=flights[j+1];
}
System.out.println("删除成功");
break;
}
System.out.println("没有该航班编号,删除失败");
}
}
//更新指定航班界面
public void add(){
System.out.print("请输入需要更新的航班编号:");
int number1 = input.nextInt();
System.out.print("请输入新的航班号:");
int flightNumber1 = input.nextInt();
System.out.print("请输入新的目的地:");
String destination1 = input.next();
System.out.print("请输入新的起飞日期:");
String dataFlight1 = input.next();
for (int i = 0; i < flights.length; i++) {
if (flights[i] == null) {
System.out.println(flights.length);
System.out.println(i);
flights[i] = new Flight(number1, flightNumber1, destination1, dataFlight1);
System.out.println("添加成功");
break;
}
}
}
}
package com.aircraftflight;
/*
* 程序入口
* */
public class Menu {
public static void main(String[] args) {
Operationfly operationfly = new Operationfly();
operationfly.initial();
operationfly.intoSystem();
}
}