Skip to main content
Problem the other answer avoids.
Source Link
Joop Eggen
  • 4.7k
  • 15
  • 19

Small things first:

  • You can use the diamond operator <>, which preciously saves repetitive typing.
  • Program as much as possible against interfaces, here List with as implementing class ArrayList.
    • You can change the implementation later.
    • As method return, you can use use any implementation, especially Collections.singleton/emptyList(). Most important.
    • As method parameter you can pass any kind of List like a LinkedList.

So:

private static List<Dog> dogList = new ArrayList<>();

For the optional boolean value you are overthinking things. Good meaning by good naming might have helped. A while may be even better:

boolean reserved = false;
boolean inputValid = false;
while (!inputValid) {
    System.out.println("Is the monkey reserved?");
    System.out.println("Enter Y for yes and N for no.");
    String reserveAnswer = scanner.nextLine();
    if (reserveAnswer.equalsIgnoreCase("y")) {
        reserved = true;
        inputValid = true;                    
    } else if (reserveAnswer.equalsIgnoreCase("n")) {
        reserved = false;
        inputValid = true;
    } else {
        System.out.println("Invalid input");
        System.out.println();
    }
}

inputVal is only needed for looping as long as the input is not valid. More reserved would be candidate for yes, no, I do not know yet / later. Then a Boolean null would be one solution; nowadays one can use Optional<Boolean>, without any null value. But that probably still is not treated.

Calling overridable methods in a constructor is very bad. The reason is that any child is still not initialized (fields are filled later, rest of child constructor executed after super constructor). When then an overriden child method is called it uses uninitialized child fields, that will be overwritten after the child method. You could make a Setter final to repair it.

Small things first:

  • You can use the diamond operator <>, which preciously saves repetitive typing.
  • Program as much as possible against interfaces, here List with as implementing class ArrayList.
    • You can change the implementation later.
    • As method return, you can use use any implementation, especially Collections.singleton/emptyList(). Most important.
    • As method parameter you can pass any kind of List like a LinkedList.

So:

private static List<Dog> dogList = new ArrayList<>();

For the optional boolean value you are overthinking things. Good meaning by good naming might have helped. A while may be even better:

boolean reserved = false;
boolean inputValid = false;
while (!inputValid) {
    System.out.println("Is the monkey reserved?");
    System.out.println("Enter Y for yes and N for no.");
    String reserveAnswer = scanner.nextLine();
    if (reserveAnswer.equalsIgnoreCase("y")) {
        reserved = true;
        inputValid = true;                    
    } else if (reserveAnswer.equalsIgnoreCase("n")) {
        reserved = false;
        inputValid = true;
    } else {
        System.out.println("Invalid input");
        System.out.println();
    }
}

inputVal is only needed for looping as long as the input is not valid. More reserved would be candidate for yes, no, I do not know yet / later. Then a Boolean null would be one solution; nowadays one can use Optional<Boolean>, without any null value. But that probably still is not treated.

Small things first:

  • You can use the diamond operator <>, which preciously saves repetitive typing.
  • Program as much as possible against interfaces, here List with as implementing class ArrayList.
    • You can change the implementation later.
    • As method return, you can use use any implementation, especially Collections.singleton/emptyList(). Most important.
    • As method parameter you can pass any kind of List like a LinkedList.

So:

private static List<Dog> dogList = new ArrayList<>();

For the optional boolean value you are overthinking things. Good meaning by good naming might have helped. A while may be even better:

boolean reserved = false;
boolean inputValid = false;
while (!inputValid) {
    System.out.println("Is the monkey reserved?");
    System.out.println("Enter Y for yes and N for no.");
    String reserveAnswer = scanner.nextLine();
    if (reserveAnswer.equalsIgnoreCase("y")) {
        reserved = true;
        inputValid = true;                    
    } else if (reserveAnswer.equalsIgnoreCase("n")) {
        reserved = false;
        inputValid = true;
    } else {
        System.out.println("Invalid input");
        System.out.println();
    }
}

inputVal is only needed for looping as long as the input is not valid. More reserved would be candidate for yes, no, I do not know yet / later. Then a Boolean null would be one solution; nowadays one can use Optional<Boolean>, without any null value. But that probably still is not treated.

Calling overridable methods in a constructor is very bad. The reason is that any child is still not initialized (fields are filled later, rest of child constructor executed after super constructor). When then an overriden child method is called it uses uninitialized child fields, that will be overwritten after the child method. You could make a Setter final to repair it.

added 12 characters in body
Source Link
Joop Eggen
  • 4.7k
  • 15
  • 19

Small things first:

  • You can use the diamond operator <>, which preciously saves repetitive typing.
  • Program as much as possible against interfaces, here List with as implementing class ArrayList.
    • You can change the implementation later.
    • As method return, you can use use any implementation, especially Collections.singleton/emptyList(). Most important.
    • As method parameter you can pass any kind of List like a LinkedList.

