I’m creating an air traffic control system. I have a class called plane that is the one being observed for any changes which is the name changes when a plane is generated at random. The issue is though that the airport needs to access the name to the name to store it into an array list possibly a queue if I can get it working. Anyway this is the code for the airport class:
/*
* 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;
/**
*
* @author
*/
public class Airport
{
Subject s = new SubjectImpl();
Plane point = new Plane();
Queue <String> waiting = new LinkedList<String>();
public Airport()
{
//String name =
s.getState();
System.out.println(s);
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);
}
}
}
}
I wasn’t sure if I had to have access to the subject that is being observed as in the subject I have a getstate method which is currently what is being pointed to, however it comes out generated in random numbers and letters:
airtrafficcontrolv3.SubjectImpl@5a199939
This is what I have in my subject class:
package airtrafficcontrolv3;
/*
* @author S0082708
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
interface Subject
{
public void addObserver(Observer o);
public void removeObserver(Observer o);
public String getState();
public void setState(String state);
}
/*
* Creates an observer that will watch for any changes
*/
interface Observer
{
public void update(Subject o);
}
/*
* Class implements the observer
* Observer is what is doing the watching
* The string is set to blank waiting ready for any updates that will occur
* Anything in getState is then passed to state
*/
class ObserverImpl implements Observer
{
private String state = "";
public void update(Subject o)
{
state = o.getState();
//Need to add text to the label
// AirTrafficControlv3View.Incoming.add(state);
}
}
/*
* Subject is what is being watched
* The array is then created to store the information ready to add further information
* Or ready to delete
* State is set to blank ready for whatever information is being passed
*/
class SubjectImpl implements Subject
{
private List observers = new ArrayList();
private String state = "";
/*
* Returns whatever is being passed to state
*/
public String getState()
{
//System.out.println(state);
return state;
}
/*
* Sets the state to current state and then notifies the observer of the changes
* Made to the state ready to update
*/
public void setState(String state)
{
this.state = state;
notifyObservers();
}
/*
* Adds the observer along with the name, so several observers can be implemented
*/
public void addObserver(Observer o)
{
observers.add(o);
}
/*
* Removes the observer once it has been used
*/
public void removeObserver(Observer o)
{
observers.remove(o);
}
/*
* The iterator allows the ability to loop through the array
* While the iterator has a next then it allows the ability to take in more
* Changes to be observed. It is then updated with the current information
*/
public void notifyObservers()
{
Iterator i = observers.iterator();
while (i.hasNext())
{
Observer o = (Observer) i.next();
o.update(this);
}
}
}
Can anyone point me in the right direction as of how to get the string from get state which is coming out as KLM fine, but it won’t come out in the airport class.
Any suggestions would be greatly welcomed.
You need to implement a
toString()in the class of the object that is pointed to which you return fromgetState()the random numbers you get is the default implementation oftoString()in theObjectclass which presents instance identity, not content.