高铁票
public class TrainTicket {
String number;//车次
String site;//站点
String a_time; //到站时间
String l_time; //离站时间
int price=0;
public TrainTicket(String number, String site, String a_time, String l_time, int price) {
this.number = number;
this.site = site;
this.a_time = a_time;
this.l_time = l_time;
this.price = price;
}
public String ToString(){
String str=number+"\t"+site+"\t"+a_time+"\t"+l_time+"\t"+price+"\n";
return str;
}
@Override
public String toString() {
return "TrainTicket{" +
"车次='" + number + '\'' +
", 站点='" + site + '\'' +
", 到站时间='" + a_time + '\'' +
", 发车时间='" + l_time + '\'' +
", 票价=" + price +
'}';
}
@Override
public boolean equals(Object obj) {
if(this==obj){
return true;
}
if(obj==null){
return false;
}
if(!(obj instanceof TrainTicket)){
return false;
}
TrainTicket tt=(TrainTicket) obj;
return this.number.equals(tt.number)&&this.site.equals(tt.site)&&this.a_time.equals(tt.a_time)&&
this.l_time.equals(tt.l_time)&&this.price==tt.price;
}
}
飞机票
public class PlaneTicket {
String number;
String l_site;//起点站
String a_site;//终点站
String a_time; //到站时间
String l_time; //离站时间
int price=0;
public PlaneTicket(String number, String l_site, String a_site, String a_time, String l_time, int price) {
this.number = number;
this.l_site = l_site;
this.a_site = a_site;
this.a_time = a_time;
this.l_time = l_time;
this.price = price;
}
public String ToString(){
String str=number+"\t"+l_site+"\t"+a_site+"\t"+a_time+"\t"+l_time+"\t"+price+"\n";
return str;
}
@Override
public String toString() {
return "PlaneTicket{" +
"航班号='" + number + '\'' +
", 起点站='" + l_site + '\'' +
",终点站='" + a_site + '\'' +
",起飞时间='" + a_time + '\'' +
", 到达时间='" + l_time + '\'' +
", 票价=" + price +
'}';
}
@Override
public boolean equals(Object obj) {
if(this==obj){
return true;
}
if(obj==null){
return false;
}
if(!(obj instanceof PlaneTicket)){
return false;
}
PlaneTicket pt=(PlaneTicket)obj;
return this.number.equals(pt.number)&&this.a_site.equals(pt.a_site)&&this.l_site.equals(pt.l_site)&&
this.a_time.equals(pt.a_time)&&this.l_time.equals(pt.l_time)&&this.price==pt.price;
}
}
高铁系统
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class Train {
List<TrainTicket> ll;
public Train() {
this.ll = new ArrayList<TrainTicket>();
}
public void Creat(File file){
Reader r = null;
char[] ch=new char[(int)file.length()];
int temp,len=0,i=0;
try {
r = new FileReader(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
while((temp=r.read())!=-1) {
ch[i++] = (char)temp;
}
} catch (IOException e) {
e.printStackTrace();
}
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
String str=String.valueOf(ch);
String res[]=str.split("\n");
for (int j=0;j<res.length-1;j++){
String ans[]=res[j].split("\t");
/*for (int k=0;k<ans.length;k++){
System.out.println(ans[k]);
}*/
ll.add(new TrainTicket(ans[0],ans[1],ans[2],ans[3],Integer.parseInt(ans[4].trim())));
}
}
/* HashMap<Integer,String> map = null;
for (int j=0;j<res.length;j++){
String ans[]=res[j].split("\t");
int time[]=new int[res.length];
for(int k=0;k<res.length;k++){
String t[]=ans[3].split(":");
map.put(1,ans[3]);
if(k==0){
time[k]=Math.abs(Integer.parseInt(t[0])-9)*60+Integer.parseInt(t[1]);
}else{
String tem[]=map.get(k-1).split(":");
time[k]=Math.abs(Integer.parseInt(t[k])-Integer.parseInt(tem[0]))*60
+Math.abs(Integer.parseInt(t[1])-Integer.parseInt(tem[1]));
}
}
ADJ[j].add(new WeightGraph.Edge(0,j,Integer.parseInt(ans[4]),time[j]));
}
}*/
/*public void Write_back(File file,TrainTicket tt){
this.ll.add(tt);
Writer r = null;
try {
r = new FileWriter(file,true);
//重载的构造,true表示在光标后append,此处在dom4j解析里好像抛出了NullPointerException?没整出来
r.write(tt.ToString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
System.out.println("追加的信息已保存");
}
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}*/
public void Write_back(File file,List<TrainTicket> tt,int t){
Writer r = null;
Writer append=null;
try {
r = new FileWriter(file);
append=new FileWriter(file,true);
r.write("");
for (int j=0;j<tt.size();j++){
String cur=tt.get(j).ToString();
append.write(cur);
append.flush();
}
append.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(t==3){
System.out.println("信息已经删除");
}else if (t==4){
System.out.println("信息已经修改");
}else if(t==2){
System.out.println("追加的信息已经保存");
}
}
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
飞机系统
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Plane {
List<PlaneTicket> ll;
public Plane() {
this.ll=new ArrayList<PlaneTicket>();
}
//追加
public void Write_back(File file, PlaneTicket pt) {
this.ll.add(pt);
Writer r = null;
char[] ch=new char[(int)file.length()];
int temp,len=0,i=0;
try {
r = new FileWriter(file,true);
//重载的构造,true表示在光标后append,此处在dom4j解析里好像抛出了NullPointerException?没整出来
r.write(pt.ToString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
System.out.println("追加的信息已经保存");
}
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//覆盖
public void Write_back(File file,List<PlaneTicket> planeTickets,int t) {
Writer r = null;
Writer append=null;
char[] ch=new char[(int)file.length()];
int temp,len=0,i=0;
try {
r = new FileWriter(file,false);
append=new FileWriter(file,true);
//重载的构造,true表示在光标后append,此处在dom4j解析里好像抛出了NullPointerException?没整出来
r.write("");//清空
for (int j=0;j<planeTickets.size();j++){
String cur=planeTickets.get(j).ToString();
append.write(cur);
append.flush();
}
append.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(t==3){
System.out.println("信息已经删除");
}else if(t==4){
System.out.println("信息已经修改");
}else if (t==2){
System.out.println("追加的信息已经保存");
}
}
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void Creat(File file){
Reader r = null;
char[] ch=new char[(int)file.length()];
int temp,len=0,i=0;
try {
r = new FileReader(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
while((temp=r.read())!=-1) {
ch[i++] = (char)temp;
}
} catch (IOException e) {
e.printStackTrace();
}
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
String str=String.valueOf(ch);
String res[]=str.split("\n");
for (int j=0;j<res.length-1;j++){
String ans[]=res[j].split("\t");
/*for (int k=0;k<ans.length;k++){
System.out.println(ans[k]);
}*/
ll.add(new PlaneTicket(ans[0],ans[1],ans[2],ans[3],ans[4],Integer.parseInt(ans[5].trim())));
}
}
}
时间类
用于实现时间的加减与进位
import java.text.DecimalFormat;
import java.util.Arrays;
public class Time {
/**
* 时间字段常量,表示“分”
*/
public final static int MINUTE = 0;
/**
* 时间字段常量,表示“时”
*/
public final static int HOUR = 1;
/**
* 时间字段常量,表示“天”
*/
public final static int DAY = 2;
//每个单位可容纳的最大数,即进位标志
private final int[] maxFields = { 59, 23, Integer.MAX_VALUE - 1 };
/**
* 各常量允许的最小值
*/
private final int[] minFields = { 0, 0, 0};
/**
* 默认的字符串格式时间分隔符
*/
private String timeSeparator = ":";
/**
* 时间数据容器
*/
private int[] fields = new int[3];
public Time() {
for (int i=0;i<3;i++){
this.fields[i]=0;
}
}
public Time(String str) {
String []temp=str.split(this.timeSeparator);
this.fields[0]=Integer.parseInt(temp[1]);
this.fields[1]=Integer.parseInt(temp[0]);
this.fields[2]=0;
}
/**
* 获得时间字段的值
* @param field 时间字段常量
* @return 该时间字段的值
*/
public int get(int field) {
if(field < 0 || field > fields.length - 1) {
throw new IllegalArgumentException(field + ", field value is error.");
}
return fields[field];
}
/**
* 将时间进行“加”运算,即加上一个时间
* @param time 需要加的时间
* @return 运算后的时间
*/
public Time addTime(Time time) {
Time result = new Time();
int up = 0; // 进位标志
for (int i = 0; i < fields.length; i++) {
int sum = fields[i] + time.fields[i] + up;
up = sum / (maxFields[i] + 1);
result.fields[i] = sum % (maxFields[i] + 1);
}
return result;
}
/**
* 将时间进行“减”运算,即减去一个时间
* @param time 需要减的时间
* @return 运算后的时间
*/
public int subtractTime(Time time) {
Time result = new Time();
int down = 0; // 退位标志
for (int i = 0, k = fields.length - 1; i < k; i++) {
int difference = fields[i] + down;
if (difference >= time.fields[i]) {
difference -= time.fields[i];
down = 0;
} else {
difference += maxFields[i] + 1 - time.fields[i];
down = -1;
}
result.fields[i] = difference;
}
result.fields[DAY] = fields[DAY] - time.fields[DAY] + down;
int ans=result.get(0)+result.get(1)*60+result.get(2)*24*60;
return ans;
}
}
Dijkstra
用于搜索最短时间,最低价格,最少转乘次数,两点间的所有路径
import java.io.File;
import java.net.ServerSocket;
import java.util.*;
public class Dijkstra {
LinkedHashSet<String> set;//去除重复元素用
HashMap<String,Integer> map1;
HashMap<Integer,String> map2;
public Dijkstra() {
set=new LinkedHashSet<>();
map1=new HashMap<>();
map2=new HashMap<>();
}
public void ID(Train t, Plane p){
for(int i=0;i<t.ll.size();i++){
this.set.add(t.ll.get(i).site);
}
for(int i=0;i<p.ll.size();i++){
this.set.add(p.ll.get(i).l_site);
this.set.add(p.ll.get(i).a_site);
}
Iterator it=this.set.iterator();
int m=0;
for(String str:set){
this.map1.put(str,m);
//把所有的站点都依次排好编号
this.map2.put(m,str);
//方便回传到文件中的时候流传递
m++;
}
}
public void MinPrice(Train t,Plane p,String l_site,String a_site,int op,File file){
ID(t, p);
int l=map1.get(l_site);
int a=map1.get(a_site);
int [][]price_map=new int[set.size()][set.size()];
for (int i=0;i<set.size();i++){
for(int j=0;j<set.size();j++){
if(i==j){
price_map[i][j]=0;
}else {
price_map[i][j]=-1;
}
}
}
//高铁的动态化初始价格二维数组
for(int i=0;i<t.ll.size();i++){
for(int j=0;j<t.ll.size();j++){
if(i==j){
price_map[i][j]=0;
}else if(i>j){
}else{
int temp=0;
for (int k=i+1;k<=j;k++){
temp+=t.ll.get(k).price;
}
price_map[i][j]=temp;
}
}
}
//飞机的初始化价格二维数组
for(int j=0;j<p.ll.size();j++){
int t1=map1.get(p.ll.get(j).l_site);
int t2=map1.get(p.ll.get(j).a_site);
if (price_map[t1][t2]>p.ll.get(j).price){
price_map[t1][t2]=p.ll.get(j).price;
}
}
/*for (int i=0;i<set.size();i++){
for (int j=0;j<set.size();j++){
System.out.print(price_map[i][j]+" ");
}
System.out.println();
}
set.forEach(System.out::println);*/
String[] str= set.toArray(new String[0]);
test T=new test(set.size());
T.dijkstra(price_map,str,l,a,op,file);
}
public void MinTime(Train t,Plane p,String l_site,String a_site,int op,File file){
ID(t, p);
int l=map1.get(l_site);
int a=map1.get(a_site);
int [][]time_map=new int[set.size()][set.size()];
for (int i=0;i<set.size();i++){
for(int j=0;j<set.size();j++){
if(i==j){
time_map[i][j]=0;
}else {
time_map[i][j]=-1;
}
}
}
//高铁的动态化初始时间二维数组
for(int i=0;i<t.ll.size();i++){
for(int j=0;j<t.ll.size();j++){
if(i==j){
time_map[i][j]=0;
}else if(i>j){
}else{
int temp=0;
Time t1=new Time(t.ll.get(i).l_time);
Time t2=new Time(t.ll.get(j).a_time);
temp=t2.subtractTime(t1);
time_map[i][j]=temp;
}
}
}
//飞机的初始化时间二维数组
for(int j=0;j<p.ll.size();j++){
int m1=map1.get(p.ll.get(j).l_site);
int m2=map1.get(p.ll.get(j).a_site);
Time t1=new Time(p.ll.get(j).l_time);
Time t2=new Time(p.ll.get(j).a_time);
int temp=t2.subtractTime(t1);
if (time_map[m1][m2]>temp){
time_map[m1][m2]=temp;
}
}
/*for (int i=0;i<set.size();i++){
for (int j=0;j<set.size();j++){
System.out.print(time_map[i][j]+" ");
}
System.out.println();
}*/
String[] str= set.toArray(new String[0]);
test T=new test(set.size());
T.dijkstra(time_map,str,l,a,op,file);
}
public void MinFreq(Train t,Plane p,String l_site,String a_site,int op,File file){
ID(t, p);
int l=map1.get(l_site);
int a=map1.get(a_site);
int [][]freq_map=new int[set.size()][set.size()];
for (int i=0;i<set.size();i++){
for(int j=0;j<set.size();j++){
if(i==j){
freq_map[i][j]=0;
}else {
freq_map[i][j]=-1;
}
}
}
//高铁的动态化初始时间二维数组
for(int i=0;i<t.ll.size();i++){
for(int j=0;j<t.ll.size();j++){
if(i==j){
}else if(i>j){
}else{
freq_map[i][j]=j-i;
}
}
}
//飞机的初始化时间二维数组
for(int j=0;j<p.ll.size();j++){
int m1=map1.get(p.ll.get(j).l_site);
int m2=map1.get(p.ll.get(j).a_site);
if (freq_map[m1][m2]>1||freq_map[m1][m2]==-1){
freq_map[m1][m2]=1;
}
}
String[] str= set.toArray(new String[0]);
test T=new test(set.size());
T.dijkstra(freq_map,str,l,a,op,file);
}
public void search(Train t,Plane p,String l_site,String a_site,int op,File file){
/**
* 邻接矩阵存储图的信息
* 0:两点间不可达
* 1:两点间只有高铁票
* 2:两点间只有飞机票
* 3:两点间高铁与飞机都可以到达
*/
List<Node> nodes=new ArrayList<>();
for(int i=0;i<t.ll.size()-1;i++){
String string=t.ll.get(i).site+"通过高铁到"+t.ll.get(i+1).site+"\n";
nodes.add(new Node(t.ll.get(i).site,t.ll.get(i+1).site,string));
}
Set<Node>set=new HashSet<>();
for (int i=0;i<p.ll.size();i++){
String string=p.ll.get(i).l_site+"通过飞机到"+p.ll.get(i).a_site+"\n";
set.add(new Node(p.ll.get(i).l_site,p.ll.get(i).a_site,string));
}
for(Node node:set){
nodes.add(node);
}
PointsPath.findAllPath(nodes,l_site,a_site);
}
}
test类
通过DFS实现Dijkstra算法的搜索
import java.io.*;
import java.util.*;
public class test {
private Queue visited;
int[] distance;
public test(int len) {
visited=new LinkedList();
distance=new int[len];
}
private int getIndex(Queue q,int[] dis)
{
int k=-1;
int min_num=Integer.MAX_VALUE;
for(int i=0;i<dis.length;i++)
{
if(!q.contains(i))
{
if(dis[i]<min_num)
{
min_num=dis[i];
k=i;
}
}
}
return k;
}
public void dijkstra(int[][] weight, Object[] str, int v, int a, int op, File file)
{
HashMap path;
path=new HashMap();
for(int i=0;i<str.length;i++)
path.put(i, "");
//初始化路径长度数组distance
for(int i=0;i<str.length;i++)
{
path.put(i, path.get(i)+""+str[v]);
if(i==v)
distance[i]=0;
else if(weight[v][i]!=-1)
{
distance[i]=weight[v][i];
path.put(i, path.get(i)+"-->"+str[i]);
}
else
distance[i]=Integer.MAX_VALUE;
}
visited.add(v);
while(visited.size()<str.length)
{
int k=getIndex(visited,distance);//获取未访问点中距离源点最近的点
visited.add(k);
if(k!=-1)
{
for(int j=0;j<str.length;j++)
{
if(weight[k][j]!=-1)//判断k点能够直接到达的点
{
//通过遍历各点,比较是否有比当前更短的路径,有的话,则更新distance,并更新path。
if(distance[j]>distance[k]+weight[k][j])
{
distance[j]=distance[k]+weight[k][j];
path.put(j, path.get(k)+"-->"+str[j]);
}
}
}
}
}
if(distance[a]==Integer.MAX_VALUE)
System.out.print(str[v]+"-->"+str[a]+"之间没有可通行路径");
else{
String str1,str2,str3;
str1=str[v]+"-"+str[a]+"之间的最短时间的路程的具体路径为:"+path.get(a).toString()+"\t"
+str[v]+"-"+str[a]+"的最短时间为"+distance[a]+"分钟"+"\n";
str2=str[v]+"-"+str[a]+"之间的最低价格的路程的具体路径为:"+path.get(a).toString()+"\t"
+str[v]+"-"+str[a]+"的最低价格为"+distance[a]+"元"+"\n";
str3=str[v]+"-"+str[a]+"之间的最低价格的路程的具体路径为:"+path.get(a).toString()+"\t"
+str[v]+"-"+str[a]+"的最少转乘次数为为"+distance[a]+"次"+"\n";
Writer r = null;
switch (op){
case 1:{
System.out.println(str1);
try {
r = new FileWriter(file,true);
r.write(str1);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
case 2:{
System.out.println(str2);
try{
r = new FileWriter(file,true);
r.write(str2);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
case 3:{
System.out.println(str3);
try {
r = new FileWriter(file,true);
r.write(str1);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
/*for(int h=0;h<str.length;h++)
{
System.out.printf(str[v]+"-->"+str[h]+":"+distance[h]+" ");
if(distance[h]==Integer.MAX_VALUE)
System.out.print(str[v]+"-->"+str[h]+"之间没有可通行路径");
else
System.out.print(str[v]+"-"+str[h]+"之间有最短路径,具体路径为:"+path.get(h).toString());
System.out.println();
}*/
visited.clear();
}
}
节点类
用于储存边
import java.util.Objects;
public class Node {
/**上游节点*/
private String source;
/**下游节点*/
private String target;
public String info;
public Node(String source, String target,String info) {
this.source = source;
this.target = target;
this.info=info;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Node node = (Node) o;
return node.source.equals(this.source)&&node.target.equals(this.target);
}
}
节点的所有路径搜索类
import java.util.ArrayList;
import java.util.List;
public class PointsPath {
/**当前路径*/
private static List<String> nowPath = new ArrayList<>();
public static void findAllPath(List<Node> nodeList,String source,String target){
for(int i = 0 ;i<nodeList.size();i++){
Node node = nodeList.get(i);
if(node.getSource().equals(source)){
nowPath.add(node.getSource());
if(node.getTarget().equals(target)){
nowPath.add(node.getTarget());
System.out.println("这是一条路径:"+nowPath);
/*List<Node> cur=nodeList;
for(int k=0;k<nowPath.size()-1;k++){
int index=cur.indexOf(new Node(nowPath.get(i),nowPath.get(i+1),""));
System.out.println(cur);
*//*cur.remove(index);*//*
}*/
/*因为添加了终点路径,所以要返回两次*/
nowPath.remove(nowPath.size()-1);
nowPath.remove(nowPath.size()-1);
/*已经找到路径,返回上层找其他路径*/
continue;
}
findAllPath(nodeList,node.getTarget(),target);
}
}
/*如果找不到下个节点,返回上层*/
if(nowPath.size()>0){
nowPath.remove(nowPath.size()-1);
}
}
}
Meau类
用于生成主菜单
public class Meau {
public static void meau(){
System.out.println("欢迎进入最优出行方案推荐系统");
System.out.println("---------功能清单如下----------");
System.out.println("0.退出该系统");
System.out.println("1.查询出行路线");
System.out.println("2.添加出行路线");
System.out.println("3.删除出行路线");
System.out.println("4.修改出行路线");
System.out.println("5.查询到达目的地的最优出行路线");
System.out.println("6.查询交通工具");
}
public static void add(){
System.out.println("请输入要添加的票类型为:1.高铁票 2.飞机票");
}
}
Main类,路径系统的主体
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import java.io.File;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Scanner;
public class MAIN {
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
File file_train=new File("D:\\Java_IO\\Train.txt");
//用于导入和追加高铁数据的文件地址
File file_plane=new File("D:\\Java_IO\\Plane.txt");
//用于导入和追加航班数据的文件地址
File file_routes=new File("D:\\Java_IO\\Routes.txt");
Train t=new Train();
t.Creat(file_train);
Plane p=new Plane();
p.Creat(file_plane);
boolean flag=true;
//指示标志
Meau.meau();
while(flag){
System.out.print("请输入你的选择:");
int i=sc.nextInt();
//System.in.read()读取的是字符类型,input:1 output:49 **下次注意**
/*System.out.println(i);*/
switch (i){
case 1:
t.ll.forEach(System.out::println);
p.ll.forEach(System.out::println);
break;
case 2:{
System.out.println("请输入要添加的票类型为:1.高铁票 2.飞机票");
int temp=sc.nextInt();
sc.nextLine();
/*
这个sc.nextLine用于吃掉换行符,否则下面的有效输入会被换行符直接跳过
*/
if(temp==1) {
t.ll.forEach(System.out::println);
System.out.println("请输入你要添加到的位置行数:");
int op=sc.nextInt();
sc.nextLine();
System.out.println("请依次输入车次,站点,到站时间, 发车时间,票价");
String cur = sc.nextLine();
String res[]=cur.split(" ");
t.ll.add(op-1,new TrainTicket(res[0],res[1],res[2],res[3],Integer.parseInt(res[4].trim())));
t.Write_back(file_train,t.ll,2);
/*t.Write_back(file_train,new TrainTicket(res[0],res[1],res[2],res[3],Integer.parseInt(res[4].trim())));*/
}else {
p.ll.forEach(System.out::println);
System.out.println("请输入你要添加到的位置行数:");
int op=sc.nextInt();
sc.nextLine();
System.out.println("请依次输入航班号,起点站,终点站,起飞时间,到达时间,票价=");
String cur = sc.nextLine();
String res[]=cur.split(" ");
p.ll.add(op-1,new PlaneTicket(res[0],res[1],res[2],res[3],res[4],Integer.parseInt(res[5].trim())));
p.Write_back(file_plane,p.ll,2);
/*p.Write_back(file_plane,new PlaneTicket(res[0],res[1],res[2],res[3],res[4],Integer.parseInt(res[5].trim())));*/
}
break;
}
case 3:{
System.out.println("请输入要删除的票类型为:1.高铁票 2.飞机票");
int temp=sc.nextInt();
sc.nextLine();
if(temp==1) {
System.out.println("请依次输入车次,站点,到站时间, 发车时间,票价");
String cur = sc.nextLine();
String res[]=cur.split(" ");
TrainTicket tt=new TrainTicket(res[0],res[1],res[2],res[3],Integer.parseInt(res[4].trim()));
t.ll.remove(tt);
t.Write_back(file_train,t.ll,3);
}else {
System.out.println("请依次输入航班号,起点站,终点站,起飞时间,到达时间,票价");
String cur = sc.nextLine();
String res[]=cur.split(" ");
PlaneTicket pt=new PlaneTicket(res[0],res[1],res[2],res[3],res[4],Integer.parseInt(res[5].trim()));
p.ll.remove(pt);
p.Write_back(file_plane,p.ll,3);
}
break;
}
case 4:{
System.out.println("请输入要修改的票类型:1.高铁票,2.飞机票");
int temp=sc.nextInt();
sc.nextLine();
if(temp==1) {
t.ll.forEach(System.out::println);
System.out.println("请输入你要修改的行数:");
int index=sc.nextInt();
sc.nextLine();
t.ll.remove(index-1);
//大多数人习惯从1开始,所以这里把指示减一处理
System.out.println("请依次输入修改后的车次,站点,到站时间, 发车时间,票价");
String cur = sc.nextLine();
String res[]=cur.split(" ");
TrainTicket tt=new TrainTicket(res[0],res[1],res[2],res[3],Integer.parseInt(res[4].trim()));
t.ll.add(index-1,tt);
t.Write_back(file_train,t.ll,4);
}else {
p.ll.forEach(System.out::println);
System.out.println("请输入你要修改的行数:");
int index=sc.nextInt();
sc.nextLine();
p.ll.remove(index-1);
System.out.println("请依次输入航班号,起点站,终点站,起飞时间,到达时间,票价");
String cur = sc.nextLine();
String res[]=cur.split(" ");
PlaneTicket pt=new PlaneTicket(res[0],res[1],res[2],res[3],res[4],Integer.parseInt(res[5].trim()));
p.ll.add(index-1,pt);
p.Write_back(file_plane,p.ll,4);
}
break;
}
case 5:{
System.out.println("请输入你要查询的最优路线种类:1.最短时间2.最低价格3.最少转乘次数");
int op=sc.nextInt();
System.out.println("请输入你的起点与终点:(ps请用空格隔开两地)");
sc.nextLine();
String str=sc.nextLine();
String[]cur=str.split(" ");
Dijkstra dijkstra=new Dijkstra();
if(op==1){
dijkstra.MinPrice(t,p,cur[0],cur[1],op,file_routes);
}else if(op==2){
dijkstra.MinTime(t,p,cur[0],cur[1],op,file_routes);
}else {
dijkstra.MinFreq(t,p,cur[0],cur[1],op,file_routes);
}
/* System.out.println(dijkstra.set.size());*/
break;
}
case 6:{
Dijkstra dijkstra=new Dijkstra();
dijkstra.search(t,p,"武汉","深圳",1,file_routes);
break;
}
case 0:
flag=false;
System.out.println("已成功退出系统,欢迎下次继续使用");
break;
default:
System.out.println("输入的选项有误,请重新输入0~5之间的选项");
break;
}
}
}
}
794

被折叠的 条评论
为什么被折叠?



