JAVA编写的火车票售票系统
作者:互联网
正文
一,Tanban
package
hangban;
import
java.util.Scanner;
import
hangban.Flight;
public class Tanban
{
static Flight[] flys=new Flight[6];//只能存放6条信息,null
static {
Flight filght1=new Flight(1,100,"北京","2016-01-01");
Flight filght2=new Flight(2,200,"南京","2016-02-02");
Flight filght3=new Flight(3,300,"天津","2016-03-03");
//数组可以通过下标赋值
flys[0]=filght1;
flys[1]=filght2;
flys[2]=filght3;
}
//String [] msgs= {"1:列除所有航班","2:按起飞时间查询","3:按目的地查询","4:删除航班","5:更新航班","6:退出系统"};
static Scanner sc=new Scanner(System.in);
//一
static void listFlys()
{
System.out.println("编号\t航班号\t目的地\t起飞时间");
for(int i=0;i<flys.length;i++)
{
Flight filght=flys[i];
if(filght!=null)
{
System.out.println(filght.getId()+"\t"+filght.getFlyNo()+"\t"+filght.getDes()+"\t"+filght.getStartFlyTime());
}
}
}
//二
static void
findFlyByTime()
{
int index=-1;
System.out.println("请输入起飞日期(年-月-日)");
String time=sc.next();
for(int i=0;i<flys.length;i++)
{
Flight f=flys[i];
if(f!=null)
{
if(time.equals(f.getStartFlyTime()))
{
index=i;
break;
}
}
}
if(index==-1) {
System.out.println("你要查询的指定航班不存在");
}
else {
Flight filght=flys [index];
System.out.println("编号\t航班号\t目的地\t起飞时间");
System.out.println(filght.getId()+"\t"+filght.getFlyNo()+"\t"+filght.getDes()+"\t"+filght.getStartFlyTime());
}
}
//三
static void
mudidi()
{
int index=-1;
System.out.println("请输入目的地:");
String time=sc.next();
for(int i=0;i<flys.length;i++)
{
Flight f=flys[i];
if(f!=null)
{
if(time.equals(f.getDes()))
{
index=i;
break;
}
}
}
if(index==-1) {
System.out.println("你要查询的指定航班不存在");
}
else {
Flight filght=flys [index];
System.out.println("编号\t航班号\t目的地\t起飞时间");
System.out.println(filght.getId()+"\t"+filght.getFlyNo()+"\t"+filght.getDes()+"\t"+filght.getStartFlyTime());
}
}
//四
static void deleteFly()
{
int index=5;
System.out.println("请输入要删除的航班机号:");
int num=sc.nextInt();
for(int i=0;i<flys.length;i++)
{
Flight
fly=flys[i];
if(fly!=null)
{
if(num==fly.getId())
{
index=i;
}
}
}
if(index==-5)
{
System.out.println("要删除的航班不存在 !");
}
else
{
for(int j=index;j<flys.length-1;j++)
{
flys[j]=flys[j+1];
}
flys[flys.length-1]=null;
System.out.println("删除航班编号为"+num+"的航班成功");
}
}
//五
static void gengxin()
{
int index=-5;
System.out.println("请输入想要更新的航班编号:");
int num=sc.nextInt();
for(int i=0;i<flys.length;i++)
{
Flight
xin=flys[i];
if(xin!=null)
{
if(num==xin.getId())
{
index=i;
}
}
}
if(index==-5)
{
System.out.println("要更新的航班不存在 !");
}
else
{
System.out.println("请输入新的航班号:");
int xinhangbanhao=sc.nextInt();
flys[num-1].setFlyNo(xinhangbanhao);
System.out.println("请输入新的目的地:");
String
xinmudidi=sc.next();
flys[num-1].setDes(xinmudidi);
System.out.println("请输入新的起飞时间:");
String
xinqifeishijian=sc.next();
flys[num-1].setStartFlyTime(xinqifeishijian);
}
}
public static void main(String[] args)
{
String [] msgs= {"1:列除所有航班","2:按起飞时间查询","3:按目的地查询","4:删除航班","5:更新航班","6:退出系统"};
System.out.println("**********欢迎使用航班信息**********");
for(int i=0;i<msgs.length;i++)
{
System.out.println("\t\t"+msgs[i]);
}
while(true)
{
System.out.println("请选择1---6之间的数字,转向指定功能");
int choice=sc.nextInt();
switch(choice)
{
case 1:
listFlys();
break;
case 2:
findFlyByTime();
break;
case 3:
mudidi();
break;
case 4:
deleteFly();
break;
case 5:
gengxin();
break;
case 6:
System.out.println("推出系统成功");
return ;
default :
System.out.println("你输错了!!!");
}
}
}
}
二,Filght
package
hangban;
public class Flight
{
private int id;//编号
private int flyNo;//航班号
private
String des;//目的地
private
String startFlyTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getFlyNo() {
return flyNo;
}
public void setFlyNo(int flyNo) {
this.flyNo = flyNo;
}
public
String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
public
String getStartFlyTime() {
return startFlyTime;
}
public void setStartFlyTime(String startFlyTime) {
this.startFlyTime = startFlyTime;
}
public
Flight()
{
super();
}
public
Flight(int id, int flyNo, String des, String startFlyTime) {
super();
this.id = id;
this.flyNo = flyNo;
this.des = des;
this.startFlyTime = startFlyTime;
}
}
标签:售票,Flight,Scanner,static,flys,new,JAVA,2016,火车票 来源: https://blog.csdn.net/qq_44629109/article/details/100124119