📄 busline.java
字号:
package com.rochoc.BusLine.Bean;
import java.util.ArrayList;
/**
* @author luoc
*
* 公交路线Bean,包含多个城市的路线
* @version 2005-02-21
*/
public class BusLine implements Cloneable
{
/**
* 构造函数
*/
public BusLine()
{
Citys=new ArrayList();
}
/**
* 增加城市,加到最后
* @param city City
*/
public void addCity(City city)
{
Citys.add(city);
}
/**
* 增加城市,加到指定位置后
* @param index int
* @param city City
* @return boolean
*/
public boolean addCity(int index,City city)
{
int size=getSize();
if(index<0 || index>size)//指定的位置超出范围
{
return false;
}
Citys.add(city);
return true;
}
/**
* 删除指定位置的城市
* @param index int
* @return boolean
*/
public boolean removeCity(int index)
{
int size=getSize();
if(index<1 || index>size)//指定的位置超出范围
{
return false;
}
Citys.remove(index-1);
return true;
}
/**
* 删除指定编号的城市
* @param no String
* @return boolean
*/
public boolean removeCityById(String no)
{
int size=getSize();
for(int i=1;i<=size;i++)
{
City ct=getCity(i);
if(ct.getId().equals(no))
{
removeCity(i);
return true;
}
}
return false;
}
/**
* 删除指定名称的城市
* @param name String
* @return boolean
*/
public boolean removeCityByName(String name)
{
int size=getSize();
for(int i=1;i<=size;i++)
{
City ct=getCity(i);
if(ct.getName().equals(name))
{
removeCity(i);
return true;
}
}
return false;
}
// /**
// * 更新指定位置的城市
// * @param index int
// * @param ct City
// * @return boolean
// */
// public boolean updateCity(int index,City ct)
// {
// int size=getSize();
// if(index<1 || index>size)//指定的位置超出范围
// {
// return false;
// }
// addCity(index,ct);
// removeCity(index);
// return true;
// }
// /**
// * 更新指定编号的城市
// * @param no String
// * @param ct City
// * @return boolean
// */
// public boolean updateCityById(String no,City ct)
// {
// System.out.println("update前:");
// for(int i=1;i<=getSize();i++)
// {
// System.out.println(getCity(i));
// }
// int size=getSize();
// for(int i=1;i<=size;i++)
// {
// City city=getCity(i);
// if(city.getId().equals(no))
// {
// updateCity(i,ct);
// //return true;
// }
// }
// System.out.println("update后:");
// for(int i=1;i<=getSize();i++)
// {
// System.out.println(getCity(i));
// }
// return false;
// }
// /**
// * 更新指定名称的城市
// * @param name String
// * @param ct City
// * @return boolean
// */
// public boolean updateCityByName(String name,City ct)
// {
// int size=getSize();
// for(int i=1;i<=size;i++)
// {
// City city=getCity(i);
// if(city.getName().equals(name))
// {
// updateCity(i,ct);
// return true;
// }
// }
// return false;
// }
/**
* 获取指定位置的城市
* @param index int
* @return City
*/
public City getCity(int index)
{
int size=getSize();
if(index<1 || index>size)//指定的位置超出范围
{
return null;
}
return (City)Citys.get(index-1);
}
/**
* 获取城市的数量
* @return int
*/
public int getSize()
{
return Citys.size();
}
/**
* 获取指定编号的城市
* @param name String
* @return City
*/
public City getCityById(String no)
{
int size=getSize();
for(int i=1;i<=size;i++)
{
City ct=getCity(i);
if(ct.getId().equals(no))
{
return ct;
}
}
return null;
}
/**
* 获取指定名称的城市
* @param name String
* @return City
*/
public City getCityByName(String name)
{
int size=getSize();
for(int i=1;i<=size;i++)
{
City ct=getCity(i);
if(ct.getName().equals(name))
{
return ct;
}
}
return null;
}
public Object clone()
{
try
{
return super.clone();
}catch(CloneNotSupportedException e)
{
return null;
}
}
/**
* @see Object#toString
*/
public String toString()
{
return "Citys: City size="+Citys.size()+"\r\n";
}
/*全局变量*/
private ArrayList Citys=null;//城市
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -