Reputation: 65
I'm a beginner Java programmer and I'm trying to add a selected object called Person in one linked list to the linked list contained in another instance class called Boat. I am able to select both objects I want, but I do not know how to add the selected Person to the linked list in the selected Boat object. I've included the Person and Boat class details to assist.
I have stripped down the code to the bare essentials to convey the question, if more of the program would assist please let me know. Any assistance is appreciated. If my question isn't clear please let me know and I'll try to rephrase the question.
Here are the two methods I'm using currently, as well as the list the boats are stored in. I've marked key areas with '// !!'
public class Base implements Serializable
{ private LinkedList<Boat> boats = new LinkedList<Boat>();
private Clients clients = new Clients();
public Base(){
}
// !! Here is where the boats are created
public void setup()
{ boats.add(new Boat(1, "Ed", 2));
boats.add(new Boat(2, "Fred", 7));
boats.add(new Boat(3, "Freda", 5)); }
public void getBoat(Boat boat) { `// !! Boat has already been selected by another method`
boat.passengers(add(getPassenger(passenger))); `// !! This is where I try to add the Person object into the linked list of the Boat object, this is the line that generates errors.`
}
public Person getPassenger(Person passenger) { `// !! passenger has already been selected by another method`
return passenger;
}
And here is the boat class
import java.io.*;
import java.util.*;
public class Boat implements Serializable
{
private LinkedList<Person> passengers = new LinkedList<Person>();
public Boat()
{
}
}
Here is the person class
import java.io.*;
import java.text.*;
public class Person implements Serializable
{ private String name;
private int id;
private double cash = 100.00;
private int start = 0;
private int end = 0;
private double charge = 0;
public Person(String name, int id)
{ this.name = name;
this.id = id + 100; }
}
Upvotes: 1
Views: 234
Reputation: 9049
You can create a method in class Boat
to do this:
public class Boat implements Serializable {
private LinkedList<Person> passengers = new LinkedList<Person>();
public Boat() { }
public void addPassenger(Person p) {
this.passengers.add(p);
}
}
Then you can add passengers to a particular boat by passing a Person
object to addPassenger()
. For example:
Boat boat = new Boat();
boat.addPassenger(new Person("Bob", 1));
Do not return the passenger list in a getter method. This would break encapsulation, as other classes would be able to modify a boat's passenger list and even clean it.
There are a couple more problems in your code. For example:
public Person getPassenger(Person passenger) {
return passenger;
}
This method receives an object and return that same object. It does nothing else. You don't need it.
Your getBoat()
method receives a Boat
as argument, but inside it you try to access a Passenger
object. Where did this passenger come from?
public void getBoat(Boat boat) {
boat.passengers(add(getPassenger(passenger)));
}
It seems that this method should be something like:
public void addPassengerToBoat(Boat boat, Passenger passenger) {
boat.addPassenger(passenger);
}
Upvotes: 1
Reputation: 513
//Since passengers in the Boat class is in private scope. You can't access like this. If you have a getter() and setter() method in Boat class for passesngers
public void getBoat(Boat boat) { `// !! Boat has already been selected by another method`
boat.getPassengers().add(getPassenger(passenger)));
}
In Boat class,
public LinkedList<Person> getPassengers(){
return this.passengers;
}
Upvotes: 0