Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package parking;
- public class Car {
- private String manufacturer;
- private String model;
- private int year;
- public void setManufacturer(String manufacturer) {
- this.manufacturer = manufacturer;
- }
- public void setModel(String model) {
- this.model = model;
- }
- public void setYear(int year) {
- this.year = year;
- }
- public Car(String manufacturer, String model, int year) {
- this.setManufacturer(manufacturer);
- this.setModel(model);
- this.setYear(year);
- }
- public String getManufacturer() {
- return this.manufacturer;
- }
- public String getModel() {
- return this.model;
- }
- public int getYear() {
- return this.year;
- }
- @Override
- public String toString (){
- return String.format("%s %s (%d)",this.getManufacturer(),this.getModel(),this.getYear());
- // TODO: 28/06/2020 принт без интервал, интервал/ трим да си проверя
- }
- }
- package parking;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Comparator;
- public class Parking {
- private String type;
- private int capacity;
- private Collection<Car> data;
- public Parking(String type, int capacity) {
- this.type = type;
- this.capacity = capacity;
- data = new ArrayList<>();
- }
- public void add(Car car) {
- if (this.data.size() < this.capacity) {
- this.data.add(car);
- }
- }
- public boolean remove(String manufacturer, String model) {
- boolean hasCar = this.data.stream().anyMatch(e -> e.getManufacturer().equals(manufacturer) && e.getModel().equals(model));
- if (hasCar) {
- Car car = this.data.stream().filter(e -> e.getManufacturer().equals(manufacturer) && e.getModel().equals(model))
- .findFirst().orElse(null);
- this.data.remove(car);
- return true;
- } else {
- return false;
- }
- }
- public Car getLatestCar() {
- return this.data.stream().max(Comparator.comparingInt(Car::getYear)).orElse(null);
- }
- public Car getCar(String manufacturer, String model) {
- return this.data.stream().filter(e -> e.getManufacturer().equals(manufacturer) && e.getModel().equals(model))
- .findFirst().orElse(null);
- }
- public int getCount(){
- return this.data.size();
- }
- public String getStatistics() {
- StringBuilder perPrint = new StringBuilder();
- perPrint.append(String.format("The cars are parked in %s:", this.type));
- perPrint.append(System.lineSeparator());
- this.data.forEach(e -> perPrint.append(e.toString()).append(System.lineSeparator()));
- return perPrint.toString().trim();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement