• 石家庄地铁模拟


    实验要求

    https://pan.baidu.com/s/1kqfW9f28Z7wkDIYDDsh_XQ

    源代码

    package subway;
    
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;
    
    class Subway{
        private String lineNumber=null;
        private String lineName=null;
        private String transferInformation=null;
        public void lineInformation() {}
        public void querySite() {}
        public int buyTicket(int siteNumber) {
            if(siteNumber%3==0)
                return siteNumber/3;
            else
                return siteNumber/3+1;
        }
        public String getLineNumber() {
            return lineNumber;
        }
        public void setLineNumber(String lineNumber) {
            this.lineNumber = lineNumber;
        }
        public String getLineName() {
            return lineName;
        }
        public void setLineName(String lineName) {
            this.lineName = lineName;
        }
        public String getTransferInformation() {
            return transferInformation;
        }
        public void setTransferInformation(String transferInformation) {
            this.transferInformation = transferInformation;
        }
    }
    class FirstLineSubway extends Subway{
        private String[] lineNames;     //1号线所有站点名
        FirstLineSubway(){
            super();
            try {
                BufferedReader br = new BufferedReader(new FileReader("shijiazhuang-subway.txt"));
                String firstLine = br.readLine();
                String[] str = firstLine.split(",");
                setLineNumber(str[0]);
                setLineName(str[1]);
                setTransferInformation(str[2]);
                lineNames = getLineName().split("、");
                br.close();
            } catch (FileNotFoundException e1) {
                System.out.println("shijiazhuang-subway.txt文件不存在!");
            } catch (IOException e2) {
                System.out.println("IO流异常!");
            }
        }
        public void lineInformation() {
            for(int i=0;i<lineNames.length;i++){
                if(lineNames[i].compareTo(getTransferInformation()) == 0)
                    System.out.println(getLineNumber()+ ":" + lineNames[i] + "	可换乘3号线");
                else
                    System.out.println(getLineNumber()+ ":"  + lineNames[i]);
            }
        }
        public String[] querySite(String begin,String end) {
            int be=-1,en=-1,mid=-1;
            String[] s = new String[6];
            for(int i=0;i<lineNames.length;i++){
                if(lineNames[i].compareTo(getTransferInformation()) == 0)
                    mid = i;
                if(lineNames[i].compareTo(begin) == 0)
                    be = i;
                if(lineNames[i].compareTo(end) == 0)
                    en = i;
            }
            if(be>-1 && en>-1) {
                s[0]="true";       //s[0]代表是否为起点,若是则s[1]为起点站在数组内位置。
                s[1]=Integer.toString(be);
                s[2]="true";       //s[2]代表是否为终点站,若是则s[3]为终点站在数组内位置。
                s[3]=Integer.toString(en);
                s[4]=Integer.toString(be<en?en-be:be-en);   //s[4]为站点数差值。
            }
            else if(be>-1 && en==-1 && be!=mid) {
                s[0]="true";
                s[1]=Integer.toString(be);
                s[2]="false";
                s[3]="-1";
                s[4]= Integer.toString(be<mid?mid-be:be-mid);
            }
            else if(be==-1 && en!=-1 && en!=mid) {
                s[0]="false";
                s[1]="-1";
                s[2]="true";
                s[3]=Integer.toString(en);
                s[4]= Integer.toString(en<mid?mid-en:en-mid);
            }
            else {
                s[0]="false";
                s[1]="-1";
                s[2]="false";
                s[3]="-1";
                s[4]="0";
            }
            s[5]=Integer.toString(mid);    //s[5]为换乘站点(即新百广场)在数组内的位置
            return s;
        }
        public String[] getLineNames() {
            return lineNames;
        }
    }
    class ThirdLineSubway extends Subway{
        private String[] lineNames;     //3号线所有站点名
        ThirdLineSubway(){
            super();
            try {
                BufferedReader br = new BufferedReader(new FileReader("shijiazhuang-subway.txt"));
                String thirdLine = br.readLine();
                thirdLine = br.readLine();
                String[] str = thirdLine.split(",");
                setLineNumber(str[0]);
                setLineName(str[1]);
                setTransferInformation(str[2]);
                lineNames = getLineName().split("、");
                br.close();
            } catch (FileNotFoundException e1) {
                System.out.println("shijiazhuang-subway.txt文件不存在!");
            } catch (IOException e2) {
                System.out.println("IO流异常!");
            }
        }
        public void lineInformation() {
            for(int i=0;i<lineNames.length;i++){
                if(lineNames[i].compareTo(getTransferInformation()) == 0)
                    System.out.println(getLineNumber()+ ":"  + lineNames[i] + "	可换乘1号线");
                else
                    System.out.println(getLineNumber()+ ":"  + lineNames[i]);
            }
        }
        public String[] querySite(String begin,String end) {
            int be=-1,en=-1,mid=-1;
            String[] s = new String[6];
            for(int i=0;i<lineNames.length;i++){
                if(lineNames[i].compareTo(getTransferInformation()) == 0)
                    mid = i;
                if(lineNames[i].compareTo(begin) == 0)
                    be = i;
                if(lineNames[i].compareTo(end) == 0)
                    en = i;
            }
            if(be>-1 && en>-1) {
                s[0]="true";
                s[1]=Integer.toString(be);
                s[2]="true";
                s[3]=Integer.toString(en);
                s[4]=Integer.toString(be<en?en-be:be-en);
            }
            else if(be>-1 && en==-1 && be!=mid) {
                s[0]="true";
                s[1]=Integer.toString(be);
                s[2]="false";
                s[3]="-1";
                s[4]= Integer.toString(be<mid?mid-be:be-mid);
            }
            else if(be==-1 && en!=-1 && en!=mid) {
                s[0]="false";
                s[1]="-1";
                s[2]="true";
                s[3]=Integer.toString(en);
                s[4]= Integer.toString(en<mid?mid-en:en-mid);
            }
            else {
                s[0]="false";
                s[1]="-1";
                s[2]="false";
                s[3]="-1";
                s[4]="0";
            }
            s[5]=Integer.toString(mid);
            return s;
        }
        public String[] getLineNames() {
            return lineNames;
        }
    }
    public class SubwayManager {
        public static void main(String[] args) {
            int i;
            Scanner sc = new Scanner(System.in);
            Subway s = new Subway();
            FirstLineSubway fls = new FirstLineSubway();
            ThirdLineSubway tls = new ThirdLineSubway();
            do
            {
                welcome1();
                welcome2();
                i=sc.nextInt();
                switch(i) {
                case 1:
                    System.out.print("请线路号(1或3;0显示全部):");
                    i=sc.nextInt();
                    line();
                    switch(i) {
                    case 0:fls.lineInformation();tls.lineInformation();break;
                    case 1:fls.lineInformation();break;
                    case 3:tls.lineInformation();break;
                    default:System.out.println("请正确选择!");
                    }break;
                case 2:
                    welcome1();
                    System.out.print("起始站:");
                    String begin = sc.next();
                    System.out.print("终点站:");
                    String end = sc.next();
                    System.out.print("购票数量:");
                    int number = sc.nextInt();
                    System.out.println("***************************************************************");
                    welcome1();
                    String[] s1 = fls.querySite(begin, end);
                    String[] s2 = tls.querySite(begin, end);
                    String[] str1 = fls.getLineNames();
                    String[] str2 = tls.getLineNames();
                    int be,en,unitPrice=0;
                    //全在1号线
                    if(s1[0].compareTo("true")==0 && s1[2].compareTo("true")==0) {
                        System.out.println(s1[4] + "站");
                        be=Integer.parseInt(s1[1]);
                        en=Integer.parseInt(s1[3]);
                        if(en>be)
                            for(i=be;i<=en;i++)
                                System.out.println(str1[i]);
                        else
                            for(i=be;i>=en && i>=0;i--)
                                System.out.println(str1[i]);
                        unitPrice = s.buyTicket(Integer.parseInt(s1[4]));
                    }
                    //一半在1号线
                    if(s1[0].compareTo("true")==0 && s1[2].compareTo("false")==0) {
                        System.out.println(Integer.parseInt(s1[4]) + Integer.parseInt(s2[4]) + "站");
                        be=Integer.parseInt(s1[1]);
                        en=Integer.parseInt(s1[5]);
                        if(en>be)
                            for(i=be;i<en;i++)
                                System.out.println(str1[i]);
                        else
                            for(i=be;i>en && i>=0;i--)
                                System.out.println(str1[i]);
                        System.out.println(str1[i] + "	换乘3号线");
                        be=Integer.parseInt(s2[5]);
                        en=Integer.parseInt(s2[3]);
                        if(en>be)
                            for(i=be+1;i<=en;i++)
                                System.out.println(str2[i]);
                        else
                            for(i=be-1;i>=en && i>=0;i--)
                                System.out.println(str2[i]);
                        unitPrice = s.buyTicket(Integer.parseInt(s1[4]) + Integer.parseInt(s2[4]));
                    }
                    if(s1[0].compareTo("false")==0 && s1[2].compareTo("true")==0) {
                        System.out.println(Integer.parseInt(s1[4]) + Integer.parseInt(s2[4]) + "站");
                        be=Integer.parseInt(s2[1]);
                        en=Integer.parseInt(s2[5]);
                        if(en>be)
                            for(i=be;i<en;i++)
                                System.out.println(str2[i]);
                        else
                            for(i=be;i>en && i>=0;i--)
                                System.out.println(str2[i]);
                        System.out.println(str2[i] + "	换乘1号线");
                        be=Integer.parseInt(s1[5]);
                        en=Integer.parseInt(s1[3]);
                        if(en>be)
                            for(i=be+1;i<=en;i++)
                                System.out.println(str1[i]);
                        else 
                            for(i=be-1;i>=en && i>=0;i--)
                                System.out.println(str1[i]);
                        unitPrice = s.buyTicket(Integer.parseInt(s1[4]) + Integer.parseInt(s2[4]));
                    }
                    //全在3号线
                    if(s1[0].compareTo("false")==0 && s1[2].compareTo("false")==0) {
                        System.out.println(s2[4] + "站");
                        be=Integer.parseInt(s2[1]);
                        en=Integer.parseInt(s2[3]);
                        if(en>be)
                            for(i=be;i<=en;i++)
                                System.out.println(str2[i]);
                        else
                            for(i=be;i>=en && i>=0;i--)
                                System.out.println(str2[i]);
                        unitPrice = s.buyTicket(Integer.parseInt(s2[4]));
                    }
                    System.out.println("购票单价:" + unitPrice + "元");
                    System.out.println("购票金额:" + unitPrice*number + "元");
                    System.out.println("***************************************************************");
                    break;
                default:System.out.println("请正确选择!");
                }
            }while(true);
        }
        public static void welcome1() {
            System.out.println("***************************************************************");
            System.out.println("欢迎乘坐石家庄地铁");
            System.out.println("***************************************************************");
        }
        public static void welcome2() {
            System.out.println("1、显示具体线路信息;");
            System.out.println("2、购票;");
            System.out.println("***************************************************************");
        }
        public static void line() {
            System.out.println("***************************************************************");
            System.out.println("石家庄地铁线路图");
            System.out.println("***************************************************************");
        }
    }

    shijiazhuang-subway.txt内容

    1号线,洨河大道、南村、石家庄东站、火炬广场、留村、白佛、朝晖桥、谈固、北宋、体育场、博物院、北国商城、平安大街、解放广场、新百广场、烈士陵园、和平医院、长城桥、时光街、西王,新百广场
    3号线,市二中、新百广场、东里、槐安桥、西三教、石家庄站,新百广场

  • 相关阅读:
    八月二十九学习报告
    文本操作
    EL表达式
    注解开发
    逆向
    内置对象和方法
    每日日报2020.11.10 1905
    每日日报2020.11.12 1905
    每日日报2020.11.17 1905
    每日日报2020.11.20 1905
  • 原文地址:https://www.cnblogs.com/dream0-0/p/9821113.html
Copyright © 2020-2023  润新知