motorc

motorc
mail@pastecode.io avatar
unknown
java
2 years ago
2.8 kB
2
Indexable
package com.company;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.NoSuchElementException;

public class Motorcycle {
    final static double vat = 1.23;

    private static List<Motorcycle> motorcycleList = new ArrayList<>(); //ekstensja
    private static int serialNumer = 1; //atrybut klasowy

    private List<String> parts; //atrybut powtarzalny
    private Engine engine;  //atrybut złożony
    private String brand;
    private double netPrice;
    private int serial;
    private String typeOfAssistSystem;  //atrybut opcjonalny

    public static Motorcycle getMotorcycleWithHighestEngineSize() {  //metoda klasowa
        return motorcycleList.stream().max(Comparator.comparing(c -> c.getEngineSize()))
                .orElseThrow(NoSuchElementException::new);
    }

    public Motorcycle(String brand, double netPrice, List<String> parts, Engine engine) {
        serial = serialNumer;
        serialNumer++;
        this.brand = brand;
        this.netPrice = netPrice;
        this.typeOfAssistSystem = null;
        this.parts = parts;
        this.engine = engine;
    }

    public Motorcycle(String brand, double netPrice, String typeOfAssistSystem, List<String> parts, Engine engine) {
        serial = serialNumer;
        serialNumer++;
        this.brand = brand;
        this.netPrice = netPrice;
        this.typeOfAssistSystem = typeOfAssistSystem;
        this.parts = parts;
        this.engine = engine;
    }

    public Engine getEngine() {
        return engine;
    }

    public int getEngineSize() {
        return this.engine.getEngineSize();
    }

    public static void addMotorcycle(Motorcycle motorcycle) {
        motorcycleList.add(motorcycle);
    }

    public static void removeMotorcycle(Motorcycle motorcycle) {
        motorcycleList.remove(motorcycle);
    }

    public static void showMotorcycle() {
        for (Motorcycle motorcycle : motorcycleList) {
            System.out.println(motorcycle);
        }
    }

    public double getGrossPriceOfMotorcycle() {  //atrybut pochodny
        return netPrice * vat;
    }

    public double getGrossPriceOfMotorcycle(double discount) { //przeciążenie
        return netPrice * vat - discount;
    }

    public String toString() {  // przesłonięcie
        return " Memory allocation: " + getClass().getName() + "@" + Integer.toHexString(hashCode()) +
                "Motorcycle:" + " serial number: " + serial + ", brand: " + brand + ", net price: " + netPrice
                + ", type of assist system: " + typeOfAssistSystem + "engine data: (" + engine + ")"
                + ", parts in motorcycle: ( " + parts + ")";
    }
}