-
Notifications
You must be signed in to change notification settings - Fork 0
Home work2 #2
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: main
Are you sure you want to change the base?
Home work2 #2
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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>org.example</groupId> | ||
| <artifactId>SberJavaHomeWork2</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <properties> | ||
| <maven.compiler.source>17</maven.compiler.source> | ||
| <maven.compiler.target>17</maven.compiler.target> | ||
| </properties> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.13.2</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
|
|
||
| </project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package ru.sber.mironov; | ||
|
|
||
| public class Car { | ||
| private final long carId; | ||
| private final String brand; | ||
| private final String modelName; | ||
| private final int maxVelocity; | ||
| private final int power; | ||
| private final int ownerId; | ||
|
|
||
| public Car(long carId, String brand, String modelName, int maxVelocity, int power, int ownerId) { | ||
|
Collaborator
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. разнести по строкам, читается тяжело когда много аргументов у конструктора в одну строку |
||
| this.carId = carId; | ||
| this.brand = brand; | ||
| this.modelName = modelName; | ||
| this.maxVelocity = maxVelocity; | ||
| this.power = power; | ||
| this.ownerId = ownerId; | ||
| } | ||
|
|
||
|
|
||
| public String getBrand() { | ||
| return brand; | ||
| } | ||
|
|
||
| public String getModelName() { | ||
| return modelName; | ||
| } | ||
|
|
||
| public int getMaxVelocity() { | ||
| return maxVelocity; | ||
| } | ||
|
|
||
| public int getOwnerId() { | ||
| return ownerId; | ||
| } | ||
|
|
||
| public long getCarId() { | ||
| return carId; | ||
| } | ||
|
|
||
| public int getPower() { | ||
| return power; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package ru.sber.mironov; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| import static java.util.stream.Collectors.toList; | ||
|
|
||
| public interface Garage { | ||
|
Collaborator
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. Реализовать интерфейс это значит написать класс реализующий интерфейс, а не сделать дефолтные методы в интерфейсе и константы вместо полей |
||
| HashMap<Owner, ArrayList<Car>> mapOwnerToCars = new HashMap<>(); | ||
| HashMap<Car, Owner> mapCarToOwner = new HashMap<>(); | ||
| HashMap<String, ArrayList<Car>> mapBrandToCar = new HashMap<>(); | ||
| HashMap<Long, Car> cars = new HashMap<>(); | ||
| HashMap<Long, Owner> owners = new HashMap<>(); | ||
|
Comment on lines
+8
to
+12
Collaborator
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. использовать интерфейс вместо реализации при объявлении полей. |
||
|
|
||
| default Collection<Owner> allCarsUniqueOwners() { | ||
| return mapOwnerToCars.keySet().stream() | ||
|
Collaborator
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/hashcode |
||
| .filter(o -> mapOwnerToCars.get(o).size() == 1) | ||
| .distinct() | ||
| .collect(toList()); | ||
| } | ||
|
|
||
| /** | ||
| * Complexity should be less than O(n) | ||
| */ | ||
| default Collection<Car> topThreeCarsByMaxVelocity() { | ||
| ArrayList<Car> sorted = new ArrayList<>(); | ||
| for (Car car : cars.values()) { | ||
| sorted.add(car); | ||
| sorted.sort(Comparator.comparingInt(Car::getMaxVelocity)); | ||
|
Collaborator
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. сортировка минимум O(n log n), а надо меньше чем за O(n)
Owner
Author
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. if (sorted.size() > 3) { |
||
| if (sorted.size() > 3) { | ||
| sorted.remove(0); | ||
| } | ||
| } | ||
| return sorted; | ||
| } | ||
|
|
||
| /** | ||
| * Complexity should be O(1) | ||
| */ | ||
| default Collection<Car> allCarsOfBrand(String brand) { | ||
| return mapBrandToCar.get(brand); | ||
|
Collaborator
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/hashcode |
||
| } | ||
|
|
||
| /** | ||
| * Complexity should be less than O(n) | ||
| */ | ||
| default Collection<Car> carsWithPowerMoreThan(int power) { | ||
|
Collaborator
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. Это просто полный перебор. Не удовлетворяет условиям задачи |
||
| return mapOwnerToCars.values().stream() | ||
| .flatMap(Collection::stream) | ||
| .filter(car -> car.getPower() >= power) | ||
| .distinct() | ||
| .collect(toList()); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Complexity should be O(1) | ||
| */ | ||
| default Collection<Car> allCarsOfOwner(Owner owner) { | ||
| return mapOwnerToCars.get(owner); | ||
|
Collaborator
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/hashcode |
||
| } | ||
|
|
||
| /** | ||
| * @return mean value of owner age that has cars with given brand | ||
| */ | ||
| default int meanOwnersAgeOfCarBrand(String brand) { | ||
| return (int) mapBrandToCar.get(brand).stream() | ||
|
Collaborator
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. один и тот же оунер учитывается несколько раз, если у него несколько машин одного бренда |
||
| .map(mapCarToOwner::get) | ||
| .mapToInt(Owner::getAge) | ||
| .average().getAsDouble(); | ||
| } | ||
|
|
||
| /** | ||
| * @return mean value of cars for all owners | ||
| */ | ||
| default int meanCarNumberForEachOwner() { | ||
| return (int) mapOwnerToCars.keySet().stream() | ||
| .mapToInt(owner -> mapOwnerToCars.get(owner).size()) | ||
| .average().getAsDouble(); | ||
| } | ||
|
|
||
| /** | ||
| * Complexity should be less than O(n) | ||
| * | ||
| * @return removed car | ||
| */ | ||
| default Car removeCar(int carId) { | ||
|
Collaborator
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/hashcode |
||
| Car deleteCar = cars.get((long) carId); | ||
| Owner deleteOwner = owners.get((long) deleteCar.getOwnerId()); | ||
|
|
||
| mapOwnerToCars.get(deleteOwner).remove(deleteCar); | ||
| if (mapOwnerToCars.get(deleteOwner).isEmpty()) { | ||
| mapOwnerToCars.remove(deleteOwner); | ||
| owners.remove(deleteOwner.getOwnerId()); | ||
| } | ||
|
|
||
| mapBrandToCar.get(deleteCar.getBrand()).remove(deleteCar); | ||
| if (mapBrandToCar.get(deleteCar.getBrand()).isEmpty()) { | ||
| mapBrandToCar.remove(deleteCar.getBrand()); | ||
| } | ||
|
|
||
| mapCarToOwner.remove(deleteCar); | ||
|
|
||
|
|
||
| cars.remove((long)carId); | ||
| return deleteCar; | ||
| } | ||
|
|
||
| /** | ||
| * Complexity should be less than O(n) | ||
| */ | ||
| default void addCar(Car car, Owner owner) { | ||
|
|
||
| put(mapOwnerToCars, owner, car); | ||
| put(mapBrandToCar, car.getBrand(), car); | ||
|
|
||
| mapCarToOwner.put(car, owner); | ||
| cars.put(car.getCarId(), car); | ||
| owners.put(owner.getOwnerId(), owner); | ||
| } | ||
|
|
||
| private <K, V> void put(HashMap<K, ArrayList<V>> collectionHashMap, K key, V value) { | ||
|
Collaborator
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. см. computeIfAbsent |
||
| if (collectionHashMap.containsKey(key)) { | ||
| collectionHashMap.get(key).add(value); | ||
| } else { | ||
| collectionHashMap.put(key, new ArrayList<>(Collections.singletonList(value))); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package ru.sber.mironov; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package ru.sber.mironov; | ||
|
|
||
| public class MyGarage implements Garage{ | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package ru.sber.mironov; | ||
|
|
||
| public class Owner { | ||
|
Collaborator
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/hashcode |
||
| private final long ownerId; | ||
| private final String name; | ||
| private final String lastName; | ||
| private final int age; | ||
|
|
||
| public Owner(long ownerId, String name, String lastName, int age) { | ||
| this.ownerId = ownerId; | ||
| this.name = name; | ||
| this.lastName = lastName; | ||
| this.age = age; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public String getLastName() { | ||
| return lastName; | ||
| } | ||
|
|
||
| public long getOwnerId() { | ||
| return ownerId; | ||
| } | ||
|
|
||
| public int getAge() { | ||
| return age; | ||
| } | ||
| } | ||
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.
Не переопределил equals/hashcode