-
Notifications
You must be signed in to change notification settings - Fork 103
Lesson 49 (optional) #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: for-pr
Are you sure you want to change the base?
Lesson 49 (optional) #108
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| package com.walking.lesson49_optional.task1; | ||
|
|
||
| import com.walking.lesson49_optional.task1.model.Car; | ||
| import com.walking.lesson49_optional.task1.service.CarService; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.Scanner; | ||
|
|
||
| /** | ||
| * Реализуйте задачу 2 урока 43: | ||
| * <a href="https://github.com/KFalcon2022/practical-tasks/tree/master/src/com/walking/lesson43_map/task2">...</a> | ||
|
|
@@ -8,5 +14,49 @@ | |
| */ | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
| CarService carService = new CarService(initCars()); | ||
|
|
||
| Car desiredCar = createCar(); | ||
| Optional<Car> foundCar = carService.findCar(desiredCar); | ||
|
|
||
| String found = foundCar.map("Найдена машина:\n%s"::formatted) | ||
| .orElse("Подходящая машина не обнаружена"); | ||
|
|
||
| System.out.println(found); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. можно было впихнуть в ifPresentOrElse() |
||
| } | ||
|
|
||
| private static Car createCar() { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| System.out.println("Enter a car's number"); | ||
| String number = scanner.nextLine(); | ||
|
|
||
| System.out.println("Enter a car's color"); | ||
| String color = scanner.nextLine(); | ||
|
|
||
| System.out.println("Enter a car's year"); | ||
| int year = scanner.nextInt(); | ||
|
|
||
| System.out.println("Enter a actuality of technical inspection"); | ||
| boolean actualTechnicalInspection = scanner.nextBoolean(); | ||
|
|
||
| scanner.close(); | ||
|
|
||
| return new Car(number, year, color, actualTechnicalInspection); | ||
| } | ||
|
|
||
| private static Car[] initCars() { | ||
| Car car1 = new Car("RR-111-RR", 2015, "yellow", true); | ||
| Car car2 = new Car("RR-222-RR", 2016, "yellow", true); | ||
| Car car3 = new Car("RR-333-RR", 2017, "yellow", true); | ||
| Car car4 = new Car("RR-444-RR", 2018, "yellow", true); | ||
| Car car5 = new Car("RR-555-RR", 2018, "yellow", true); | ||
| Car car6 = new Car("RR-777-RR", 2015, "yellow", true); | ||
| Car car7 = new Car("RR-777-RR", 2018, "yellow", true); | ||
| Car car8 = new Car("RR-888-RR", 2018, "yellow", true); | ||
| Car car9 = new Car("RR-999-RR", 2018, "yellow", true); | ||
| Car car10 = new Car("RR-000-RR", 2018, "yellow", true); | ||
|
|
||
| return new Car[]{car1, car2, car3, car4, car5, car6, car7, car8, car9, car10}; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.walking.lesson49_optional.task1.model; | ||
|
|
||
|
|
||
| public class Car { | ||
| private final CarIdentifier identifier; | ||
|
|
||
| private String color; | ||
| private boolean actualTechnicalInspection; | ||
|
|
||
| public Car(String number, int year, String color, boolean actualTechnicalInspection) { | ||
| this(new CarIdentifier(number, year), color, actualTechnicalInspection); | ||
| } | ||
|
|
||
| public Car(CarIdentifier identifier, String color, boolean actualTechnicalInspection) { | ||
| this.identifier = identifier; | ||
| this.color = color; | ||
| this.actualTechnicalInspection = actualTechnicalInspection; | ||
| } | ||
|
|
||
| public CarIdentifier getIdentifier() { | ||
| return identifier; | ||
| } | ||
|
|
||
| public String getColor() { | ||
| return color; | ||
| } | ||
|
|
||
| public void setColor(String color) { | ||
| this.color = color; | ||
| } | ||
|
|
||
| public boolean isActualTechnicalInspection() { | ||
| return actualTechnicalInspection; | ||
| } | ||
|
|
||
| public void setActualTechnicalInspection(boolean actualTechnicalInspection) { | ||
| this.actualTechnicalInspection = actualTechnicalInspection; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "number: %s | year: %d | color: %s | actualTechnicalInspection: %s".formatted(identifier.getNumber(), | ||
| identifier.getYear(), color, actualTechnicalInspection ? "actual" : "not actual"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package com.walking.lesson49_optional.task1.model; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class CarIdentifier { | ||
| private final String number; | ||
| private final int year; | ||
|
|
||
| public CarIdentifier(String number, int year) { | ||
| this.number = number; | ||
| this.year = year; | ||
| } | ||
|
|
||
| public String getNumber() { | ||
| return number; | ||
| } | ||
|
|
||
| public int getYear() { | ||
| return year; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
|
|
||
| if (o == null || !getClass().equals(o.getClass())) { | ||
| return false; | ||
| } | ||
|
|
||
| CarIdentifier identifier = (CarIdentifier) o; | ||
|
|
||
| return year == identifier.year && Objects.equals(number, identifier.number); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int hashcode = number.hashCode(); | ||
| hashcode += 31 * year; | ||
|
|
||
| return hashcode; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.walking.lesson49_optional.task1.service; | ||
|
|
||
| import com.walking.lesson49_optional.task1.model.Car; | ||
| import com.walking.lesson49_optional.task1.model.CarIdentifier; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| public class CarService { | ||
| private final Map<CarIdentifier, Car> cars = new HashMap<>(); | ||
|
|
||
| public CarService(Car[] cars) { | ||
| for (Car car : cars) { | ||
| this.cars.put(car.getIdentifier(), car); | ||
| } | ||
| } | ||
|
|
||
| public Optional<Car> findCar(Car car) { | ||
| return Optional.ofNullable(car) | ||
| .map(Car::getIdentifier) | ||
| .map(cars::get); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,15 @@ | ||
| package com.walking.lesson49_optional.task2; | ||
|
|
||
| import com.walking.lesson49_optional.task2.model.Car; | ||
| import com.walking.lesson49_optional.task2.model.CarIdentifier; | ||
| import com.walking.lesson49_optional.task2.model.Home; | ||
| import com.walking.lesson49_optional.task2.model.Person; | ||
| import com.walking.lesson49_optional.task2.service.CarService; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Реализуйте рад классов: | ||
| * 1. Жилье, должен иметь поле «адрес»; | ||
|
|
@@ -17,5 +27,33 @@ | |
| */ | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
| Home testHome = new Home("ADDRESS"); | ||
| Car testCar = new Car(new CarIdentifier("NUMBER", 2000), null, "red", true); | ||
| Person testCarOwner = new Person("OWNER", null, null, new ArrayList<>(), testHome, testCar, "fireman"); | ||
| testCar.setOwner(testCarOwner); | ||
| Person testCarOwnerChild = new Person("CHILD", null, testCarOwner, null, testHome, null, "fireman"); | ||
| testCarOwner.addChild(testCarOwnerChild); | ||
|
|
||
| CarService carService = new CarService(new Car[]{testCar}); | ||
|
|
||
| Optional<String> foundAddress = carService.findCar(testCar) | ||
| .filter(car -> car.getIdentifier() | ||
| .getYear() <= 2021) | ||
| .map(Car::getOwner) | ||
| .filter(person -> !person.getProfession() | ||
| .equals("policeman")) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Конструкции с equals и константой/литералом зачастую лучше описывать в другом порядке: "policeman".equals(person.getProfession())во избежание NPE на ровном месте
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Конструкции с equals и константой/литералом зачастую лучше описывать в другом порядке: "policeman".equals(person.getProfession())во избежание NPE на ровном месте |
||
| .map(Person::getChildren) | ||
| .filter(list -> !list.isEmpty()) | ||
| .map(List::getFirst) | ||
| .filter(person -> !person.getProfession() | ||
| .equals("policeman")) | ||
| .map(Person::getHome) | ||
| .map(Home::getAddress) | ||
| .filter(address -> !address.contains("Рублевское шоссе")); | ||
|
|
||
| String address = foundAddress.map("Найден адрес:\n%s"::formatted) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. зачем отдельная переменная |
||
| .orElse("Адресс не найден"); | ||
|
|
||
| System.out.println(address); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ifPresentOrElse()? |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package com.walking.lesson49_optional.task2.model; | ||
|
|
||
|
|
||
| public class Car { | ||
| private final CarIdentifier identifier; | ||
|
|
||
| private Person owner; | ||
| private String color; | ||
| private boolean actualTechnicalInspection; | ||
|
|
||
| public Car(String number, int year, Person owner, String color, boolean actualTechnicalInspection) { | ||
| this(new CarIdentifier(number, year), owner, color, actualTechnicalInspection); | ||
| } | ||
|
|
||
| public Car(CarIdentifier identifier, Person owner, String color, boolean actualTechnicalInspection) { | ||
| this.identifier = identifier; | ||
| this.owner = owner; | ||
| this.color = color; | ||
| this.actualTechnicalInspection = actualTechnicalInspection; | ||
| } | ||
|
|
||
| public CarIdentifier getIdentifier() { | ||
| return identifier; | ||
| } | ||
|
|
||
| public Person getOwner() { | ||
| return owner; | ||
| } | ||
|
|
||
| public void setOwner(Person owner) { | ||
| this.owner = owner; | ||
| } | ||
|
|
||
| public String getColor() { | ||
| return color; | ||
| } | ||
|
|
||
| public void setColor(String color) { | ||
| this.color = color; | ||
| } | ||
|
|
||
| public boolean isActualTechnicalInspection() { | ||
| return actualTechnicalInspection; | ||
| } | ||
|
|
||
| public void setActualTechnicalInspection(boolean actualTechnicalInspection) { | ||
| this.actualTechnicalInspection = actualTechnicalInspection; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "number: %s | year: %d | owner: %s | color: %s | actualTechnicalInspection: %s".formatted(identifier.getNumber(), | ||
| identifier.getYear(), owner.getName(), color, actualTechnicalInspection ? "actual" : "not actual"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package com.walking.lesson49_optional.task2.model; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class CarIdentifier { | ||
| private final String number; | ||
| private final int year; | ||
|
|
||
| public CarIdentifier(String number, int year) { | ||
| this.number = number; | ||
| this.year = year; | ||
| } | ||
|
|
||
| public String getNumber() { | ||
| return number; | ||
| } | ||
|
|
||
| public int getYear() { | ||
| return year; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
|
|
||
| if (o == null || !getClass().equals(o.getClass())) { | ||
| return false; | ||
| } | ||
|
|
||
| CarIdentifier identifier = (CarIdentifier) o; | ||
|
|
||
| return year == identifier.year && Objects.equals(number, identifier.number); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int hashcode = number.hashCode(); | ||
| hashcode += 31 * year; | ||
|
|
||
| return hashcode; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.walking.lesson49_optional.task2.model; | ||
|
|
||
| public class Home { | ||
| private final String address; | ||
|
|
||
| public Home(String address) { | ||
| this.address = address; | ||
| } | ||
|
|
||
| public String getAddress() { | ||
| return address; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Избыточная переменная