我正在做一个我必须为出租车公司制作节目的分配。到目前为止,我所写的内容确实奏效了。这是......
import java.util.ArrayList;
import java.util.Iterator;
public class TaxiCo
{
private ArrayList<Taxi> taxiCollection;
public TaxiCo()
{
taxiCollection = new ArrayList<>();
}
public void addTaxi(Taxi blackCab)
{
taxiCollection.add(blackCab);
}
public int getNumberOfTaxis()
{
return taxiCollection.size();
}
public int getNumberOfTaxis(int area)
{
int Taxi= 0;
for (Taxi blackCab : taxiCollection) {
if(blackCab.getArea() == (area)) {
Taxi++;
}
}
return Taxi;
}
public void list()
{
System.out.println("Taxi Information");
for(Taxi blackCab : taxiCollection)
{
System.out.println(blackCab.getDetails());
}
System.out.println();
}
public boolean removeTaxi(int id)
{
Iterator<Taxi> it = taxiCollection.iterator();
while(it.hasNext()) {
Taxi blackCab = it.next();
int taxiIdentification = blackCab.getID();
if(taxiIdentification == id) {
it.remove();
return true;
}
}
return false;
}
现在接下来的任务就是创建一个允许我输入出租车ID并改变其状态的方法。免费或使用。我认为它是这样的,但不知道如何做布尔值。例如,如果我输入已在列表中设置为免费的出租车ID,我可以输入已占用状态,这将改变出租车的状态。
Iterator<Taxi> it = taxiCollection.iterator();
while(it.hasNext()) {
Taxi blackCab = it.next();
int taxiIdentification = blackCab.getID();
if(taxiIdentification == id) {
}
}
请你帮我解决这个问题。
答案 0 :(得分:2)
将此方法添加到TaxiCo
public void changeStatus(int id, Status status){
for(Taxi taxi : taxiCollection) {
if(taxi.getID() == id){
taxi.setStatus(status);
}
}
}