定义了两个类,在Subway类中定义三个私有数据变量,线路号,经过站点,换乘站。以及4个方法分别实现从txt文件中导入线路信息;输出线路信息;查询两个站点经过站点数,并输出经过站点以及在某站换乘几号线;计算票价。在SubwayManger中打印界面并根据用户选择调用相关的函数。
Subway:
package subway;
import java.util.*;
import java.io.*;
import java.math.*;
class Subway {
private String num;//存放线路号
private String[] str=new String[21];//存放站点名称
private String str1;//存放换乘站点信息
public Subway() {
num="0";
for(int j=0;j<21;j++) {
str[j]=" ";
}
str1="新百广场";
}
public void get() {//输出线路信息
System.out.print(num+"号线:");
for(int i=0;str[i]!=" ";i++) {
System.out.print("站点"+(i+1)+":"+str[i]+";");
}
System.out.println();
}
public int chaxun(String t1,String t2,Subway s1,Subway s2) {//查询两个站点间的经过站点
int k,k1=0,k2=0;
boolean bo1=false;
boolean bo2=false;
List<String> list1=Arrays.asList(s1.str);
List<String> list2=Arrays.asList(s2.str);
if(list1.contains(t1)&&list1.contains(t2)) {
k1=list1.indexOf(t1);
k2=list1.indexOf(t2);
k=Math.abs(k1-k2);
System.out.println("经过"+k+"站");
if(k1<k2) {
while(k1<=k2) {
System.out.println(s1.str[k1]);
++k1;
}
}
if(k1>k2) {
while(k1>=k2) {
System.out.println(s1.str[k1]);
--k1;
}
}
return k;
}//end if1
if(list2.contains(t1)&&list2.contains(t2)) {
k1=list2.indexOf(t1);
k2=list2.indexOf(t2);
k=Math.abs(k1-k2);
System.out.println("经过"+k+"站");
if(k1<k2) {
while(k1<=k2) {
System.out.println(s2.str[k1]);
++k1;
}
}
if(k1>k2) {
while(k1>=k2) {
System.out.println(s2.str[k1]);
--k1;
}
}
return k;
}//end if2
else {
int k3=0,k4=0;
if(list1.contains(t1)) {
k1=list1.indexOf(t1);
k3=Math.abs(k1-14);
bo1=true;
}
if(list1.contains(t2)) {
k2=list1.indexOf(t2);
k4=Math.abs(k2-14); }
if(list2.contains(t1)) {
k1=list2.indexOf(t1);
k3=Math.abs(k1-1);
bo2=true;
}
if(list2.contains(t2)) {
k2=list1.indexOf(t2);
k4=Math.abs(k1-1);
}
k=k3+k4;
System.out.println("经过"+k+"站");
if(bo1==true) {
if(k1>14) {
while(k1>=14) {
System.out.println(s1.str[k1]);
--k1;
}
System.out.println("转乘3号线");
}
else if(k1<14) {
while(k1<=14) {
System.out.println(s1.str[k1]);
++k1;
}
System.out.println("转乘3号线");
}
int i=1;
if(k2>i) {
while(i<k2) {
System.out.println(s2.str[i+1]);
++i;
}
}
else if(k2<i) {
while(i>k2) {
System.out.println(s2.str[i-1]);
--i;
}
} }//end if1
else if(bo2==true) {
if(k1>1) {
while(k1>=1) {
System.out.println(s2.str[k1]);
--k1;
}
System.out.println("转乘1号线");
}
else if(k1<1) {
while(k1<=1) {
System.out.println(s2.str[k1]);
++k1;
}
System.out.println("转乘1号线");
}
int i=14;
if(k2>i) {
while(i<k2) {
System.out.println(s1.str[i+1]);
++i;
}
}
else if(k2<i) {
while(i>k2) {
System.out.println(s1.str[i-1]);
--i;
}
}
}//end if2 return k;
}//end else
}
public void goupiao(int k,int q) {//计算票价
int x=1,k3=1,k4=1;
k3=k/3;
k4=k-k3*3;
if(k4==0) {
x=k3;
}
else {
x=(k3+1);
} System.out.println("购票单价:"+x);
System.out.println("购票总价:"+x*q);
}
public void readFile(Subway s1,Subway s2) { try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("shijiazhuang-subway.txt"),"UTF-8"));
s1.num=br.readLine();
String line;
while ((line = br.readLine()) != null) {
int i=0;
String [] arr = line.split("\\s+");
for(String ss : arr){
s1.str[i]=ss;
i++;
}
break;
}//end while
while ((line = br.readLine()) != null) {
s1.str1=line;
break;
}
while ((line = br.readLine()) != null) {
s2.num=line;
break;
} while ((line = br.readLine()) != null) {
int i=0;
String [] arr = line.split("\\s+");
for(String ss : arr){
s2.str[i]=ss;
i++;
}
break;
}//end while
while ((line = br.readLine()) != null) {
s2.str1=br.readLine();
break;
} }//end try
catch (IOException e){
e.printStackTrace();
} }//end readFile }
SubwayManger:
package subway;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class SubwayManger { public static void main(String[] args) {
Subway s1=new Subway();
Subway s2=new Subway();
Scanner sca=new Scanner(System.in);
int n;
s1.readFile(s1, s2);
System.out.println("***************************************************************");
System.out.println(" 欢迎乘坐石家庄地铁");
System.out.println("***************************************************************");
System.out.println(" 1、显示具体线路信息");
System.out.println(" 2、购票");
System.out.println("***************************************************************");
n=sca.nextInt();
if(n==1) {
System.out.print("请输入需要查询的线路(1:一号线,3:三号线,0:全部线路)");
int n1=sca.nextInt();
if(n1==1) {
System.out.println("***************************************************************");
System.out.println(" 石家庄地铁线路图");
System.out.println("***************************************************************");
s1.get();
System.out.println("***************************************************************");
}
if(n1==3) {
System.out.println("***************************************************************");
System.out.println(" 石家庄地铁线路图");
System.out.println("***************************************************************");
s2.get();
System.out.println("***************************************************************");
}
if(n1==0) {
System.out.println("***************************************************************");
System.out.println(" 石家庄地铁线路图");
System.out.println("***************************************************************");
s1.get();
s2.get();
System.out.println("***************************************************************");
}
}//end n=1
if(n==2) {
String t1,t2;
int q=1,q1=0,k=0;
System.out.println("***************************************************************");
System.out.println(" 欢迎乘坐石家庄地铁");
System.out.println("***************************************************************");
System.out.println("请输入起始地:");
t1=sca.next();
System.out.println("请输入目的地:");
t2=sca.next();
System.out.println("请输入购票数:");
q=sca.nextInt();
k=s1.chaxun(t1, t2, s1, s2);
s1.goupiao(k, q);
}//end n=2
}//end main }//end SubwayManger