公交路线查询系统 一:目标一:类的定义+构造方法 +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; } 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; } 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{
public abstract double getFare ( ) ; public abstract double getDistance ( ) ; public abstract void queryStations ( ) ;
}
class SpecificRoute extends 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 ;
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 { 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 { 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 ( ) ) ; }
}