#Assign object(?) to class

14 messages · Page 1 of 1 (latest)

fierce sail
#

Hey guys, im new to java and im working on a lil project here trying to make my code more clean. So im trying to make a vehicle asigned to a station. Right now i just have an class Vehicle and a class Van that extends vehicle. I also have created Station A-E with a builder. Im sure its super easy for you pros but i just cant seem to get it to work. I want to be able to assign my van to station A, whats the code to do that? I have a addvehicle(Vehicle vehicle){vehicles.add(vehicle) !! now and i've also tried to get stationA.addvehicle(Van) to work but im guessing its not the correct way to do it? Any tips appreciated ! Thanks

kind heronBOT
#

Hey, @fierce sail!
Please remember to /close this post once your question has been answered!

dusty scarab
#

Your idea with addVehicle and getVehicle is correct

#

If you try to assign Vehicle to Station, you have to say that Station is some Vehicle. What's, of course, very strange and incorrect.

#

Station has to store vehicles, not be vehicle.

fierce sail
#

Thats a more correct to say it, my bad. Thats what im trying to do, but i cant figure out the correct what to store a vehicle in a station

dusty scarab
#

You can use List, or Set, or whatever other Collection, I guess

#

It depends on your needs

fierce sail
#

Okay i will try some more, i've been trying to add but its clear to me now that ofc i need to store and not assign. Thanks

dusty scarab
#

You're welcome

fierce sail
#

Hmmm im still stuck. I have
Station stationA = new StationBuilder() .stationNumber("A") .isFree(true) .build();

But how do i store my Vehicle in stationA ?
I have Vehicle van = new Van();

#

Before i just had Vehicle.addVehicle(van); And it stored it in the List<Vehicle>, but i want one vehicle in each station, and not all vehicles in a list if you get me

dusty scarab
#

Just replace List with common Vehicle variable and add setVehicle method I guess.
Or just add check to your addVehicle method, if there's already a vehicle at the station