公交路线查询系统
- [一:目标一:类的定义+构造方法 +set和get方法:](#一:目标一:类的定义+构造方法 +set和get方法:)
- [目标二:静态属性 +静态方法 +toString方法:](#目标二:静态属性 +静态方法 +toString方法:)
- [目标三:抽象类的定义 +抽象方法 + 实际应用:abstract class AbstractRoute{](#目标三:抽象类的定义 +抽象方法 + 实际应用:abstract class AbstractRoute{)
- 目标四:接口的定义+实现接口类的定义+实际应用
- [终极目标:集合泛型定义+集合添加元素 +集合显示内容:](#终极目标:集合泛型定义+集合添加元素 +集合显示内容:)
一:目标一:类的定义+构造方法 +set和get方法:
java
复制代码
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方法:
java
复制代码
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{
java
复制代码
//抽象类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()+"元");
}
}
目标四:接口的定义+实现接口类的定义+实际应用
java
复制代码
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()+"元");
}
}
终极目标:集合泛型定义+集合添加元素 +集合显示内容:
java
复制代码
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());
}
}