Commit f2ee5d89 authored by Arthur POIGNONNEC's avatar Arthur POIGNONNEC

[FIX] Refactoring du projet

parent f84f3201
ProjetJava
\ No newline at end of file
plugins {
id 'java'
}
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
#Mon May 20 10:31:00 CEST 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-all.zip
#!/usr/bin/env sh
##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn () {
echo "$*"
}
die () {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Escape application args
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
APP_ARGS=$(save "$@")
# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi
exec "$JAVACMD" "$@"
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windows variants
if not "%OS%" == "Windows_NT" goto win9xME_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
rootProject.name = 'ProjetJava'
package garage.garage;
public class ExecutionFailedException extends Exception{
public ExecutionFailedException(String message) {
super(message);
}
}
package garage.garage;
import garage.vehicle.Vehicle;
import java.util.LinkedList;
public class Garage {
private LinkedList<Vehicle> stock;
private String owner;
private String garageName;
// CONSTRUCTOR **********************************************************************
public Garage() {
this.stock = new LinkedList<Vehicle>();
this.owner = "Owner";
this.garageName = "Name garage";
}
public Garage(LinkedList<Vehicle> stockEnregistrer) {
this.stock = stockEnregistrer;
this.owner = "Owner";
this.garageName = "Nom du garage";
}
// Accesseur : ***********************************************************************************
public LinkedList<Vehicle> getStock() {
return this.stock;
}
public String getOwner() {
return this.owner;
}
public String getGarageName() {
return this.garageName;
}
public void addVoiture(Vehicle car){
this.stock.add(car);
}
}
\ No newline at end of file
package garage.garage;
import java.lang.reflect.Method;
import java.util.List;
public class Task {
private Object obj;
private String method;
private List<Object> args;
private Integer t;
public Task(Object obj, String method, List<Object> args, Integer t) {
this.obj = obj;
this.method = method;
this.args = args;
this.t = t;
}
//METHODS
public void execute() throws ExecutionFailedException {
Class[] mArgs = new Class[0];
if(args != null){
mArgs = new Class[args.size()];
for(Integer i = 0; i < args.size(); i++) mArgs[i] = args.get(i).getClass();
}
try {
System.out.println("Calling method " + method + " of class -> " + obj.getClass());
Method m = obj.getClass().getMethod(method, mArgs);
if(args == null) m.invoke(obj, null);
else m.invoke(obj, args.toArray());
}catch(Exception e){
throw new ExecutionFailedException(e.getMessage());
}
}
// GETTERS
public Object getObj() {
return obj;
}
public String getMethod() {
return method;
}
public List<Object> getArgs() {
return args;
}
public Integer getT() {
return t;
}
}
package garage.garage;
import garage.vehicle.Vehicle;
import java.util.ArrayList;
import java.util.List;
public class TestBench {
private Vehicle vehicle;
private List<Task> tasks;
private Double distance;
public TestBench() {
tasks = new ArrayList<>();
vehicle = null;
}
public void addVehicle(Vehicle vehicle) {
if (this.vehicle == null) {
this.vehicle = vehicle;
} else {
System.out.println("A car is already on the testbench");
}
}
public void removeVehicle() {
if (this.vehicle != null) {
this.vehicle = null;
} else {
System.out.println("No vehicle in the test bench.");
}
}
public Vehicle swapVehicle(Vehicle vehicule) {
if (this.vehicle != null) {
Vehicle temp = this.vehicle;
this.vehicle = vehicule;
return temp;
} else {
System.out.println("No vehicle in the test bench.");
return null;
}
}
public void addTask(Task task) {
tasks.add(task);
}
public void removeTask(Task task) {
tasks.remove(task);
}
public Vehicle getVehicle() {
return this.vehicle;
}
public List<Task> getTasks() {
return tasks;
}
public void run(Integer time) {
if(vehicle != null) {
for (Integer t = 0; t < time; t++) {
final Integer tick = t;
tasks.stream().filter(task -> tick.equals(task.getT())).forEach(task -> {
try {
task.execute();
vehicle.update();
} catch (ExecutionFailedException e) {
System.out.println("ERROR -> " + e.getMessage());
}
});
}
}else{
System.out.println("Aucune véhicule sur le banc d'essai");
}
}
}
package garage.main;
import garage.garage.Garage;
import garage.vehicle.Vehicle;
import java.io.*;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) throws Exception {
// Déclaration des objets :
ObjectInputStream ois;
ObjectOutputStream oos;
Garage stockGarage = new Garage();
// Affichage titre :
System.out.println(" *********************** \n * " + stockGarage.getGarageName() + " * \n ***********************\n");
// Lecture du fichier ou les véhicules sont sauvegardés :
try {
ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File("garage.txt"))));
stockGarage = new Garage((LinkedList<Vehicle>) ois.readObject());
ois.close();
}catch (FileNotFoundException e) {
System.out.println("Aucune voitures sauvegarder !\n");
}catch (IOException e) {
System.out.println("Aucune voitures sauvegarder !\n");
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
// CREATE vehicle LAGUNA
// CREATE vehicle D4
// CREATE vehicle A300B
// Ecriture de la liste de véhicule dans le fichier :
try {
oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(new File("garage.txt"))));
oos.writeObject(stockGarage.getStock());
oos.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
package garage.vehicle;
import garage.vehicle.part.brake.Brake;
import garage.vehicle.part.engine.Engine;
import garage.vehicle.part.frame.Frame;
import garage.vehicle.part.gearbox.GearBox;
import garage.vehicle.part.gearbox.GearBoxAutomatic;
import garage.vehicle.part.gearbox.GearBoxManual;
import garage.vehicle.part.option.Option;
import garage.vehicle.part.pedal.Pedal;
import garage.vehicle.part.pedal.PedalAccelerator;
import garage.vehicle.part.pedal.PedalBrake;
import garage.vehicle.part.pedal.PedalClutch;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Vehicle {
private Double price;
private String name;
private VehicleBrand brand;
// Parts
private Frame frame;
private List<Pedal> pedals;
private GearBox gearBox;
private List<Brake> brakes;
private Engine engine;
private List<Option> options;
// Physic
private Double velocity;
public Vehicle(VehicleModel model){
brand = model.getBrand();
name = model.getName();
// CREATING FRAME
frame = new Frame(model.getFrameType());
// CREATING ENGINE
engine = new Engine("", 0d); // TODO Reprendre constructeur engine
// CREATING gearBox
if("MANUAL".equals(model.getEngineModel().getGearBoxType())){
gearBox = new GearBoxManual(model.getEngineModel().getMaxGear());
}else {
gearBox = new GearBoxAutomatic(model.getEngineModel().getMaxGear());
}
// CREATING BRAKES
brakes = new ArrayList<>();
for(Integer i = 0; i < model.getNbOfWheels(); i++ ){
brakes.add(new Brake(model.getBrakesType()));
}
// CREATING PEDALS
pedals = new ArrayList<>();
pedals.add(new PedalAccelerator(engine));
pedals.add(new PedalBrake(brakes));
if("MANUAL".equals(model.getEngineModel().getGearBoxType())) {
pedals.add(new PedalClutch((GearBoxManual) gearBox));
}
// CREATING OPTIONS
options = new ArrayList<>();
}
// METHODS /////////////////////////////////////////////////////////////////////////////////////////////////////////
public void update(){
for(Pedal p : pedals) p.update();
}
// Print the name of the car with the options and the price.
// public String toString()
// return "\t" + this.brandName + "is a " + name + " with that options : + " ?? " + ". Price : " + price.
// Add an option
// public void addOption(opt : vehicle.part.option)
// GETTER //////////////////////////////////////////////////////////////////////////////////////////////////////////
public Double getHeight(){
return frame.getHeight();
}
public Double getWidth(){
return frame.getWidth();
}
public Double getLength(){
return frame.getLength();
}
public Double getweight(){
return frame.getWeight();
}
public Frame getFrame() {
return frame;
}
public List<Pedal> getPedals() {
return pedals;
}
public GearBox getGearBox() {
return gearBox;
}
public List<Brake> getBrakes() {
return brakes;
}
public Engine getEngine() {
return engine;
}
public List<Option> getOptions() {
return options;
}
public VehicleBrand getBrand(){
return this.brand;
}
public double getPrice(List<Option> options){
int index = 0;
double price = 0;
if (options.size() == 0) {
return price;
}
else{
price = options.stream().collect(Collectors.summingDouble(Option::getPrice));
}
return price;
}
// DISPLAY /////////////////////////////////////////////////////////////////////////////////////////////////////////
public void displayVehicleInformation(){
// Clear screen
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("|------------------------------------------------------------------|");
System.out.println("| VEHICULE INFORMATION |");
System.out.println("|------------------------------------------------------------------|");
System.out.println(" BRAND: " + brand + " MODEL: " + name +" PRICE: " + price);
System.out.println("|-----------------------------------------------------------------|");
}
}
\ No newline at end of file
package garage.vehicle;
public enum VehicleBrand {
RENAULT("Renault"),
PEUGEOT("Peugeot"),
CITROEN("Citroen"),
FIAT("Fiat");
private final String value;
VehicleBrand(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public static boolean contains(String value) {
for (VehicleBrand brand : VehicleBrand.values()) {
if (brand.getValue().equals(value)) {
return true;
}
}
return false;
}
}
package garage.vehicle;
import garage.vehicle.part.brake.BrakeType;
import garage.vehicle.part.engine.EngineModel;
import garage.vehicle.part.frame.FrameType;
public enum VehicleModel {
FIAT_500(VehicleBrand.FIAT, "500", EngineModel.TDI_M_D, FrameType.MICRO, BrakeType.PAD_FX200, 4),
PEUGEOT_5008(VehicleBrand.PEUGEOT, "5008", EngineModel.TDI_A_P, FrameType.SUV, BrakeType.DISC_FX200, 4);
private final VehicleBrand brand;
private final String name;
private final EngineModel engineModel;
private final FrameType frameType;
private final BrakeType brakesType;
private final Integer nbOfWheels;
VehicleModel(VehicleBrand brand, String name, EngineModel engineModel, FrameType frameType, BrakeType brakesType, Integer nbOfWheels){
this.brand = brand;
this.name = name;
this.engineModel = engineModel;
this.frameType = frameType;
this.brakesType = brakesType;
this.nbOfWheels = nbOfWheels;
}
public VehicleBrand getBrand() {
return brand;
}
public String getName() {
return name;
}
public EngineModel getEngineModel() {
return engineModel;
}
public FrameType getFrameType() {
return frameType;
}
public BrakeType getBrakesType() {
return brakesType;
}
public Integer getNbOfWheels() {
return nbOfWheels;
}
}
package garage.vehicle.part.brake;
public class Brake{
private BrakeType type;
private Integer strength;
private Integer resistance;
private Double wear;
private Double state;
public Brake(BrakeType type) {
this.type = type;
this.strength = type.getStrength();
this.resistance = type.getResistance();
this.wear = 0d;
this.state = 0d;
}
public void update() {
}
public void setState(Double state){
this.state = state;
}
}
package garage.vehicle.part.brake;
public enum BrakeType {
DISC_FX100(15, 1),
DISC_FX200(25,1),
DISC_FX300(30,2),
DISC_FX400(35,2),
DISC_FX500(50,3),
PAD_FX100(10, 1),
PAD_FX200(15,1),
PAD_FX300(25,2),
PAD_FX400(30,3),
PAD_FX500(45,4);
private final Integer strength;
private final Integer resistance;
BrakeType(Integer strength, Integer resistance) {
this.resistance = resistance;
this.strength = strength;
}
public Integer getStrength() {
return strength;
}
public Integer getResistance() {
return resistance;
}
}
package garage.vehicle.part.door;
public class Door {
private Window window;
}
package garage.vehicle.part.door;
public class Window {
}
package garage.vehicle.part.engine;
public class DieselEngine extends Engine{
public DieselEngine(String _cylindre, Double _price) {
super(_cylindre, _price);
this.type = EngineType.DIESEL;
}
public String toString() {
return "Diesel vehicle.parts.Engine";
}
}
package garage.vehicle.part.engine;
public class ElectricEngine extends Engine {
public ElectricEngine(String _cylindre, Double _price) {
super(_cylindre, _price);
this.type = EngineType.ELECTRIC;
}
public String toString() {
return "Electric Engine";
}
}
package garage.vehicle.part.engine;
public class Engine {
protected EngineType type;
protected String cylindre;
protected Double price;
public Engine (String _cylindre, Double _price) {
this.cylindre = _cylindre;
this.price = getPrice();
}
public String toString() {
return "This vehicle has a " + cylindre + "engine, it's a " + type + " engine and it costs " + price + " €.";
}
public String getCylindre() {
return this.cylindre;
}
public Double getPrice() {
if (this.getClass() == DieselEngine.class) {
return 2000d;
} else if (this.getClass() == ElectricEngine.class) {
return 1800d;
} else if (this.getClass() == HybridEngine.class) {
return 3000d;
} else if (this.getClass() == PetrolEngine.class) {
return 1400d;
} else {
return 0d;
}
}
}
package garage.vehicle.part.engine;
public class EngineBrokenException extends Exception {
}
package garage.vehicle.part.engine;
import garage.vehicle.part.gearbox.Gear;
public enum EngineModel {
TDI_M_D(EngineType.DIESEL, "MANUAL", Gear.FIFTH, 200d, 2000d, 300d),
TDI_A_D(EngineType.DIESEL, "AUTOMATIC", Gear.FIFTH, 200d, 2500d, 300d),
TDI_A_P(EngineType.PETROL, "AUTOMATIC", Gear.FIFTH, 200d, 2300d, 300d);
private final EngineType engineType;
private final String gearBoxType;
private final Gear maxGear;
private final Double cylindre;
private final Double price;
private final Double weight;
EngineModel(EngineType engineType, String gearBoxType, Gear maxGear, Double cylindre, Double price, Double weight){
this.engineType = engineType;
this.gearBoxType = gearBoxType;
this.maxGear = maxGear;
this.cylindre = cylindre;
this.price = price;
this.weight = weight;
}
public EngineType getEngineType() {
return engineType;
}
public String getGearBoxType() {
return gearBoxType;
}
public Gear getMaxGear() {
return maxGear;
}
public Double getCylindre() {
return cylindre;
}
public Double getPrice() {
return price;
}
public Double getWeight() {
return weight;
}
}
package garage.vehicle.part.engine;
public enum EngineType {
DIESEL("Diesel"),
PETROL("Petrol"),
HYBRID("Hybrid"),
ELECTRIC("Electric");
private final String value;
EngineType(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public static EngineType getEnum(String value) {
for (EngineType type : EngineType.values()) {
if (type.getValue().equals(value)) {
return type;
}
}
return null;
}
public static boolean contains(String value) {
for (EngineType type : EngineType.values()) {
if (type.getValue().equals(value)) {
return true;
}
}
return false;
}
}
package garage.vehicle.part.engine;
public class HybridEngine extends Engine {
public HybridEngine(String _cylindre, Double _price) {
super(_cylindre, _price);
this.type = EngineType.HYBRID;
}
public String toString() {
return "Hybrid Engine";
}
}
package garage.vehicle.part.engine;
public class PetrolEngine extends Engine {
public PetrolEngine(String _cylindre, Double _price) {
super(_cylindre, _price);
this.type = EngineType.PETROL;
}
public String toString() {
return "Petrol Engine";
}
}
package garage.vehicle.part.frame;
import garage.vehicle.part.door.Door;
import java.util.ArrayList;
import java.util.List;
public class Frame {
private Double weight;
private Double length;
private Double width;
private Double height;
private List<Door> doors;
public Frame(FrameType type) {
weight = type.getWeigth();
length = type.getLength();
width = type.getWidth();
height = type.getHeight();
doors = new ArrayList<>();
for(Integer i = 0; i < type.getNbOfDoors(); i++) doors.add(new Door());
}
public Double getWeight() {
return weight;
}
public Double getLength() {
return length;
}
public Double getWidth() {
return width;
}
public Double getHeight() {
return height;
}
public List<Door> getDoors() {
return doors;
}
}
package garage.vehicle.part.frame;
public enum FrameType {
MICRO(2.5, 2.0, 1.7, 800d, 2),
SEDAN(2.5, 2.0, 1.7, 800d, 2),
SUV(2.5, 2.0, 1.7, 800d, 2),
ROADSTER(2.5, 2.0, 1.7, 800d, 2),
PICKUP(2.5, 2.0, 1.7, 800d, 2),
COUPE(2.5, 2.0, 1.7, 800d, 2),
SUPERCAR(2.5, 2.0, 1.7, 800d, 2),
CABRIOLET(2.5, 2.0, 1.7, 800d, 2),
MINIVAN(2.5, 2.0, 1.7, 800d, 2),
TRUCK(2.5, 2.0, 1.7, 800d, 2),
MINITRUCK(2.5, 2.0, 1.7, 800d, 2);
private final Double length;
private final Double width;
private final Double height;
private final Double weigth;
private final Integer nbOfDoors;
FrameType(Double length, Double width, Double height, Double weigth, Integer nbOfDoors){
this.length = length;
this.width = width;
this.height = height;
this.weigth = weigth;
this.nbOfDoors = nbOfDoors;
}
public Double getLength() {
return length;
}
public Double getWidth() {
return width;
}
public Double getHeight() {
return height;
}
public Double getWeigth() {
return weigth;
}
public Integer getNbOfDoors() {
return nbOfDoors;
}
}
package garage.vehicle.part.gearbox;
public enum Gear {
REVERSE(-1),
NEUTRAL(0),
FIRST(1),
SECOND(2),
THRID(3),
FORTH(4),
FIFTH(5),
SIXTH(6),
AUTO(10);
private final Integer value;
Gear(Integer value) {
this.value = value;
}
public Integer getValue() {
return value;
}
public static Gear getEnum(Integer value) {
for (Gear type : Gear.values()) {
if (type.getValue().equals(value)) {
return type;
}
}
return null;
}
public static boolean contains(Integer value) {
for (Gear type : Gear.values()) {
if (type.getValue().equals(value)) {
return true;
}
}
return false;
}
}
package garage.vehicle.part.gearbox;
import garage.vehicle.part.engine.EngineBrokenException;
public interface GearBox {
void gearUp() throws StallException;
void gearDown() throws StallException;
void neutral() throws StallException;
void reverse() throws StallException, EngineBrokenException;
Gear getCurrentGear();
}
\ No newline at end of file
package garage.vehicle.part.gearbox;
import java.util.Arrays;
public class GearBoxAutomatic implements GearBox {
private Gear currentGear;
private Gear maxGear;
public GearBoxAutomatic(Gear maxGear) {
this.maxGear = maxGear;
this.currentGear = Gear.NEUTRAL;
}
@Override
public void gearUp() {
if (!currentGear.equals(maxGear)) {
currentGear = Gear.getEnum(currentGear.getValue() + 1);
}
}
@Override
public void gearDown() {
if (!currentGear.equals(Gear.REVERSE)) {
currentGear = Gear.getEnum(currentGear.getValue() - 1);
}
}
public void automatic(){
}
@Override
public void neutral() {
if(Arrays.asList(Gear.REVERSE, Gear.AUTO).contains(currentGear)){
currentGear = Gear.NEUTRAL;
}
}
@Override
public void reverse() {
if(Arrays.asList(Gear.NEUTRAL, Gear.AUTO).contains(currentGear)){
currentGear = Gear.REVERSE;
}
}
@Override
public Gear getCurrentGear() {
return currentGear;
}
}
package garage.vehicle.part.gearbox;
import garage.vehicle.part.engine.EngineBrokenException;
public class GearBoxManual implements GearBox{
private Gear currentGear;
private Gear maxGear;
private Boolean engaged;
/**
* Constructor
* @param maxGear
*/
public GearBoxManual(Gear maxGear) {
this.maxGear = maxGear;
this.currentGear = Gear.NEUTRAL;
this.engaged = false;
}
/**
* Method to gear up
* @throws StallException
*/
@Override
public void gearUp() throws StallException{
if(engaged) {
if (!currentGear.equals(maxGear)) {
currentGear = Gear.getEnum(currentGear.getValue() + 1);
}
}
else
throw new StallException();
}
/**
* Method to gear down
* @throws StallException
*/
@Override
public void gearDown() throws StallException{
if(engaged) {
if (!currentGear.equals(Gear.REVERSE)) {
currentGear = Gear.getEnum(currentGear.getValue() - 1);
}
}
else
throw new StallException();
}
/**
* Go to neutral gear
* @throws StallException
*/
@Override
public void neutral() throws StallException{
if(engaged) {
currentGear = Gear.NEUTRAL;
}
else
throw new StallException();
}
/**
* Go to reverse gear
* @throws StallException
* @throws EngineBrokenException
*/
@Override
public void reverse() throws StallException, EngineBrokenException {
if(engaged) {
// TODO Récupérer la vitesse de la voiture pour savoir si le moteur casse
if(true) {
currentGear = Gear.REVERSE;
}else{
throw new EngineBrokenException();
}
}
else
throw new StallException();
}
/**
* Engage
*/
public void engage(){
engaged = true;
}
/**
* Disengage
*/
public void disengage(){
engaged = false;
}
@Override
public Gear getCurrentGear() {
return currentGear;
}
}
package garage.vehicle.part.gearbox;
public class StallException extends Exception {
public StallException() {
super("Le moteur a calé");
}
}
package garage.vehicle.part.option;
public class Aircon implements Option {
public double getPrice() {
return 300d;
}
public String toString() {
return "Aircon (" + this.getPrice() + " €)";
}
}
package garage.vehicle.part.option;
public class CareerBars implements Option{
public double getPrice() {
return 100;
}
public String toString() {
return "Career bars (" + this.getPrice() + " €)";
}
}
package garage.vehicle.part.option;
public class ElectricGlass implements Option{
public double getPrice() {
return 200d;
}
public String toString() {
return "Electric glass (" + this.getPrice() + " €)";
}
}
package garage.vehicle.part.option;
public class GPS implements Option {
public double getPrice() {
return 50d;
}
public String toString() {
return "GPS (" + this.getPrice() + " €)";
}
}
package garage.vehicle.part.option;
public class HotSeat implements Option {
public double getPrice() {
return 150d;
}
public String toString() {
return "Hot seat (" + this.getPrice() + " €)";
}
}
package garage.vehicle.part.option;
public interface Option {
double getPrice();
String toString();
}
package garage.vehicle.part.pedal;
public interface Pedal {
void release();
void update();
void setState(Double pressure);
Double getState();
}
package garage.vehicle.part.pedal;
import garage.vehicle.part.engine.Engine;
public class PedalAccelerator implements Pedal{
private Engine engine;
private Double state;
public PedalAccelerator(Engine engine) {
this.engine = engine;
this.state = 0d;
}
@Override
public void setState(Double pressure) {
this.state = pressure;
}
@Override
public Double getState() {
return state;
}
public Engine getEngine() {
return engine;
}
@Override
public void release() {
state = 0d;
}
@Override
public void update() {
}
}
package garage.vehicle.part.pedal;
import garage.vehicle.part.brake.Brake;
import java.util.List;
public class PedalBrake implements Pedal{
private List<Brake> brakes;
private Double state;
public PedalBrake(List<Brake> brakes) {
this.brakes = brakes;
}
@Override
public void setState(Double pressure) {
state = pressure;
}
@Override
public Double getState() {
return state;
}
@Override
public void release() {
state = 0d;
}
@Override
public void update() {
for(Brake brake : brakes){
brake.setState(state);
}
}
}
package garage.vehicle.part.pedal;
import garage.vehicle.part.gearbox.GearBoxManual;
public class PedalClutch implements Pedal{
private GearBoxManual gearBox;
private Double state;
public PedalClutch(GearBoxManual gearBox) {
this.gearBox = gearBox;
this.state = 0d;
}
@Override
public void setState(Double pressure) {
state = pressure;
}
@Override
public Double getState() {
return state;
}
@Override
public void release() {
state = 0d;
}
@Override
public void update() {
if(state > 40d)
gearBox.engage();
else
gearBox.disengage();
}
}
package garage.vehicle.part.pedal;
public enum PedalPosition {
ACCELERATOR(0),
BRAKE(1),
CLUTCH(2);
private final Integer value;
PedalPosition(Integer value) {
this.value = value;
}
public Integer getValue() {
return value;
}
public static PedalPosition getEnum(Integer value) {
for (PedalPosition position : PedalPosition.values()) {
if (position.getValue().equals(value)) {
return position;
}
}
return null;
}
}
package geometry;
public class Block {
// VARIABLES
private Double x;
private Double y;
private Double r;
// CONSTRUCTOR
public Block(Double x, Double y, Double r) {
this.x = x;
this.y = y;
this.r = r;
}
// GETTERS / SETTERS
public Double getX() {
return x;
}
public void setX(Double x) {
this.x = x;
}
public Double getY() {
return y;
}
public void setY(Double y) {
this.y = y;
}
public Double getR() {
return r;
}
public void setR(Double r) {
this.r = r;
}
}
package geometry;
public class Edge extends Point {
// VARIABLES
private Boolean begin;
private Segment segment;
private Double angle;
private Boolean visualize;
// CONSTRUCOR
public Edge(Double _x, Double _y){
super(_x, _y);
}
// GETTERS / SETTERS
public Boolean getBegin() {
return begin;
}
public void setBegin(Boolean begin) {
this.begin = begin;
}
public Segment getSegment() {
return segment;
}
public void setSegment(Segment segment) {
this.segment = segment;
}
public Double getAngle() {
return angle;
}
public void setAngle(Double angle) {
this.angle = angle;
}
public Boolean getVisualize() {
return visualize;
}
public void setVisualize(Boolean visualize) {
this.visualize = visualize;
}
}
\ No newline at end of file
package geometry;
public class Main {
public static void main(String[] args) {
System.out.println("STARTING GEOMETRY PROJECT");
}
}
package geometry;
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point() {
this.x = 0.0;
this.y = 0.0;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public String toString(){
return "(" + x + "," + y + ")";
}
}
\ No newline at end of file
package geometry;
public class Segment {
// VARIABLES
private Edge p1;
private Edge p2;
private Double d;
//CONSTRUCTOR
public Segment(Edge _p1, Edge _p2){
this.p1 = _p1;
this.p2 = _p2;
p1.setSegment(this);
p2.setSegment(this);
}
// GETTERS / SETTERS
public Edge getP1() {
return p1;
}
public void setP1(Edge p1) {
this.p1 = p1;
}
public Edge getP2() {
return p2;
}
public void setP2(Edge p2) {
this.p2 = p2;
}
public Double getD() {
return d;
}
public void setD(Double d) {
this.d = d;
}
}
package geometry;
import java.util.ArrayList;
import java.util.List;
public class View {
// VARIABLES
private List<Segment> segments;
private List<Edge> edges;
private Point viewer;
// CONSTRUCTOR
public View() {
this.segments = new ArrayList<>();
this.edges = new ArrayList<>();
this.viewer = new Point(0d, 0d);
}
// METHODES
/**
* Creation of the map borders
* @param size
*/
public void loadEdgeOfMap(Double size){
addSegment(0d, 0d, 0d, size);
addSegment(0d,0d, size,0d);
addSegment(size,0d, size, size);
addSegment(0d, size, size, size);
}
/**
* Add a segment to the view
* @param x1
* @param y1
* @param x2
* @param y2
*/
public void addSegment(Double x1, Double y1, Double x2, Double y2){
Edge p1 = new Edge(x1, y1);
Edge p2 = new Edge(x2, y2);
Segment segment = new Segment(p1, p2);
segments.add(segment);
edges.add(p1);
edges.add(p2);
}
/**
* Loading elements composing the map
* @param size
* @param blocks
* @param walls
*/
public void loadMap(Double size, List<Block> blocks, List<Segment> walls){
segments.clear();
edges.clear();
// Loading the edge of the map
loadEdgeOfMap(size);
// Streaming the list of blocks to create their segments
blocks.stream().forEach(b -> {
// Recovering properties of the current block
Double x = b.getX();
Double y = b.getY();
Double r = b.getR();
// Building the segments composing the block
addSegment(x - r, y - r, x - r, y + r);
addSegment(x - r, y + r, x + r, y + r);
addSegment(x + r, y + r, x + r, y - r);
addSegment(x + r, y - r, x - r, y - r);
});
// Streaming the list of walls to create corresponding segments
walls.stream().forEach(w -> {
addSegment(w.getP1().getX(), w.getP1().getY(), w.getP2().getX(), w.getP2().getY());
});
}
/**
* Set the position of the viewer
* @param x
* @param y
*/
public void setViewerLocation(Double x, Double y){
viewer.setX(x);
viewer.setY(y);
segments.stream().forEach(seg -> {
Double dx = 0.5 * (seg.getP1().getX() + seg.getP2().getX()) - x;
Double dy = 0.5 * (seg.getP1().getY() + seg.getP2().getY()) - y;
seg.setD(dx * dx + dy * dy);
seg.getP1().setAngle(Math.atan2(seg.getP1().getY() - y, seg.getP1().getX() - x));
seg.getP2().setAngle(Math.atan2(seg.getP2().getY() - y, seg.getP2().getX() - x));
Double dAngle = seg.getP2().getAngle() - seg.getP1().getAngle();
if(dAngle <= -Math.PI) dAngle += 2 * Math.PI;
if(dAngle > Math.PI) dAngle -= 2 * Math.PI;
seg.getP1().setBegin(dAngle > 0d);
seg.getP2().setBegin(!seg.getP1().getBegin());
});
}
/**
* Indicate if a segment is in front of another relative to a point
* @param a
* @param b
* @param point
* @return
*/
protected Boolean segmentAInFrontOfB(Segment a, Segment b, Point point){
Boolean A1 = leftOf(a, interpolate(b.getP1(), b.getP2(), 0.01));
Boolean A2 = leftOf(a, interpolate(b.getP1(), b.getP2(), 0.01));
Boolean A3 = leftOf(a, point);
Boolean B1 = leftOf(b, interpolate(a.getP1(), a.getP2(), 0.01));
Boolean B2 = leftOf(b, interpolate(a.getP1(), a.getP2(), 0.01));
Boolean B3 = leftOf(b, point);
if( B1.equals(B2) && !B2.equals(B3)) return true;
if( A1.equals(A2) && A2.equals(A3)) return true;
if( A1.equals(A2) && !A2.equals(A3)) return false;
if( B1.equals(B2) && B2.equals(B3)) return false;
return false;
}
/**
* Method to scan for visible edges
* @param maxAngle
*/
public void scan(Double maxAngle){
}
/**
*
* @param a
* @param b
* @return
*/
protected Integer compareEdge(Edge a, Edge b){
if(a.getAngle() > b.getAngle()) return 1;
if(a.getAngle() < b.getAngle()) return -1;
if(!a.getBegin() && b.getBegin()) return 1;
if(a.getBegin() && !b.getBegin()) return -1;
return 0;
}
/**
*
* @param s
* @param p
* @return
*/
protected Boolean leftOf(Segment s, Point p){
Double cross = (s.getP2().getX() - s.getP1().getX()) * (p.getY() - s.getP1().getY()) - (s.getP2().getY() - s.getP1().getY()) * (p.getX() - s.getP1().getX());
return cross < 0;
}
/**
*
* @param p
* @param q
* @param f
* @return
*/
protected Point interpolate(Point p, Point q, Double f){
return new Point(p.getX() * (1 - f) + q.getX() * f, p.getY() * (1 - f) + q.getY() * f);
}
// GETTERS / SETTERS
public List<Segment> getSegments() {
return segments;
}
public void setSegments(List<Segment> segments) {
this.segments = segments;
}
public List<Edge> getEdges() {
return edges;
}
public void setEdges(List<Edge> edges) {
this.edges = edges;
}
public Point getViewer() {
return viewer;
}
public void setViewer(Point viewer) {
this.viewer = viewer;
}
}
package openclassroom;
public enum Couleur {
MARRON("marron"),
BLEU("bleu"),
VERT("vert"),
VERRON("verron"),
INCONNU("non déterminé"),
ROUGE("rouge mais j'avais piscine...");
private String name = "";
Couleur (String n) {name = n;}
public String toString() {return name;}
}
package openclassroom;
public class Main {
public static void main(String[] args) {
}
}
package openclassroom;
import java.util.Scanner;
public class Personne {
public Double taille = 0.0d, poids = 0.0d;
public String nom = "", prenom = "";
public Couleur yeux = Couleur.INCONNU;
public Personne() { }
public Personne(double taille, double poids, String nom, String prenom, Couleur yeux) {
super();
this.taille = taille;
this.poids = poids;
this.nom = nom;
this.prenom = prenom;
this.yeux = yeux;
}
public String toString() {
String s = "Je m'appelle " + nom + " " + prenom;
s += ", je pèse " + poids + " Kg";
s += ", et je mesure " + taille + " cm.";
return s;
}
}
package garage;
import garage.vehicle.part.engine.DieselEngine;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class DieselEngineTest {
private DieselEngine dieselEngine;
@Before
public void setUp(){
dieselEngine = new DieselEngine("Un cylindre", 10.00);
}
@Test
public String toString() {
String expected = "Diesel vehicle.parts.Engine";
assertEquals(expected, dieselEngine.toString());
return null;
}
}
\ No newline at end of file
package garage;
import org.junit.Test;
public class EngineTest {
@Test
public void toString1() {
}
@Test
public void getCylindre() {
}
@Test
public void getPrice() {
}
}
\ No newline at end of file
package garage;
import garage.garage.Task;
import garage.garage.TestBench;
import garage.vehicle.Vehicle;
import garage.vehicle.VehicleModel;
import garage.vehicle.part.gearbox.Gear;
import garage.vehicle.part.pedal.PedalPosition;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import static org.junit.Assert.*;
public class TestBenchTest {
private TestBench testBench;
private Vehicle fiat500;
private Vehicle peugeot5008;
private Task task;
@Before
public void setUp(){
testBench = new TestBench();
fiat500 = new Vehicle(VehicleModel.FIAT_500);
peugeot5008 = new Vehicle(VehicleModel.PEUGEOT_5008);
task = new Task(fiat500.getPedals().get(0), "setState", Arrays.asList(50d), 5);
}
@Test
public void addVehicleTest(){
assertNull(testBench.getVehicle());
testBench.addVehicle(fiat500);
assertEquals(fiat500, testBench.getVehicle());
}
@Test
public void addVehicleImpossibleTest(){
assertNull(testBench.getVehicle());
testBench.addVehicle(fiat500);
testBench.addVehicle(peugeot5008);
assertEquals(fiat500, testBench.getVehicle());
}
@Test
public void removeVehicleTest(){
testBench.addVehicle(fiat500);
assertEquals(fiat500, testBench.getVehicle());
testBench.removeVehicle();
assertNull(testBench.getVehicle());
}
@Test
public void removeVehicleImpossibleTest(){
testBench.removeVehicle();
assertNull(testBench.getVehicle());
}
@Test
public void swapVehicleTest(){
testBench.addVehicle(fiat500);
assertEquals(fiat500, testBench.getVehicle());
Vehicle swapped = testBench.swapVehicle(peugeot5008);
assertEquals(fiat500, swapped);
assertEquals(peugeot5008, testBench.getVehicle());
}
@Test
public void swapVehicleImpossibleTest(){
Vehicle swapped = testBench.swapVehicle(peugeot5008);
assertNull(swapped);
assertNull(testBench.getVehicle());
}
@Test
public void addTaskTest(){
assertEquals(0, testBench.getTasks().size());
testBench.addTask(task);
assertEquals(1, testBench.getTasks().size());
assertTrue(testBench.getTasks().contains(task));
}
@Test
public void removeTaskTest(){
testBench.addTask(task);
assertEquals(1, testBench.getTasks().size());
testBench.removeTask(task);
assertEquals(0, testBench.getTasks().size());
}
@Test
public void runTest(){
testBench.addVehicle(fiat500);
testBench.addTask(task);
assertEquals(0d, fiat500.getPedals().get(0).getState(), 0d);
testBench.run(10);
assertEquals(50d, fiat500.getPedals().get(0).getState(), 0d);
}
@Test
public void runWrongTaskTest(){
testBench.addVehicle(fiat500);
task = new Task(fiat500.getPedals().get(0), "setStat", Arrays.asList(50d), 5);
testBench.addTask(task);
testBench.run(10);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Test
public void scenario1Test(){
testBench.addVehicle(fiat500);
testBench.addTask(new Task(fiat500.getPedals().get(PedalPosition.CLUTCH.getValue()), "setState", Arrays.asList(70d), 2));
testBench.addTask(new Task(fiat500.getGearBox(), "gearUp", null, 3));
testBench.addTask(new Task(fiat500.getPedals().get(PedalPosition.CLUTCH.getValue()), "setState", Arrays.asList(0d), 4));
assertEquals(Gear.NEUTRAL, fiat500.getGearBox().getCurrentGear());
testBench.run(10);
assertEquals(Gear.FIRST, fiat500.getGearBox().getCurrentGear());
}
}
package geometry;
import org.junit.Test;
public class TestEdge {
@Test
public void testCreatingEdge() {
// test
}
}
package geometry;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestPoint {
@Test
public void testCreatingOriginPoint() {
Point p = new Point();
assertEquals(0, p.getX(), 1e-9);
assertEquals(0, p.getY(), 1e-9);
}
@Test
public void testCreatingPointFromCoordinates() {
Point p = new Point(4.2, 5.1);
assertEquals(4.2, p.getX(), 1e-9);
assertEquals(5.1, p.getY(), 1e-9);
}
}
package geometry;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestView {
private View view;
@Before
public void setUp(){
view = new View();
}
@Test
public void loadEdgeOfMapTest(){
view.loadEdgeOfMap(20d);
assertEquals(4, view.getSegments().size());
}
}
package openclassroom;
import openclassroom.Personne;
import java.util.Arrays;
import java.util.List;
public class TestStream {
List<Personne> listP = Arrays.asList(
new Personne(1.80, 70, "A", "Nicolas", Couleur.BLEU),
new Personne(1.56, 50, "B", "Nicole", Couleur.VERRON),
new Personne(1.75, 65, "C", "Germain", Couleur.VERT),
new Personne(1.68, 50, "D", "Michel", Couleur.ROUGE),
new Personne(1.96, 65, "E", "Cyrille", Couleur.BLEU),
new Personne(2.10, 120, "F", "Denis", Couleur.ROUGE),
new Personne(1.90, 90, "G", "Olivier", Couleur.VERRON)
);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment