2 Commits
v3 ... v4

27 changed files with 226 additions and 384 deletions

View File

@ -1,5 +1,10 @@
import net.itsthesky.projetbut2.qualdev.FindInstrumentTester;
import net.itsthesky.projetbut2.dip.DIPClient;
import net.itsthesky.projetbut2.ocp.OCPClient;
void main() {
FindInstrumentTester.main(new String[]{});
void main(String[] args) {
if (args.length == 0) return;
if (args[0].equalsIgnoreCase("ocp")) OCPClient.main();
else if (args[0].equalsIgnoreCase("dip")) DIPClient.main();
else IO.println("Invalid argument: '" + args[0] + "'");
}

View File

@ -0,0 +1,10 @@
package net.itsthesky.projetbut2.dip;
public class BackEndDeveloper implements Developer {
@Override
public void develop() {
IO.println("I write Java code !");
}
}

View File

@ -0,0 +1,10 @@
package net.itsthesky.projetbut2.dip;
public final class DIPClient {
public static void main() {
Project project = new Project();
project.implement();
}
}

View File

@ -0,0 +1,7 @@
package net.itsthesky.projetbut2.dip;
public interface Developer {
void develop();
}

View File

@ -0,0 +1,9 @@
package net.itsthesky.projetbut2.dip;
public class FrontEndDeveloper implements Developer{
@Override
public void develop() {
IO.println("I write Javascript code !");
}
}

View File

@ -0,0 +1,21 @@
package net.itsthesky.projetbut2.dip;
import java.util.ArrayList;
import java.util.List;
public class Project {
private final List<Developer> developers = new ArrayList<>();
public Project() {
developers.add(new BackEndDeveloper());
developers.add(new FrontEndDeveloper());
}
public void implement() {
for (var developer : developers) {
developer.develop();
}
}
}

View File

@ -0,0 +1,17 @@
package net.itsthesky.projetbut2.ocp;
import net.itsthesky.projetbut2.ocp.base.Color;
import net.itsthesky.projetbut2.ocp.base.Product;
public class ColorCriteria implements Criteria<Product> {
private final Color color;
public ColorCriteria(Color color) {
this.color = color;
}
@Override
public boolean isSatisfied(Product product) {
return product.color == color;
}
}

View File

@ -0,0 +1,7 @@
package net.itsthesky.projetbut2.ocp;
public interface Criteria<Product> {
boolean isSatisfied(Product product);
}

View File

@ -0,0 +1,18 @@
package net.itsthesky.projetbut2.ocp;
import net.itsthesky.projetbut2.ocp.base.Product;
import java.util.stream.Stream;
public class MultipleCriteria implements Criteria<Product> {
private final Criteria<Product>[] criteria;
public MultipleCriteria(Criteria<Product>... criteria) {
this.criteria = criteria;
}
@Override
public boolean isSatisfied(Product product) {
return Stream.of(criteria).allMatch(c -> c.isSatisfied(product));
}
}

View File

@ -0,0 +1,16 @@
package net.itsthesky.projetbut2.ocp;
import net.itsthesky.projetbut2.ocp.base.Product;
public class NameCriteria implements Criteria<Product> {
private final String name;
public NameCriteria(String name) {
this.name = name;
}
@Override
public boolean isSatisfied(Product product) {
return product.name.contains(name);
}
}

View File

@ -0,0 +1,24 @@
package net.itsthesky.projetbut2.ocp;
import net.itsthesky.projetbut2.ocp.base.Color;
import net.itsthesky.projetbut2.ocp.base.Product;
import net.itsthesky.projetbut2.ocp.base.Size;
import java.util.List;
public final class OCPClient {
public static void main() {
Product apple = new Product("Apple", Color.GREEN, Size.SMALL);
Product tree = new Product("Tree", Color.GREEN, Size.LARGE);
Product house = new Product("House", Color.BLUE, Size.LARGE);
List<Product> products = List.of(apple, tree, house);
ProductFilter filter = new ProductFilter();
System.out.println("Green products : ");
filter.filter(products, new MultipleCriteria(new ColorCriteria(Color.GREEN), new SizeCriteria(Size.SMALL)))
.forEach(product -> System.out.println(" - " + product.name + " is green and small"));
}
}

View File

@ -0,0 +1,18 @@
package net.itsthesky.projetbut2.ocp;
import net.itsthesky.projetbut2.ocp.base.Product;
public class PriceRangeCriteria implements Criteria<Product> {
private final double minPrice;
private final double maxPrice;
public PriceRangeCriteria(double minPrice, double maxPrice) {
this.minPrice = minPrice;
this.maxPrice = maxPrice;
}
@Override
public boolean isSatisfied(Product product) {
return product.price >= minPrice && product.price <= maxPrice;
}
}

View File

@ -0,0 +1,14 @@
package net.itsthesky.projetbut2.ocp;
import net.itsthesky.projetbut2.ocp.base.Product;
import java.util.List;
import java.util.stream.Stream;
public class ProductFilter {
public Stream<Product> filter(List<Product> products, Criteria<Product> criteria) {
return products.stream().filter(criteria::isSatisfied);
}
}

View File

@ -0,0 +1,17 @@
package net.itsthesky.projetbut2.ocp;
import net.itsthesky.projetbut2.ocp.base.Product;
import net.itsthesky.projetbut2.ocp.base.Size;
public class SizeCriteria implements Criteria<Product> {
private final Size size;
public SizeCriteria(Size size) {
this.size = size;
}
@Override
public boolean isSatisfied(Product product) {
return product.size == size;
}
}

View File

@ -0,0 +1,5 @@
package net.itsthesky.projetbut2.ocp.base;
public enum Color {
RED, GREEN, BLUE
}

View File

@ -0,0 +1,20 @@
package net.itsthesky.projetbut2.ocp.base;
public class Product {
public String name;
public Color color;
public Size size;
public double price;
public Product(String name, Color color, Size size, double price) {
this.name = name;
this.color = color;
this.size = size;
this.price = price;
}
public Product(String name, Color color, Size size) {
this(name, color, size, 0);
}
}

View File

@ -0,0 +1,5 @@
package net.itsthesky.projetbut2.ocp.base;
public enum Size {
SMALL, MEDIUM, LARGE, HUGE
}

View File

@ -1,115 +0,0 @@
package net.itsthesky.projetbut2.qualdev;
import net.itsthesky.projetbut2.qualdev.model.Specification;
import net.itsthesky.projetbut2.qualdev.model.Instrument;
import net.itsthesky.projetbut2.qualdev.model.InstrumentSpec;
import net.itsthesky.projetbut2.qualdev.model.specs.*;
import java.util.Arrays;
import java.util.List;
public class FindInstrumentTester {
public static void main(String[] args) {
// Set up Rick's inventory
Inventory inventory = new Inventory();
initializeInventory(inventory);
InstrumentSpec whatBryanLikes = new InstrumentSpec(Arrays.asList(
Specification.INSTRUMENT_TYPE, InstrumentType.MANDOLIN,
Specification.STYLE, Style.F));
whatBryanLikes = new InstrumentSpec(Arrays.asList(
Specification.INSTRUMENT_TYPE, InstrumentType.BANJO));
List matchingInstruments = inventory.search(whatBryanLikes);
if (!matchingInstruments.isEmpty())
System.out.println("Here's what we have for you:\n" + matchingInstruments);
else
System.out.println("Sorry, we have nothing for you.");
}
public static void initializeInventory(Inventory inventory) {
inventory.addInstrument(new Instrument("82765501", 1890.95,
new InstrumentSpec(Arrays.asList(
Specification.INSTRUMENT_TYPE, InstrumentType.GUITAR,
Specification.TYPE, Type.ELECTRIC,
Specification.MODEL, "SG '61 Reissue",
Specification.BACKWOOD, Wood.MAHOGANY,
Specification.TOPWOOD, Wood.MAHOGANY))));
inventory.addInstrument(new Instrument("9019920", 5495.99,
new InstrumentSpec(Arrays.asList(
Specification.INSTRUMENT_TYPE, InstrumentType.MANDOLIN,
Specification.TYPE, Type.ACOUSTIC,
Specification.MODEL, "DRSM-008 E",
Specification.BACKWOOD, Wood.MAPLE,
Specification.TOPWOOD, Wood.MAPLE,
Specification.BUILDER, Builder.PRS,
Specification.STYLE, Style.A
))));
inventory.addInstrument(new Instrument("7819920", 549,
new InstrumentSpec(Arrays.asList(
Specification.INSTRUMENT_TYPE, InstrumentType.MANDOLIN,
Specification.TYPE, Type.ACOUSTIC,
Specification.MODEL, "F-5G",
Specification.BACKWOOD, Wood.MAPLE,
Specification.TOPWOOD, Wood.MAPLE,
Specification.BUILDER, Builder.GIBSON,
Specification.STYLE, Style.F
))));
inventory.addInstrument(new Instrument("8900231", 2945.95,
new InstrumentSpec(Arrays.asList(
Specification.INSTRUMENT_TYPE, InstrumentType.BANJO,
Specification.MODEL, "RB-3 Wreath",
Specification.YEAR_OF_MANUFACTURE, 1967
))));
inventory.addInstrument(new Instrument("70108276", 2295.95,
new InstrumentSpec(Arrays.asList(
Specification.INSTRUMENT_TYPE, InstrumentType.GUITAR,
Specification.BUILDER, Builder.GIBSON,
Specification.MODEL, "Les Paul",
Specification.BACKWOOD, Wood.MAPLE,
Specification.TOPWOOD, Wood.MAPLE
))));
inventory.addInstrument(new Instrument("V95693", 1499.95,
new InstrumentSpec(Arrays.asList(
Specification.INSTRUMENT_TYPE, InstrumentType.GUITAR,
Specification.BUILDER, Builder.FENDER,
Specification.MODEL, "Stratocastor",
Specification.BACKWOOD, Wood.ALDER,
Specification.TOPWOOD, Wood.ALDER,
Specification.TYPE, Type.ELECTRIC
))));
inventory.addInstrument(new Instrument("V9512", 1549.95,
new InstrumentSpec(Arrays.asList(
Specification.INSTRUMENT_TYPE, InstrumentType.GUITAR,
Specification.BUILDER, Builder.FENDER,
Specification.MODEL, "Stratocastor",
Specification.BACKWOOD, Wood.ALDER,
Specification.TOPWOOD, Wood.ALDER,
Specification.TYPE, Type.ELECTRIC
))));
inventory.addInstrument(new Instrument("122784", 5495.95,
new InstrumentSpec(Arrays.asList(
Specification.INSTRUMENT_TYPE, InstrumentType.GUITAR,
Specification.BUILDER, Builder.MARTIN,
Specification.MODEL, "D-18",
Specification.BACKWOOD, Wood.MAHOGANY,
Specification.TOPWOOD, Wood.ADIRONDACK
))));
inventory.addInstrument(new Instrument("11277", 3999.95,
new InstrumentSpec(Arrays.asList(
Specification.INSTRUMENT_TYPE, InstrumentType.GUITAR,
Specification.BUILDER, Builder.COLLINGS,
Specification.MODEL, "CJ",
Specification.BACKWOOD, Wood.INDIAN_ROSEWOOD,
Specification.TOPWOOD, Wood.SITKA,
Specification.TYPE, Type.ACOUSTIC,
Specification.NUMBER_OF_STRINGS, 6
))));
}
}

View File

@ -1,54 +0,0 @@
package net.itsthesky.projetbut2.qualdev;
import net.itsthesky.projetbut2.qualdev.model.Instrument;
import net.itsthesky.projetbut2.qualdev.model.InstrumentSpec;
import java.util.ArrayList;
import java.util.List;
/**
* Gère l'inventaire des instruments
*/
public class Inventory {
private final List<Instrument> instruments = new ArrayList<>();
/**
* Rajoute un instrument à cet inventaire, basé sur son serial number,
* prix et spécifications.
* @param instrument l'instrument à ajouter
* @see InstrumentSpec
*/
public void addInstrument(Instrument instrument) {
instruments.add(instrument);
}
/**
* Récupère un instrument de cet instrument basé sur son numéro de série.
* @param serialNumber le numéro de série à chercher
* @return l'instrument trouvé avec le même numéro de série fourni, sinon <code>null</code>
*/
public Instrument getInstrument(String serialNumber) {
return instruments.stream()
.filter(instrument -> instrument.getSerialNumber().equals(serialNumber))
.findFirst()
.orElse(null);
}
/**
* Recherche tous les instruments correspondant aux spécifications fournies.
* @param instrumentSpec les spécifications à rechercher
* @return une liste d'instruments correspondant aux spécifications
*/
public List<Instrument> search(InstrumentSpec instrumentSpec) {
final var result = new ArrayList<Instrument>();
for (Instrument instrument : instruments) {
if (instrumentSpec.matches(instrument.getInstrumentSpec()))
result.add(instrument);
}
return result;
}
}

View File

@ -1,64 +0,0 @@
package net.itsthesky.projetbut2.qualdev.model;
/**
* Classe abstraite représentant un instrument de musique.
*/
public class Instrument {
private final String serialNumber;
private final InstrumentSpec instrumentSpec;
private double price;
/**
* Constructeur d'un instrument.
* @param serialNumber le numéro de série de l'instrument
* @param price le prix de l'instrument
* @param instrumentSpec les spécifications de l'instrument
*/
public Instrument(String serialNumber, double price, InstrumentSpec instrumentSpec) {
this.serialNumber = serialNumber;
this.instrumentSpec = instrumentSpec;
this.price = price;
}
/**
* Récupère le numéro de série de l'instrument.
* @return le numéro de série
*/
public String getSerialNumber() {
return serialNumber;
}
/**
* Récupère les spécifications de l'instrument.
* @return les spécifications
*/
public InstrumentSpec getInstrumentSpec() {
return instrumentSpec;
}
/**
* Récupère le prix de l'instrument.
* @return le prix
*/
public double getPrice() {
return price;
}
/**
* Modifie le prix de l'instrument.
* @param price le nouveau prix
*/
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Instrument{" +
"serialNumber='" + serialNumber + '\'' +
", instrumentSpec=" + instrumentSpec +
", price=" + price +
'}';
}
}

View File

@ -1,69 +0,0 @@
package net.itsthesky.projetbut2.qualdev.model;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Classe abstraite représentant les spécifications d'un instrument de musique.
*/
public class InstrumentSpec {
private final Map<Specification<?>, Object> caracteristiques;
/**
* Constructeur des spécifications de l'instrument.
*/
public InstrumentSpec() {
this.caracteristiques = new HashMap<>();
}
public InstrumentSpec(List<Object> specs) {
this.caracteristiques = new HashMap<>();
for (int i = 0; i < specs.size(); i += 2) {
Specification<?> key = (Specification<?>) specs.get(i);
Object value = specs.get(i + 1);
if (value.getClass() != key.type())
throw new IllegalArgumentException("Type de valeur incorrect pour la caractéristique " + key.label());
this.caracteristiques.put(key, value);
}
}
/**
* Vérifie si ces spécifications correspondent à d'autres spécifications.
* @param otherSpec les spécifications à comparer
* @return <code>true</code> si les spécifications correspondent, <code>false</code> sinon
*/
public boolean matches(InstrumentSpec otherSpec) {
if (otherSpec == null) return false;
for (var caracteristique : caracteristiques.entrySet()) {
var otherValue = otherSpec.caracteristiques.get(caracteristique.getKey());
if (otherValue == null || !caracteristique.getValue().equals(otherValue)) {
return false;
}
}
return true;
}
public Map<Specification<?>, ?> getCaracteristiques() {
return caracteristiques;
}
public <T> T getCaracteristique(Specification<T> specification) {
return (T) caracteristiques.get(specification);
}
public <T> InstrumentSpec addCaracteristique(Specification<T> specification, T value) {
this.caracteristiques.put(specification, value);
return this;
}
@Override
public String toString() {
return "InstrumentSpec{" +
"caracteristiques=" + caracteristiques +
'}';
}
}

View File

@ -1,19 +0,0 @@
package net.itsthesky.projetbut2.qualdev.model;
import net.itsthesky.projetbut2.qualdev.model.specs.*;
public record Specification<T>(Class<T> type, String label) {
public static Specification<Builder> BUILDER = new Specification<>(Builder.class, "Fabricantt");
public static Specification<String> MODEL = new Specification<>(String.class, "Modèle");
public static Specification<Integer> NUMBER_OF_STRINGS = new Specification<>(Integer.class, "Nombre de cordes");
public static Specification<Type> TYPE = new Specification<>(Type.class, "Type");
public static Specification<Style> STYLE = new Specification<>(Style.class, "Style");
public static Specification<Integer> YEAR_OF_MANUFACTURE = new Specification<>(Integer.class, "Année de fabrication");
public static Specification<Wood> TOPWOOD = new Specification<>(Wood.class, "Bois dessus");
public static Specification<Wood> BACKWOOD = new Specification<>(Wood.class, "Bois dessous");
public static Specification<InstrumentType> INSTRUMENT_TYPE = new Specification<>(InstrumentType.class, "Type d'instrument");
}

View File

@ -1,14 +0,0 @@
package net.itsthesky.projetbut2.qualdev.model.specs;
/**
* Représente un constructeur d'instrument de musique.
*/
public enum Builder {
COLLINGS,
FENDER,
GIBSON,
MARTIN,
OLSON,
RYAN,
PRS
}

View File

@ -1,11 +0,0 @@
package net.itsthesky.projetbut2.qualdev.model.specs;
public enum InstrumentType {
GUITAR, MANDOLIN, CELLO, BANJO, VIOLIN, DOBRO, FIDDLE, BASS, ;
public String toString () {
return this.name().toLowerCase();
}
}

View File

@ -1,10 +0,0 @@
package net.itsthesky.projetbut2.qualdev.model.specs;
/**
* Représente le style d'une {@link net.itsthesky.projetbut2.qualdev.model.mandoline.MandolineSpec mandoline}; A pour
* Acoustique et F pour Fender.
*/
public enum Style {
A,
F
}

View File

@ -1,8 +0,0 @@
package net.itsthesky.projetbut2.qualdev.model.specs;
/**
* Représent le type d'une {@link net.itsthesky.projetbut2.qualdev.model.guitar.GuitarSpec guitar}.
*/
public enum Type {
ACOUSTIC, ELECTRIC
}

View File

@ -1,17 +0,0 @@
package net.itsthesky.projetbut2.qualdev.model.specs;
/**
* Représente le type de bois utilisé dans la confection
* de l'{@link net.itsthesky.projetbut2.qualdev.model.Instrument instrument}.
*/
public enum Wood {
ADIRONDACK,
CEDAR,
COCOBOLO,
MAHOGANY,
MAPLE,
SITKA,
ALDER,
BRAZILIAN_ROSEWOOD,
INDIAN_ROSEWOOD
}