Commit 51118632 authored by Gaetan's avatar Gaetan
parents 7478c6ee c27312fe
package garage;
public class ExecutionFailedException extends Exception{
public ExecutionFailedException(String message) {
super(message);
}
}
...@@ -16,4 +16,41 @@ public class Task { ...@@ -16,4 +16,41 @@ public class Task {
this.args = args; this.args = args;
this.t = t; 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;
}
} }
...@@ -17,47 +17,64 @@ public class TestBench { ...@@ -17,47 +17,64 @@ public class TestBench {
vehicle = null; vehicle = null;
} }
public void addVehicle(Vehicle vehicle){ public void addVehicle(Vehicle vehicle) {
if(this.vehicle == null){ if (this.vehicle == null) {
this.vehicle = vehicle; this.vehicle = vehicle;
}else{ } else {
System.out.println("A car is already on the testbench"); System.out.println("A car is already on the testbench");
} }
} }
public void removeVehicle(){ public void removeVehicle() {
if(this.vehicle != null){ if (this.vehicle != null) {
this.vehicle = null; this.vehicle = null;
}else{ } else {
System.out.println("No vehicle in the test bench."); System.out.println("No vehicle in the test bench.");
} }
} }
public Vehicle swapVehicle(Vehicle vehicule){ public Vehicle swapVehicle(Vehicle vehicule) {
if(this.vehicle != null){ if (this.vehicle != null) {
Vehicle temp = this.vehicle; Vehicle temp = this.vehicle;
this.vehicle = vehicule; this.vehicle = vehicule;
return temp; return temp;
}else{ } else {
System.out.println("No vehicle in the test bench."); System.out.println("No vehicle in the test bench.");
return null; return null;
} }
} }
public void addTask(Task task){ public void addTask(Task task) {
tasks.add(task); tasks.add(task);
} }
public void removeTask(Task task){ public void removeTask(Task task) {
tasks.remove(task); tasks.remove(task);
} }
public Vehicle getVehicle(){ public Vehicle getVehicle() {
return this.vehicle; return this.vehicle;
} }
public List<Task> getTasks(){ public List<Task> getTasks() {
return tasks; 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");
}
}
} }
...@@ -74,6 +74,7 @@ public class Vehicle { ...@@ -74,6 +74,7 @@ public class Vehicle {
// METHODS ///////////////////////////////////////////////////////////////////////////////////////////////////////// // METHODS /////////////////////////////////////////////////////////////////////////////////////////////////////////
public void update(){ public void update(){
for(Pedal p : pedals) p.update();
} }
...@@ -157,9 +158,7 @@ public class Vehicle { ...@@ -157,9 +158,7 @@ public class Vehicle {
return price; return price;
} }
else{ else{
/*while (index < options.size()){ // price = options.Stream.forEach(o -> o.getPrice);
price = options.Stream.forEach(o -> o.getPrice);
}*/
price = options.stream().collect(Collectors.summingDouble(Option::getPrice)); price = options.stream().collect(Collectors.summingDouble(Option::getPrice));
} }
......
...@@ -6,7 +6,7 @@ import vehicle.part.frame.FrameType; ...@@ -6,7 +6,7 @@ import vehicle.part.frame.FrameType;
public enum VehicleModel { public enum VehicleModel {
FIAT_500(VehicleBrand.FIAT, "500", EngineModel.TDI_A_D, FrameType.MICRO, BrakeType.PAD_FX200, 4), 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); PEUGEOT_5008(VehicleBrand.PEUGEOT, "5008", EngineModel.TDI_A_P, FrameType.SUV, BrakeType.DISC_FX200, 4);
private final VehicleBrand brand; private final VehicleBrand brand;
......
...@@ -12,4 +12,6 @@ public interface GearBox { ...@@ -12,4 +12,6 @@ public interface GearBox {
void reverse() throws StallException, EngineBrokenException; void reverse() throws StallException, EngineBrokenException;
public Gear getCurrentGear();
} }
\ No newline at end of file
...@@ -43,4 +43,9 @@ public class GearBoxAutomatic implements GearBox { ...@@ -43,4 +43,9 @@ public class GearBoxAutomatic implements GearBox {
currentGear = Gear.REVERSE; currentGear = Gear.REVERSE;
} }
} }
@Override
public Gear getCurrentGear() {
return currentGear;
}
} }
...@@ -95,4 +95,9 @@ public class GearBoxManual implements GearBox{ ...@@ -95,4 +95,9 @@ public class GearBoxManual implements GearBox{
public void disengage(){ public void disengage(){
engaged = false; engaged = false;
} }
@Override
public Gear getCurrentGear() {
return currentGear;
}
} }
package vehicle.part.pedal; package vehicle.part.pedal;
public interface Pedal { import vehicle.part.engine.Engine;
void setState(Double pressure); public interface Pedal {
void release(); void release();
void update(); void update();
void setState(Double pressure);
Double getState();
} }
...@@ -9,7 +9,7 @@ public class PedalAccelerator implements Pedal{ ...@@ -9,7 +9,7 @@ public class PedalAccelerator implements Pedal{
public PedalAccelerator(Engine engine) { public PedalAccelerator(Engine engine) {
this.engine = engine; this.engine = engine;
this.state = state; this.state = 0d;
} }
@Override @Override
...@@ -17,6 +17,15 @@ public class PedalAccelerator implements Pedal{ ...@@ -17,6 +17,15 @@ public class PedalAccelerator implements Pedal{
this.state = pressure; this.state = pressure;
} }
@Override
public Double getState() {
return state;
}
public Engine getEngine() {
return engine;
}
@Override @Override
public void release() { public void release() {
state = 0d; state = 0d;
...@@ -26,4 +35,5 @@ public class PedalAccelerator implements Pedal{ ...@@ -26,4 +35,5 @@ public class PedalAccelerator implements Pedal{
public void update() { public void update() {
} }
} }
package vehicle.part.pedal; package vehicle.part.pedal;
import vehicle.part.brake.Brake; import vehicle.part.brake.Brake;
import vehicle.part.engine.Engine;
import java.util.List; import java.util.List;
...@@ -18,6 +19,11 @@ public class PedalBrake implements Pedal{ ...@@ -18,6 +19,11 @@ public class PedalBrake implements Pedal{
state = pressure; state = pressure;
} }
@Override
public Double getState() {
return state;
}
@Override @Override
public void release() { public void release() {
state = 0d; state = 0d;
......
package vehicle.part.pedal; package vehicle.part.pedal;
import vehicle.part.engine.Engine;
import vehicle.part.gearbox.GearBox; import vehicle.part.gearbox.GearBox;
import vehicle.part.gearbox.GearBoxManual; import vehicle.part.gearbox.GearBoxManual;
...@@ -18,6 +19,11 @@ public class PedalClutch implements Pedal{ ...@@ -18,6 +19,11 @@ public class PedalClutch implements Pedal{
state = pressure; state = pressure;
} }
@Override
public Double getState() {
return state;
}
@Override @Override
public void release() { public void release() {
state = 0d; state = 0d;
......
package 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;
}
}
import garage.ExecutionFailedException;
import garage.Task; import garage.Task;
import garage.TestBench; import garage.TestBench;
import org.junit.Before; import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import vehicle.Vehicle; import vehicle.Vehicle;
import vehicle.VehicleModel; import vehicle.VehicleModel;
import vehicle.part.gearbox.Gear;
import vehicle.part.pedal.PedalAccelerator; import vehicle.part.pedal.PedalAccelerator;
import vehicle.part.pedal.PedalPosition;
import java.util.Arrays; import java.util.Arrays;
...@@ -89,5 +93,35 @@ public class TestBenchTest { ...@@ -89,5 +93,35 @@ public class TestBenchTest {
assertEquals(0, testBench.getTasks().size()); assertEquals(0, testBench.getTasks().size());
} }
@Test
public void runTest(){
testBench.addVehicle(fiat500);
testBench.addTask(task);
assertEquals(0d, fiat500.getPedals().get(0).getState());
testBench.run(10);
assertEquals(50d, fiat500.getPedals().get(0).getState());
}
@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());
}
} }
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
</project>
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectKey">
<option name="state" value="project://63537948-39a4-48a0-9c97-34259a0fa913" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/openclassroom.iml" filepath="$PROJECT_DIR$/openclassroom.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="0faeadbc-d047-4802-8e58-72aee2d0c2b4" name="Default Changelist" comment="" />
<ignored path="$PROJECT_DIR$/out/" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="DefaultGradleProjectSettings">
<option name="isMigrated" value="true" />
</component>
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="45">
<caret line="3" selection-start-line="3" selection-end-line="3" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/test/TestStream.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="90">
<caret line="6" column="9" selection-start-line="6" selection-start-column="9" selection-end-line="6" selection-end-column="9" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/Personne.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="390">
<caret line="26" selection-start-line="26" selection-end-line="26" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/Couleur.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="195">
<caret line="13" lean-forward="true" selection-start-line="13" selection-end-line="13" />
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/src/Personne.java" />
<option value="$PROJECT_DIR$/src/Couleur.java" />
<option value="$PROJECT_DIR$/src/Main.java" />
<option value="$PROJECT_DIR$/src/test/TestStream.java" />
</list>
</option>
</component>
<component name="ProjectFrameBounds">
<option name="x" value="21" />
<option name="y" value="45" />
<option name="width" value="1878" />
<option name="height" value="1014" />
</component>
<component name="ProjectView">
<navigator proportions="" version="1">
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="Scope" />
<pane id="PackagesPane" />
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="openclassroom" type="b2602c69:ProjectViewProjectNode" />
<item name="openclassroom" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="openclassroom" type="b2602c69:ProjectViewProjectNode" />
<item name="openclassroom" type="462c0819:PsiDirectoryNode" />
<item name="src" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="openclassroom" type="b2602c69:ProjectViewProjectNode" />
<item name="openclassroom" type="462c0819:PsiDirectoryNode" />
<item name="src" type="462c0819:PsiDirectoryNode" />
<item name="test" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
</panes>
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager">
<configuration name="Main" type="Application" factoryName="Application" temporary="true">
<option name="MAIN_CLASS_NAME" value="Main" />
<module name="openclassroom" />
<option name="WORKING_DIRECTORY" value="file://$PROJECT_DIR$" />
<RunnerSettings RunnerId="Run" />
<ConfigurationWrapper RunnerId="Run" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.Main" />
</list>
</recent_temporary>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="0faeadbc-d047-4802-8e58-72aee2d0c2b4" name="Default Changelist" comment="" />
<created>1557148891393</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1557148891393</updated>
</task>
<servers />
</component>
<component name="ToolWindowManager">
<frame x="21" y="45" width="1878" height="1014" extended-state="0" />
<layout>
<window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.12200436" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info id="Image Layers" order="2" />
<window_info id="Designer" order="3" />
<window_info id="UI Designer" order="4" />
<window_info id="Capture Tool" order="5" />
<window_info id="Favorites" order="6" side_tool="true" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info anchor="bottom" id="Run" order="2" weight="0.32890365" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Terminal" order="7" />
<window_info anchor="bottom" id="Event Log" order="8" side_tool="true" />
<window_info anchor="bottom" id="Version Control" order="9" />
<window_info anchor="bottom" id="Messages" order="10" visible="true" weight="0.32890365" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
<window_info anchor="right" id="Palette" order="3" />
<window_info anchor="right" id="Maven" order="4" />
<window_info anchor="right" id="Theme Preview" order="5" />
<window_info anchor="right" id="Capture Analysis" order="6" />
<window_info anchor="right" id="Palette&#9;" order="7" />
</layout>
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/src/Personne.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="390">
<caret line="26" selection-start-line="26" selection-end-line="26" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/Couleur.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="195">
<caret line="13" lean-forward="true" selection-start-line="13" selection-end-line="13" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="45">
<caret line="3" selection-start-line="3" selection-end-line="3" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/test/TestStream.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="90">
<caret line="6" column="9" selection-start-line="6" selection-start-column="9" selection-end-line="6" selection-end-column="9" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
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;}
}
public class Main {
public static void main(String[] args) {
}
}
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 test;
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