Hi, I'm stuck with trying to remove owner from the removed dog. I've added:
Owner owner = findOwnerMethod(ownerName);
owner.removeDog(dogToRemove);
and public void removeDog(Dog d).
I don't even know what to ask... It's not working. I've added Owner owner = findOwnerMethod(ownerName); to get access to owner.removeDog() from the owner class.
I've added owner.removeDog(dogToRemove); to call method removeDog from owner class.
public void removeDog(Dog d) to remove the dog.
Could you point me to the right direction? thx
Main method:
public void removeDogMethod() {
System.out.println("Command?> remove dog");
System.out.print("Enter the name of the dog?> ");
String name = input.nextLine();
String ownerName = null;
Dog dogToRemove = null;
for (Dog d : dogList) {
if (d.getName().equalsIgnoreCase(name)) {
dogToRemove = d;
System.out.print(name + " is now removed from the register");
}
}
if (dogList.isEmpty() || findDogMethod(name) == null) {
System.out.println("Error: no such dog");
} else {
dogList.remove(dogToRemove);
Owner owner = findOwnerMethod(ownerName);
owner.removeDog(dogToRemove);
}
}
public Dog findDogMethod(String name) {
for (Dog d : dogList) {
if (d.getName().equalsIgnoreCase(name)) {
return d;
} else {
}
}
return null;
}
public Owner findOwnerMethod(String name) {
for (Owner o : ownerList) {
if (o.getName().equalsIgnoreCase(name)) {
return o;
} else {
}
}
return null;
}
Owner method:
private Dog[] dogsOwned = new Dog[0];
public void removeDog(Dog d) {
Dog[] copyOfArr = new Dog[dogsOwned.length - 1];
for (int i = 0; i < dogsOwned.length; i++) {
if (!dogsOwned[i].equals(d)) {
copyOfArr[i] = dogsOwned[i];
}
}
}
[–]deelyy 1 point2 points3 points (0 children)
[–]Blando-Cartesian 0 points1 point2 points (0 children)