公交路线查询系统

2023-11-27 07:44
文章标签 系统 查询 公交路线

本文主要是介绍公交路线查询系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

公交路线查询系统

  • 一:目标一:类的定义+构造方法 +set和get方法:
  • 目标二:静态属性 +静态方法 +toString方法:
  • 目标三:抽象类的定义 +抽象方法 + 实际应用:abstract class AbstractRoute{
  • 目标四:接口的定义+实现接口类的定义+实际应用
  • 终极目标:集合泛型定义+集合添加元素 +集合显示内容:

一:目标一:类的定义+构造方法 +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());}@Overridepublic 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继承父类AbstractRouteprivate 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;}//重写父类的抽象方法@Overridepublic double getFare() {return this.stations.length*1;}@Overridepublic double getDistance() {return this.stations.length*2;}@Overridepublic 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);}@Overridepublic double getFare() {return stations.length*1;}@Overridepublic double getDistance() {return stations.length*2;}//增加数据信息@Overridepublic 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;}}//删除数据信息@Overridepublic 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);}}//更改数据信息@Overridepublic 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;}}}//查找公交站点信息@Overridepublic 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 实现接口 Routeprotected 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;}@Overridepublic double getFare() {// 计算费用的逻辑return 0;}//向集合中添加数据信息@Overridepublic void addStation(String station) {System.out.println("要增加的站点信息为:"+station);stations.add(station);}//删除集合中的数据信息@Overridepublic void removeStation(String station) {System.out.println("要删除的站点信息为:"+station);stations.remove(station);}//更改结合中的数据信息@Overridepublic void updateStation(String oldStation, String newStation) {System.out.println("要更改的站点信息为:"+oldStation+"->"+newStation);int index = stations.indexOf(oldStation);if (index != -1) {stations.set(index, newStation);}}//查看集合中的数据信息@Overridepublic void queryStations() {System.out.println(getName() + "的站点有:");for (String station : stations) {System.out.println(station);}}
}
class SpecificRoute extends AbstractRoute {//SpecificRoute 继承抽象类 AbstractRoutepublic 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;}@Overridepublic double getDistance() {return stations.size() * 2;}@Overridepublic double getFare() {return stations.size() * 1;}@Overridepublic 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());}
}

这篇关于公交路线查询系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/427303

相关文章

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Linux系统中卸载与安装JDK的详细教程

《Linux系统中卸载与安装JDK的详细教程》本文详细介绍了如何在Linux系统中通过Xshell和Xftp工具连接与传输文件,然后进行JDK的安装与卸载,安装步骤包括连接Linux、传输JDK安装包... 目录1、卸载1.1 linux删除自带的JDK1.2 Linux上卸载自己安装的JDK2、安装2.1

浅谈mysql的sql_mode可能会限制你的查询

《浅谈mysql的sql_mode可能会限制你的查询》本文主要介绍了浅谈mysql的sql_mode可能会限制你的查询,这个问题主要说明的是,我们写的sql查询语句违背了聚合函数groupby的规则... 目录场景:问题描述原因分析:解决方案:第一种:修改后,只有当前生效,若是mysql服务重启,就会失效;

MySQL多列IN查询的实现

《MySQL多列IN查询的实现》多列IN查询是一种强大的筛选工具,它允许通过多字段组合快速过滤数据,本文主要介绍了MySQL多列IN查询的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析与优化1.

Linux系统之主机网络配置方式

《Linux系统之主机网络配置方式》:本文主要介绍Linux系统之主机网络配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、查看主机的网络参数1、查看主机名2、查看IP地址3、查看网关4、查看DNS二、配置网卡1、修改网卡配置文件2、nmcli工具【通用

Linux系统之dns域名解析全过程

《Linux系统之dns域名解析全过程》:本文主要介绍Linux系统之dns域名解析全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、dns域名解析介绍1、DNS核心概念1.1 区域 zone1.2 记录 record二、DNS服务的配置1、正向解析的配置

Linux系统中配置静态IP地址的详细步骤

《Linux系统中配置静态IP地址的详细步骤》本文详细介绍了在Linux系统中配置静态IP地址的五个步骤,包括打开终端、编辑网络配置文件、配置IP地址、保存并重启网络服务,这对于系统管理员和新手都极具... 目录步骤一:打开终端步骤二:编辑网络配置文件步骤三:配置静态IP地址步骤四:保存并关闭文件步骤五:重

mybatis-plus 实现查询表名动态修改的示例代码

《mybatis-plus实现查询表名动态修改的示例代码》通过MyBatis-Plus实现表名的动态替换,根据配置或入参选择不同的表,本文主要介绍了mybatis-plus实现查询表名动态修改的示... 目录实现数据库初始化依赖包配置读取类设置 myBATis-plus 插件测试通过 mybatis-plu

MySQL中实现多表查询的操作方法(配sql+实操图+案例巩固 通俗易懂版)

《MySQL中实现多表查询的操作方法(配sql+实操图+案例巩固通俗易懂版)》本文主要讲解了MySQL中的多表查询,包括子查询、笛卡尔积、自连接、多表查询的实现方法以及多列子查询等,通过实际例子和操... 目录复合查询1. 回顾查询基本操作group by 分组having1. 显示部门号为10的部门名,员

Windows系统下如何查找JDK的安装路径

《Windows系统下如何查找JDK的安装路径》:本文主要介绍Windows系统下如何查找JDK的安装路径,文中介绍了三种方法,分别是通过命令行检查、使用verbose选项查找jre目录、以及查看... 目录一、确认是否安装了JDK二、查找路径三、另外一种方式如果很久之前安装了JDK,或者在别人的电脑上,想