empty input will now be considered as null for some fields (Person.passportId, Person.height and Location.name), fixed data classes setters for some fields

This commit is contained in:
Egor 2022-02-21 18:23:04 +03:00
parent dccded9944
commit 0ba5fca0c1
4 changed files with 9 additions and 9 deletions

View file

@ -190,7 +190,7 @@ public final class PeopleDatabaseCommands {
System.out.println("Создание нового объекта класса Person"); System.out.println("Создание нового объекта класса Person");
String name = CMD.awaitInput("Введите имя:", "Введите непустую строку", String name = CMD.awaitInput("Введите имя:", "Введите непустую строку",
input -> !input.isEmpty()); input -> !input.isEmpty());
int height = CMD.awaitInput("Введите рост:", "Введите целое число, большее нуля", Integer height = CMD.awaitInput("Введите рост:", "Введите целое число, большее нуля",
input -> { input -> {
Integer result = UtilFunctions.intOrNull(input); Integer result = UtilFunctions.intOrNull(input);
return result != null && result > 0 || input.isEmpty(); return result != null && result > 0 || input.isEmpty();

View file

@ -41,8 +41,8 @@ public class Coordinates {
* @throws IllegalArgumentException Если Y меньше или равен -816 * @throws IllegalArgumentException Если Y меньше или равен -816
*/ */
public void setY(float y) { public void setY(float y) {
this.y = y;
if (y <= -816) if (y <= -816)
throw new IllegalArgumentException("Поле y класса Coordinates должно быть больше -816"); throw new IllegalArgumentException("Поле y класса Coordinates должно быть больше -816");
this.y = y;
} }
} }

View file

@ -54,9 +54,9 @@ public class Location implements Comparable<Location> {
* @throws IllegalArgumentException Если name является пустой строкой * @throws IllegalArgumentException Если name является пустой строкой
*/ */
public void setName(String name) { public void setName(String name) {
this.name = name; if (name != null && name.isEmpty())
if (name.isEmpty())
throw new IllegalArgumentException("Поле name класса Location не может быть пустым"); throw new IllegalArgumentException("Поле name класса Location не может быть пустым");
this.name = name;
} }
/** /**

View file

@ -130,9 +130,9 @@ public class Person implements Comparable<Person> {
* Если имя является пустой строкой * Если имя является пустой строкой
*/ */
public void setName(String name) { public void setName(String name) {
this.name = name;
if (name.isEmpty()) if (name.isEmpty())
throw new IllegalArgumentException("Поле name класса Person не может быть null или пустым"); throw new IllegalArgumentException("Поле name класса Person не может быть null или пустым");
this.name = name;
} }
/** /**
@ -143,9 +143,9 @@ public class Person implements Comparable<Person> {
* @throws IllegalArgumentException Если рост меньше 0 * @throws IllegalArgumentException Если рост меньше 0
*/ */
public void setHeight(Integer height) { public void setHeight(Integer height) {
this.height = height; if (height != null && height <= 0)
if (height <= 0)
throw new IllegalArgumentException("Поле height класса Person должно быть больше 0"); throw new IllegalArgumentException("Поле height класса Person должно быть больше 0");
this.height = height;
} }
/** /**
@ -156,9 +156,9 @@ public class Person implements Comparable<Person> {
* @throws IllegalArgumentException Если номер паспорта меньше 8 символов в длину * @throws IllegalArgumentException Если номер паспорта меньше 8 символов в длину
*/ */
public void setPassportID(String passportID) { public void setPassportID(String passportID) {
this.passportID = passportID; if (passportID != null && passportID.length() < 8)
if (passportID.length() < 8)
throw new IllegalArgumentException("Поле passportID класса Person не может быть меньше 8 символов в длину"); throw new IllegalArgumentException("Поле passportID класса Person не может быть меньше 8 символов в длину");
this.passportID = passportID;
} }
/** /**