公交路线查询系统

简介: 公交路线查询系统

一:目标一:类的定义+构造方法 +set和get方法:

class Route{
    //成员变量体现封装性
    private String name;
    private String[] stations;
    private String startStation;
    private String endStation;
    private double fare;
    private double distance;
    //不带参数的构造方法
    public Route() {
    }
    //带参数的构造方法
    public Route(String name, String[] stations, String startStation, String endStation) {
        this.name = name;
        this.stations = stations;
        this.startStation = startStation;
        this.endStation = endStation;
    }
    //set和get方法
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String[] getStations() {
        return stations;
    }
    public void setStations(String[] stations) {
        this.stations = stations;
    }
    public String getStartStation() {
        return startStation;
    }
    public void setStartStation(String startStation) {
        this.startStation = startStation;
    }
    public String getEndStation() {
        return endStation;
    }
    public void setEndStation(String endStation) {
        this.endStation = endStation;
    }
    public void setFare(double fare) {
        this.fare = fare;
    }
    public void setDistance(double distance) {
        this.distance = distance;
    }
    //查询路线的方法
    public void queryStations() {
        System.out.println(getName() + "的站点有:");
        for (String station : stations) {
            System.out.print(station+" ");
        }
        System.out.println();
    }
    public double getDistance() {
        // 计算距离的逻辑
        return stations.length * 10;
    }
    public double getFare() {
        // 计算费用的逻辑
        return stations.length * 1;
    }
}
public class Test {
    public static void main(String[] args) {
        //创建对象
       Route route1=new Route("101",new String[]{"S1","S2","S3","S4","S5"},"S1","S5");
       Route route2=new Route("102",new String[]{"S2","S3","S4","S5","S6"},"S2","S6");
       route1.queryStations();
       System.out.println("路程为:"+route1.getDistance()+"  "+"费用为"+route1.getFare());
       route2.queryStations();
    }


目标二:静态属性 +静态方法 +toString方法:

mport java.util.Arrays;
class Route{
    //静态成员变量
    public static double fare;
    public static double distance;
    private String name;
    private String[] stations;
    private String startStation;
    private String endStation;
    //不带参数的构造方法
    public Route() {
    }
    //带参数的构造方法
    public Route(String name, String[] stations, String startStation, String endStation) {
        this.name = name;
        this.stations = stations;
        this.startStation = startStation;
        this.endStation = endStation;
    }
    //set和get方法
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String[] getStations() {
        return stations;
    }
    public void setStations(String[] stations) {
        this.stations = stations;
    }
    public String getStartStation() {
        return startStation;
    }
    public void setStartStation(String startStation) {
        this.startStation = startStation;
    }
    public String getEndStation() {
        return endStation;
    }
    public void setEndStation(String endStation) {
        this.endStation = endStation;
    }
    public void setFare(double fare) {
        this.fare = fare;
    }
    public void setDistance(double distance) {
        this.distance = distance;
    }
    //查询路线的方法
    public void queryStations() {
        System.out.println("查询的信息为:");
        System.out.println(toString());
    }
    @Override
    public String toString() {
        return "Route{" +
                "name='" + name + '\'' +
                ", stations=" + Arrays.toString(stations) +
                ", startStation='" + startStation + '\'' +
                ", endStation='" + endStation + '\'' +
                '}';
    }
}
public class Test {
    //静态成员方法
    public static void print(){
        System.out.println("欢迎下次查询");
    }
    public static void main(String[] args) {
        Route.fare=5.0;
        Route.distance=10.0;
        Route route1=new Route("101",new String[]{"S1","S2","S3","S4","S5"},"S1","S5");
        route1.queryStations();
        System.out.println("路程为:"+ Route.fare+"  "+"费用为"+Route.distance);
        print();
    }
}


目标三:抽象类的定义 +抽象方法 + 实际应用:abstract class AbstractRoute{

    //抽象类AbstractRoute,3个抽象方法getFare(),getDistance(),queryStations()
    public abstract double getFare();
    public abstract double getDistance();
   public abstract void queryStations();
}
class SpecificRoute extends AbstractRoute{
    //子类SpecificRoute继承父类AbstractRoute
    private String name;
    private String[] stations;
    private String startStation;
    private String endStation;
    public SpecificRoute() {
    }
    public SpecificRoute(String name, String[] stations, String startStation, String endStation) {
        this.name = name;
        this.stations = stations;
        this.startStation = startStation;
        this.endStation = endStation;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String[] getStations() {
        return stations;
    }
    public void setStations(String[] stations) {
        this.stations = stations;
    }
    public String getStartStation() {
        return startStation;
    }
    public void setStartStation(String startStation) {
        this.startStation = startStation;
    }
    public String getEndStation() {
        return endStation;
    }
    public void setEndStation(String endStation) {
        this.endStation = endStation;
    }
    //重写父类的抽象方法
    @Override
    public double getFare() {
        return this.stations.length*1;
    }
    @Override
    public double getDistance() {
        return this.stations.length*2;
    }
    @Override
    public void queryStations() {
        System.out.println(getName()+"的站点有:");
        for (String station: stations) {
            System.out.print(station+" ");
        }
        System.out.println();
    }
}
public class Test{
    public static void main(String[] args) {
        AbstractRoute abstractRoute=new SpecificRoute("101",new String[]{"S1","S2","S3","S4","S5"},
                "S1","S5");
        abstractRoute.queryStations();
        System.out.println("路程为:"+ abstractRoute.getDistance()+"公里"+"  "+"费用为"+abstractRoute.getFare()+"元");
    }
}


目标四:接口的定义+实现接口类的定义+实际应用

import java.util.Arrays;
//定义了一个接口Route
interface Route {
    //接口中的抽象方法
    public abstract double getFare();
    double getDistance();
    void addStation(String station);
    void removeStation(String station);
    void updateStation(String oldStation,String newStation);
    void queryStations();
}
abstract class AbstractRoute implements Route{
    private String name;
    public  String[] stations;
    private String startStation;
    private String endStation;
    public AbstractRoute(String name, String[] stations, String startStation, String endStation) {
        this.name = name;
        this.stations = stations;
        this.startStation = startStation;
        this.endStation = endStation;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String[] getStations() {
        return stations;
    }
    public void setStations(String[] stations) {
        this.stations = stations;
    }
    public String getStartStation() {
        return startStation;
    }
    public void setStartStation(String startStation) {
        this.startStation = startStation;
    }
    public String getEndStation() {
        return endStation;
    }
    public void setEndStation(String endStation) {
        this.endStation = endStation;
    }
}
class SpecificRoute extends AbstractRoute{
    public SpecificRoute(String name, String[] stations, String startStation, String endStation) {
        super(name, stations, startStation, endStation);
    }
    @Override
    public double getFare() {
        return stations.length*1;
    }
    @Override
    public double getDistance() {
        return stations.length*2;
    }
    //增加数据信息
    @Override
    public void addStation(String station){
        System.out.println("要增加的站点信息为:"+station);
        int index=0;
        while ((index<stations.length)){
            index++;
        }
        if(index<stations.length){
            stations[index]=station;
        }
        else{
            stations= Arrays.copyOf(stations,stations.length+1);
            stations[index]=station;
        }
    }
    //删除数据信息
    @Override
    public void removeStation(String station) {
        System.out.println("要删除的站点为:"+station);
        boolean find=false;
        for(int i=0;i<stations.length;i++){
            if(stations[i].equals(station)){
                find = true;
                int j=i;
                for (; j < stations.length-1; j++) {
                    stations[j]=stations[j+1];
                }
            }
        }
        if(find){
            stations=Arrays.copyOf(stations,stations.length-1);
        }
    }
    //更改数据信息
    @Override
    public void updateStation(String oldStation, String newStation) {
        System.out.println("要更改的站点信息为:"+oldStation+"->"+newStation);
        for (int i = 0; i < stations.length; i++) {
            if(stations[i].equals(oldStation)){
                stations[i]=newStation;
                break;
            }
        }
    }
    //查找公交站点信息
    @Override
    public void queryStations() {
        System.out.println(getName() + "的站点有:");
        for (int i = 0; i < stations.length; i++) {
            System.out.print(stations[i]+" ");
        }
        System.out.println();
    }
}
public class Test {
    public static void main(String[] args) {
        AbstractRoute abstractRoute=new SpecificRoute("101",new String[]{"S1","S2","S3","S4","S5"},
                "S1","S5");
        abstractRoute.queryStations();
        abstractRoute.addStation("S6");
        abstractRoute.queryStations();
       abstractRoute.removeStation("S2");
        abstractRoute.queryStations();
        abstractRoute.updateStation("S3","S7");
        abstractRoute.queryStations();
        System.out.println("路程为:"+ abstractRoute.getDistance()+"公里"+"  "+"费用为"+abstractRoute.getFare()+"元");
    }
}

终极目标:集合泛型定义+集合添加元素 +集合显示内容:

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
interface Route {
//接口
    double getDistance();
    double getFare();
    void addStation(String station);
    void removeStation(String station);
    void updateStation(String oldStation, String newStation);
    void queryStations();
}
abstract class AbstractRoute implements Route {
    //抽象类 AbstractRoute 实现接口 Route
    protected String name;
    //泛型
    protected List<String> stations;
    public AbstractRoute(String name, List<String> stations) {
        this.name = name;
        this.stations =stations;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<String> getStations() {
        return stations;
    }
    public void setStations(List<String> stations) {
        this.stations = stations;
    }
    public double getDistance() {
        // 计算距离的逻辑
        return 0;
    }
    @Override
    public double getFare() {
        // 计算费用的逻辑
        return 0;
    }
    //向集合中添加数据信息
    @Override
    public void addStation(String station) {
        System.out.println("要增加的站点信息为:"+station);
        stations.add(station);
    }
    //删除集合中的数据信息
    @Override
    public void removeStation(String station) {
        System.out.println("要删除的站点信息为:"+station);
        stations.remove(station);
    }
    //更改结合中的数据信息
    @Override
    public void updateStation(String oldStation, String newStation) {
        System.out.println("要更改的站点信息为:"+oldStation+"->"+newStation);
        int index = stations.indexOf(oldStation);
        if (index != -1) {
            stations.set(index, newStation);
        }
    }
    //查看集合中的数据信息
    @Override
    public void queryStations() {
        System.out.println(getName() + "的站点有:");
        for (String station : stations) {
            System.out.println(station);
        }
    }
}
class SpecificRoute extends AbstractRoute {
    //SpecificRoute 继承抽象类 AbstractRoute
    public String startStation;
    public String endStation;
    public SpecificRoute(String name, List<String> stations, String startStation, String endStation) {
        super(name, stations);
        this.startStation = startStation;
        this.endStation = endStation;
    }
    @Override
    public double getDistance() {
        return stations.size() * 2;
    }
    @Override
    public double getFare() {
        return stations.size() * 1;
    }
    @Override
    public void queryStations() {
        System.out.println(getName() + "的站点有:");
        for (String station : stations) {
            System.out.print(station+" ");
        }
        System.out.println();
    }
}
public class TestRouteSystem {
    public static void main(String[] args) {
        Route route = new SpecificRoute("Route 1", new ArrayList<>(Arrays.asList("S1", "S2", "S3",
                "S4", "S5")),"S1", "S5");
        route.addStation("S6");
        route.removeStation("S2");
        route.updateStation("S3", "S7");
        route.queryStations();
        System.out.println("距离:" + route.getDistance() + ",费用:" + route.getFare());
    }
}
目录
相关文章
|
4月前
|
Java
设计信息录入界面,完成人员基本信息的录入工作,其中籍贯中涉及的“省、市”能实现联动,即选择“省”时,“市”会根据选择的“省”做相应的变换。
该博客文章展示了如何设计一个包含联动下拉列表(省、市)的信息录入界面,使用Java Swing组件库实现人员基本信息的录入,并提供了完整的界面布局和事件处理代码。
设计信息录入界面,完成人员基本信息的录入工作,其中籍贯中涉及的“省、市”能实现联动,即选择“省”时,“市”会根据选择的“省”做相应的变换。
|
7月前
|
算法
航班管理系统(最全最细)
航班管理系统(最全最细)
|
NoSQL 定位技术 数据库
GIS如何分析台风影响范围和受灾人数
GIS如何分析台风影响范围和受灾人数
154 0
|
JSON 分布式计算 大数据
行政区统计_功能实现|学习笔记
快速学习行政区统计_功能实现
121 0
行政区统计_功能实现|学习笔记
好客租房135-获取并处理城市列表数据(热门处理数据)
好客租房135-获取并处理城市列表数据(热门处理数据)
136 0
好客租房135-获取并处理城市列表数据(热门处理数据)
|
索引
好客租房145-渲染城市列表(展示城市索引)
好客租房145-渲染城市列表(展示城市索引)
162 0
好客租房145-渲染城市列表(展示城市索引)
好客租房133-获取并处理城市列表数据
好客租房133-获取并处理城市列表数据
100 0
好客租房133-获取并处理城市列表数据
|
数据格式
好客租房134-获取并处理城市列表数据
好客租房134-获取并处理城市列表数据
111 0
好客租房134-获取并处理城市列表数据
|
供应链 数据可视化 BI
OA系统日常物资管理方案:物资有序分类,数量动态展现
OA系统以“流程+建模”为主要手段,针对电商和各大组织日常的物资采购,打通采购、资产、财务等模块,提升库存管理效率…
《玩法变了》销售状况表、产品属性表
利润率 单品 销量占比 总利润占比 理想的占比 行动计划 0.05 A B C D 0.1 E 0.2 F ..... 必备属性 产品必须具备的属性。
737 0