i am still having trouble with displaying information to the screen
in my vehicle hire program here is the structure of what i have done.
View Class Controller Class Program(main etc...)
Vehicle Class
Car Class Van Class *both inherit from Vehicle
anyway i am using the observable class to notify when changes have
been made to the View. however my method displayVehicle (in view)
cannot display methods that are particular to the car and van
classes. This is because i am passing a vehicle object to the method
in order to create one method for displaying information on both cars
and vans.
Here is some code i have done.
View Class
public void update(Observable obj, Object arg) {
Object object = arg;
displayVehicle((Vehicle)object);
}
public void displayVehicle(Vehicle vehicle) {
System.out.print("\nVehicle Details \n---------------");
System.out.print("\nMake: " + vehicle.getMake());
System.out.print("\nReg No: " + vehicle.getRegNo());
//System.out.print("\nColour: " + vehicle.getColour());
//System.out.print("\nNo Of Doors: " + vehicle.getNoOfDoors());
}
NB: the print() that are commented are methods specific to the car
class and i cannot access them. i have tried creating a Car object in
Vehicle and accessing it from view but it gives a null pointer ex
Vehicle Class
private int findVehicle(String regNo) {
int location = -1;
for(int i = 0; i < vector.size(); i++) {
if(vector.get(i) instanceof Car) {
Car car = (Car)vector.get(i);
if(car.getRegNo().equalsIgnoreCase(regNo)) {
location = i;
setChanged();
notifyObservers(new Vehicle(car.getMake(), car.getRegNo(),
car.getColour(), car.getNoOfDoors()));
}
}
if(vector.get(i) instanceof Van) {
Van van = (Van)vector.get(i);
if(van.getRegNo().equalsIgnoreCase(regNo)) {
location = i;
//setChanged();
//notifyObservers(new Van(van.getMake(), van.getRegNo(),
van.getCarryCapacity()));
}
}
}
return location;
}
public void displayDetails(String regNo) {
vector.get(findVehicle(regNo));
}
Car class
this class just provides a constructor which contains make, regNo,
colour etc... and also provides the getColour and getNoOfDoors methods
any help would be useful (if you understand me that is)