java公交线路管理系统
答案:2 悬赏:40 手机版
解决时间 2021-04-13 04:43
- 提问者网友:听门外雪花风
- 2021-04-12 22:51
java公交线路管理系统
最佳答案
- 五星知识达人网友:何以畏孤独
- 2021-04-12 23:38
//写了两个小时啊,兄弟,要采纳我啊
//Site(站点类)
package transit;
import java.util.ArrayList;
import java.util.List;
public class Site
{
private Integer id; // 给每个站点分配一个ID
private String name; // 站点的名字
private List list; // 经过该站点的线路
public Site()
{
super();
}
public Site(Integer id, String name)
{
super();
this.id = id;
this.name = name;
this.list = new ArrayList();
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public List getList()
{
return list;
}
public void setList(List list)
{
this.list = list;
}
// 添加线路
public boolean addRoute(Route route)
{
return this.getList().add(route);
}
// 删除线路
public boolean removeRoute(Route route)
{
return this.getList().remove(route);
}
@Override
public String toString()
{
return name;
}
@Override
public boolean equals(Object obj)
{
if (obj instanceof Site)
{
Site s = (Site) obj;
return this.id == s.getId() && this.name.equals(s.getName());
}
return false;
}
} //线路类
package transit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Route
{
private Integer id; // 给每个线路分配一个ID
private String name; // 线路名称
private List list; // 线路所包含的站点
private Date startDate; // 发班时间,从首站点算起
private Date endDate; // 收班时间,从首站点算起
public Route()
{
super();
}
public Route(Integer id, String name, Date startDate, Date endDate)
{
super();
this.id = id;
this.name = name;
this.startDate = startDate;
this.endDate = endDate;
this.list = new ArrayList();
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public List getList()
{
return list;
}
public void setList(List list)
{
this.list = list;
}
public Date getStartDate()
{
return startDate;
}
public void setStartDate(Date startDate)
{
this.startDate = startDate;
}
public Date getEndDate()
{
return endDate;
}
public void setEndDate(Date endDate)
{
this.endDate = endDate;
}
public boolean isHasThisSite(Site site)
{
for (Site l_site : this.getList())
{
if (l_site.equals(site))
{
return true;
}
}
return false;
}
// 添加站点,成功返回true
public boolean addSite(Site site)
{
return this.getList().add(site);
}
// 移除站点,成功返回true
public boolean removeSite(Site site)
{
return this.getList().remove(site);
}
@Override
public String toString()
{
return name;
}
} //公交系统类
package transit;
import java.util.ArrayList;
import java.util.List;
public class TransitSystem
{
private List routeList ; //系统所管理的线路
private List siteList; //系统所管理的站点
public TransitSystem(){
routeList = new ArrayList();
siteList = new ArrayList();
}
//增加一条线路
public void addRoute(Route route){
if(!routeList.add(route)){
throw new RuntimeException("系统中已存在该线路");
}
}
//修改一条线路
public void updateRoute(Route route){
//根据route 的id 获取已存在系统中的route
Route l_route = getRoute(route.getId());
if(l_route!=null){
//注入值
l_route.setName(route.getName());
l_route.setList(route.getList());
l_route.setStartDate(route.getStartDate());
l_route.setEndDate(route.getEndDate());
}else{
throw new RuntimeException("线路id不匹配,无法修改");
}
}
//获取该系统中的线路,根据id获取
public Route getRoute(Integer id){
Route route2 = null;
for (Route route : this.getRouteList())
{
if(route.getId().equals(id)){
route2 = route;
}
}
return route2;
}
//删除一条线路,根据对象删除
public void removeRoute(Route route){
if(!this.getRouteList().remove(route)){
throw new RuntimeException(route.getId()+"线路不存在");
}
}
//增加一条公交站点
public void addSite(Site site){
if(!this.siteList.add(site)){
throw new RuntimeException(site.getId()+"站点已存在");
}
}
//删除一条公交站点
public void removeSite(Site site){
if(!this.getSiteList().remove(site)){
throw new RuntimeException(site.getId()+"站点不存在");
}
}
//修改一条公交站点
public void updateSite(Site site){
//根据route 的id 获取已存在系统中的route
Site l_site = getSite(site.getId());
if(l_site!=null){
//注入值
l_site.setName(site.getName());
l_site.setList(site.getList());
}else{
throw new RuntimeException("站点id不匹配,无法修改");
}
}
//获取该系统中的公交站点,根据id获取
public Site getSite(Integer id){
Site site2 = null;
for (Site site : this.getSiteList())
{
if(site.getId().equals(id)){
site2 = site;
}
}
return site2;
}
//按线路名称查询公交线路信息
public Route getRouteByName(String name){
for (Route route : this.getRouteList())
{
if(route.getName().equals(name)){
return route;
}
}
return null;
}
//根据途经车站查询公交线路
public List getRouteBySite(Site site){
return site.getList();
}
public List getRouteList()
{
return routeList;
}
public void setRouteList(List routeList)
{
this.routeList = routeList;
}
public List getSiteList()
{
return siteList;
}
public void setSiteList(List siteList)
{
this.siteList = siteList;
}
} //测试类
package transit;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
public class _Test
{
public static void main(String[] args) throws ParseException
{
TransitSystem system = new TransitSystem();
SimpleDateFormat format = new SimpleDateFormat("hh:MM");
Date startDate = format.parse("07:30");
Date endDate = format.parse("22:30");
// 向系统中添加10条线路,20个站点
for (int i = 0; i < 20; i++)
{
if (i < 10)
{
system.addRoute(new Route(i, "线路" + i, startDate, endDate));
}
system.addSite(new Site(i, "站点" + i));
}
List siteList = system.getSiteList();
// 为系统中的站点添加线路
for (Route route : system.getRouteList())
{
// 每条线路 随机添加N个站点
int n = new Random().nextInt(siteList.size());
for (int i = 0; i < n; i++)
{
// 每个站点也是随机,如重复,则添加失败
int x = new Random().nextInt(siteList.size());
if (!route.isHasThisSite(siteList.get(x)))
{
route.addSite(siteList.get(x));
// 同时该站点也添加该线路
siteList.get(x).addRoute(route);
}
}
}
// 查询所有的线路所包含的站点
for (Route route : system.getRouteList())
{
System.out
.println(route.getName() + " :对应的站点集合:" + route.getList());
}
// 查询所有的站点对应的线路
for (Site site : system.getSiteList())
{
System.out.println("站点" + site.getName() + " :对应的线路集合: "
+ site.getList());
}
}
}追问大神啊,我运行出来我这里好多错,可以加你Q问一问么追答我的Q76752951,我都是调试完了才传上来的啊,因为用jdk5.0的东西,所以是不是你那jdk版本低了?追问主要是我笨,看不懂。。。我加你了。你瞧瞧
//Site(站点类)
package transit;
import java.util.ArrayList;
import java.util.List;
public class Site
{
private Integer id; // 给每个站点分配一个ID
private String name; // 站点的名字
private List
public Site()
{
super();
}
public Site(Integer id, String name)
{
super();
this.id = id;
this.name = name;
this.list = new ArrayList
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public List
{
return list;
}
public void setList(List
{
this.list = list;
}
// 添加线路
public boolean addRoute(Route route)
{
return this.getList().add(route);
}
// 删除线路
public boolean removeRoute(Route route)
{
return this.getList().remove(route);
}
@Override
public String toString()
{
return name;
}
@Override
public boolean equals(Object obj)
{
if (obj instanceof Site)
{
Site s = (Site) obj;
return this.id == s.getId() && this.name.equals(s.getName());
}
return false;
}
} //线路类
package transit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Route
{
private Integer id; // 给每个线路分配一个ID
private String name; // 线路名称
private List
private Date startDate; // 发班时间,从首站点算起
private Date endDate; // 收班时间,从首站点算起
public Route()
{
super();
}
public Route(Integer id, String name, Date startDate, Date endDate)
{
super();
this.id = id;
this.name = name;
this.startDate = startDate;
this.endDate = endDate;
this.list = new ArrayList
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public List
{
return list;
}
public void setList(List
{
this.list = list;
}
public Date getStartDate()
{
return startDate;
}
public void setStartDate(Date startDate)
{
this.startDate = startDate;
}
public Date getEndDate()
{
return endDate;
}
public void setEndDate(Date endDate)
{
this.endDate = endDate;
}
public boolean isHasThisSite(Site site)
{
for (Site l_site : this.getList())
{
if (l_site.equals(site))
{
return true;
}
}
return false;
}
// 添加站点,成功返回true
public boolean addSite(Site site)
{
return this.getList().add(site);
}
// 移除站点,成功返回true
public boolean removeSite(Site site)
{
return this.getList().remove(site);
}
@Override
public String toString()
{
return name;
}
} //公交系统类
package transit;
import java.util.ArrayList;
import java.util.List;
public class TransitSystem
{
private List
private List
public TransitSystem(){
routeList = new ArrayList
siteList = new ArrayList
}
//增加一条线路
public void addRoute(Route route){
if(!routeList.add(route)){
throw new RuntimeException("系统中已存在该线路");
}
}
//修改一条线路
public void updateRoute(Route route){
//根据route 的id 获取已存在系统中的route
Route l_route = getRoute(route.getId());
if(l_route!=null){
//注入值
l_route.setName(route.getName());
l_route.setList(route.getList());
l_route.setStartDate(route.getStartDate());
l_route.setEndDate(route.getEndDate());
}else{
throw new RuntimeException("线路id不匹配,无法修改");
}
}
//获取该系统中的线路,根据id获取
public Route getRoute(Integer id){
Route route2 = null;
for (Route route : this.getRouteList())
{
if(route.getId().equals(id)){
route2 = route;
}
}
return route2;
}
//删除一条线路,根据对象删除
public void removeRoute(Route route){
if(!this.getRouteList().remove(route)){
throw new RuntimeException(route.getId()+"线路不存在");
}
}
//增加一条公交站点
public void addSite(Site site){
if(!this.siteList.add(site)){
throw new RuntimeException(site.getId()+"站点已存在");
}
}
//删除一条公交站点
public void removeSite(Site site){
if(!this.getSiteList().remove(site)){
throw new RuntimeException(site.getId()+"站点不存在");
}
}
//修改一条公交站点
public void updateSite(Site site){
//根据route 的id 获取已存在系统中的route
Site l_site = getSite(site.getId());
if(l_site!=null){
//注入值
l_site.setName(site.getName());
l_site.setList(site.getList());
}else{
throw new RuntimeException("站点id不匹配,无法修改");
}
}
//获取该系统中的公交站点,根据id获取
public Site getSite(Integer id){
Site site2 = null;
for (Site site : this.getSiteList())
{
if(site.getId().equals(id)){
site2 = site;
}
}
return site2;
}
//按线路名称查询公交线路信息
public Route getRouteByName(String name){
for (Route route : this.getRouteList())
{
if(route.getName().equals(name)){
return route;
}
}
return null;
}
//根据途经车站查询公交线路
public List
return site.getList();
}
public List
{
return routeList;
}
public void setRouteList(List
{
this.routeList = routeList;
}
public List
{
return siteList;
}
public void setSiteList(List
{
this.siteList = siteList;
}
} //测试类
package transit;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
public class _Test
{
public static void main(String[] args) throws ParseException
{
TransitSystem system = new TransitSystem();
SimpleDateFormat format = new SimpleDateFormat("hh:MM");
Date startDate = format.parse("07:30");
Date endDate = format.parse("22:30");
// 向系统中添加10条线路,20个站点
for (int i = 0; i < 20; i++)
{
if (i < 10)
{
system.addRoute(new Route(i, "线路" + i, startDate, endDate));
}
system.addSite(new Site(i, "站点" + i));
}
List
// 为系统中的站点添加线路
for (Route route : system.getRouteList())
{
// 每条线路 随机添加N个站点
int n = new Random().nextInt(siteList.size());
for (int i = 0; i < n; i++)
{
// 每个站点也是随机,如重复,则添加失败
int x = new Random().nextInt(siteList.size());
if (!route.isHasThisSite(siteList.get(x)))
{
route.addSite(siteList.get(x));
// 同时该站点也添加该线路
siteList.get(x).addRoute(route);
}
}
}
// 查询所有的线路所包含的站点
for (Route route : system.getRouteList())
{
System.out
.println(route.getName() + " :对应的站点集合:" + route.getList());
}
// 查询所有的站点对应的线路
for (Site site : system.getSiteList())
{
System.out.println("站点" + site.getName() + " :对应的线路集合: "
+ site.getList());
}
}
}追问大神啊,我运行出来我这里好多错,可以加你Q问一问么追答我的Q76752951,我都是调试完了才传上来的啊,因为用jdk5.0的东西,所以是不是你那jdk版本低了?追问主要是我笨,看不懂。。。我加你了。你瞧瞧
全部回答
- 1楼网友:醉吻情书
- 2021-04-13 00:30
需不需要连接网络进行通信?追问不需要,就是个学校的课程设计
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