If i have class Driver, and class Manager.
Manager is inherited from Driver
public class Driver { ...
.
public class Manager extends Driver { ...
I'm trying to write a simple login so the manager has a different menu to perform more actions. But I can't figure out how to store both types in an ArrayList in my Depot class.
public class Depot {
public ArrayList<Driver> arrayDrivers = new ArrayList<Driver>();
Because obviously now anything added to this array would now be a Driver, not Manager when applicable.
And when i try:
public ArrayList<? extends Driver> arrayDrivers = new ArrayList<Driver>();
I get errors with:
public Depot( ArrayList<Driver> tempDriver) {
arrayDrivers.addAll(tempDriver);
}
Even when i change to
ArrayList<? extends Driver> tempDriver
Can anyone help me out I cant figure this out.
I understand it would be better to create an abstract class of Users for example and then Driver and Manager but it is for coursework and i have to follow the diagram and instructions.
EDIT:
I read somewhere that wasn't the case, i must of been misinformed.
So even when i obtain the pointer like this it should still perform as Manager?
public boolean authenticateDriver(String username, String password, Driver driver) {
Driver userDriver = GetDriver(username); //Not a manager anymore?
if(userDriver != null) {
driver = userDriver;
return userDriver.checkPassword(password);
}
else {
return false;
}
}
.
public Driver GetDriver(String driver) {
for (Driver currentDriver : arrayDrivers) {
if (currentDriver.userName.equals(driver)) {
return currentDriver;
}
}
return null;
}