So:

private static List<Dog> dogList = new ArrayList<>();

For the optional boolean value you are overthinking things. Good meaning by good naming might have helped. A while may be even better:

boolean reserved = false;
boolean inputValid = false;
while (!inputValid) {
    System.out.println("Is the monkey reserved?");
    System.out.println("Enter Y for yes and N for no.");
    String reserveAnswer = scanner.nextLine();
    if (reserveAnswer.equalsIgnoreCase("y")) {
        reserved = true;
        inputValid = true;                    
    } else if (reserveAnswer.equalsIgnoreCase("n")) {
        reserved = false;
        inputValid = true;
    } else {
        System.out.println("Invalid input");
        System.out.println();
    }
}

inputVal is only needed for looping as long as the input is not valid. More reserved would be candidate for yes, no, I do not know yet / later. Then a Boolean null would be one solution; nowadays one can use Optional<Boolean>, without any null value. But that probably still is not treated.

Small things first:

  • You can use the diamond operator <>, which preciously saves repetitive typing.
  • Program as much against interfaces, here List with as implementing class ArrayList.
    • You can change the implementation later.
    • As method return, you can use use any implementation, especially Collections.singleton/emptyList(). Most important.
    • As method parameter you can pass any kind of List like a LinkedList.

So:

private static List<Dog> dogList = new ArrayList<>();

For the optional boolean value you are overthinking things. Good meaning by good naming might have helped. A while may be even better:

boolean reserved = false;
boolean inputValid = false;
while (!inputValid) {
    System.out.println("Is the monkey reserved?");
    System.out.println("Enter Y for yes and N for no.");
    String reserveAnswer = scanner.nextLine();
    if (reserveAnswer.equalsIgnoreCase("y")) {
        reserved = true;
        inputValid = true;                    
    } else if (reserveAnswer.equalsIgnoreCase("n")) {
        reserved = false;
        inputValid = true;
    } else {
        System.out.println("Invalid input");
        System.out.println();
    }
}

inputVal is only needed for looping as long as the input is not valid. More reserved would be candidate for yes, no, I do not know yet / later. Then a Boolean null would be one solution; nowadays one can use Optional<Boolean>, without any null value. But that probably still is not treated.

Small things first:

  • You can use the diamond operator <>, which preciously saves repetitive typing.
  • Program as much as possible against interfaces, here List with as implementing class ArrayList.
    • You can change the implementation later.
    • As method return, you can use use any implementation, especially Collections.singleton/emptyList(). Most important.
    • As method parameter you can pass any kind of List like a LinkedList.

So:

private static List<Dog> dogList = new ArrayList<>();

For the optional boolean value you are overthinking things. Good meaning by good naming might have helped. A while may be even better:

boolean reserved = false;
boolean inputValid = false;
while (!inputValid) {
    System.out.println("Is the monkey reserved?");
    System.out.println("Enter Y for yes and N for no.");
    String reserveAnswer = scanner.nextLine();
    if (reserveAnswer.equalsIgnoreCase("y")) {
        reserved = true;
        inputValid = true;                    
    } else if (reserveAnswer.equalsIgnoreCase("n")) {
        reserved = false;
        inputValid = true;
    } else {
        System.out.println("Invalid input");
        System.out.println();
    }
}

inputVal is only needed for looping as long as the input is not valid. More reserved would be candidate for yes, no, I do not know yet / later. Then a Boolean null would be one solution; nowadays one can use Optional<Boolean>, without any null value. But that probably still is not treated.

Source Link
Joop Eggen
  • 4.7k
  • 15
  • 19

Small things first:

  • You can use the diamond operator <>, which preciously saves repetitive typing.
  • Program as much against interfaces, here List with as implementing class ArrayList.
    • You can change the implementation later.
    • As method return, you can use use any implementation, especially Collections.singleton/emptyList(). Most important.
    • As method parameter you can pass any kind of List like a LinkedList.

So:

private static List<Dog> dogList = new ArrayList<>();

For the optional boolean value you are overthinking things. Good meaning by good naming might have helped. A while may be even better:

boolean reserved = false;
boolean inputValid = false;
while (!inputValid) {
    System.out.println("Is the monkey reserved?");
    System.out.println("Enter Y for yes and N for no.");
    String reserveAnswer = scanner.nextLine();
    if (reserveAnswer.equalsIgnoreCase("y")) {
        reserved = true;
        inputValid = true;                    
    } else if (reserveAnswer.equalsIgnoreCase("n")) {
        reserved = false;
        inputValid = true;
    } else {
        System.out.println("Invalid input");
        System.out.println();
    }
}

inputVal is only needed for looping as long as the input is not valid. More reserved would be candidate for yes, no, I do not know yet / later. Then a Boolean null would be one solution; nowadays one can use Optional<Boolean>, without any null value. But that probably still is not treated.