I have an Arraylist
of 3 dogs, and I can register more dogs to the list, list them by calling a toString
function, increase the age of a dog by one, and remove a dog from the list. As long as you only use those two last ones, remove()
and increaseAge()
they both work, but as soon as you use any of the other commands, they can't find any dogs and send back -1 as it should when it doesn't find any dogs by the name given. So it's remove()
and increaseAge()
that's being weird.
Main code:
public class DogTest {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
ArrayList<Dog> Dogs = new ArrayList<Dog>();
DogFunctions DF = new DogFunctions();
Dogs.add(new Dog("Peggy", "Labrador", 9, 30));
Dogs.add(new Dog("Max", "Tax", 4, 13));
Dogs.add(new Dog("Sanna", "Schäfer", 7, 25));
Boolean loop = true;
while(loop){
String input = keyboard.nextLine();
switch (input){
case "Register":
Dogs.add(DF.register());
break;
case "IncreaseAge":
Dogs.get(DF.returnDogIndex(Dogs, "Input Message", "Output Message")).increaseAge();
break;
case "List":
DF.list(Dogs);
break;
case "Remove":
Dogs.remove(DF.returnDogIndex(Dogs, "Input Message", "Output Message"));
break;
case "Quit":
loop = false;
break;
}
}
keyboard.close();
}
}
Code for the functions:
import java.util.ArrayList;
import java.util.Scanner;
public class DogFunctions {
private Scanner keyboard = new Scanner(System.in);
public Dog register(){
System.out.println("Namn:");
String name = keyboard.nextLine();
System.out.println("Ras:");
String breed = keyboard.nextLine();
System.out.println("Ålder:");
int age = keyboard.nextInt();
System.out.println("Vikt:");
double weight = keyboard.nextDouble();
Dog d = new Dog(name, breed, age, weight);
System.out.println("Hund tillagd i registret");
return d;
}
public void list(ArrayList<Dog> Dogs){
System.out.println("Ange svanslängd:");
double input = keyboard.nextDouble();
if(input == 0){
for (int i = 0; i < Dogs.size(); i++){
System.out.println(Dogs.get(i).toString());
}
}
else{
for (int i = 0; i < Dogs.size(); i++){
if(Dogs.get(i).getTailLenght() >= input){
System.out.println(Dogs.get(i).toString());
}
}
}
}
public int returnDogIndex(ArrayList<Dog> Dogs, String inputMessage, String outputMessage){
System.out.println(inputMessage);
String input = keyboard.nextLine();
for(int i = 0; i < Dogs.size(); i++){
if(input.equals(Dogs.get(i).getName())){
System.out.println(outputMessage);
return i;
}
}
return -1;
}
}
It's this last function "returnDogIndex" that I think is something wrong with.
Code for the Dog class:
public class Dog {
private String breed, name;
private int age;
private double weight, tailLength;
public Dog(String name, String breed, int age, double weight) {
this.name = name;
this.breed = breed;
this.age = age;
this.weight = weight;
calcTailLength();
}
public void calcTailLength(){
if (breed.toLowerCase().equals("tax")) {
tailLength = 3.7;
}
else {
tailLength = (age*weight)/10;
}
}
public String toString() {
return name + " är en " + age + " år gammal " + breed + " som väger " + weight + " kg och har en svanslängd på " + tailLength;
}
public String getName() {
return name;
}
public void increaseAge(){
age++;
calcTailLength();
}
public double getTailLenght(){
return tailLength;
}
}