I have been trying most things now but it doesn't seem to work. I want to remove a specific object from an Arraylist if the user enters a specific name. And if not I want the program to print "Can't find dog". I have a getName and toString method in my other class that is working well with all the other options i have in this program, this is the only thing that is not working.
Case 4 is where the removal is supposed to happen. But I also want you to look at Case 3 to compare that Case 3 prints out "can't find dog" no matter what, and case 4 prints out "Cant find dog" as many times as there are objects.
Here is the main method:
ArrayList<Dog> doglist = new ArrayList<Dog>();
Scanner myscan = new Scanner(System.in);
boolean running = true;
while (running) {
System.out.println("\n************************************");
System.out.println("\nVälkommen till Kennelklubben!");
System.out.println("\n************************************");
System.out.println("\n[1] Register new dog");
System.out.println("[2] Print out list");
System.out.println("[3] Increase age");
System.out.println("[4] Remove dog");
System.out.println("[5] Quit program");
System.out.println("\n************************************");
System.out.println("\nChoose: ");
int option = myscan.nextInt();
switch (option) {
case 1:
System.out.println("Write name:");
String name = myscan.next();
System.out.println("Write race:");
String race = myscan.next();
System.out.println("Age:");
int age = myscan.nextInt();
System.out.println("Weight:");
double weight = myscan.nextDouble();
Dog dog = new Dog(name, race, age, weight);
doglist.add(dog);
break;
case 2:
System.out.println("Minimum length of tail:");
double userInput1 = myscan.nextDouble();
for (Dog d : doglist) {
if (d.getTailLength() >= userInput1) {
System.out.println(d.toString());
} else {
System.out.println("Can't find dog");
}
}
break;
case 3:
System.out.println("Name of dog:");
String userInput2 = myscan.next();
for (Dog d : doglist) {
if (d.getName().equals(userInput2)) {
d.increaseAge();
break;
}
}
System.out.println("Can't find dog");
break;
case 4:
System.out.println("Name of dog:");
String userInput3 = myscan.next();
for (Dog d : doglist) {
if (d.getName().equals(userInput3)) {
doglist.remove(d.toString());
} else {
System.out.println("Can't find dog");
}
}
break;
case 5:
running = false;//Avslutar loopen och därmed programmet
System.out.println("Programmet avslutat");
break;
default:
System.out.println("Nu blev det fel, välj mellan [1] [2] [3] [4] [5]");//Felmeddelande om valet är någon annan siffra än de som menyn innehåller
break;
}
}
Here is the other class with my methods
public class Dog
{
private String name;
private String race;
private int age;
private double weight;
private double tailLength;
public Dog (String name, String race, int age, double weight)
{
this.name = name;
this.setRace(race);
this.age = age;
this.weight = weight;
if(race.equals("tax"))
{
this.setTailLength(3.7);
}
else
{
this.setTailLength((age - weight) / 10);
}
}
public String getRace()
{
return race;
}
public void setRace(String race)
{
this.race = race;
}
public double getTailLength()
{
return tailLength;
}
public void setTailLength(double tailLength)
{
this.tailLength = tailLength;
}
public int increaseAge()
{
age++;
return age;
}
public String toString()
{
return name + " " + getRace() +
" " + age + " " + "år" + " " + weight + " " + "kg" + " " + getTailLength();
}
public String getName()
{
return name;
}
}