我正在制定一個空中交通管制系統,並且我有一個班級,根據是否有飛機來到,名稱被調用。如果有1架飛機,那麼它說KLM如果沒有飛機,那麼它說沒有飛機。如何從一個班級獲取字符串到另一個班級
我正在尋找一種方法,將飛機名稱從飛機班級送到機場班級放入隊列。這是飛機班的代碼
package airtrafficcontrolv3;
import java.util.TimerTask;
class Plane
extends TimerTask
{
public int nextPlaneLoop = 0;
public int planes;
public int fuel;
public String planeName;
@Override
public void run()
{
Observer o = new ObserverImpl();
Subject s = new SubjectImpl();
if (nextPlaneLoop <= 167)
{
//Currently only running 1 or 0 planes...
planes = (int) (Math.random() * ((2 - 1) + 1));
//System.out.println("Random generated plane amount: " + planes);
//System.out.println("Method called, one whole day loop");
//Adds to the plane in the airspace loop
nextPlaneLoop++;
//System.out.println("Loop incrementing: " + nextPlaneLoop);
if (planes == 0)
{
//System.out.println("No fuel is required as no planes are coming in");
planeName = "No incoming plane";
//System.out.println("Planes name is: " + planeName);
System.out.println("Inbound amount of planes: "+planes);
System.out.println("Inbound: " + planeName);
System.out.println("Inbound fuel amount: None ");
System.out.println(" ");
}
else
{
//Amount of fuel
fuel = 30 + (int) (Math.random() * ((120 - 30) + 1));
//System.out.println("Random fuel: " + fuel);
planeName = "KLM AirFrance";
System.out.println("Inbound amount of planes: "+planes);
System.out.println("Inbound: " + planeName);
System.out.println("Inbound fuel amount: "+fuel);
System.out.println(" ");
}
}
else
{
this.cancel();
System.out.println("Day Finished");
}
s.addObserver(o);
s.setState(planeName);
System.out.println(planeName);
//finalName = planeName;
Airport point = new Airport();
//System.out.println(planeName);
}
}
這是我的機場班。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package airtrafficcontrolv3;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Airport
{
Plane point = new Plane();
Queue <String> waiting = new LinkedList<String>();
public Airport()
{
//waiting.add(point.);
while (!waiting.isEmpty())
{
System.out.println("Waiting to take off: "+waiting);
try
{
System.out.println("Preparing to taxi: "+waiting.remove());
Thread.sleep(5000);
}
catch (InterruptedException ex)
{
Logger.getLogger(Airport.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
有沒有可能任何人都可以建議如何從飛機班級名稱到機場級別請。
可能你可以讓機場成爲飛機的觀察者,所以飛機應該實現可觀測的.. – aishwarya 2012-04-06 21:11:56
@aishwarya只是想指出你必須擴展Observable,因爲它是一個類,而不是一個接口。構圖是解決此問題的好方法。 – jpm 2012-04-06 21:14:40
但是,我仍然可以執行隊列嗎? – 2012-04-06 21:17:20